-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.c
executable file
·200 lines (172 loc) · 5.54 KB
/
player.c
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "player.h"
void init_players(int n_players)
{
players = calloc(n_players, sizeof(struct player));
for (int i = 0; i < n_players; ++i)
{
struct player *p = &players[i];
p->sprite = tile_character;
p->my_bombs = 0;
p->fire_power = starter_firepower;
p->max_bombs = starter_bombs;
p->speed = starter_speed;
if (i == 0)
{
p->x = p1_start_x * TILE_SIZE + TILE_SIZE / 2;
p->y = p1_start_y * TILE_SIZE + TILE_SIZE / 2;
p->keymap.left = SDLK_a;
p->keymap.right = SDLK_d;
p->keymap.up = SDLK_w;
p->keymap.down = SDLK_s;
p->keymap.drop = SDLK_SPACE;
}
if (i == 1)
{
p->x = p2_start_x * TILE_SIZE + TILE_SIZE / 2;
p->y = p2_start_y * TILE_SIZE + TILE_SIZE / 2;
p->keymap.left = SDLK_LEFT;
p->keymap.right = SDLK_RIGHT;
p->keymap.up = SDLK_UP;
p->keymap.down = SDLK_DOWN;
p->keymap.drop = SDLK_RCTRL;
}
}
}
void update_players(Uint32 dt)
{
for (int i = 0; i < n_players; ++i)
{
struct player *p = &players[i];
if (fire_map[map_residential_index(p->x, p->y)])
kill_player(i);
if (key_down[p->keymap.drop] && p->my_bombs < p->max_bombs)
{
drop_bomb(i);
key_down[p->keymap.drop] = 0;
}
if (key_down[p->keymap.left] == 1)
p->vx = -p->speed;
else if (key_down[p->keymap.right] == 1)
p->vx = p->speed;
else
p->vx = 0.0;
if (key_down[p->keymap.up] == 1)
p->vy = -p->speed;
else if (key_down[p->keymap.down] == 1)
p->vy = p->speed;
else
p->vy = 0.0;
/* Compensate so that the total speed when player is moving diagonally
doesn't exceed normal movement speed. */
if (p->vx != 0 && p->vy != 0)
{
p->vx *= 0.707106;
p->vy *= 0.707106;
}
if (p->vx > 0)
p->sprite = tile_character;
else if (p->vx < 0)
p->sprite = tile_character_m;
scm_call_3(blocks[block_map[map_residential_index(p->x, p->y)]]
.trigger_function,
scm_from_int(i),
scm_from_int(residential(p->x)),
scm_from_int(residential(p->y)));
move_player(i, dt);
}
}
void move_player(int player, Uint32 dt)
{
struct player *p = &players[player];
p->x += p->vx * dt;
if (hit_map[map_residential_index(p->x, p->y)] ||
hit_map[map_residential_index(p->x, p->y + TILE_SIZE / 3)] ||
hit_map[map_residential_index(p->x + TILE_SIZE / 3, p->y)] ||
hit_map[map_residential_index(p->x - TILE_SIZE / 3, p->y)] ||
hit_map[map_residential_index(p->x - TILE_SIZE / 3,
p->y + TILE_SIZE / 3)] ||
hit_map[map_residential_index(p->x + TILE_SIZE / 3,
p->y + TILE_SIZE / 3)])
p->x -= p->vx * dt;
p->y += p->vy * dt;
if (hit_map[map_residential_index(p->x, p->y)] ||
hit_map[map_residential_index(p->x, p->y + TILE_SIZE / 3)] ||
hit_map[map_residential_index(p->x + TILE_SIZE / 3, p->y)] ||
hit_map[map_residential_index(p->x - TILE_SIZE / 3, p->y)] ||
hit_map[map_residential_index(p->x - TILE_SIZE / 3,
p->y + TILE_SIZE / 3)] ||
hit_map[map_residential_index(p->x + TILE_SIZE / 3,
p->y + TILE_SIZE / 3)])
p->y -= p->vy * dt;
if (p->x - TILE_SIZE / 2 < 0)
p->x = TILE_SIZE / 2;
else if (p->x + TILE_SIZE / 2 > tile_x * TILE_SIZE)
p->x = tile_x * TILE_SIZE - TILE_SIZE / 2;
if (p->y - TILE_SIZE / 2 < 0)
p->y = TILE_SIZE / 2;
else if (p->y + TILE_SIZE / 2 > tile_y * TILE_SIZE)
p->y = tile_y * TILE_SIZE - TILE_SIZE / 2;
}
void drop_bomb(int player)
{
struct player *p = &players[player];
int index = map_residential_index(p->x, p->y);
if (bomb_map[index])
return;
++(p->my_bombs);
add_element(residential(p->x), residential(p->y), 0, player, BOMB,
BOMB_TIME, bombs, &active_bombs, max_bombs, bomb_map);
}
void kill_player(int player)
{
struct player *p = &players[player];
p->x = 0.0;
p->y = 0.0;
}
SCM scm_kill_player(SCM player)
{
kill_player(scm_to_int(player));
return SCM_BOOL_T;
}
SCM scm_teleport_player(SCM player, SCM x, SCM y)
{
struct player *p = &players[scm_to_int(player)];
p->x = scm_to_int(x) * TILE_SIZE + TILE_SIZE / 2;
p->y = scm_to_int(y) * TILE_SIZE + TILE_SIZE / 2;
return SCM_BOOL_T;
}
SCM scm_move_player(SCM player, SCM x, SCM y)
{
struct player *p = &players[scm_to_int(player)];
p->vx += scm_to_double(x);
p->vy += scm_to_double(y);
return SCM_BOOL_T;
}
SCM scm_inc_player_firepower(SCM player, SCM amount)
{
struct player *p = &players[scm_to_int(player)];
play_sound(residential(p->x), residential(p->y), SOUND_POWERUP);
p->fire_power += scm_to_int(amount);
return SCM_BOOL_T;
}
SCM scm_inc_player_bombs(SCM player, SCM amount)
{
struct player *p = &players[scm_to_int(player)];
play_sound(residential(p->x), residential(p->y), SOUND_POWERUP);
p->max_bombs += scm_to_int(amount);
return SCM_BOOL_T;
}
SCM scm_inc_player_speed(SCM player, SCM amount)
{
struct player *p = &players[scm_to_int(player)];
play_sound(residential(p->x), residential(p->y), SOUND_POWERUP);
p->speed += scm_to_double(amount);
return SCM_BOOL_T;
}
SCM scm_modify_player_movement(SCM player, SCM amount)
{
struct player *p = &players[scm_to_int(player)];
p->vx *= scm_to_double(amount);
p->vy *= scm_to_double(amount);
return SCM_BOOL_T;
}