1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include "animation.h"
#include "components.h"
#include "game_config.h"
#include "input.h"
#include "player.h"
#include "standard.h"
#include "world.h"
#include "bullet.h"
void init_player(Player* player, World* world) {
CSprite* sprite;
CPosition* pos;
CAnimated* animated;
Entity e;
e = new_entity(world);
player->entity = e;
add_components(
world,
e,
ctype_sprite |
ctype_position |
ctype_animated
);
pos = &world->positions[e];
sprite = &world->sprites[e];
animated = &world->animateds[e];
pos->x = 32 << fbits;
pos->y = 70 << fbits;
sprite->id = asset_id_char;
sprite->rect = make_rect(0, 16, 16, 16);
animated->id = animation_player_walk_left;
animated->frame = 0;
animated->timer = 0;
player->face = 0;
player->shoot_cooldown = 15;
player->shoot_countdown = 0;
}
void update_player(Player* player, World* world) {
int dx, dy;
int face, moving = 0;
Entity e;
CPosition* pos;
CAnimated* animated;
e = player->entity;
pos = &world->positions[e];
animated = &world->animateds[e];
dx = dy = 0;
if (button_pressed(btn_dpad_left)) {
dx -= 1 << fbits;
}
if (button_pressed(btn_dpad_right)) {
dx += 1 << fbits;
}
if (button_pressed(btn_dpad_up)) {
dy -= 1 << fbits;
}
if (button_pressed(btn_dpad_down)) {
dy += 1 << fbits;
}
if (dx || dy) {
vec_nrmise(&dx, &dy);
if (dx) {
face = dx < 0 ? 0 : 1;
} else {
face = player->face;
}
pos->x += (dx * player_move_speed) >> fbits;
pos->y += (dy * player_move_speed) >> fbits;
player->ldx = dx;
player->ldy = dy;
moving = 1;
}
if (!moving) {
animated->frame = 0;
animated->id =
player->face ?
animation_player_idle_right :
animation_player_idle_left;
} else {
player->face = face;
animated->id =
player->face ?
animation_player_walk_right :
animation_player_walk_left;
}
if (button_pressed(btn_shoot) && player->shoot_countdown <= 0) {
new_player_bullet(
world,
pos->x,
pos->y,
(player->ldx * player_bullet_speed) >> fbits,
(player->ldy * player_bullet_speed) >> fbits,
100
);
player->shoot_countdown = player->shoot_cooldown;
}
player->shoot_countdown--;
}
|