so://phie

freezo

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

blob: d24d4f3a191bfa64c58c9f6d64f8837d2a69754e (14902 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 case ENTITY_BOSS:

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

268 break;

269 }

270 if (rect_collide(shooting_rect, entity_rect)) {

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

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

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

274 target_in_range = true;

275 // if target entity set distance to that

276 player->shooting_distance = entity_distance;

277 }

278 if (entity_distance < closest_distance) {

279 closest_distance = entity_distance;

280 closest_entity = entity;

281 }

282 }

283 }

284 if (closest_entity != NULL) {

285 if (!target_in_range) {

286 // if no target entity use closest distance

287 player->shooting_distance = closest_distance;

288 player->target_entity = closest_entity;

289 }

290 }

291 else {

292 player->target_entity = NULL;

293 player->shooting_distance = PLAYER_SHOOTING_RANGE;

294 }

295 // detect shooting wall/solid

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

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

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

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

300 if (rect_collide(shooting_rect, tile_rect)) {

301 if (player->dir > 0) {

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

303 player->target_entity = NULL;

304 }

305 else {

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

307 player->target_entity = NULL;

308 }

309 }

310 }

311 }

312 }

313 else {

314 player->target_entity = NULL;

315 player->shooting_distance = PLAYER_SHOOTING_RANGE;

316 }

317 // detect holding

318 if (holding) {

319 bool picked_up = false;

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

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

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

323 if (enemy->frozen) {

324 float tolerance = 0.0;

325 if (

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

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

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

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

330 ) {

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

332 player->held_enemy = enemy;

333 picked_up = true;

334 }

335 }

336 }

337 }

338 }

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

340 player->held_enemy = NULL;

341 }

342 }

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

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

345 .x = player->pos.x,

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

347 };

348 }

349 // detect door entering

350 if (holding) {

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

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

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

354 float tolerance = 0.0;

355 if (

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

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

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

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

360 ) {

361 if (door_unlock(door, game)) {

362 game->door = door;

363 game->victory = true;

364 }

365 else {

366 PlaySound(game->assets.locked);

367 }

368 }

369 }

370 }

371 }

372 // detect damage

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

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

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

376 if (!enemy->frozen) {

377 float tolerance = TILE_WIDTH / 5.0;

378 if (

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

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

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

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

383 ) {

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

385 if (player->health > 0) {

386 player->health--;

387 player->damage_timer = 80;

388 player->velocity = 8.0;

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

390 player->pos.x += 20.0;

391 }

392 else {

393 player->pos.x -= 20.0;

394 }

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

396 player->held_enemy = NULL;

397 }

398 }

399 }

400 }

401 }

402 }

403 }

404 // fall out of map

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

406 if (player->health > 0) {

407 player->health = 0;

408 }

409 }

410 // update timer

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

412 fz_timer_update(player->timer_walking);

413 fz_timer_update(player->timer_sneaking);

414 }

415 else {

416 fz_timer_reset(player->timer_walking);

417 fz_timer_reset(player->timer_sneaking);

418 }

419}

420

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

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

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

424 ? fz_timer_get(player->timer_sneaking)

425 : fz_timer_get(player->timer_walking);

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

427 if (player->sneaking) {

428 if (player->dir > 0) {

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

430 }

431 else {

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

433 }

434 }

435 else {

436 if (player->dir > 0) {

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

438 }

439 else {

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

441 }

442 }

443 if (player->shooting) {

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

445 if (player->dir > 0) {

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

447 }

448 else {

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

450 }

451 }

452 }

453}

454

455void player_free(player_t *player) {

456 fz_timer_free(player->timer_walking);

457 fz_timer_free(player->timer_sneaking);

458 free(player);

459}