#include "keraforge/bini.h" #include static void _player_tick_move(struct kf_actor *self) { struct kf_vec2(f32) v = {0, 0}; /* gamepad axis movement */ f32 gpx = kf_getgamepadaxis(kf_inputbind_move_left); f32 gpy = kf_getgamepadaxis(kf_inputbind_move_up); if (gpx > kf_deadzone || gpx < -kf_deadzone || gpy > kf_deadzone || gpy < -kf_deadzone) { v.y = gpy; v.x = gpx; f32 angle = Vector2LineAngle(Vector2Zero(), (Vector2){gpx, gpy}) * RAD2DEG; angle /= 90; switch ((int)roundf(angle)) { case 0: self->pointing = kf_east; break; case 1: self->pointing = kf_north; break; case -2: /* fallthrough */ case 2: self->pointing = kf_west; break; case -1: self->pointing = kf_south; break; } goto done; } /* non-axis movement */ bool w = kf_checkinputdown(kf_inputbind_move_up); bool s = kf_checkinputdown(kf_inputbind_move_down); bool a = kf_checkinputdown(kf_inputbind_move_left); bool d = kf_checkinputdown(kf_inputbind_move_right); if (a && d) { v.x = 0; } else if (a) { v.x = -1; self->pointing = kf_west; } else if (d) { v.x = 1; self->pointing = kf_east; } if (w && s) { v.y = 0; } else if (w) { v.y = -1; self->pointing = kf_north; } else if (s) { v.y = 1; self->pointing = kf_south; } v = kf_normalize_vec2(f32)(v); done: if (v.x || v.y) kf_actor_addforce(self, v); } void kf_player_tick(struct kf_actor *self) { if (!kf_window.menu && self->controlled) { _player_tick_move(self); if (kf_checkinputpress(kf_inputbind_run)) { self->running = true; self->speedmod = 1.5; } else if (kf_checkinputrelease(kf_inputbind_run)) { self->running = false; self->speedmod = 1; } } kf_actor_move(kf_window.room, self, kf_dts); } void kf_player_draw(struct kf_actor *self) { kf_actor_draw(self); if (self->controlled) { kf_window.cam.target.x = self->pos.x + (self->size.x / 2); kf_window.cam.target.y = self->pos.y + (self->size.y / 2); } } void kf_player_serialize(struct kf_actor *self, struct bini_stream *bs) { bini_wf(bs, self->pos.x); bini_wf(bs, self->pos.y); bini_wb(bs, self->controlled); } void kf_player_deserialize(struct kf_actor *self, struct bini_stream *bs) { self->pos.x = bini_rf(bs); self->pos.y = bini_rf(bs); self->controlled = bini_rb(bs); }