so://phie

freezo

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

blob: ad14b4601861880d0cdcb7b324f3c55773cd9aa5 (13555 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 rect_t entity_rect;

252 switch (entity->type) {

253 case ENTITY_ENEMY:

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

255 break;

256 case ENTITY_GATE:

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

258 break;

259 }

260 if (rect_collide(shooting_rect, entity_rect)) {

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

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

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

264 target_in_range = true;

265 }

266 if (entity_distance < closest_distance) {

267 closest_distance = entity_distance;

268 closest_entity = entity;

269 }

270 }

271 }

272 if (closest_entity != NULL) {

273 player->shooting_distance = closest_distance;

274 if (!target_in_range) {

275 player->target_entity = closest_entity;

276 }

277 }

278 else {

279 player->target_entity = NULL;

280 player->shooting_distance = PLAYER_SHOOTING_RANGE;

281 }

282 // detect shooting wall/solid

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

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

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

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

287 if (rect_collide(shooting_rect, tile_rect)) {

288 if (player->dir > 0) {

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

290 player->target_entity = NULL;

291 }

292 else {

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

294 player->target_entity = NULL;

295 }

296 }

297 }

298 }

299 }

300 else {

301 player->target_entity = NULL;

302 player->shooting_distance = PLAYER_SHOOTING_RANGE;

303 }

304 // detect holding

305 if (holding) {

306 bool picked_up = false;

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

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

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

310 if (enemy->frozen) {

311 float tolerance = 0.0;

312 if (

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

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

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

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

317 ) {

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

319 player->held_enemy = enemy;

320 picked_up = true;

321 }

322 }

323 }

324 }

325 }

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

327 player->held_enemy = NULL;

328 }

329 }

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

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

332 .x = player->pos.x,

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

334 };

335 }

336 // detect damage

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

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

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

340 if (!enemy->frozen) {

341 float tolerance = TILE_WIDTH / 5.0;

342 if (

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

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

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

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

347 ) {

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

349 if (player->health > 0) {

350 player->health--;

351 player->damage_timer = 80;

352 player->velocity = 8.0;

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

354 player->pos.x += 20.0;

355 }

356 else {

357 player->pos.x -= 20.0;

358 }

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

360 player->held_enemy = NULL;

361 }

362 }

363 }

364 }

365 }

366 }

367 }

368 // fall out of map

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

370 if (player->health > 0) {

371 player->health = 0;

372 }

373 }

374 // update timer

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

376 fz_timer_update(player->timer_walking);

377 fz_timer_update(player->timer_sneaking);

378 }

379 else {

380 fz_timer_reset(player->timer_walking);

381 fz_timer_reset(player->timer_sneaking);

382 }

383}

384

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

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

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

388 ? fz_timer_get(player->timer_sneaking)

389 : fz_timer_get(player->timer_walking);

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

391 if (player->sneaking) {

392 if (player->dir > 0) {

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

394 }

395 else {

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

397 }

398 }

399 else {

400 if (player->dir > 0) {

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

402 }

403 else {

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

405 }

406 }

407 if (player->shooting) {

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

409 if (player->dir > 0) {

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

411 }

412 else {

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

414 }

415 }

416 }

417}

418

419void player_free(player_t *player) {

420 fz_timer_free(player->timer_walking);

421 fz_timer_free(player->timer_sneaking);

422 free(player);

423}