diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-06-25 22:44:23 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-06-25 22:44:23 +0100 |
commit | 6bd6757583510ba3edf75451309e4b8ec8c9b0f1 (patch) | |
tree | e63f401238e4118ff6d1670ea86508f0181c19ea /src/map.c | |
parent | 2d7fbc07acf0c5766d662d2629e72600b65f744b (diff) | |
download | gold-mine-run-6bd6757583510ba3edf75451309e4b8ec8c9b0f1.tar.gz gold-mine-run-6bd6757583510ba3edf75451309e4b8ec8c9b0f1.zip |
Add entity system, add new enemy (snake)
Diffstat (limited to 'src/map.c')
-rw-r--r-- | src/map.c | 45 |
1 files changed, 41 insertions, 4 deletions
@@ -1,18 +1,33 @@ #include <stdint.h> +#include <stdio.h> #include <string.h> +#include <stdlib.h> #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() |