A retro platform game
git clone git://soophie.de/freezo
31
void entity_draw(entity_t *entity, game_t *game);32
void entity_detach(entity_t *entity);33
void entity_free(entity_t *entity);35
rect_t entity_get_rect(entity_t *entity);
4
#include "../include/enemy.h"5
#include "../include/gate.h"6
#include "../include/door.h"7
#include "../include/const.h"9
entity_t *entity_create(entity_e type) {10
entity_t *entity = malloc(sizeof(entity_t));
80
void entity_free(entity_t *entity) {81
free(entity);82
}84
rect_t entity_get_rect(entity_t *entity) {85
rect_t rect;86
switch (entity->type) {87
case ENTITY_ENEMY:88
rect = rect_from_pos(entity->enemy->pos, ENEMY_WIDTH, ENEMY_HEIGHT);89
break;90
case ENTITY_GATE:91
rect = rect_from_pos((pos_t) { entity->gate->pos.x, entity->gate->pos.y - TILE_HEIGHT }, ENEMY_WIDTH, ENEMY_HEIGHT);92
break;93
case ENTITY_DOOR: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);98
break;99
}100
return rect;101
}
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
}255
rect_t entity_rect = entity_get_rect(entity);256
if (rect_collide(shooting_rect, entity_rect)) {257
float entity_x = entity_rect.x + entity_rect.width / 2.0;258
float entity_distance = fabs(entity_x - (player->dir > 0 ? player->pos.x + PLAYER_WIDTH : player->pos.x));
286
if (rect_collide(shooting_rect, tile_rect)) {287
if (player->dir > 0) {288
player->shooting_distance = tile->pos.x - (player->pos.x + PLAYER_WIDTH);303
player->target_entity = NULL;289
// clear target if it stands behind a wall290
if (player->target_entity != NULL) {291
rect_t rect = entity_get_rect(player->target_entity);292
if (rect.x > tile->pos.x) {293
player->target_entity = NULL;294
}295
}296
}297
else {298
player->shooting_distance = player->pos.x - (tile->pos.x + TILE_WIDTH);307
player->target_entity = NULL;299
// clear target if it stands behind a wall300
if (player->target_entity != NULL) {301
rect_t rect = entity_get_rect(player->target_entity);302
if (rect.x + rect.width < tile->pos.x + TILE_WIDTH) {303
player->target_entity = NULL;304
}305
}306
}307
}308
}