Kinematic movement + path following
This commit is contained in:
@@ -130,7 +130,7 @@ static bool indexContains(BzSpatialGridIndex index, i32 x, i32 y) {
|
|||||||
return x >= index.minX && x <= index.maxX && y >= index.minY && y <= index.maxY;
|
return x >= index.minX && x <= index.maxX && y >= index.minY && y <= index.maxY;
|
||||||
}
|
}
|
||||||
|
|
||||||
BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
|
void bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY) {
|
||||||
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
|
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
|
||||||
BZ_ASSERT(entry && entry->id == id);
|
BZ_ASSERT(entry && entry->id == id);
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32
|
|||||||
oldIndex.minY == newIndex.minY &&
|
oldIndex.minY == newIndex.minY &&
|
||||||
oldIndex.maxX == newIndex.maxX &&
|
oldIndex.maxX == newIndex.maxX &&
|
||||||
oldIndex.maxY == newIndex.maxY) {
|
oldIndex.maxY == newIndex.maxY) {
|
||||||
return id;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i32 y = oldIndex.minY; y <= oldIndex.maxY; y++) {
|
for (i32 y = oldIndex.minY; y <= oldIndex.maxY; y++) {
|
||||||
@@ -159,7 +159,6 @@ BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32
|
|||||||
}
|
}
|
||||||
|
|
||||||
entry->index = newIndex;
|
entry->index = newIndex;
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id) {
|
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id) {
|
||||||
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
|
BzSpatialGridEntry *entry = bzObjectPoolGetObject(grid->entriesPool, id);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ void bzSpatialGridDestroy(BzSpatialGrid *grid);
|
|||||||
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id);
|
void *bzSpatialGridGetData(const BzSpatialGrid *grid, BzSpatialGridID id);
|
||||||
|
|
||||||
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
|
BzSpatialGridID bzSpatialGridInsert(BzSpatialGrid *grid, void *data, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
|
||||||
BzSpatialGridID bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
|
void bzSpatialGridUpdate(BzSpatialGrid *grid, BzSpatialGridID id, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
|
||||||
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id);
|
void bzSpatialGridRemove(BzSpatialGrid *grid, BzSpatialGridID id);
|
||||||
|
|
||||||
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
|
BzSpatialGridIter bzSpatialGridIter(BzSpatialGrid *grid, f32 posX, f32 posY, f32 sizeX, f32 sizeY);
|
||||||
|
|||||||
@@ -120,6 +120,10 @@ bool init(void *userData) {
|
|||||||
ECS_OBSERVER(ECS, entitySpatialRemoved, EcsOnRemove, Position, SpatialGridID);
|
ECS_OBSERVER(ECS, entitySpatialRemoved, EcsOnRemove, Position, SpatialGridID);
|
||||||
ECS_OBSERVER(ECS, pathRemoved, EcsOnRemove, Path);
|
ECS_OBSERVER(ECS, pathRemoved, EcsOnRemove, Path);
|
||||||
|
|
||||||
|
ECS_SYSTEM(ECS, entityUpdateSpatialID, EcsOnUpdate, Position, Size, Velocity, SpatialGridID);
|
||||||
|
ECS_SYSTEM(ECS, entityUpdateKinematic, EcsOnUpdate, Position, Rotation, Velocity, AngularVelocity, SteeringOutput);
|
||||||
|
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Position, Rotation, Velocity, AngularVelocity, SteeringOutput, Path);
|
||||||
|
|
||||||
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
|
ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path);
|
||||||
|
|
||||||
ECS_SYSTEM(ECS, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain);
|
ECS_SYSTEM(ECS, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain);
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
|||||||
object.shape.sizeX, object.shape.sizeY);
|
object.shape.sizeX, object.shape.sizeY);
|
||||||
ecs_set(ECS, e, SpatialGridID, {spatialID});
|
ecs_set(ECS, e, SpatialGridID, {spatialID});
|
||||||
ecs_set(ECS, e, Rotation, {0.0f});
|
ecs_set(ECS, e, Rotation, {0.0f});
|
||||||
|
ecs_set(ECS, e, Velocity, {});
|
||||||
|
ecs_set(ECS, e, AngularVelocity, {0.0f});
|
||||||
|
ecs_set(ECS, e, SteeringOutput, {});
|
||||||
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
|
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
|
||||||
ecs_set(ECS, e, Animation, {
|
ecs_set(ECS, e, Animation, {
|
||||||
.firstFrame=(TextureRegion) {
|
.firstFrame=(TextureRegion) {
|
||||||
|
|||||||
@@ -18,11 +18,19 @@
|
|||||||
void entitySpatialRemoved(ecs_iter_t *it);
|
void entitySpatialRemoved(ecs_iter_t *it);
|
||||||
|
|
||||||
/* Observer (for releasing path objects)
|
/* Observer (for releasing path objects)
|
||||||
* 0: Game (singleton)l for object pool
|
* 0: Game (singleton) for object pool
|
||||||
* 1: Path
|
* 1: Path
|
||||||
*/
|
*/
|
||||||
void pathRemoved(ecs_iter_t *it);
|
void pathRemoved(ecs_iter_t *it);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 0: Game (singleton) for entity map
|
||||||
|
* 1: Position
|
||||||
|
* 2: Size
|
||||||
|
* 3: Velocity (only entities with velocity change position)
|
||||||
|
* 4: SpatialGridID
|
||||||
|
*/
|
||||||
|
void entityUpdateSpatialID(ecs_iter_t *it);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 0: Game (singleton) for collisions
|
* 0: Game (singleton) for collisions
|
||||||
|
|||||||
@@ -34,22 +34,76 @@ void pathRemoved(ecs_iter_t *it) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void entityUpdateSpatialID(ecs_iter_t *it) {
|
||||||
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
||||||
|
Position *position = ecs_field(it, Position, 1);
|
||||||
|
Position *size = ecs_field(it, Position, 2);
|
||||||
|
//Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||||
|
SpatialGridID *id = ecs_field(it, SpatialGridID, 4);
|
||||||
|
|
||||||
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
|
Position pos = getBottomLeftPos(position[i], size[i]);
|
||||||
|
bzSpatialGridUpdate(game->entityGrid, id[i], pos.x, pos.y, size[i].x, size[i].y);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void entityUpdateKinematic(ecs_iter_t *it) {
|
void entityUpdateKinematic(ecs_iter_t *it) {
|
||||||
Position *pos = ecs_field(it, Position, 1);
|
Position *position = ecs_field(it, Position, 1);
|
||||||
Rotation *rotation = ecs_field(it, Rotation, 2);
|
Rotation *rotation = ecs_field(it, Rotation, 2);
|
||||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||||
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
|
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
|
||||||
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
|
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
|
||||||
|
|
||||||
|
f32 dt = it->delta_time;
|
||||||
|
|
||||||
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
|
|
||||||
|
// and velocity and angular velocity
|
||||||
|
velocity[i] = Vector2Scale(velocity[i], 0.9f);
|
||||||
|
velocity[i].x += steeringOutput[i].linear.x * dt;
|
||||||
|
velocity[i].y += steeringOutput[i].linear.y * dt;
|
||||||
|
f32 maxSpeed = 15.0f;
|
||||||
|
velocity[i] = Vector2Clamp(velocity[i],
|
||||||
|
(Velocity) {-maxSpeed, -maxSpeed},
|
||||||
|
(Velocity){maxSpeed, maxSpeed});
|
||||||
|
angularVelocity[i] += steeringOutput[i].angular * dt;
|
||||||
|
steeringOutput[i] = (SteeringOutput){};
|
||||||
|
|
||||||
|
// Update position and rotation
|
||||||
|
position[i].x += velocity[i].x * dt * 10;
|
||||||
|
position[i].y += velocity[i].y * dt * 10;
|
||||||
|
rotation[i] += angularVelocity[i] * dt * 10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void entityFollowPath(ecs_iter_t *it) {
|
void entityFollowPath(ecs_iter_t *it) {
|
||||||
Position *pos = ecs_field(it, Position, 1);
|
Position *position = ecs_field(it, Position, 1);
|
||||||
Rotation *rotation = ecs_field(it, Rotation, 2);
|
Rotation *rotation = ecs_field(it, Rotation, 2);
|
||||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||||
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
|
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
|
||||||
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
|
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
|
||||||
Path *path = ecs_field(it, Path, 6);
|
Path *path = ecs_field(it, Path, 6);
|
||||||
|
|
||||||
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
|
Position target = path[i].paths->waypoints[path[i].curWaypoint];
|
||||||
|
steeringOutput[i].linear.x = target.x - position[i].x;
|
||||||
|
steeringOutput[i].linear.y = target.y - position[i].y;
|
||||||
|
|
||||||
|
f32 dst = Vector2LengthSqr(steeringOutput[i].linear);
|
||||||
|
if (dst < 8.0f) {
|
||||||
|
path[i].curWaypoint++;
|
||||||
|
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
||||||
|
path[i].curWaypoint = 0;
|
||||||
|
path[i].paths = path[i].paths->next;
|
||||||
|
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
steeringOutput[i].linear = Vector2Normalize(steeringOutput[i].linear);
|
||||||
|
steeringOutput[i].linear = Vector2Scale(steeringOutput[i].linear, 10.0f);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateAnimations(ecs_iter_t *it) {
|
void updateAnimations(ecs_iter_t *it) {
|
||||||
|
|||||||
Reference in New Issue
Block a user