diff --git a/code/game/src/vehicle.c b/code/game/src/vehicle.c index 65be8be..a4abb40 100644 --- a/code/game/src/vehicle.c +++ b/code/game/src/vehicle.c @@ -10,7 +10,7 @@ uint64_t vehicle_spawn(void) { Vehicle *veh = ecs_get_mut(world_ecs(), e, Vehicle, NULL); zpl_zero_item(veh); - veh->wheel_base = 60.0f; + veh->wheel_base = 50.0f; return (uint64_t)e; } diff --git a/code/game/src/world/blocks_list.c b/code/game/src/world/blocks_list.c index 12ebb82..97ef0bc 100644 --- a/code/game/src/world/blocks_list.c +++ b/code/game/src/world/blocks_list.c @@ -2,7 +2,7 @@ static block blocks[] = { {.name = "base-ground", .flags = 0, .kind = BLOCK_KIND_GROUND, .biome = 0, .symbol = '.', .drag = 1.0f}, - {.name = "base-dirt", .flags = 0, .kind = BLOCK_KIND_DIRT, .biome = 0, .symbol = ',', .drag = 4.0f }, + {.name = "base-dirt", .flags = 0, .kind = BLOCK_KIND_DIRT, .biome = 0, .symbol = ',', .drag = 2.1f }, {.name = "base-wall", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WALL, .biome = 0, .symbol = '#', .drag = 1.0f }, {.name = "base-hill", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL, .biome = 0, .symbol = '^', .drag = 1.0f }, {.name = "base-hill-snow", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL_SNOW, .biome = 0, .symbol = '*', .drag = 1.0f }, diff --git a/code/game/src/world/worldgen/worldgen_test.c b/code/game/src/world/worldgen/worldgen_test.c index 48f0453..d7e8167 100644 --- a/code/game/src/world/worldgen/worldgen_test.c +++ b/code/game/src/world/worldgen/worldgen_test.c @@ -98,6 +98,10 @@ static WORLD_BLOCK_OBSERVER(shaper_noise50) { static WORLD_BLOCK_OBSERVER(shaper_noise33) { return world_perlin_cond(block_idx, 0.33) ? shaper(id, block_idx) : BLOCK_INVALID; } + +static WORLD_BLOCK_OBSERVER(shaper_noise05) { + return world_perlin_cond(block_idx, 0.05) ? shaper(id, block_idx) : BLOCK_INVALID; +} #else static WORLD_BLOCK_OBSERVER(shaper_noise80) { return rand()%10 < 8 ? shaper(id, block_idx) : BLOCK_INVALID; @@ -139,7 +143,7 @@ int32_t worldgen_test(world_data *wld) { // ground world_fill_rect(grnd_id, 1, 1, world->dim-2, world->dim-2, NULL); - world_fill_rect(dirt_id, 1, 1, world->dim-2, world->dim-2, shaper_noise33); + world_fill_rect(dirt_id, 1, 1, world->dim-2, world->dim-2, shaper_noise05); // water #if 1 diff --git a/code/modules/modules/components.h b/code/modules/modules/components.h index 8cd185f..dabb20a 100644 --- a/code/modules/modules/components.h +++ b/code/modules/modules/components.h @@ -52,7 +52,7 @@ ECS_STRUCT(Classify, { ECS_STRUCT(Vehicle, { uint64_t seats[4]; - float speed; + float force; float heading; float steer; float wheel_base; diff --git a/code/modules/modules/systems.c b/code/modules/modules/systems.c index 22eaa7d..1d9112a 100644 --- a/code/modules/modules/systems.c +++ b/code/modules/modules/systems.c @@ -11,17 +11,9 @@ #define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f) #define PHY_CORRECTION(x) ((zpl_max(0.0f, (WORLD_BLOCK_SIZE/2.0f) - zpl_abs(x))*zpl_sign(x)))*(WORLD_BLOCK_SIZE/2.0f) -void MoveWalk(ecs_iter_t *it) { - Position *p = ecs_column(it, Position, 1); - Velocity *v = ecs_column(it, Velocity, 2); - - for (int i = 0; i < it->count; i++) { - world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); - float drag = blocks_get_drag(lookup.block_id); - v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag); - v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag); - } -} +#include "source/system_onfoot.c" +#include "source/system_demo.c" +#include "source/system_vehicle.c" void IntegratePositions(ecs_iter_t *it) { profile(PROF_INTEGRATE_POS) { @@ -70,47 +62,6 @@ void UpdateTrackerPos(ecs_iter_t *it) { } } -#define PLR_MOVE_SPEED 50.0 -#define PLR_MOVE_SPEED_MULT 4.0 - -void MovementImpulse(ecs_iter_t *it) { - Input *in = ecs_column(it, Input, 1); - Velocity *v = ecs_column(it, Velocity, 2); - - for (int i = 0; i < it->count; i++) { - if (ecs_is_alive(world_ecs(), in[i].parent)) continue; - double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0); - if (zpl_abs(v[i].x) < speed && in[i].x) - v[i].x = in[i].x*speed; - if (zpl_abs(v[i].y) < speed && in[i].y) - v[i].y = in[i].y*speed; - } -} - -#define DEMO_NPC_CHANGEDIR_FACTOR 0.1 -#define DEMO_NPC_MOVE_SPEED 1500 - -void DemoNPCMoveAround(ecs_iter_t *it) { - Velocity *v = ecs_column(it, Velocity, 1); - for (int i = 0; i < it->count; i++) { - v[i].x = zpl_lerp(v[i].x, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR); - v[i].y = zpl_lerp(v[i].y, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR); - } -} - -void DemoPlaceIceBlock(ecs_iter_t *it) { - Input *in = ecs_column(it, Input, 1); - Position *p = ecs_column(it, Position, 2); - uint8_t watr_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER); - - for (int i = 0; i < it->count; i++) { - if (in[i].use) { - world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y); - world_chunk_replace_block(l.chunk_id, l.id, watr_id); - } - } -} - #define HAZARD_BLOCK_TIME 1.0f #define HAZARD_BLOCK_DMG 5.0f @@ -148,121 +99,6 @@ void RegenerateHP(ecs_iter_t *it) { } } -#define VEH_ENTER_RADIUS 45.0f - -void EnterOrLeaveVehicle(ecs_iter_t *it) { - Input *in = ecs_column(it, Input, 1); - Position *p = ecs_column(it, Position, 2); - - for (int i = 0; i < it->count; i++) { - if (!in[i].use) continue; - - if (!ecs_is_alive(world_ecs(), in[i].parent)) { - size_t ents_count; - int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2); - - for (size_t j = 0; j < ents_count; j++) { - if (ecs_get(world_ecs(), ents[j], Vehicle)) { - Vehicle *veh = ecs_get_mut(world_ecs(), ents[j], Vehicle, NULL); - Position const* p2 = ecs_get(world_ecs(), ents[j], Position); - - float dx = p2->x - p[i].x; - float dy = p2->y - p[i].y; - float range = zpl_sqrt(dx*dx + dy*dy); - if (range <= VEH_ENTER_RADIUS) { - for (int k = 0; k < 4; k++) { - if (veh->seats[k] != 0) continue; - - // NOTE(zaklaus): We can enter the vehicle, yay! - veh->seats[k] = it->entities[i]; - in[i].parent = ents[j]; - p[i] = *p2; - break; - } - } - } - } - } else { - if (ecs_get(world_ecs(), in[i].parent, Vehicle)) { - Vehicle *veh = ecs_get_mut(world_ecs(), in[i].parent, Vehicle, NULL); - - for (int k = 0; k < 4; k++) { - if (veh->seats[k] == it->entities[i]) { - veh->seats[k] = 0; - break; - } - } - - in[i].parent = 0; - } else { - ZPL_PANIC("unreachable code"); - } - } - } -} - -#define VEHICLE_MAX_SPEED 500.0f -#define VEHICLE_ACCEL 2.1f -#define VEHICLE_STEER 0.01f - -void VehicleHandling(ecs_iter_t *it) { - Vehicle *veh = ecs_column(it, Vehicle, 1); - Position *p = ecs_column(it, Position, 2); - Velocity *v = ecs_column(it, Velocity, 3); - - for (int i = 0; i < it->count; i++) { - Vehicle *car = &veh[i]; - - // NOTE(zaklaus): Apply friction - car->speed *= 0.99f; - car->steer *= 0.97f; - - for (int j = 0; j < 4; j++) { - // NOTE(zaklaus): Perform seat cleanup - if (!ecs_is_alive(world_ecs(), veh[i].seats[j])) { - veh[i].seats[j] = 0; - continue; - } - - ecs_entity_t pe = veh[i].seats[j]; - - // NOTE(zaklaus): Update passenger position - { - Position *p2 = ecs_get_mut(world_ecs(), pe, Position, NULL); - Velocity *v2 = ecs_get_mut(world_ecs(), pe, Velocity, NULL); - *p2 = p[i]; - *v2 = v[i]; - } - - // NOTE(zaklaus): Handle driver input - if (j == 0) { - Input const* in = ecs_get(world_ecs(), pe, Input); - - car->speed += in->y * VEHICLE_ACCEL; - car->speed = zpl_clamp(car->speed, -VEHICLE_MAX_SPEED, VEHICLE_MAX_SPEED); - car->steer += in->x * -VEHICLE_STEER * (zpl_abs(car->speed*2.5f) / VEHICLE_MAX_SPEED); - car->steer = zpl_clamp(car->steer, -40.0f, 40.0f); - } - } - - // NOTE(zaklaus): Vehicle physics - float fr_x = p[i].x + (car->wheel_base/2.0f) * zpl_cos(car->heading); - float fr_y = p[i].y + (car->wheel_base/2.0f) * zpl_sin(car->heading); - - float bk_x = p[i].x - (car->wheel_base/2.0f) * zpl_cos(car->heading); - float bk_y = p[i].y - (car->wheel_base/2.0f) * zpl_sin(car->heading); - - bk_x += car->speed * zpl_cos(car->heading); - bk_y += car->speed * zpl_sin(car->heading); - fr_x += car->speed * zpl_cos(car->heading + zpl_to_radians(car->steer)); - fr_y += car->speed * zpl_sin(car->heading + zpl_to_radians(car->steer)); - - v[i].x = (fr_x + bk_x) / 2.0f - p[i].x; - v[i].y = (fr_y + bk_y) / 2.0f - p[i].y; - car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); - } -} - void SystemsImport(ecs_world_t *ecs) { ECS_MODULE(ecs, Systems); ECS_IMPORT(ecs, Components); @@ -272,7 +108,7 @@ void SystemsImport(ecs_world_t *ecs) { ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnLoad, components.Velocity, components.EcsDemoNPC); ECS_SYSTEM(ecs, EnterOrLeaveVehicle, EcsOnLoad, components.Input, components.Position); - ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, components.Position, components.Velocity, !components.Vehicle); + ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, components.Position, components.Velocity); ECS_SYSTEM(ecs, HurtOnHazardBlock, EcsOnUpdate, components.Position, components.Health); ECS_SYSTEM(ecs, RegenerateHP, EcsOnUpdate, components.Health); ECS_SYSTEM(ecs, VehicleHandling, EcsOnUpdate, components.Vehicle, components.Position, components.Velocity); diff --git a/code/modules/source/system_demo.c b/code/modules/source/system_demo.c new file mode 100644 index 0000000..93b4add --- /dev/null +++ b/code/modules/source/system_demo.c @@ -0,0 +1,24 @@ + +#define DEMO_NPC_CHANGEDIR_FACTOR 0.1 +#define DEMO_NPC_MOVE_SPEED 1500 + +void DemoNPCMoveAround(ecs_iter_t *it) { + Velocity *v = ecs_column(it, Velocity, 1); + for (int i = 0; i < it->count; i++) { + v[i].x = zpl_lerp(v[i].x, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR); + v[i].y = zpl_lerp(v[i].y, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR); + } +} + +void DemoPlaceIceBlock(ecs_iter_t *it) { + Input *in = ecs_column(it, Input, 1); + Position *p = ecs_column(it, Position, 2); + uint8_t watr_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER); + + for (int i = 0; i < it->count; i++) { + if (in[i].use) { + world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y); + world_chunk_replace_block(l.chunk_id, l.id, watr_id); + } + } +} diff --git a/code/modules/source/system_onfoot.c b/code/modules/source/system_onfoot.c new file mode 100644 index 0000000..45694b2 --- /dev/null +++ b/code/modules/source/system_onfoot.c @@ -0,0 +1,29 @@ + +void MoveWalk(ecs_iter_t *it) { + Position *p = ecs_column(it, Position, 1); + Velocity *v = ecs_column(it, Velocity, 2); + + for (int i = 0; i < it->count; i++) { + world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); + float drag = blocks_get_drag(lookup.block_id); + v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag); + v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag); + } +} + +#define PLR_MOVE_SPEED 50.0 +#define PLR_MOVE_SPEED_MULT 4.0 + +void MovementImpulse(ecs_iter_t *it) { + Input *in = ecs_column(it, Input, 1); + Velocity *v = ecs_column(it, Velocity, 2); + + for (int i = 0; i < it->count; i++) { + if (ecs_is_alive(world_ecs(), in[i].parent)) continue; + double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0); + if (zpl_abs(v[i].x) < speed && in[i].x) + v[i].x = in[i].x*speed; + if (zpl_abs(v[i].y) < speed && in[i].y) + v[i].y = in[i].y*speed; + } +} diff --git a/code/modules/source/system_vehicle.c b/code/modules/source/system_vehicle.c new file mode 100644 index 0000000..9f0bdf1 --- /dev/null +++ b/code/modules/source/system_vehicle.c @@ -0,0 +1,116 @@ + +#define VEH_ENTER_RADIUS 45.0f + +void EnterOrLeaveVehicle(ecs_iter_t *it) { + Input *in = ecs_column(it, Input, 1); + Position *p = ecs_column(it, Position, 2); + + for (int i = 0; i < it->count; i++) { + if (!in[i].use) continue; + + if (!ecs_is_alive(world_ecs(), in[i].parent)) { + size_t ents_count; + int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2); + + for (size_t j = 0; j < ents_count; j++) { + if (ecs_get(world_ecs(), ents[j], Vehicle)) { + Vehicle *veh = ecs_get_mut(world_ecs(), ents[j], Vehicle, NULL); + Position const* p2 = ecs_get(world_ecs(), ents[j], Position); + + float dx = p2->x - p[i].x; + float dy = p2->y - p[i].y; + float range = zpl_sqrt(dx*dx + dy*dy); + if (range <= VEH_ENTER_RADIUS) { + for (int k = 0; k < 4; k++) { + if (veh->seats[k] != 0) continue; + + // NOTE(zaklaus): We can enter the vehicle, yay! + veh->seats[k] = it->entities[i]; + in[i].parent = ents[j]; + p[i] = *p2; + break; + } + } + } + } + } else { + if (ecs_get(world_ecs(), in[i].parent, Vehicle)) { + Vehicle *veh = ecs_get_mut(world_ecs(), in[i].parent, Vehicle, NULL); + + for (int k = 0; k < 4; k++) { + if (veh->seats[k] == it->entities[i]) { + veh->seats[k] = 0; + break; + } + } + + in[i].parent = 0; + } else { + ZPL_PANIC("unreachable code"); + } + } + } +} + +#define VEHICLE_FORCE 12.8f +#define VEHICLE_ACCEL 0.17f +#define VEHICLE_DECEL 0.28f +#define VEHICLE_STEER 0.11f +#define VEHICLE_STEER_MUL 0.087f + +void VehicleHandling(ecs_iter_t *it) { + Vehicle *veh = ecs_column(it, Vehicle, 1); + Position *p = ecs_column(it, Position, 2); + Velocity *v = ecs_column(it, Velocity, 3); + + for (int i = 0; i < it->count; i++) { + Vehicle *car = &veh[i]; + + // NOTE(zaklaus): Apply friction + car->force = zpl_lerp(car->force, 0.0f, VEHICLE_DECEL); + car->steer *= 0.97f; + + for (int j = 0; j < 4; j++) { + // NOTE(zaklaus): Perform seat cleanup + if (!ecs_is_alive(world_ecs(), veh[i].seats[j])) { + veh[i].seats[j] = 0; + continue; + } + + ecs_entity_t pe = veh[i].seats[j]; + + // NOTE(zaklaus): Update passenger position + { + Position *p2 = ecs_get_mut(world_ecs(), pe, Position, NULL); + Velocity *v2 = ecs_get_mut(world_ecs(), pe, Velocity, NULL); + *p2 = p[i]; + *v2 = v[i]; + } + + // NOTE(zaklaus): Handle driver input + if (j == 0) { + Input const* in = ecs_get(world_ecs(), pe, Input); + + car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL); + car->steer += in->x * -VEHICLE_STEER; + car->steer = zpl_clamp(car->steer, -40.0f, 40.0f); + } + } + + // NOTE(zaklaus): Vehicle physics + float fr_x = p[i].x + (car->wheel_base/2.0f) * zpl_cos(car->heading); + float fr_y = p[i].y + (car->wheel_base/2.0f) * zpl_sin(car->heading); + + float bk_x = p[i].x - (car->wheel_base/2.0f) * zpl_cos(car->heading); + float bk_y = p[i].y - (car->wheel_base/2.0f) * zpl_sin(car->heading); + + bk_x += car->force * zpl_cos(car->heading); + bk_y += car->force * zpl_sin(car->heading); + fr_x += car->force * zpl_cos(car->heading + zpl_to_radians(car->steer)); + fr_y += car->force * zpl_sin(car->heading + zpl_to_radians(car->steer)); + + v[i].x += (fr_x + bk_x) / 2.0f - p[i].x; + v[i].y += (fr_y + bk_y) / 2.0f - p[i].y; + car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); + } +}