so://phie

freezo

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

blob: 4dff351f9c4a61cb3213e5086303e8962d65f3b3 (2257 bytes)[raw]

1#include <stdlib.h>

2#include <math.h>

3

4#include "../include/boss.h"

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

6#include "../include/entity.h"

7#include "../include/effect.h"

8

9boss_t *boss_create(pos_t pos, boss_e type) {

10 boss_t *boss = malloc(sizeof(boss_t));

11 *boss = (boss_t) {

12 .pos = pos,

13 .type = type,

14 .dir = 1,

15 .float_timer = 0,

16 .is_angry = false,

17 };

18 return boss;

19}

20

21void boss_update(boss_t *boss, game_t *game) {

22 pos_t player_origin = {

23 .x = game->player->pos.x + PLAYER_WIDTH / 2.0,

24 .y = game->player->pos.y + PLAYER_HEIGHT / 2.0,

25 };

26 pos_t boss_origin = {

27 .x = boss->pos.x + BOSS_WIDTH / 2.0,

28 .y = boss->pos.y + BOSS_HEIGHT / 2.0,

29 };

30 float dx = player_origin.x - boss_origin.x;

31 float dy = player_origin.y - boss_origin.y;

32 float distance = sqrtf(dx * dx + dy * dy);

33 boss->is_angry = distance < BOSS_RANGE && distance > BOSS_HEIGHT / 2.0;

34 if (boss->is_angry) {

35 float norm_dx = dx / distance;

36 float norm_dy = dy / distance;

37 boss->pos.x += norm_dx * BOSS_SPEED;

38 boss->pos.y += norm_dy * BOSS_SPEED;

39 boss->dir = (norm_dx > 0) ? 1 : -1;

40 }

41 else {

42 if (boss->float_timer == BOSS_FLOAT_TIME) {

43 boss->float_timer = 0;

44 }

45 boss->float_timer++;

46 boss->pos.y += 0.4 * sin((2 * PI) / BOSS_FLOAT_TIME * boss->float_timer);

47 }

48}

49

50void boss_draw(boss_t *boss, game_t *game) {

51 pos_t pos = pos_snap(boss->pos);

52 if (boss->dir > 0) {

53 DrawTextureRec(game->assets.entities, texture_rect_v(boss->is_angry ? 12 : 10, 6, 2, 1, ENEMY_WIDTH, ENEMY_HEIGHT), pos, WHITE);

54 }

55 else {

56 DrawTextureRec(game->assets.entities, texture_rect_v(boss->is_angry ? 12 : 10, 7, 2, 1, ENEMY_WIDTH, ENEMY_HEIGHT), pos, WHITE);

57 }

58}

59

60void boss_kill(boss_t *boss, game_t *game) {

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

62 if (game->entities[i].type == ENTITY_BOSS && game->entities[i].boss == boss) {

63 if (i + 1 < game->entities_len) {

64 for (int j = i; j < game->entities_len - 1; j++) {

65 game->entities[j] = game->entities[j + 1];

66 }

67 }

68 game->entities_len--;

69 game->entities = realloc(game->entities, sizeof(entity_t) * game->entities_len);

70 game->xp += 10;

71 break;

72 }

73 }

74}

75

76void boss_free(boss_t *boss) {

77 free(boss);

78}