Add entity physics
This commit is contained in:
@@ -143,6 +143,53 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
|||||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
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) {
|
void entityMoveToTarget(ecs_iter_t *it) {
|
||||||
Position *position = ecs_field(it, Position, 1);
|
Position *position = ecs_field(it, Position, 1);
|
||||||
|
|||||||
@@ -246,8 +246,8 @@ void drawMainMenuUI(Game *game, f32 dt) {
|
|||||||
if (uiMainMenuButton("Play", true)) {
|
if (uiMainMenuButton("Play", true)) {
|
||||||
setScreen(game, SCREEN_GAME);
|
setScreen(game, SCREEN_GAME);
|
||||||
unloadMap(game);
|
unloadMap(game);
|
||||||
loadMap(game, "assets/maps/tree_test.tmj");
|
//loadMap(game, "assets/maps/tree_test.tmj");
|
||||||
//loadMap(game, "assets/maps/map_01.tmj");
|
loadMap(game, "assets/maps/map_01.tmj");
|
||||||
}
|
}
|
||||||
if (uiMainMenuButton("Settings", true)) {
|
if (uiMainMenuButton("Settings", true)) {
|
||||||
setScreen(game, SCREEN_SETTINGS);
|
setScreen(game, SCREEN_SETTINGS);
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ void setupSystems() {
|
|||||||
|
|
||||||
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID);
|
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, HitBox, Velocity, SpatialGridID);
|
||||||
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Velocity, Steering, Unit);
|
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, entityMoveToTarget, EcsOnUpdate, Position, Velocity, TargetPosition, Steering);
|
||||||
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
|
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
|
||||||
|
|||||||
@@ -98,7 +98,6 @@ void entityUnConsumePopCapacity(ecs_iter_t *it);
|
|||||||
void entityUpdateSpatialID(ecs_iter_t *it);
|
void entityUpdateSpatialID(ecs_iter_t *it);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 0: Game (singleton) for collisions
|
|
||||||
* 1: Position
|
* 1: Position
|
||||||
* 2: Velocity
|
* 2: Velocity
|
||||||
* 3: Steering
|
* 3: Steering
|
||||||
@@ -106,6 +105,15 @@ void entityUpdateSpatialID(ecs_iter_t *it);
|
|||||||
*/
|
*/
|
||||||
void entityUpdateKinematic(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
|
* 1: Position
|
||||||
* 2: Velocity
|
* 2: Velocity
|
||||||
|
|||||||
Reference in New Issue
Block a user