QUOTE: Let your heart guide you always.

freezo

A retro platform game

entity.c (2383B)


      1#include <stdlib.h>
      2
      3#include "../include/entity.h"
      4#include "../include/enemy.h"
      5#include "../include/gate.h"
      6#include "../include/door.h"
      7#include "../include/const.h"
      8
      9entity_t *entity_create(entity_e type) {
     10 entity_t *entity = malloc(sizeof(entity_t));
     11  entity->type = type;
     12  switch (entity->type) {
     13    case ENTITY_ENEMY:
     14      entity->is_bad = true;
     15      break;
     16    case ENTITY_GATE:
     17      entity->is_bad = true;
     18      break;
     19    case ENTITY_DOOR:
     20      entity->is_bad = false;
     21      break;
     22    case ENTITY_BOSS:
     23      entity->is_bad = true;
     24      break;
     25  }
     26  return entity;
     27}
     28
     29void entity_update(entity_t *entity, game_t *game) {
     30  switch (entity->type) {
     31    case ENTITY_ENEMY:
     32      enemy_update(entity->enemy, game);
     33      break;
     34    case ENTITY_GATE:
     35      gate_update(entity->gate, game);
     36      break;
     37    case ENTITY_DOOR:
     38      door_update(entity->door, game);
     39      break;
     40    case ENTITY_BOSS:
     41      boss_update(entity->boss, game);
     42      break;
     43  }
     44}
     45
     46void entity_draw(entity_t *entity, game_t *game) {
     47  switch (entity->type) {
     48    case ENTITY_ENEMY:
     49      enemy_draw(entity->enemy, game);
     50      break;
     51    case ENTITY_GATE:
     52      gate_draw(entity->gate, game);
     53      break;
     54    case ENTITY_DOOR:
     55      door_draw(entity->door, game);
     56      break;
     57    case ENTITY_BOSS:
     58      boss_draw(entity->boss, game);
     59      break;
     60  }
     61}
     62
     63void entity_detach(entity_t *entity) {
     64  switch (entity->type) {
     65    case ENTITY_ENEMY:
     66      enemy_free(entity->enemy);
     67      break;
     68    case ENTITY_GATE:
     69      gate_free(entity->gate);
     70      break;
     71    case ENTITY_DOOR:
     72      door_free(entity->door);
     73      break;
     74    case ENTITY_BOSS:
     75      boss_free(entity->boss);
     76      break;
     77  }
     78}
     79
     80void entity_free(entity_t *entity) {
     81  free(entity);
     82}
     83
     84rect_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(entity->boss->pos, BOSS_WIDTH, BOSS_HEIGHT);
     98      break;
     99  }
    100  return rect;
    101}