so://phie

freezo

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

blob: 7adea93a5b31a871e14c0aaba4104ad09db21174 (3043 bytes)[raw]

1#include <stdlib.h>

2

3#include "../include/entity.h"

4#include "../include/gate.h"

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

6

7gate_t *gate_create(pos_t pos, int enemy_max, int time) {

8 gate_t *gate = malloc(sizeof(gate_t));

9 gate->pos = pos;

10 gate->enemy_max = enemy_max;

11 gate->timer_spawn = fz_timer_create(1, time);

12 gate->frozen = false;

13 gate->frozen_timer = 0;

14 return gate;

15}

16

17void gate_update(gate_t *gate, game_t *game) {

18 if (!gate->frozen) {

19 bool stunned = false;

20 if (

21 game->player->shooting &&

22 game->player->target_entity != NULL &&

23 game->player->target_entity->type == ENTITY_GATE &&

24 game->player->target_entity->gate == gate

25 ) {

26 if (gate->frozen) {

27 if (game->player->dir > 0) {

28 gate->pos.x += 2.0;

29 }

30 else {

31 gate->pos.x -= 2.0;

32 }

33 }

34 else {

35 stunned = true;

36 }

37 }

38 // handle freezing

39 if (stunned) {

40 if (gate->frozen_timer >= 300) {

41 gate->frozen = true;

42 gate->frozen_timer = 0;

43 }

44 gate->frozen_timer++;

45 }

46 else if (gate->frozen_timer > 0) {

47 gate->frozen_timer--;

48 }

49 // handle spawning

50 fz_timer_update(gate->timer_spawn);

51 int enemies_len = 0;

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

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

54 enemies_len++;

55 }

56 }

57 if (fz_timer_check(gate->timer_spawn) && enemies_len < gate->enemy_max) {

58 game->entities = realloc(game->entities, sizeof(entity_t) * (game->entities_len + 1));

59 enemy_t *enemy = enemy_create((pos_t) {

60 .x = gate->pos.x + (TILE_WIDTH - ENEMY_WIDTH) / 2.0,

61 .y = gate->pos.y + TILE_HEIGHT - ENEMY_HEIGHT,

62 }, ENEMY_TEST);

63 enemy->dir = (rand() & 1) ? 1 : -1;

64 game->entities[game->entities_len] = (entity_t) {

65 .type = ENTITY_ENEMY,

66 .enemy = enemy,

67 .is_bad = true,

68 };

69 game->entities_len++;

70 }

71 }

72}

73

74void gate_draw(gate_t *gate, game_t *game) {

75 DrawTextureRec(game->assets.tiles, texture_rect(0, 2, TILE_WIDTH, TILE_HEIGHT), (pos_t) { gate->pos.x, gate->pos.y - TILE_HEIGHT }, WHITE);

76 DrawTextureRec(game->assets.tiles, texture_rect(0, 3, TILE_WIDTH, TILE_HEIGHT), gate->pos, WHITE);

77 pos_t pos = pos_snap((pos_t) {

78 .x = gate->pos.x,

79 .y = gate->pos.y - (ENEMY_HEIGHT - TILE_HEIGHT),

80 });

81 if (gate->frozen) {

82 DrawTextureRec(game->assets.entities, texture_rect(3, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

83 }

84 if (!gate->frozen && gate->frozen_timer > 0) {

85 if (gate->frozen_timer <= 100) {

86 DrawTextureRec(game->assets.entities, texture_rect(0, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

87 }

88 else if (gate->frozen_timer <= 200) {

89 DrawTextureRec(game->assets.entities, texture_rect(1, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

90 }

91 else {

92 DrawTextureRec(game->assets.entities, texture_rect(2, 2, PLAYER_WIDTH, PLAYER_HEIGHT), pos, WHITE);

93 }

94 }

95}

96

97void gate_free(gate_t *gate) {

98 free(gate->timer_spawn);

99 free(gate);

100}