A retro platform game
git clone git://soophie.de/freezo
Binary file
5
#include "game.h"6
#include "util.h"8
#define BOSS_WIDTH 12 * SCALE9
#define BOSS_HEIGHT 12 * SCALE10
#define BOSS_SPEED 0.511
#define BOSS_FLOAT_TIME 8012
#define BOSS_RANGE 6 * TILE_WIDTH14
typedef enum {15
BOSS_TEST,16
} boss_e;
18
struct Boss {19
pos_t pos;20
boss_e type;15
bool on_ground;16
float velocity;17
float gravity;21
int dir;19
bool stunned;20
bool frozen;21
int frozen_timer;22
float fall_height;23
fz_timer_t *timer_walking;24
fz_timer_t *timer_sneaking;22
int float_timer;23
bool is_angry;24
};26
boss_t *boss_create(pos_t pos, boss_e type);
30
rect_t rect_from_pos(pos_t pos, int width, int height);31
bool rect_collide(rect_t a, rect_t b);32
bool rect_collide_threshold(rect_t a, rect_t b, float threshold);34
Texture texture_load(char *filename, int scale);35
Rectangle texture_rect(int x, int y, int width, int height);
11
*boss = (boss_t) {12
.pos = pos,13
.type = type,14
.on_ground = false,15
.velocity = 0.0,16
.gravity = 0.0,14
.dir = 1,18
.stunned = false,19
.frozen = false,20
.frozen_timer = 0,21
.fall_height = 0.0,22
.timer_walking = fz_timer_create(9, 8),23
.timer_sneaking = fz_timer_create(9, 16),15
.float_timer = 0,16
.is_angry = false,17
};18
return boss;19
}21
void boss_update(boss_t *boss, game_t *game) {29
(void) boss;30
(void) 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
}50
void boss_draw(boss_t *boss, game_t *game) {51
pos_t pos = pos_snap(boss->pos);35
DrawTextureRec(game->assets.entities, texture_rect_v(10, 6, 2, 1, ENEMY_WIDTH, ENEMY_HEIGHT), pos, WHITE);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
}60
void boss_kill(boss_t *boss, game_t *game) {
74
}76
void boss_free(boss_t *boss) {55
fz_timer_free(boss->timer_walking);56
fz_timer_free(boss->timer_sneaking);77
free(boss);78
}
94
rect = rect_from_pos((pos_t) { entity->door->pos.x, entity->door->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);95
break;96
case ENTITY_BOSS:97
rect = rect_from_pos((pos_t) { entity->boss->pos.x, entity->boss->pos.y - TILE_HEIGHT }, 2 * ENEMY_WIDTH, ENEMY_HEIGHT);97
rect = rect_from_pos(entity->boss->pos, BOSS_WIDTH, BOSS_HEIGHT);98
break;99
}100
return rect;
253
if (!entity->is_bad) {254
continue;255
}256
// ignore bosses257
if (entity->type == ENTITY_BOSS) {258
continue;259
}260
rect_t entity_rect = entity_get_rect(entity);261
if (rect_collide(shooting_rect, entity_rect)) {262
float entity_x = entity_rect.x + entity_rect.width / 2.0;
375
}376
}377
// detect damage378
rect_t player_rect = {379
.x = player->pos.x,380
.y = player->pos.y,381
.width = PLAYER_WIDTH,382
.height = PLAYER_HEIGHT,383
};384
for (int i = 0; i < game->entities_len; i++) {375
if (game->entities[i].type == ENTITY_ENEMY) {376
enemy_t *enemy = game->entities[i].enemy;377
if (!enemy->frozen) {378
float tolerance = TILE_WIDTH / 5.0;379
if (380
player->pos.x <= enemy->pos.x + ENEMY_WIDTH - tolerance &&381
player->pos.x + PLAYER_WIDTH >= enemy->pos.x + tolerance &&382
player->pos.y <= enemy->pos.y + ENEMY_HEIGHT - tolerance &&383
player->pos.y + PLAYER_HEIGHT >= enemy->pos.y + tolerance384
) {385
if (player->damage_timer == 0) {386
if (player->health > 0) {387
player->health--;388
player->damage_timer = 80;389
player->velocity = 8.0;390
if (player->pos.x + PLAYER_WIDTH / 2.0 > enemy->pos.x + ENEMY_WIDTH / 2.0) {391
player->pos.x += 20.0;392
}393
else {394
player->pos.x -= 20.0;395
}396
if (player->held_enemy != NULL) {397
player->held_enemy = NULL;398
}385
entity_t *entity = &game->entities[i];386
if (entity->is_bad) {387
// ignore frozen enemies388
if (entity->type == ENTITY_ENEMY && entity->enemy->frozen) {389
continue;390
}391
rect_t entity_rect = entity_get_rect(entity);392
if (rect_collide_threshold(entity_rect, player_rect, PLAYER_WIDTH / 4.0)) {393
if (player->damage_timer == 0) {394
if (player->health > 0) {395
player->health--;396
player->damage_timer = 80;397
player->velocity = 8.0;398
if (player->pos.x + PLAYER_WIDTH / 2.0 > entity_rect.x + entity_rect.width / 2.0) {399
player->pos.x += 20.0;400
}401
else {402
player->pos.x -= 20.0;403
}404
if (player->held_enemy != NULL) {405
player->held_enemy = NULL;406
}407
}408
}
54
);55
}57
bool rect_collide_threshold(rect_t a, rect_t b, float threshold) {58
return !(59
a.x > b.x + (b.width - threshold) ||60
a.x + (a.width - threshold) < b.x ||61
a.y + (a.height - threshold) < b.y ||62
a.y > b.y + (b.height - threshold)63
);64
}66
Texture2D texture_load(char *filename, int scale) {67
Image image = LoadImage(filename);68
ImageResizeNN(&image, scale * image.width, scale * image.height);