simple entity system
This commit is contained in:
parent
7ef2f09cfd
commit
ba8cb4fcea
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
};
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
15
src/main.c
15
src/main.c
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
typedef struct {
|
||||
int _;
|
||||
} Player;
|
||||
|
||||
struct Entity;
|
||||
struct Game;
|
||||
|
||||
void player_init(struct Entity *this);
|
Loading…
Reference in New Issue