A game engine for top-down 2D RPG games.
rpg game-engine raylib c99
at main 2.3 kB view raw
1#include "keraforge/bini.h" 2#include <keraforge.h> 3 4 5static 6void _player_tick_move(struct kf_actor *self) 7{ 8 struct kf_vec2(f32) v = {0, 0}; 9 10 /* gamepad axis movement */ 11 f32 gpx = kf_getgamepadaxis(kf_inputbind_move_left); 12 f32 gpy = kf_getgamepadaxis(kf_inputbind_move_up); 13 if (gpx > kf_deadzone || gpx < -kf_deadzone || gpy > kf_deadzone || gpy < -kf_deadzone) 14 { 15 v.y = gpy; 16 v.x = gpx; 17 18 f32 angle = Vector2LineAngle(Vector2Zero(), (Vector2){gpx, gpy}) * RAD2DEG; 19 angle /= 90; 20 switch ((int)roundf(angle)) 21 { 22 case 0: self->pointing = kf_east; break; 23 case 1: self->pointing = kf_north; break; 24 case -2: /* fallthrough */ 25 case 2: self->pointing = kf_west; break; 26 case -1: self->pointing = kf_south; break; 27 } 28 29 goto done; 30 } 31 32 /* non-axis movement */ 33 bool w = kf_checkinputdown(kf_inputbind_move_up); 34 bool s = kf_checkinputdown(kf_inputbind_move_down); 35 bool a = kf_checkinputdown(kf_inputbind_move_left); 36 bool d = kf_checkinputdown(kf_inputbind_move_right); 37 38 if (a && d) { v.x = 0; } 39 else if (a) { v.x = -1; self->pointing = kf_west; } 40 else if (d) { v.x = 1; self->pointing = kf_east; } 41 42 if (w && s) { v.y = 0; } 43 else if (w) { v.y = -1; self->pointing = kf_north; } 44 else if (s) { v.y = 1; self->pointing = kf_south; } 45 46 v = kf_normalize_vec2(f32)(v); 47 48done: 49 if (v.x || v.y) 50 kf_actor_addforce(self, v); 51} 52 53void kf_player_tick(struct kf_actor *self) 54{ 55 if (!kf_window.menu && self->controlled) 56 { 57 _player_tick_move(self); 58 59 if (kf_checkinputpress(kf_inputbind_run)) 60 { 61 self->running = true; 62 self->speedmod = 1.5; 63 } 64 else if (kf_checkinputrelease(kf_inputbind_run)) 65 { 66 self->running = false; 67 self->speedmod = 1; 68 } 69 } 70 71 kf_actor_move(kf_window.room, self, kf_dts); 72} 73 74void kf_player_draw(struct kf_actor *self) 75{ 76 kf_actor_draw(self); 77 78 if (self->controlled) 79 { 80 kf_window.cam.target.x = self->pos.x + (self->size.x / 2); 81 kf_window.cam.target.y = self->pos.y + (self->size.y / 2); 82 } 83} 84 85void kf_player_serialize(struct kf_actor *self, struct bini_stream *bs) 86{ 87 bini_wf(bs, self->pos.x); 88 bini_wf(bs, self->pos.y); 89 bini_wb(bs, self->controlled); 90} 91 92void kf_player_deserialize(struct kf_actor *self, struct bini_stream *bs) 93{ 94 self->pos.x = bini_rf(bs); 95 self->pos.y = bini_rf(bs); 96 self->controlled = bini_rb(bs); 97}