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