diff --git a/game/systems/s_entity.c b/game/systems/s_entity.c index 2a2d50f..cd1d078 100644 --- a/game/systems/s_entity.c +++ b/game/systems/s_entity.c @@ -143,6 +143,53 @@ void entityUpdateKinematic(ecs_iter_t *it) { position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt)); } } +void entityUpdatePhysics(ecs_iter_t *it) { + Game *game = ecs_singleton_get_mut(ECS, Game); + Position *position = ecs_field(it, Position, 1); + HitBox *hitbox = ecs_field(it, HitBox, 2); + Velocity *velocity = ecs_field(it, Velocity, 3); + SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 4); + + f32 dt = it->delta_time; + + for (i32 i = 0; i < it->count; i++) { + // Only update "stationary" entities + bool stationary = Vector2Length(velocity[i]) <= 0.2f; + Rectangle bounds = entityTransformHitBox(position[i], hitbox[i]); + BzSpatialGridIter spatialIt = bzSpatialGridIter(game->entityGrid, bounds.x, bounds.y, + bounds.width, bounds.height); + Vector2 dir = Vector2Zero(); + f32 slowDown = 0.0f; + while (bzSpatialGridQueryNext(&spatialIt)) { + ecs_entity_t other = *(ecs_entity_t *) spatialIt.data; + if (other == it->entities[i]) + continue; + Position otherPos; + Rectangle otherBounds; + if (!entityGetHitBox(other, &otherPos, &otherBounds)) + continue; + + if (!CheckCollisionRecs(bounds, otherBounds)) { + continue; + } + slowDown += 0.1f; + Position dif = Vector2Subtract(otherPos, position[i]); + //dif = Vector2Normalize(dif); + dir = Vector2Add(dir, dif); + } + + if (stationary) { + dir = Vector2Normalize(dir); + dir = Vector2Scale(dir, 40); + velocity[i] = Vector2Subtract(velocity[i], dir); + } + + slowDown = BZ_MIN(slowDown, 0.85f); + if (!stationary && slowDown > 0.0f) { + velocity[i] = Vector2Scale(velocity[i], 1 - slowDown); + } + } +} void entityMoveToTarget(ecs_iter_t *it) { Position *position = ecs_field(it, Position, 1); diff --git a/game/systems/s_ui.c b/game/systems/s_ui.c index 3a7618b..c144132 100644 --- a/game/systems/s_ui.c +++ b/game/systems/s_ui.c @@ -246,8 +246,8 @@ void drawMainMenuUI(Game *game, f32 dt) { if (uiMainMenuButton("Play", true)) { setScreen(game, SCREEN_GAME); unloadMap(game); - loadMap(game, "assets/maps/tree_test.tmj"); - //loadMap(game, "assets/maps/map_01.tmj"); + //loadMap(game, "assets/maps/tree_test.tmj"); + loadMap(game, "assets/maps/map_01.tmj"); } if (uiMainMenuButton("Settings", true)) { setScreen(game, SCREEN_SETTINGS); diff --git a/game/systems/systems.c b/game/systems/systems.c index 62a8eb3..ec65c0e 100644 --- a/game/systems/systems.c +++ b/game/systems/systems.c @@ -119,6 +119,7 @@ void setupSystems() { ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID); ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Velocity, Steering, Unit); + ECS_SYSTEM(ECS, entityUpdatePhysics, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID); ECS_SYSTEM(ECS, entityMoveToTarget, EcsOnUpdate, Position, Velocity, TargetPosition, Steering); ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path); diff --git a/game/systems/systems.h b/game/systems/systems.h index db77c6b..804bd9e 100644 --- a/game/systems/systems.h +++ b/game/systems/systems.h @@ -98,7 +98,6 @@ void entityUnConsumePopCapacity(ecs_iter_t *it); void entityUpdateSpatialID(ecs_iter_t *it); /* - * 0: Game (singleton) for collisions * 1: Position * 2: Velocity * 3: Steering @@ -106,6 +105,15 @@ void entityUpdateSpatialID(ecs_iter_t *it); */ void entityUpdateKinematic(ecs_iter_t *it); +/* + * 0: Game (singleton) for collisions + * 1: Position + * 2: HitBox + * 2: Velocity + * 3: SpatialGridID + */ +void entityUpdatePhysics(ecs_iter_t *it); + /* * 1: Position * 2: Velocity