From 6bd6757583510ba3edf75451309e4b8ec8c9b0f1 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 25 Jun 2023 22:44:23 +0100 Subject: Add entity system, add new enemy (snake) --- src/map.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'src/map.c') diff --git a/src/map.c b/src/map.c index 4c5390f..c62c5c1 100644 --- a/src/map.c +++ b/src/map.c @@ -1,18 +1,33 @@ #include +#include #include +#include #include "vga.h" #include "data.h" +#include "entities.h" #include "player.h" +#include "snake.h" #include "map.h" +typedef enum +{ + Player = 0, + Snake, +} EntityType; + /* current map; set via map_init */ static const uint8_t *cmap; static uint8_t gold[MAP_W * MAP_H]; static uint8_t total_gold; +static void (* const init[])(Entity *) = +{ + snake_init, +}; + void map_init(const uint8_t map[]) { cmap = map; @@ -27,18 +42,40 @@ void map_init(const uint8_t map[]) if (gold[i] != 0xff) total_gold++; + entities_init(); + + Entity *e; + /* spawn entities, 0xff is the list terminator */ for ( const uint8_t *ent = map + MAP_W * MAP_H * 2; *ent != 0xff; ent += 4 ) - switch (*ent) + { + /* the player is not part of the entity system */ + if (*ent == Player) { - case Player: - player_init(ent[1] * MAP_TILE_W, ent[2] * MAP_TILE_H, ent[3] & 1); - break; + player_init(ent[1] * MAP_TILE_W, ent[2] * MAP_TILE_H, ent[3] & 1); + continue; } + + e = entities_new(); +#ifdef DEBUG + if (!e) + { + set_mode(3); + fprintf(stderr, "ERROR: run out of entities\n"); + exit(1); + } +#endif + + e->x = ent[1] * MAP_TILE_W; + e->y = ent[2] * MAP_TILE_H; + e->dir = ent[3] & 1; + + init[*ent - 1](e); + } } void map_render() -- cgit v1.2.3