Rework input
This commit is contained in:
@@ -17,6 +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(Rotation);
|
||||
ECS_COMPONENT_DECLARE(AngularVelocity);
|
||||
ECS_COMPONENT_DECLARE(SteeringOutput);
|
||||
@@ -44,6 +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, Rotation);
|
||||
ECS_COMPONENT_DEFINE(ecs, AngularVelocity);
|
||||
ECS_COMPONENT_DEFINE(ecs, SteeringOutput);
|
||||
|
||||
@@ -47,10 +47,11 @@ extern ECS_COMPONENT_DECLARE(Owner);
|
||||
typedef BzSpatialGridID SpatialGridID;
|
||||
extern ECS_COMPONENT_DECLARE(SpatialGridID);
|
||||
|
||||
typedef Vector2 Position, Size, Velocity;
|
||||
typedef Vector2 Position, Size, Velocity, Offset;
|
||||
extern ECS_COMPONENT_DECLARE(Position);
|
||||
extern ECS_COMPONENT_DECLARE(Size);
|
||||
extern ECS_COMPONENT_DECLARE(Velocity);
|
||||
extern ECS_COMPONENT_DECLARE(Offset);
|
||||
typedef f32 Rotation, AngularVelocity;
|
||||
extern ECS_COMPONENT_DECLARE(Rotation);
|
||||
extern ECS_COMPONENT_DECLARE(AngularVelocity);
|
||||
@@ -94,6 +95,12 @@ typedef struct Path {
|
||||
} Path;
|
||||
extern ECS_COMPONENT_DECLARE(Path);
|
||||
|
||||
typedef struct EntityArms {
|
||||
ecs_entity_t left;
|
||||
ecs_entity_t right;
|
||||
} EntityArms;
|
||||
//extern ECS_COMPONENT_DECLARE(EntityArms);
|
||||
|
||||
|
||||
void initComponentIDs(ecs_world_t *ecs);
|
||||
|
||||
|
||||
@@ -161,6 +161,7 @@ bool init(void *userData) {
|
||||
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities);
|
||||
|
||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
||||
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation);
|
||||
|
||||
renderDebugPathSystem = renderDebugPath;
|
||||
renderCollidersSystem = renderColliders;
|
||||
|
||||
@@ -47,6 +47,11 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
.elapsed=i * 0.1f,
|
||||
});
|
||||
ecs_set(ECS, e, AnimationType, {ANIM_IDLE});
|
||||
EntityArms arms = {
|
||||
.left=ecs_new_id(ECS),
|
||||
.right=ecs_new_id(ECS),
|
||||
};
|
||||
//ecs_set_ptr(ECS, e, EntityArms, &arms);
|
||||
}
|
||||
return true;
|
||||
|
||||
|
||||
@@ -86,6 +86,12 @@ void renderEntities(ecs_iter_t *it);
|
||||
*/
|
||||
void renderColliders(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Position
|
||||
* 2: Rotation
|
||||
*/
|
||||
void renderRotationDirection(ecs_iter_t *it);
|
||||
|
||||
/*
|
||||
* 1: Path
|
||||
*/
|
||||
|
||||
@@ -64,34 +64,43 @@ void entityUpdateKinematic(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 *angularVelocity = ecs_field(it, Rotation, 4);
|
||||
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
|
||||
AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4);
|
||||
SteeringOutput *steering = 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;
|
||||
// position += velocity * dt
|
||||
// rotation += angularVelocity * dt
|
||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
||||
rotation[i] += angularVel[i] * dt;
|
||||
|
||||
// Update velocity and angular velocity
|
||||
// velocity += steering.liner * dt
|
||||
// angularVelocity += steering.angular * dt
|
||||
velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i].linear, dt * 10));
|
||||
angularVel[i] += steering[i].angular;
|
||||
|
||||
if (Vector2LengthSqr(steering[i].linear) == 0) {
|
||||
// Decay velocity
|
||||
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
|
||||
}
|
||||
// Reset steering
|
||||
steering[i] = (SteeringOutput) {};
|
||||
|
||||
// Check for speeding and clip
|
||||
const f32 maxSpeed = 15.0f;
|
||||
if (Vector2Length(velocity[i]) > maxSpeed) {
|
||||
velocity[i] = Vector2Normalize(velocity[i]);
|
||||
velocity[i] = Vector2Scale(velocity[i], maxSpeed);
|
||||
}
|
||||
|
||||
// Update flipX
|
||||
ecs_entity_t entity = it->entities[i];
|
||||
if (ecs_has(it->world, entity, TextureRegion)) {
|
||||
TextureRegion *text = ecs_get_mut(it->world, entity, TextureRegion);
|
||||
text->flipX = velocity[i].x < 0.0f;
|
||||
text->flipX = velocity[i].x < 0.01f;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,16 +110,16 @@ void entityFollowPath(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 *angularVelocity = ecs_field(it, Rotation, 4);
|
||||
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
|
||||
AngularVelocity *angularVel = ecs_field(it, AngularVelocity , 4);
|
||||
SteeringOutput *steering = ecs_field(it, SteeringOutput, 5);
|
||||
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;
|
||||
steering[i].linear.x = target.x - position[i].x;
|
||||
steering[i].linear.y = target.y - position[i].y;
|
||||
|
||||
f32 dst = Vector2LengthSqr(steeringOutput[i].linear);
|
||||
f32 dst = Vector2LengthSqr(steering[i].linear);
|
||||
if (dst < 8.0f) {
|
||||
path[i].curWaypoint++;
|
||||
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
||||
@@ -122,8 +131,8 @@ void entityFollowPath(ecs_iter_t *it) {
|
||||
}
|
||||
}
|
||||
|
||||
steeringOutput[i].linear = Vector2Normalize(steeringOutput[i].linear);
|
||||
steeringOutput[i].linear = Vector2Scale(steeringOutput[i].linear, 10.0f);
|
||||
steering[i].linear = Vector2Normalize(steering[i].linear);
|
||||
steering[i].linear = Vector2Scale(steering[i].linear, 10.0f);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -201,6 +210,19 @@ void renderColliders(ecs_iter_t *it) {
|
||||
}
|
||||
}
|
||||
|
||||
void renderRotationDirection(ecs_iter_t *it) {
|
||||
Position *pos = ecs_field(it, Position, 1);
|
||||
Rotation *rot = ecs_field(it, Rotation, 2);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Vector2 v = {0.0f, 4.0f};
|
||||
v = Vector2Rotate(v, rot[i]);
|
||||
v = Vector2Add(v, pos[i]);
|
||||
DrawCircle(v.x, v.y, 1.0f, RED);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void renderDebugPath(ecs_iter_t *it) {
|
||||
Path *path = ecs_field(it, Path, 1);
|
||||
|
||||
Reference in New Issue
Block a user