More ecs tweaks

This commit is contained in:
Carlos Sanchez 2024-08-19 12:36:35 -04:00
parent 4a27948ead
commit 9554d0062f
2 changed files with 155 additions and 115 deletions

@ -1 +1 @@
Subproject commit 75417f662edaf756c1c286f533007df6fcfd5aaa
Subproject commit e5ec412b3b20ff87106aae1dc50148b0af7a071d

268
maze.c
View File

@ -10,7 +10,6 @@
#include "unigi/unigi.platform.sdl1/src/main.c"
#include "ecs2.h"
// #include "ecs2_comps.h"
#include "keys.h"
#include <stdlib.h>
@ -296,22 +295,29 @@ typedef struct {
int timer;
} ecs_autorotate;
// A component which has pointers back to global world state.
// Component which enables synchronized scale y from 0 to 1 or 1 to 0. It is
// expected that an external source is modifying the value
typedef struct {
worldstate *state;
} ecs_worldstate;
haloo3d_obj_instance *obj;
mfloat_t *scale;
int *timer;
} ecs_syncgrow;
#define MAXGROWERS 10
// A component which has pointers back to global world state.
// typedef struct {
// worldstate *state;
// } ecs_worldstate;
// #define MAXGROWERS 10
// A component which holds onto data specifically for world
// entities. You can think of it as private data so people
// can't poke at the worldstate data
typedef struct {
worldstate *state;
int numgrowers;
haloo3d_obj_instance *growers[MAXGROWERS];
haloo3d_obj_instance *walls;
uint16_t timer;
haloo3d_obj *wallmodel;
int scaletimer;
mfloat_t scaleto;
} ecs_world;
// void sys_move_placement(ecs_movement *m, ecs_placement *p) {
@ -319,32 +325,54 @@ typedef struct {
// p->yaw += m->yawvel;
// }
void sys_autonav(ecs_autonav *nav, ecs_placement *p) {
if (nav->timer <= 0) {
p->pos = nav->dest;
return;
void sys_syncgrow(ecs_syncgrow *sg) {
// Only perform logic when a timer is running
if (*sg->timer) {
if (*sg->timer == 1) {
// Just jump right to it on the last frame. We don't run on frame 0
sg->obj->scale.y = *sg->scale;
} else {
mfloat_t scaleleft = *sg->scale - sg->obj->scale.y;
sg->obj->scale.y += scaleleft / *sg->timer;
}
}
}
void sys_autonav(ecs_autonav *nav, ecs_placement *p) {
// Only perform logic when the timer is running
if (nav->timer) {
// On the last frame, just set it outright, nothing else to do
if (nav->timer == 1) {
p->pos = nav->dest;
} else {
mfloat_t xdiff = nav->dest.x - p->pos.x;
mfloat_t ydiff = nav->dest.y - p->pos.y;
mfloat_t zdiff = nav->dest.z - p->pos.z;
p->pos.x += xdiff / nav->timer;
p->pos.y += ydiff / nav->timer;
p->pos.z += zdiff / nav->timer;
}
nav->timer--;
}
mfloat_t xdiff = nav->dest.x - p->pos.x;
mfloat_t ydiff = nav->dest.y - p->pos.y;
mfloat_t zdiff = nav->dest.z - p->pos.z;
p->pos.x += xdiff / nav->timer;
p->pos.y += ydiff / nav->timer;
p->pos.z += zdiff / nav->timer;
nav->timer--;
}
void sys_autorotate(ecs_autorotate *arot, ecs_placement *p) {
if (arot->timer <= 0) {
p->rot = arot->dest;
return;
// Only perform logic when the timer is running
if (arot->timer) {
// On the last frame, set it outright
if (arot->timer == 1) {
p->rot = arot->dest;
} else {
mfloat_t xdiff = arot->dest.x - p->rot.x;
mfloat_t ydiff = arot->dest.y - p->rot.y;
p->rot.x += xdiff / arot->timer;
p->rot.y += ydiff / arot->timer;
}
arot->timer--;
}
mfloat_t xdiff = arot->dest.x - p->rot.x;
mfloat_t ydiff = arot->dest.y - p->rot.y;
p->rot.x += xdiff / arot->timer;
p->rot.y += ydiff / arot->timer;
arot->timer--;
}
// update camera with placement value on entity
void sys_camera(ecs_camera *cam, ecs_placement *p) {
cam->camera->pos = p->pos;
cam->camera->yaw = p->rot.x;
@ -354,47 +382,36 @@ void sys_camera(ecs_camera *cam, ecs_placement *p) {
}
void sys_world(ecs_world *w) {
float scaleleft;
const int spinspeed = w->state->fps * 4 / 5;
switch (w->state->state) {
case WSTATE_INIT:
maze_generate(w->state->maze, MAZESIZE, &w->state->start, &w->state->end);
maze_wall_generate(w->state->maze, MAZESIZE, w->walls->model);
maze_wall_generate(w->state->maze, MAZESIZE, w->wallmodel);
eprintf("INIT MAZE COMPLETE, spinning up walls\n");
w->state->state = WSTATE_SPINUP;
w->timer = spinspeed;
w->walls->scale.y = 0;
w->scaletimer = spinspeed;
w->scaleto = 1;
break;
case WSTATE_SPINUP:
// Bring walls up, timer down
for (int i = 0; i < w->numgrowers; i++) {
scaleleft = 1 - w->growers[i]->scale.y;
w->growers[i]->scale.y += scaleleft / w->timer;
}
w->timer--;
if (w->timer == 0) {
w->scaletimer--;
if (w->scaletimer == 0) {
eprintf("SPINUP COMPLETE, starting gameplay\n");
w->walls->scale.y = 1;
// Start gameplay. We won't know when it's done
// Start gameplay. We won't know when it's done
w->state->state = WSTATE_GAMEPLAY;
}
break;
case WSTATE_GAMEOVER:
w->timer = spinspeed;
w->scaletimer = spinspeed;
w->scaleto = 0;
w->state->state = WSTATE_SPINDOWN;
eprintf("GAME OVER, spinning down\n");
break;
case WSTATE_SPINDOWN:
// Bring walls down, timer down
for (int i = 0; i < w->numgrowers; i++) {
scaleleft = w->growers[i]->scale.y;
w->growers[i]->scale.y -= scaleleft / w->timer;
}
w->timer--;
if (w->timer == 0) {
w->scaletimer--;
if (w->scaletimer == 0) {
eprintf("SPINDOWN COMPLETE, reinitializing\n");
w->walls->scale.y = 0;
// Start gameplay. We won't know when it's done
// Start gameplay. We won't know when it's done
w->state->state = WSTATE_INIT;
}
break;
@ -414,24 +431,30 @@ typedef struct {
uint8_t dir;
uint32_t timer;
struct vec2i mpos;
worldstate *ws;
} ecs_smartai;
static void sys_smartai(ecs_smartai *smartai, ecs_worldstate *ws,
static void sys_smartai(ecs_smartai *smartai, ecs_placement *p,
ecs_autonav *anav, ecs_autorotate *arot) {
const int actiontime = ws->state->fps / 2;
const int actiontime = smartai->ws->fps / 2;
const int rotdelaytime = 2 * actiontime / 5;
switch (smartai->state) {
case SAI_INIT:
// Here, we wait until the world state is spinup, in which
// case we can spawn and face
if (ws->state->state == WSTATE_SPINUP) {
smartai->mpos = ws->state->start;
anav->dest.x = ws->state->cellsize * (ws->state->start.x + 0.5);
anav->dest.z = ws->state->cellsize * (ws->state->start.y + 0.5);
if (smartai->ws->state == WSTATE_SPINUP) {
smartai->mpos = smartai->ws->start;
p->pos.x = smartai->ws->cellsize * (smartai->ws->start.x + 0.5);
p->pos.z = smartai->ws->cellsize * (smartai->ws->start.y + 0.5);
// Reset autonav
anav->dest = p->pos;
anav->timer = 0;
smartai->dir = maze_longesthallway(
ws->state->maze, MAZESIZE, ws->state->start.x, ws->state->start.y);
arot->dest.x = dirtoyaw(smartai->dir);
smartai->dir =
maze_longesthallway(smartai->ws->maze, MAZESIZE, smartai->ws->start.x,
smartai->ws->start.y);
p->rot.x = dirtoyaw(smartai->dir);
// reset autorotate
arot->dest = p->rot;
arot->timer = 0;
smartai->state = SAI_READY;
eprintf("PLAYER READY: %f %f (%f), waiting for spinup\n", anav->dest.x,
@ -439,7 +462,7 @@ static void sys_smartai(ecs_smartai *smartai, ecs_worldstate *ws,
}
break;
case SAI_READY:
if (ws->state->state == WSTATE_GAMEPLAY) {
if (smartai->ws->state == WSTATE_GAMEPLAY) {
smartai->state = SAI_GAMEPLAY;
eprintf("PLAYER STARTING GAMEPLAY\n");
}
@ -474,24 +497,24 @@ static void sys_smartai(ecs_smartai *smartai, ecs_worldstate *ws,
// most important thing
if (anav->timer == 0) {
eprintf("SMARTAI: %d TIMER: %d DIR: %d POS: (%f, %f)\n",
smartai->rotstate, smartai->timer, smartai->dir, anav->dest.x,
anav->dest.z);
if (smartai->mpos.x == ws->state->end.x &&
smartai->mpos.y == ws->state->end.y) {
smartai->rotstate, smartai->timer, smartai->dir, p->pos.x,
p->pos.z);
if (smartai->mpos.x == smartai->ws->end.x &&
smartai->mpos.y == smartai->ws->end.y) {
eprintf("YOU WIN\n");
ws->state->state = WSTATE_GAMEOVER;
smartai->ws->state = WSTATE_GAMEOVER;
smartai->state = SAI_INIT;
return;
}
// Player can only move forward if there's nothing in front of them
if (maze_connected(ws->state->maze, smartai->mpos.x, smartai->mpos.y,
if (maze_connected(smartai->ws->maze, smartai->mpos.x, smartai->mpos.y,
MAZESIZE, smartai->dir)) {
struct vec2i movement = dirtovec(smartai->dir);
smartai->mpos.x += movement.x;
smartai->mpos.y += movement.y;
anav->timer = actiontime;
anav->dest.x += ws->state->cellsize * movement.x;
anav->dest.z += ws->state->cellsize * movement.y;
anav->dest.x = p->pos.x + smartai->ws->cellsize * movement.x;
anav->dest.z = p->pos.z + smartai->ws->cellsize * movement.y;
smartai->rotstate = 0;
}
// Figure out if a rotation should be scheduled
@ -500,13 +523,13 @@ static void sys_smartai(ecs_smartai *smartai, ecs_worldstate *ws,
// rotation based on the FUTURE direction we want to turn.
uint8_t rightdir = TURNRIGHT(smartai->dir);
uint8_t leftdir = TURNLEFT(smartai->dir);
if (maze_connected(ws->state->maze, smartai->mpos.x, smartai->mpos.y,
if (maze_connected(smartai->ws->maze, smartai->mpos.x, smartai->mpos.y,
MAZESIZE, rightdir)) {
// Always choose right over left
smartai->rotstate = 1;
smartai->timer = rotdelaytime;
eprintf("WILL TURN RIGHT TO: %d\n", rightdir);
} else if (!maze_connected(ws->state->maze, smartai->mpos.x,
} else if (!maze_connected(smartai->ws->maze, smartai->mpos.x,
smartai->mpos.y, MAZESIZE, smartai->dir)) {
// We only move left if the player can't move forward or right
smartai->rotstate = 2;
@ -520,18 +543,16 @@ static void sys_smartai(ecs_smartai *smartai, ecs_worldstate *ws,
}
}
// static void sys_ecs_world(ecs_world *world, ecs_maze *maze, ecs_moveto *mt,
// ecs_rotateto *rt) {}
// Setup ECS system for our game
ECS_START(mecs)
ECS_COMPONENT(ecs_worldstate);
// ECS_COMPONENT(ecs_worldstate);
ECS_COMPONENT(ecs_world);
ECS_COMPONENT(ecs_autonav);
ECS_COMPONENT(ecs_autorotate);
ECS_COMPONENT(ecs_placement);
ECS_COMPONENT(ecs_camera);
ECS_COMPONENT(ecs_smartai);
ECS_COMPONENT(ecs_syncgrow);
ECS_END(mecs)
// And then a copy of the components here... that sucksssss
@ -542,16 +563,17 @@ ECS_CID(ecs_autorotate, 3);
ECS_CID(ecs_placement, 4);
ECS_CID(ecs_camera, 5);
ECS_CID(ecs_smartai, 6);
ECS_CID(ecs_syncgrow, 7);
ECS_SYSTEM1(mecs, sys_world, ecs_world);
ECS_SYSTEM1(mecs, sys_syncgrow, ecs_syncgrow);
ECS_SYSTEM2(mecs, sys_autonav, ecs_autonav, ecs_placement);
ECS_SYSTEM2(mecs, sys_autorotate, ecs_autorotate, ecs_placement);
ECS_SYSTEM2(mecs, sys_camera, ecs_camera, ecs_placement);
ECS_SYSTEM4(mecs, sys_smartai, ecs_smartai, ecs_worldstate, ecs_autonav,
ECS_SYSTEM4(mecs, sys_smartai, ecs_smartai, ecs_placement, ecs_autonav,
ecs_autorotate);
void init_floortexture(haloo3d_fb *floort) {
// uint16_t cols[4] = {0xFD93, 0xFB83, 0xFFFF}; //, 0xFDDD};
uint16_t cols[1] = {0xFD93};
haloo3d_fb_init_tex(floort, 64, 64);
haloo3d_apply_alternating(floort, cols, 1);
@ -559,8 +581,7 @@ void init_floortexture(haloo3d_fb *floort) {
}
void init_ceilingtexture(haloo3d_fb *ceilt) {
uint16_t cols[1] = {0xFFFF}; //, 0xFDDD};
// haloo3d_apply_alternating(floort, cols, 2);
uint16_t cols[1] = {0xFFFF};
haloo3d_fb_init_tex(ceilt, 64, 64);
haloo3d_apply_alternating(ceilt, cols, 1);
haloo3d_apply_noise(ceilt, NULL, 1.0 / 4);
@ -583,6 +604,27 @@ void init_starttexture(haloo3d_fb *startt) {
haloo3d_apply_rect(startt, &rect, 0xF777, 1);
}
void init_mazeinstances(haloo3d_obj_instance *floori,
haloo3d_obj_instance *ceili,
haloo3d_obj_instance *walli,
haloo3d_obj_instance *starti) {
floori->cullbackface = 0;
ceili->cullbackface = 0;
walli->cullbackface = 0;
starti->cullbackface = 0;
vec3(floori->scale.v, HSCALE, 1, HSCALE);
vec3(ceili->scale.v, HSCALE, 1, HSCALE);
vec3(walli->scale.v, HSCALE, 0, HSCALE);
vec3(starti->scale.v, 1, 0, 1);
floori->pos.x += MAZESIZE / 2.0 * HSCALE;
floori->pos.z += MAZESIZE / 2.0 * HSCALE;
ceili->pos.x += MAZESIZE / 2.0 * HSCALE;
ceili->pos.z += MAZESIZE / 2.0 * HSCALE;
walli->pos.x += MAZESIZE / 2.0 * HSCALE;
walli->pos.z += MAZESIZE / 2.0 * HSCALE;
ceili->pos.y = 1;
}
int main() { // int argc, char **argv) {
haloo3d_easystore storage;
@ -597,10 +639,11 @@ int main() { // int argc, char **argv) {
render.tprint.fb = &screen;
eprintf("Initialized renderer\n");
haloo3d_easytimer frametimer, sdltimer, filltimer;
haloo3d_easytimer frametimer, sdltimer, filltimer, logictimer;
haloo3d_easytimer_init(&frametimer, AVGWEIGHT);
haloo3d_easytimer_init(&sdltimer, AVGWEIGHT);
haloo3d_easytimer_init(&filltimer, AVGWEIGHT);
haloo3d_easytimer_init(&logictimer, AVGWEIGHT);
// Load the junk + generate stuff
haloo3d_obj *flooro = haloo3d_easystore_addobj(&storage, "floor");
@ -645,21 +688,7 @@ int main() { // int argc, char **argv) {
haloo3d_easyrender_addinstance(&render, ceilo, ceilt);
haloo3d_obj_instance *starti =
haloo3d_easyrender_addinstance(&render, starto, startt);
floori->cullbackface = 0;
ceili->cullbackface = 0;
walli->cullbackface = 0;
starti->cullbackface = 0;
vec3(floori->scale.v, HSCALE, 1, HSCALE);
vec3(ceili->scale.v, HSCALE, 1, HSCALE);
vec3(walli->scale.v, HSCALE, 1, HSCALE);
// vec3(walli->scale.v, HSCALE, 0, HSCALE);
floori->pos.x += MAZESIZE / 2.0 * HSCALE;
floori->pos.z += MAZESIZE / 2.0 * HSCALE;
ceili->pos.x += MAZESIZE / 2.0 * HSCALE;
ceili->pos.z += MAZESIZE / 2.0 * HSCALE;
walli->pos.x += MAZESIZE / 2.0 * HSCALE;
walli->pos.z += MAZESIZE / 2.0 * HSCALE;
ceili->pos.y = 1; //-1;
init_mazeinstances(floori, ceili, walli, starti);
eprintf("Setup all object instances\n");
unigi_type_event event;
@ -684,7 +713,7 @@ int main() { // int argc, char **argv) {
haloo3d_debugconsole dc;
haloo3d_debugconsole_init(&dc);
haloo3d_debugconsole_set(&dc, "render/fps.i", &fps);
haloo3d_debugconsole_set(&dc, "render/fps.i", &wstate.fps);
haloo3d_debugconsole_set(&dc, "render/fov.f", &fov);
haloo3d_debugconsole_set(&dc, "render/trifunc.i", &render.trifunc);
haloo3d_debugconsole_set(&dc, "render/ditherstart.f", &ditherstart);
@ -700,24 +729,30 @@ int main() { // int argc, char **argv) {
ecs_eid worldid = mecs_newentity(&ecs, 0);
eprintf("World eid: %d\n", worldid);
ECS_SETCOMPONENT(&ecs, worldid,
ecs_world){.state = &wstate, .walls = walli, .timer = 0};
// THis is horrible but whatever. The yshould be entities
ecs.c_ecs_world[worldid].growers[0] = walli;
ecs.c_ecs_world[worldid].growers[1] = starti;
ecs.c_ecs_world[worldid].numgrowers = 2;
ECS_SETCOMPONENT(&ecs, worldid, ecs_world){
.state = &wstate, .wallmodel = wallo, .scaletimer = 0};
ecs_world *eworld = ecs.c_ecs_world + worldid;
// Setup some dynamic objects
ecs_eid wallid = mecs_newentity(&ecs, 0);
ECS_SETCOMPONENT(&ecs, wallid, ecs_syncgrow){
.obj = walli, .scale = &eworld->scaleto, .timer = &eworld->scaletimer};
ecs_eid startid = mecs_newentity(&ecs, 0);
ECS_SETCOMPONENT(&ecs, startid, ecs_syncgrow){
.obj = starti, .scale = &eworld->scaleto, .timer = &eworld->scaletimer};
// Player is ofc most complicated
ecs_eid playerid = mecs_newentity(&ecs, 0);
eprintf("Player eid: %d\n", playerid);
ECS_SETCOMPONENT(&ecs, playerid, ecs_worldstate){.state = &wstate};
ECS_SETCOMPONENT(&ecs, playerid, ecs_placement){};
// ECS_SETCOMPONENT(&ecs, playerid, ecs_worldstate){.state = &wstate};
ECS_SETCOMPONENT(&ecs, playerid, ecs_placement){
.pos = render.camera.pos,
.rot = {.x = render.camera.yaw, .y = render.camera.pitch}};
ECS_SETCOMPONENT(&ecs, playerid, ecs_camera){.camera = &render.camera};
ECS_SETCOMPONENT(&ecs, playerid, ecs_autonav){.timer = 0,
.dest = render.camera.pos};
ECS_SETCOMPONENT(&ecs, playerid, ecs_autorotate){
.timer = 0, .dest = {.x = render.camera.yaw, .y = render.camera.pitch}};
ECS_SETCOMPONENT(&ecs, playerid,
ecs_smartai){.state = SAI_INIT, .rotstate = 0, .timer = 0};
ECS_SETCOMPONENT(&ecs, playerid, ecs_autonav){.timer = 0};
ECS_SETCOMPONENT(&ecs, playerid, ecs_autorotate){.timer = 0};
ECS_SETCOMPONENT(&ecs, playerid, ecs_smartai){
.state = SAI_INIT, .ws = &wstate, .rotstate = 0, .timer = 0};
// -----------------------------------
// Actual rendering
@ -759,13 +794,16 @@ int main() { // int argc, char **argv) {
// Game logic?
// ---------------------------
haloo3d_easytimer_start(&logictimer);
for (int i = 0; i < ECS_MAXENTITIES; i++) {
sys_world_run(&ecs, i);
sys_smartai_run(&ecs, i);
sys_syncgrow_run(&ecs, i);
sys_autonav_run(&ecs, i);
sys_autorotate_run(&ecs, i);
sys_camera_run(&ecs, i);
}
haloo3d_easytimer_end(&logictimer);
starti->pos = render.camera.pos;
starti->pos.z -= 1;
@ -797,13 +835,15 @@ int main() { // int argc, char **argv) {
haloo3d_easytimer_end(&filltimer);
haloo3d_print(&render.tprint,
"Last frame: %05.2f (%05.2f)\nLast fill: %05.2f "
"(%05.2f)\nLast SDLFl: %05.2f "
"(%05.2f)\nTris: %d / %d\nVerts: %d\n",
"Pframe: %05.2f (%05.2f)\nPSDLFl: %05.2f "
"(%05.2f)\nFill: %05.2f "
"(%05.2f)\nLogic: %05.2f (%05.2f)\nTris: %d / %d\nVerts: "
"%d\nWState: %d",
frametimer.last * 1000, frametimer.sum * 1000,
sdltimer.last * 1000, sdltimer.sum * 1000,
filltimer.last * 1000, filltimer.sum * 1000,
sdltimer.last * 1000, sdltimer.sum * 1000, totaldrawn,
render.totalfaces, render.totalverts);
logictimer.last * 1000, logictimer.sum * 1000, totaldrawn,
render.totalfaces, render.totalverts, wstate.state);
haloo3d_easytimer_start(&sdltimer);
unigi_graphics_blit(0, (unigi_type_color *)screen.buffer,