From ba8cb4fcea09190bbe6e80c45ef59314feea8600 Mon Sep 17 00:00:00 2001 From: kdx Date: Fri, 17 Mar 2023 10:51:17 +0100 Subject: [PATCH] simple entity system --- CMakeLists.txt | 4 +++- src/entity.h | 28 ++++++++++++++++++++++++++++ src/game.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/game.h | 15 +++++++++++++++ src/main.c | 15 +++++++++++++++ src/player.c | 25 +++++++++++++++++++++++++ src/player.h | 10 ++++++++++ 7 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/entity.h create mode 100644 src/game.c create mode 100644 src/game.h create mode 100644 src/player.c create mode 100644 src/player.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 74aa891..28424ad 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,10 @@ include(Fxconv) find_package(Gint 2.9 REQUIRED) set(SOURCES - src/main.c + src/game.c src/lzy.c + src/main.c + src/player.c ) set(ASSETS diff --git a/src/entity.h b/src/entity.h new file mode 100644 index 0000000..9f5861c --- /dev/null +++ b/src/entity.h @@ -0,0 +1,28 @@ +#pragma once +#include "player.h" +#include + +struct Game; + +typedef enum { + ET_NONE, + ET_PLAYER +} EntityType; + +typedef struct Entity Entity; +struct Entity { + unsigned long uuid; + void (*update)(Entity *this, struct Game *g); + void (*draw)(Entity *this, struct Game *g); + unsigned int type; + int id; + int pos[2]; + double vel[2]; + double rem[2]; + int width; + int height; + bool solid; + union { + Player player; + }; +}; diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..2a3ceeb --- /dev/null +++ b/src/game.c @@ -0,0 +1,49 @@ +#include "game.h" +#include + +void +game_init(Game *this) +{ + memset(this, 0, sizeof(*this)); +} + +void +game_deinit(Game *this) +{ + (void)this; +} + +void +game_update(Game *this) +{ + for (int i = 0; i < MAX_ENTITIES; i++) { + Entity *const e = &this->entities[i]; + if (e->type != ET_NONE && e->update != NULL) + e->update(e, this); + } +} + +void +game_draw(Game *this) +{ + for (int i = 0; i < MAX_ENTITIES; i++) { + Entity *const e = &this->entities[i]; + if (e->type != ET_NONE && e->draw != NULL) + e->draw(e, this); + } +} + +Entity * +game_create_entity(Game *this) +{ + Entity *e = &this->entities[MAX_ENTITIES - 1]; + for (int i = 0; i < MAX_ENTITIES; i++) + if (this->entities[i].type == ET_NONE) { + e = &this->entities[i]; + break; + } + e->type = ET_NONE; + e->uuid = this->uuid; + this->uuid += 1; + return e; +} diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..185c89b --- /dev/null +++ b/src/game.h @@ -0,0 +1,15 @@ +#pragma once +#include "entity.h" + +enum { MAX_ENTITIES = 64 }; + +typedef struct Game { + unsigned int uuid; + Entity entities[MAX_ENTITIES]; +} Game; + +void game_init(Game *this); +void game_deinit(Game *this); +void game_update(Game *this); +void game_draw(Game *this); +Entity *game_create_entity(Game *this); diff --git a/src/main.c b/src/main.c index 03b612f..744c96b 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,8 @@ #include "lzy.h" +#include "game.h" +#include "player.h" +#include +#include int main(void) { @@ -8,6 +12,15 @@ int main(void) return 1; } + Game *const game = malloc(sizeof(Game)); + if (game == NULL) { + LZY_Log("malloc failed"); + LZY_Quit(); + return 1; + } + game_init(game); + player_init(game_create_entity(game)); + while (!LZY_ShouldQuit()) { LZY_CycleEvents(); @@ -17,6 +30,8 @@ int main(void) LZY_DrawEnd(); } + game_deinit(game); + free(game); LZY_Quit(); return 0; } diff --git a/src/player.c b/src/player.c new file mode 100644 index 0000000..d751857 --- /dev/null +++ b/src/player.c @@ -0,0 +1,25 @@ +#include "player.h" +#include "entity.h" +#include "game.h" +#include + +static void +player_update(Entity *this, Game *g) +{ + (void)this, (void)g; +} + +static void +player_draw(Entity *this, Game *g) +{ + (void)this, (void)g; +} + +void +player_init(Entity *this) +{ + memset(this, 0, sizeof(*this)); + this->type = ET_PLAYER; + this->update = player_update; + this->draw = player_draw; +} diff --git a/src/player.h b/src/player.h new file mode 100644 index 0000000..a6fe6c9 --- /dev/null +++ b/src/player.h @@ -0,0 +1,10 @@ +#pragma once + +typedef struct { + int _; +} Player; + +struct Entity; +struct Game; + +void player_init(struct Entity *this);