-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_player.c
335 lines (270 loc) Β· 8.14 KB
/
c_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "c_player.h"
#include "p_engine.h"
#include "c_enemy.h"
#include "c_audio.h"
#include "c_item.h"
#include "i_math.h"
#include "p_game.h"
#include "c_map.h"
#include "main.h"
#include "pm.h"
#include "c_sched.h"
player_t * player_ctor (engine_t * engine)
{
player_t * self = (player_t *) malloc(sizeof(struct player_s));
__GUARD(self, "player_t obj");
self->x = (511 / 2) * 256 + 128;
self->y = (511 / 2) * 256 + 128;
self->ang = ANG90;
self->dyoff = BOBBING_STEP;
self->yoff = 0;
self->walking_audio_timer = true;
self->health = 100;
self->is_alive = true;
// printf("creating player ipc: %d %d\n", engine->ipc.co)
if (engine->ipc.target == 666 && engine->ipc.code > 50 && engine->ipc.code < 60)
player_load (self, engine->ipc.code % 10);
return self;
}
void player_dtor (player_t * self)
{
__UNGUARD(self, "player_t obj");
free(self);
}
void player_io_handler (game_t * game, engine_t * engine)
{
// keyboard
player_t * self = game->player;
if (self->is_alive == false)
{
game->player->ang = atan2(game->enemy->y - game->player->y, game->enemy->x - game->player->x);
game->player->vx = 0;
game->player->vy = 0;
return;
}
ALLEGRO_KEYBOARD_STATE state;
al_get_keyboard_state(&state);
self->vx = 0; self->vy = 0;
if (al_key_down(&state, 'w' - 'a' + 1))
{
self->vx += cos(self->ang) * 2;
self->vy += sin(self->ang) * 2;
}
if (al_key_down(&state, 's' - 'a' + 1))
{
self->vx += cos(self->ang + ANG180);
self->vy += sin(self->ang + ANG180);
}
if (al_key_down(&state, 'd' - 'a' + 1))
{
self->vx += cos(self->ang + ANG90);
self->vy += sin(self->ang + ANG90);
}
if (al_key_down(&state, 'a' - 'a' + 1))
{
self->vx += cos(self->ang + ANGN90);
self->vy += sin(self->ang + ANGN90);
}
if (al_key_down(&state, 'q' - 'a' + 1))
{
engine->pm->state = PM_STOPPED;
}
if (al_key_down(&state, 'e' - 'a' + 1))
{
const float dist = q_dist(game->player->x, game->player->y, game->item->x, game->item->y);
if (dist < ITEM_PICKUP_DIST)
{
game->engine->ipc.target = 1;
game->engine->ipc.code = 2;
pm_stop(game->pm, "game");
pm_stop(game->pm, "render");
pm_run(game->pm, "cutscene");
}
}
if (al_key_down(&state, ALLEGRO_KEY_ESCAPE))
{
// suspend game process
// rise menu process
pm_hold(game->engine->pm, "game");
pm_run(game->engine->pm, "menu");
}
// mouse
ALLEGRO_MOUSE_STATE mstate;
al_get_mouse_state(&mstate);
self->ang = ((mstate.x + 100) * (3.1415 * 2) / (engine->width - 200));// + (3.1415 * 2);
if (mstate.x < 100)
al_set_mouse_xy(engine->display, engine->width - 100, mstate.y);
if (mstate.x > engine->width - 100)
al_set_mouse_xy(engine->display, 100, mstate.y);
if (mstate.y < 100)
al_set_mouse_xy(engine->display, mstate.x, engine->height - 100);
if (mstate.y > engine->height - 100)
al_set_mouse_xy(engine->display, mstate.x, 100);
}
void player_update (game_t * game)
{
player_io_handler(game, game->engine);
player_movement_handler(game->player, game);
}
void player_bobbing (player_t * self, game_t * game)
{
const float bobbing_limit = (float) game->engine->buffer_height / 25;
const float bobbing_step = (float) game->engine->buffer_height / 1200;
self->yoff += self->dyoff;
if (self->yoff > bobbing_limit)
self->dyoff = -1 * bobbing_step;
else if (self->yoff < -1 * bobbing_limit)
self->dyoff = bobbing_step;
}
void player_reset_walking_aut (sched_t * sched, void * ptr, int val)
{
player_t * player = (player_t *) ptr;
player->walking_audio_timer = true;
}
void player_stuck_preventer (game_t * game)
{
// check if player is somehow stuck
// inside a wall
const long mx = (long) game->player->x >> 8;
const long my = (long) game->player->y >> 8;
const long tile = game->map->tiles[mx][my];
if (tile > MAP_WALL_START && tile < MAP_WALL_END)
{
// printf("Stuck!!\n");
// tp to start position, got nothing better to do
game->player->x = 256 * 255 + 128;
game->player->y = 256 * 255 + 128;
}
}
void player_movement_handler (player_t * self, game_t * game)
{
if (time(NULL) < game->sched->syncro)
return;
player_stuck_preventer(game);
// apply velocity vectors (vx, vy)
const float speed = 0.25;
self->x += self->vx * (speed / game->engine->delta);
self->y += self->vy * (speed / game->engine->delta);
// check if player is inside the wall
bool is_inside = false;
bool is_door = false;
uint8_t tile;
long mx;
long my;
int x, y;
for (y = -1; y <= 1; y += 2)
for (x = -1; x <= 1; x += 2)
{
mx = (long) (((int) self->x) + x * 9) >> 8;
my = (long) (((int) self->y) + y * 9) >> 8;
tile = game->map->tiles[mx][my];
if (tile > MAP_DOOR_START && tile < MAP_DOOR_END)
{
is_door = true;
is_inside = true;
goto _skip_check_fast;
}
else if (tile > MAP_COLLIDABLE_START && tile < MAP_COLLIDABLE_END)
{
// if prop -> recheck()
if (tile > MAP_COLLIDABLE_PROP_START)
{
// how to recheck
if (q_dist((float) mx * 256 + 128, (float) my * 256 + 128, game->player->x, game->player->y) > 128)
return;
}
is_inside = true;
goto _skip_check_fast;
}
}
// revert applied vectors if so
_skip_check_fast:
if (is_inside)
{
self->x -= self->vx * (speed / game->engine->delta);
self->y -= self->vy * (speed / game->engine->delta);
}
// if door, tp player
if (is_door)
{
long ox = mx - ((long) self->x >> 8);
long oy = my - ((long) self->y >> 8);
self->x += ox * WALL_JUMP;
self->y += oy * WALL_JUMP;
game->map->darkness = 255;
// char door_sample_name[0xF];
// sprintf(door_sample_name, "door%d", tile - MAP_DOOR_START - 1);
audio_play_master(game->audio, "door"); // one sound fits all
// cahnge floor color
const uint8_t new_tile = game->map->tiles[((long) self->x >> 8)][((long) self->y >> 8)];
// printf("New tile found %u\n", new_tile);
if (new_tile > MAP_SKY_START && new_tile < MAP_SKY_END)
{
// printf("Color changed\n");
game->map->color = new_tile - MAP_SKY_START - 1;
}
}
// handle player bobbing effect
if (self->vx != 0 || self->vy != 0)
{
player_bobbing(self, game);
if (self->walking_audio_timer)
{
self->walking_audio_timer = false;
audio_play_master(game->audio, "step");
yield(game->sched, player_reset_walking_aut, self, 0, PLAYER_WALKING_AUDIO_DELAY);
}
}
}
void player_kill (sched_t * sched, void * ptr, int val)
{
game_t * game = (game_t *) ptr;
// printf("Player has died()\n");
// kill game and renderer
// and run cutscene
pm_run(game->pm, "cutscene");
pm_stop(game->pm, "render");
pm_stop(game->pm, "game");
game->engine->ipc.target = 1; // msg to p_cutscene
game->engine->ipc.code = 1; // play death cutscene
// WARN: some pointers, are dangling from now on
// valgrind will complain
// but its not an acutuall issue
}
void player_save (game_t * game, int save_no)
{
/*
FILE FORMAT:
0 float player->x
1 float player->y
2 float player->health
*/
char save_path [0xFF];
sprintf(save_path, "./DATA/sav/player%d.sav", save_no);
#ifdef _WIN32
int fd = open(save_path, O_CREAT | O_WRONLY | O_BINARY, 0600);
#else
int fd = open(save_path, O_CREAT | O_WRONLY, 0600);
#endif
assert(write(fd, &game->player->x, sizeof(float)) == sizeof(float));
assert(write(fd, &game->player->y, sizeof(float)) == sizeof(float));
assert(write(fd, &game->player->health, sizeof(float)) == sizeof(float));
close(fd);
}
void player_load (player_t * self, int save_no)
{
char save_path [0xFF];
sprintf(save_path, "./DATA/sav/player%d.sav", save_no);
#ifdef _WIN32
int fd = open(save_path, O_CREAT | O_RDONLY | O_BINARY, 0600);
#else
int fd = open(save_path, O_CREAT | O_RDONLY, 0600);
#endif
assert(read(fd, &self->x, sizeof(float)) == sizeof(float));
assert(read(fd, &self->y, sizeof(float)) == sizeof(float));
assert(read(fd, &self->health, sizeof(float)) == sizeof(float));
close (fd);
}