diff --git a/terrain.c b/terrain.c index 29e1aa0..51dcc81 100644 --- a/terrain.c +++ b/terrain.c @@ -43,7 +43,7 @@ #define INIT_MINLIGHT 0.2 #define INIT_PLAYERSPEED \ - { .x = 0, .y = 0, .z = -40.5 } + { .x = 0, .y = 0, .z = -0.5 } // Some globals you can mess around with potentially int fps = 60; @@ -65,20 +65,17 @@ void gen_terrain(struct vec3i pos, haloo3d_obj *model) { // Don't allow the model to have more than some amount of faces/vertices. haloo3d_obj_resetfixed(model, CHUNKSIZE * CHUNKSIZE * 4, CHUNKSIZE * CHUNKSIZE * 4); - // Generate the "ocean" floor. // TODO: You need to change this to be a large plane that follows the player // so you can use lighting on the islands but not on the water // haloo3d_gen_paletteuv(0xF26F, model->vtexture[model->numvtextures++].v); struct vec3 seacol = haloo3d_gen_paletteuv(0xF000 | (rand() & 0xFFF)); int seauv = haloo3d_obj_addvtexture(model, seacol); - // eprintf("SEAUV: %f,%f\n", seacol.x, seacol.y); - // 0xF26F)); // clang-format off // REMEMBER: NEGATIVE Z IS FORWARD int tl = haloo3d_obj_addvertex(model, (struct vec4){.x = 0, .y = 0, .z = 0, .w = 1}); - int tr = haloo3d_obj_addvertex(model, (struct vec4){.x = CHUNKSIZE, .y = 0, .z = 0, .w = 1}); - int bl = haloo3d_obj_addvertex(model, (struct vec4){.x = 0, .y = 0, .z = CHUNKSIZE, .w = 1}); - int br = haloo3d_obj_addvertex(model, (struct vec4){.x = CHUNKSIZE, .y = 0, .z = CHUNKSIZE, .w = 1}); + int tr = haloo3d_obj_addvertex(model, (struct vec4){.x = CHUNKSIZE / 2.0, .y = 0, .z = 0, .w = 1}); + int bl = haloo3d_obj_addvertex(model, (struct vec4){.x = 0, .y = 0, .z = CHUNKSIZE / 2.0, .w = 1}); + int br = haloo3d_obj_addvertex(model, (struct vec4){.x = CHUNKSIZE / 2.0, .y = 0, .z = CHUNKSIZE / 2.0, .w = 1}); // clang-format on haloo3d_facei face; fastface(face, seauv, bl, br, tl); @@ -130,7 +127,7 @@ void player_chunkload(ecs_placement *ppos, render_context *ctx, tecs *ecs) { // Find + iterate over killable objects placed in world int mcount = tecs_query( ecs, ecs_playergarbage_fl | ecs_placement_fl | ecs_object_fl, matches); - eprintf("KILLABLE OBJS: %d PCHUNK: %d,%d\n", mcount, pchunk.x, pchunk.y); + // eprintf("KILLABLE OBJS: %d PCHUNK: %d,%d\n", mcount, pchunk.x, pchunk.y); for (int i = 0; i < mcount; i++) { ecs_placement *placement = &ECS_GETCOMPONENT(ecs, matches[i], ecs_placement); @@ -340,12 +337,88 @@ void sys_playergarbage(ecs_playergarbage *pg, ecs_object *ob, tecs **ecs) { } } +void sys_placement_lock(ecs_placement_lock *lock, ecs_placement *pl) { + if (lock->options & TECS_PLOCK_LOCKUP) { + pl->up = lock->link->up; + } + if (lock->options & TECS_PLOCK_LOCKPOSX) { + pl->pos.x = lock->link->pos.x; + } + if (lock->options & TECS_PLOCK_LOCKPOSY) { + pl->pos.y = lock->link->pos.y; + } + if (lock->options & TECS_PLOCK_LOCKPOSZ) { + pl->pos.z = lock->link->pos.z; + } + if (lock->options & TECS_PLOCK_LOCKLOOKX) { + pl->lookvec.x = lock->link->lookvec.x; + } + if (lock->options & TECS_PLOCK_LOCKLOOKY) { + pl->lookvec.y = lock->link->lookvec.y; + } + if (lock->options & TECS_PLOCK_LOCKLOOKZ) { + pl->lookvec.z = lock->link->lookvec.z; + } +} + // void sys_chunk(ecs_chunk *ch, ecs_object *o, tecs **ecs) { // // Is it really ok to have this check every single time? Seems kinda // wasteful if (ch->generation == 1) { // } // } +// --------------------------------------------------- +// Setup functions +// --------------------------------------------------- + +ecs_eid setup_player(tecs *ecs, render_context *context) { + ecs_eid playerid = tecs_newentity(ecs, 0); + ECS_SETCOMPONENT(ecs, playerid, ecs_placement){ + .pos = {.x = 0, .y = 1, .z = 0}, .up = DEFAULTUP}; + ECS_SETCOMPONENT(ecs, playerid, ecs_movement){.pos = INIT_PLAYERSPEED}; + ECS_SETCOMPONENT(ecs, playerid, ecs_rotation){.yaw = 0, + .pitch = CAM_INITPITCH}; + ECS_SETCOMPONENT(ecs, playerid, ecs_rendercontext) context; + ECS_SETCOMPONENT(ecs, playerid, ecs_input){}; + return playerid; +} + +ecs_eid setup_sea(tecs *ecs, render_context *ctx, ecs_placement *playerpos, + haloo3d_easystore *storage) { + ecs_eid seaid = tecs_newentity(ecs, 0); + ECS_SETCOMPONENT(ecs, seaid, ecs_placement){ + .pos = {.x = 0, .y = 0, .z = 0}, .lookvec = DEFAULTLOOK, .up = DEFAULTUP}; + ECS_SETCOMPONENT(ecs, seaid, ecs_placement_lock){ + .options = TECS_PLOCK_LOCKPOSX | TECS_PLOCK_LOCKPOSZ | TECS_PLOCK_LOCKUP, + .link = playerpos, + }; + haloo3d_obj *model = haloo3d_easystore_addobj(storage, "sea"); + haloo3d_obj_resetfixed(model, 2, 4); + struct vec3 seacol = haloo3d_gen_paletteuv(0xF26F); + int seauv = haloo3d_obj_addvtexture(model, seacol); + // clang-format off + // REMEMBER: NEGATIVE Z IS FORWARD + int tl = haloo3d_obj_addvertex(model, (struct vec4){.x = -1, .y = 0, .z = -1, .w = 1}); + int tr = haloo3d_obj_addvertex(model, (struct vec4){.x = 1, .y = 0, .z = -1, .w = 1}); + int bl = haloo3d_obj_addvertex(model, (struct vec4){.x = -1, .y = 0, .z = 1, .w = 1}); + int br = haloo3d_obj_addvertex(model, (struct vec4){.x = 1, .y = 0, .z = 1, .w = 1}); + // clang-format on + haloo3d_facei face; + fastface(face, seauv, bl, br, tl); + haloo3d_obj_addface(model, face); + fastface(face, seauv, br, tr, tl); + haloo3d_obj_addface(model, face); + ECS_SETCOMPONENT(ecs, seaid, ecs_object){ + .texture = haloo3d_easystore_gettex(&ecs->storage, PALETTEKEY), + .scale = {.x = VIEWDISTANCE, .y = VIEWDISTANCE, .z = VIEWDISTANCE}, + .lighting = NULL, + .model = model, + .cullbackface = 1, + .context = {ctx}, + .contextcount = 1}; + return seaid; +} + // --------------------------------------------------- // MAIN FUNCTION // --------------------------------------------------- @@ -402,17 +475,11 @@ int main() { // int argc, char **argv) { eprintf("Setup ECS system + obj/tex storage (%d bytes)\n", tecs_size); - ecs_eid playerid = tecs_newentity(&ecs, 0); - ECS_SETCOMPONENT(&ecs, playerid, ecs_placement){ - .pos = {.x = 0, .y = 1, .z = 0}, .up = DEFAULTUP}; - ECS_SETCOMPONENT(&ecs, playerid, ecs_movement){.pos = INIT_PLAYERSPEED}; - ECS_SETCOMPONENT(&ecs, playerid, ecs_rotation){.yaw = 0, - .pitch = CAM_INITPITCH}; - ECS_SETCOMPONENT(&ecs, playerid, ecs_rendercontext) & context; - ECS_SETCOMPONENT(&ecs, playerid, ecs_input){}; + ecs_eid playerid = setup_player(&ecs, &context); ecs_placement *playerpos = &ECS_GETCOMPONENT(&ecs, playerid, ecs_placement); + ecs_eid seaid = setup_sea(&ecs, &context, playerpos, &ecs.storage); - eprintf("Setup player\n"); + eprintf("Setup player(%d) + sea(%d)\n", playerid, seaid); // MAIN LOOP while (1) { @@ -424,10 +491,11 @@ int main() { // int argc, char **argv) { if (ecs.delta_s) { ECS_RUNSYSTEM1(&ecs, sys_playerinput, ecs_input); ECS_RUNSYSTEM2(&ecs, sys_rotation, ecs_placement, ecs_rotation); + ECS_RUNSYSTEM2(&ecs, sys_placement_lock, ecs_placement_lock, + ecs_placement); ECS_RUNSYSTEM3(&ecs, sys_movement, ecs_placement, ecs_movement, tecs); ECS_RUNSYSTEM3(&ecs, sys_rendercontext, ecs_rendercontext, ecs_placement, tecs); - // ECS_RUNSYSTEM3(&ecs, sys_chunk, ecs_chunk, ecs_object, tecs); ECS_RUNSYSTEM3(&ecs, sys_playergarbage, ecs_playergarbage, ecs_object, tecs); haloo3d_easytimer_start(&drawtimer); diff --git a/terrain_ecstypes.h b/terrain_ecstypes.h index 4eb52b8..27931e3 100644 --- a/terrain_ecstypes.h +++ b/terrain_ecstypes.h @@ -63,6 +63,23 @@ typedef struct { struct vec3 lookvec; } ecs_placement; +// If set, locks the placement of one entity to another +typedef struct { + ecs_placement *link; + // struct vec3 posofs; // The offset from linked pos + uint8_t options; // Which things to lock +} ecs_placement_lock; + +#define TECS_PLOCK_LOCKLOOKX 1 +#define TECS_PLOCK_LOCKLOOKY 2 +#define TECS_PLOCK_LOCKLOOKZ 4 +#define TECS_PLOCK_LOCKPOSX 8 +#define TECS_PLOCK_LOCKPOSY 16 +#define TECS_PLOCK_LOCKPOSZ 32 +#define TECS_PLOCK_LOCKUP 64 + +// typedef ecs_placement *ecs_placement_lock; + // Movement is applied to placement and may also be used to check for // collisions and whatever typedef struct { @@ -124,6 +141,7 @@ ECS_COMPONENT(ecs_movement) ECS_COMPONENT(ecs_input) ECS_COMPONENT(ecs_playergarbage) ECS_COMPONENT(ecs_chunk) +ECS_COMPONENT(ecs_placement_lock) ECS_END(tecs) ECS_FN_INIT(tecs) @@ -141,5 +159,6 @@ ECS_CID(ecs_movement, 4) ECS_CID(ecs_input, 5) ECS_CID(ecs_playergarbage, 6) ECS_CID(ecs_chunk, 7) +ECS_CID(ecs_placement_lock, 8) #endif