simple entity system

This commit is contained in:
kdx 2023-03-17 10:51:17 +01:00
parent 7ef2f09cfd
commit ba8cb4fcea
7 changed files with 145 additions and 1 deletions

View File

@ -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

28
src/entity.h Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include "player.h"
#include <stdbool.h>
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;
};
};

49
src/game.c Normal file
View File

@ -0,0 +1,49 @@
#include "game.h"
#include <string.h>
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;
}

15
src/game.h Normal file
View File

@ -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);

View File

@ -1,4 +1,8 @@
#include "lzy.h"
#include "game.h"
#include "player.h"
#include <stdio.h>
#include <stdlib.h>
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;
}

25
src/player.c Normal file
View File

@ -0,0 +1,25 @@
#include "player.h"
#include "entity.h"
#include "game.h"
#include <string.h>
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;
}

10
src/player.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
typedef struct {
int _;
} Player;
struct Entity;
struct Game;
void player_init(struct Entity *this);