so://phie

freezo

A retro platform game
git clone git://soophie.de/freezo

blob: d983ed486d8d157dbdeacec6f636f660b47e0adc (14613 bytes)[doc][blame][raw]

1#include <stdlib.h>

2#include <math.h>

3

4#include "../include/player.h"

5#include "../include/const.h"

6#include "../include/tile.h"

7#include "../include/util.h"

8#include "../include/entity.h"

9#include "../include/effect.h"

10

11player_t *player_create(pos_t pos) {

12 player_t *player = malloc(sizeof(player_t));

13 *player = (player_t) {

14 .pos = pos,

15 .health = 6,

16 .on_ground = false,

17 .velocity = 0.0,

18 .gravity = 0.0,

19 .dir = 1,

20 .sneaking = false,

21 .shooting = false,

22 .damage_timer = 0,

23 .fall_height = 0.0,

24 .held_enemy = NULL,

25 .moving = false,

26 .timer_walking = fz_timer_create(9, 4),

27 .timer_sneaking = fz_timer_create(9, 8),

28 .target_entity = NULL,

29 .shooting_distance = PLAYER_SHOOTING_RANGE,

30 };

31 return player;

32}

33

34void player_update(player_t *player, game_t *game) {

35 float move = 0.0;

36 bool go_down = false;

37 bool sneaking = false;

38 bool holding = false;

39 player->shooting = false;

40 player->moving = false;

41 tile_t *left_ground_tile = tile_get(game, player->pos.x + 0.2 * TILE_WIDTH, player->pos.y + PLAYER_HEIGHT + 1);

42 tile_t *right_ground_tile = tile_get(game, player->pos.x + PLAYER_WIDTH - 0.2 * TILE_WIDTH, player->pos.y + PLAYER_HEIGHT + 1);

43 // game over

44 if (player->health <= 0) {

45 game->defeat = true;

46 }

47 if (IsKeyDown(KEY_A) && !IsKeyDown(KEY_D)) {

48 move = -3.0;

49 player->dir = -1;

50 player->moving = true;

51 }

52 if (IsKeyDown(KEY_D) && !IsKeyDown(KEY_A)) {

53 move = 3.0;

54 player->dir = 1;

55 player->moving = true;

56 }

57 if (IsKeyDown(KEY_S)) {

58 sneaking = true;

59 }

60 if (IsKeyDown(KEY_S) && IsKeyDown(KEY_SPACE)) {

61 if (

62 (left_ground_tile == NULL || !tile_solid(left_ground_tile)) &&

63 (right_ground_tile == NULL || !tile_solid(right_ground_tile))

64 ) {

65 go_down = true;

66 }

67 }

68 if (IsKeyPressed(KEY_SPACE)) {

69 if (player->on_ground && !player->sneaking && !go_down) {

70 player->velocity = 13.0;

71 }

72 }

73 if (IsKeyDown(KEY_ENTER)) {

74 player->shooting = true;

75 }

76 if (IsKeyPressed(KEY_W)) {

77 holding = true;

78 }

79 // detect on ground

80 bool on_ground = false;

81 if (!(player->velocity > 0.0)) {

82 if (!go_down) {

83 for (int i = 0; i < game->tiles_len; i++) {

84 tile_t *tile = game->tiles[i];

85 if (tile_ground(tile)) {

86 if (

87 player->pos.x + PLAYER_WIDTH > tile->pos.x &&

88 player->pos.x < tile->pos.x + TILE_WIDTH

89 ) {

90 float tolerance = 4.0;

91 if (player->gravity > tolerance) {

92 tolerance = player->gravity;

93 }

94 if (fabs(player->pos.y + PLAYER_HEIGHT - tile->pos.y) < tolerance) {

95 player->pos.y = tile->pos.y - PLAYER_HEIGHT;

96 on_ground = true;

97 if (player->fall_height >= 1.5 * TILE_HEIGHT) {

98 effect_play(player->pos, EFFECT_FALL, game);

99 }

100 // fall damage

101 if (player->fall_height >= 6 * TILE_HEIGHT) {

102 if (player->damage_timer == 0) {

103 if (player->health > 0) {

104 player->health--;

105 player->damage_timer = 80;

106 player->velocity = 6.0;

107 if (player->held_enemy != NULL) {

108 player->held_enemy = NULL;

109 }

110 }

111 }

112 }

113 }

114 }

115 }

116 }

117 }

118 for (int i = 0; i < game->entities_len; i++) {

119 if (game->entities[i].type == ENTITY_ENEMY) {

120 enemy_t *enemy = game->entities[i].enemy;

121 if (enemy->frozen) {

122 if (

123 player->pos.x + PLAYER_WIDTH > enemy->pos.x &&

124 player->pos.x < enemy->pos.x + ENEMY_WIDTH

125 ) {

126 if (fabs(player->pos.y + PLAYER_HEIGHT - enemy->pos.y) < 4.0) {

127 player->pos.y = enemy->pos.y - PLAYER_HEIGHT;

128 on_ground = true;

129 if (player->sneaking && player->fall_height >= 2 * TILE_HEIGHT) {

130 effect_play(enemy->pos, EFFECT_BREAK, game);

131 effect_play(enemy->pos, EFFECT_PARTICLE, game);

132 enemy_kill(enemy, game);

133 i--;

134 player->velocity = 6.0;

135 }

136 }

137 }

138 }

139 }

140 }

141 }

142 // detect ceiling

143 if (player->velocity > 0.0) {

144 for (int i = 0; i < game->tiles_len; i++) {

145 tile_t *tile = game->tiles[i];

146 if (tile_solid(tile)) {

147 if (

148 player->pos.x + PLAYER_WIDTH - 0.2 * TILE_WIDTH > tile->pos.x &&

149 player->pos.x + 0.2 * TILE_WIDTH < tile->pos.x + TILE_WIDTH

150 ) {

151 float tolerance = 4.0;

152 if (player->velocity > tolerance) {

153 tolerance = player->velocity;

154 }

155 if (fabs(player->pos.y - (tile->pos.y + 0.2 * TILE_HEIGHT)) < tolerance) {

156 player->pos.y = tile->pos.y + 0.2 * TILE_HEIGHT;

157 player->velocity = 0.0;

158 }

159 }

160 }

161 }

162 }

163 // detect wall

164 if (fabs(move) > 0.0) {

165 for (int i = 0; i < game->tiles_len; i++) {

166 tile_t *tile = game->tiles[i];

167 if (tile_wall(tile)) {

168 if (

169 player->pos.y + PLAYER_HEIGHT > tile->pos.y &&

170 player->pos.y < tile->pos.y + TILE_HEIGHT

171 ) {

172 float tolerance = 4.0;

173 if (move > tolerance) {

174 tolerance = player->velocity;

175 }

176 if (player->dir > 0) {

177 if (fabs((player->pos.x + PLAYER_WIDTH - 0.2 * TILE_WIDTH) - tile->pos.x) < tolerance) {

178 player->pos.x = tile->pos.x - (PLAYER_WIDTH - 0.2 * TILE_WIDTH);

179 }

180 }

181 else {

182 if (fabs((player->pos.x + 0.2 * TILE_WIDTH) - (tile->pos.x + TILE_WIDTH)) < tolerance) {

183 player->pos.x = tile->pos.x + TILE_WIDTH - 0.2 * TILE_WIDTH;

184 }

185 }

186 }

187 }

188 }

189 }

190 player->on_ground = on_ground;

191 player->sneaking = sneaking;

192 // handle move

193 if (player->sneaking || player->shooting) {

194 player->pos.x += 0.5 * move;

195 }

196 else {

197 player->pos.x += move;

198 }

199 // handle map border

200 if (player->pos.x < 0.0) {

201 player->pos.x = 0.0;

202 }

203 if (player->pos.x + PLAYER_WIDTH > TILE_WIDTH * game->level->width) {

204 player->pos.x = TILE_WIDTH * game->level->width - PLAYER_WIDTH;

205 }

206 // handle jump

207 if (player->velocity > 0.0) {

208 player->pos.y -= player->velocity;

209 player->velocity -= 1.1;

210 }

211 if (player->on_ground) {

212 player->gravity = 0.0;

213 player->fall_height = 0.0;

214 }

215 else {

216 player->pos.y += player->gravity;

217 player->fall_height += player->gravity;

218 if (player->gravity < PLAYER_GRAVITY) {

219 player->gravity += 0.2;

220 }

221 else {

222 player->gravity = PLAYER_GRAVITY;

223 }

224 }

225 if (player->damage_timer > 0) {

226 player->damage_timer--;

227 }

228 // detect shooting

229 if (player->shooting) {

230 // determine target

231 float shooting_range = 2 * TILE_WIDTH;

232 rect_t shooting_rect = {

233 .x = player->dir > 0 ? player->pos.x + PLAYER_WIDTH : player->pos.x - shooting_range,

234 .y = player->sneaking ? player->pos.y + 8 * SCALE : player->pos.y + 7 * SCALE,

235 .width = shooting_range,

236 .height = SCALE,

237 };

238 float closest_distance = shooting_range;

239 entity_t *closest_entity = NULL;

240 bool target_in_range = false;

241 for (int i = 0; i < game->entities_len; i++) {

242 entity_t *entity = &game->entities[i];

243 // ignore held enemies

244 if (entity->type == ENTITY_ENEMY && player->held_enemy == entity->enemy) {

245 continue;

246 }

247 // ignore frozen gates

248 if (entity->type == ENTITY_GATE && entity->gate->frozen) {

249 continue;

250 }

251 // ignore non-bad entities

252 if (!entity->is_bad) {

253 continue;

254 }

255 rect_t entity_rect;

256 switch (entity->type) {

257 case ENTITY_ENEMY:

258 entity_rect = rect_from_pos(entity->enemy->pos, ENEMY_WIDTH, ENEMY_HEIGHT);

259 break;

260 case ENTITY_GATE:

261 entity_rect = rect_from_pos((pos_t) { entity->gate->pos.x, entity->gate->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);

262 break;

263 case ENTITY_DOOR:

264 entity_rect = rect_from_pos((pos_t) { entity->gate->pos.x, entity->gate->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);

265 break;

266 }

267 if (rect_collide(shooting_rect, entity_rect)) {

268 float entity_x = entity_rect.x + entity_rect.width / 2.0;

269 float entity_distance = fabs(entity_x - (player->dir > 0 ? player->pos.x + PLAYER_WIDTH : player->pos.x));

270 if (player->target_entity != NULL && player->target_entity == entity) {

271 target_in_range = true;

272 // if target entity set distance to that

273 player->shooting_distance = entity_distance;

274 }

275 if (entity_distance < closest_distance) {

276 closest_distance = entity_distance;

277 closest_entity = entity;

278 }

279 }

280 }

281 if (closest_entity != NULL) {

282 if (!target_in_range) {

283 // if no target entity use closest distance

284 player->shooting_distance = closest_distance;

285 player->target_entity = closest_entity;

286 }

287 }

288 else {

289 player->target_entity = NULL;

290 player->shooting_distance = PLAYER_SHOOTING_RANGE;

291 }

292 // detect shooting wall/solid

293 for (int i = 0; i < game->tiles_len; i++) {

294 tile_t *tile = game->tiles[i];

295 if (tile_wall(tile) || tile_solid(tile)) {

296 rect_t tile_rect = { tile->pos.x, tile->pos.y, TILE_WIDTH, TILE_HEIGHT };

297 if (rect_collide(shooting_rect, tile_rect)) {

298 if (player->dir > 0) {

299 player->shooting_distance = tile->pos.x - (player->pos.x + PLAYER_WIDTH);

300 player->target_entity = NULL;

301 }

302 else {

303 player->shooting_distance = player->pos.x - (tile->pos.x + TILE_WIDTH);

304 player->target_entity = NULL;

305 }

306 }

307 }

308 }

309 }

310 else {

311 player->target_entity = NULL;

312 player->shooting_distance = PLAYER_SHOOTING_RANGE;

313 }

314 // detect holding

315 if (holding) {

316 bool picked_up = false;

317 for (int i = 0; i < game->entities_len; i++) {

318 if (game->entities[i].type == ENTITY_ENEMY) {

319 enemy_t *enemy = game->entities[i].enemy;

320 if (enemy->frozen) {

321 float tolerance = 0.0;

322 if (

323 player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&

324 player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&

325 player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&

326 player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance

327 ) {

328 if (player->held_enemy == NULL) {

329 player->held_enemy = enemy;

330 picked_up = true;

331 }

332 }

333 }

334 }

335 }

336 if (!picked_up && player->held_enemy != NULL) {

337 player->held_enemy = NULL;

338 }

339 }

340 if (player->held_enemy != NULL) {

341 player->held_enemy->pos = (pos_t) {

342 .x = player->pos.x,

343 .y = player->pos.y - (player->sneaking ? 0.3 : 0.5) * TILE_HEIGHT,

344 };

345 }

346 // detect door entering

347 if (holding) {

348 for (int i = 0; i < game->entities_len; i++) {

349 if (game->entities[i].type == ENTITY_DOOR) {

350 door_t *door = game->entities[i].door;

351 float tolerance = 0.0;

352 if (

353 player->pos.x <= door->pos.x + ENEMY_WIDTH - tolerance &&

354 player->pos.x + PLAYER_WIDTH >= door->pos.x + tolerance &&

355 player->pos.y <= door->pos.y + ENEMY_HEIGHT - tolerance &&

356 player->pos.y + PLAYER_HEIGHT >= door->pos.y + tolerance

357 ) {

358 if (door_unlock(door, game)) {

359 game->victory = true;

360 }

361 }

362 }

363 }

364 }

365 // detect damage

366 for (int i = 0; i < game->entities_len; i++) {

367 if (game->entities[i].type == ENTITY_ENEMY) {

368 enemy_t *enemy = game->entities[i].enemy;

369 if (!enemy->frozen) {

370 float tolerance = TILE_WIDTH / 5.0;

371 if (

372 player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&

373 player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&

374 player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&

375 player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance

376 ) {

377 if (player->damage_timer == 0) {

378 if (player->health > 0) {

379 player->health--;

380 player->damage_timer = 80;

381 player->velocity = 8.0;

382 if (player->pos.x + PLAYER_WIDTH / 2.0 > enemy->pos.x + ENEMY_WIDTH / 2.0) {

383 player->pos.x += 20.0;

384 }

385 else {

386 player->pos.x -= 20.0;

387 }

388 if (player->held_enemy != NULL) {

389 player->held_enemy = NULL;

390 }

391 }

392 }

393 }

394 }

395 }

396 }

397 // fall out of map

398 if (player->pos.y > TILE_HEIGHT * game->level->height) {

399 if (player->health > 0) {

400 player->health = 0;

401 }

402 }

403 // update timer

404 if (player->on_ground && player->moving) {

405 fz_timer_update(player->timer_walking);

406 fz_timer_update(player->timer_sneaking);

407 }

408 else {

409 fz_timer_reset(player->timer_walking);

410 fz_timer_reset(player->timer_sneaking);

411 }

412}

413

414void player_draw(player_t *player, game_t *game) {

415 pos_t pos = pos_snap(player->pos);

416 int frame = (player->sneaking || player->shooting)

417 ? fz_timer_get(player->timer_sneaking)

418 : fz_timer_get(player->timer_walking);

419 if (player->damage_timer / 4 % 2 == 0) {

420 if (player->sneaking) {

421 if (player->dir > 0) {

422 DrawTextureRec(game->assets.entities, texture_rect(frame, 6, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

423 }

424 else {

425 DrawTextureRec(game->assets.entities, texture_rect(frame, 7, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

426 }

427 }

428 else {

429 if (player->dir > 0) {

430 DrawTextureRec(game->assets.entities, texture_rect(frame, 4, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

431 }

432 else {

433 DrawTextureRec(game->assets.entities, texture_rect(frame, 5, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

434 }

435 }

436 if (player->shooting) {

437 float y = player->sneaking ? pos.y + 8 * SCALE : pos.y + 7 * SCALE;

438 if (player->dir > 0) {

439 DrawRectangle(pos.x + PLAYER_WIDTH, y, player->shooting_distance, 1 * SCALE, BLUE);

440 }

441 else {

442 DrawRectangle(pos.x - player->shooting_distance, y, player->shooting_distance, 1 * SCALE, BLUE);

443 }

444 }

445 }

446}

447

448void player_free(player_t *player) {

449 fz_timer_free(player->timer_walking);

450 fz_timer_free(player->timer_sneaking);

451 free(player);

452}