Split follow path system with follow target position
This commit is contained in:
@@ -17,7 +17,7 @@ ECS_COMPONENT_DECLARE(SpatialGridID);
|
||||
ECS_COMPONENT_DECLARE(Position);
|
||||
ECS_COMPONENT_DECLARE(Size);
|
||||
ECS_COMPONENT_DECLARE(Velocity);
|
||||
ECS_COMPONENT_DECLARE(Offset);
|
||||
ECS_COMPONENT_DECLARE(TargetPosition);
|
||||
ECS_COMPONENT_DECLARE(Rotation);
|
||||
ECS_COMPONENT_DECLARE(AngularVelocity);
|
||||
ECS_COMPONENT_DECLARE(SteeringOutput);
|
||||
@@ -45,7 +45,7 @@ void initComponentIDs(ecs_world_t *ecs) {
|
||||
ECS_COMPONENT_DEFINE(ecs, Position);
|
||||
ECS_COMPONENT_DEFINE(ecs, Size);
|
||||
ECS_COMPONENT_DEFINE(ecs, Velocity);
|
||||
ECS_COMPONENT_DEFINE(ecs, Offset);
|
||||
ECS_COMPONENT_DEFINE(ecs, TargetPosition);
|
||||
ECS_COMPONENT_DEFINE(ecs, Rotation);
|
||||
ECS_COMPONENT_DEFINE(ecs, AngularVelocity);
|
||||
ECS_COMPONENT_DEFINE(ecs, SteeringOutput);
|
||||
|
||||
@@ -47,11 +47,11 @@ extern ECS_COMPONENT_DECLARE(Owner);
|
||||
typedef BzSpatialGridID SpatialGridID;
|
||||
extern ECS_COMPONENT_DECLARE(SpatialGridID);
|
||||
|
||||
typedef Vector2 Position, Size, Velocity, Offset;
|
||||
typedef Vector2 Position, Size, Velocity, TargetPosition;
|
||||
extern ECS_COMPONENT_DECLARE(Position);
|
||||
extern ECS_COMPONENT_DECLARE(Size);
|
||||
extern ECS_COMPONENT_DECLARE(Velocity);
|
||||
extern ECS_COMPONENT_DECLARE(Offset);
|
||||
extern ECS_COMPONENT_DECLARE(TargetPosition);
|
||||
typedef f32 Rotation, AngularVelocity;
|
||||
extern ECS_COMPONENT_DECLARE(Rotation);
|
||||
extern ECS_COMPONENT_DECLARE(AngularVelocity);
|
||||
|
||||
@@ -150,7 +150,9 @@ bool init(void *userData) {
|
||||
|
||||
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, entityMoveToTarget, EcsOnUpdate, Position, Rotation, Velocity, TargetPosition, SteeringOutput);
|
||||
ECS_SYSTEM(ECS, entityFollowPath, EcsOnUpdate, Path);
|
||||
|
||||
ECS_SYSTEM(ECS, entityUpdateAnimationState, EcsOnUpdate, Velocity, AnimationType);
|
||||
ECS_SYSTEM(ECS, entityUpdateAnimation, EcsOnUpdate, Animation, TextureRegion);
|
||||
|
||||
@@ -50,13 +50,17 @@ void entityUpdateSpatialID(ecs_iter_t *it);
|
||||
void entityUpdateKinematic(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton) for object pool
|
||||
* 1: Position
|
||||
* 2: Rotation
|
||||
* 3: Velocity
|
||||
* 4: AngularVelocity
|
||||
* 4: TargetPosition
|
||||
* 5: SteeringOutput
|
||||
* 6: Path
|
||||
*/
|
||||
void entityMoveToTarget(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 0: Game (singleton) for object pool
|
||||
* 1: Path
|
||||
*/
|
||||
void entityFollowPath(ecs_iter_t *it);
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ void entityPathRemoved(ecs_iter_t *it) {
|
||||
Path *path = ecs_field(it, Path, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
|
||||
ecs_remove(ECS, entity, TargetPosition);
|
||||
|
||||
PathData *cur = path[i].paths;
|
||||
while (cur) {
|
||||
bzObjectPoolRelease(pool, cur);
|
||||
@@ -115,17 +119,16 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
||||
}
|
||||
}
|
||||
|
||||
void entityFollowPath(ecs_iter_t *it) {
|
||||
const Game *game = ecs_singleton_get(ECS, Game);
|
||||
void entityMoveToTarget(ecs_iter_t *it) {
|
||||
Position *position = ecs_field(it, Position, 1);
|
||||
Rotation *rotation = ecs_field(it, Rotation, 2);
|
||||
Velocity *velocity = ecs_field(it, Velocity, 3);
|
||||
AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4);
|
||||
SteeringOutput *steering = ecs_field(it, SteeringOutput, 5);
|
||||
Path *path = ecs_field(it, Path, 6);
|
||||
|
||||
TargetPosition *targetPos = ecs_field(it, TargetPosition, 4);
|
||||
SteeringOutput *steering = ecs_field(it, SteeringOutput, 5);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Position target = path[i].paths->waypoints[path[i].curWaypoint];
|
||||
Position target = targetPos[i];
|
||||
steering[i].linear = Vector2Subtract(target, position[i]);
|
||||
f32 dst = Vector2LengthSqr(steering[i].linear);
|
||||
f32 maxAccel = 10.0f;
|
||||
@@ -138,6 +141,23 @@ void entityFollowPath(ecs_iter_t *it) {
|
||||
}
|
||||
|
||||
if (dst < 8.0f) {
|
||||
// Arrived
|
||||
ecs_remove(ECS, it->entities[i], TargetPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void entityFollowPath(ecs_iter_t *it) {
|
||||
const Game *game = ecs_singleton_get(ECS, Game);
|
||||
|
||||
Path *path = ecs_field(it, Path, 1);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
TargetPosition target = path[i].paths->waypoints[path[i].curWaypoint];
|
||||
|
||||
if (!ecs_has(ECS, entity, TargetPosition)) {
|
||||
ecs_set_ptr(ECS, entity, TargetPosition, &target);
|
||||
path[i].curWaypoint++;
|
||||
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
||||
path[i].curWaypoint = 0;
|
||||
|
||||
Reference in New Issue
Block a user