Add entity rotation
This commit is contained in:
@@ -64,8 +64,9 @@ extern ECS_COMPONENT_DECLARE(SteeringOutput);
|
|||||||
typedef struct TextureRegion {
|
typedef struct TextureRegion {
|
||||||
Texture2D texture;
|
Texture2D texture;
|
||||||
Rectangle rec;
|
Rectangle rec;
|
||||||
bool flipX;
|
f32 rotation;
|
||||||
bool flipY;
|
bool flipX : 1;
|
||||||
|
bool flipY : 1;
|
||||||
} TextureRegion;
|
} TextureRegion;
|
||||||
extern ECS_COMPONENT_DECLARE(TextureRegion);
|
extern ECS_COMPONENT_DECLARE(TextureRegion);
|
||||||
|
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ bool init(void *userData) {
|
|||||||
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities);
|
ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities);
|
||||||
|
|
||||||
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size);
|
||||||
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation);
|
ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation, TextureEntities);
|
||||||
|
|
||||||
renderDebugPathSystem = renderDebugPath;
|
renderDebugPathSystem = renderDebugPath;
|
||||||
renderCollidersSystem = renderColliders;
|
renderCollidersSystem = renderColliders;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
|||||||
if (!objectTileset) return true;
|
if (!objectTileset) return true;
|
||||||
|
|
||||||
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
||||||
|
if (i == 1) break;
|
||||||
BzTileObject object = objectGroup->objects[i];
|
BzTileObject object = objectGroup->objects[i];
|
||||||
ecs_entity_t e = ecs_new_id(ECS);
|
ecs_entity_t e = ecs_new_id(ECS);
|
||||||
game->entity = e;
|
game->entity = e;
|
||||||
|
|||||||
@@ -74,21 +74,31 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
|||||||
// position += velocity * dt
|
// position += velocity * dt
|
||||||
// rotation += angularVelocity * dt
|
// rotation += angularVelocity * dt
|
||||||
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
position[i] = Vector2Add(position[i], Vector2Scale(velocity[i], dt));
|
||||||
rotation[i] += angularVel[i] * dt;
|
//rotation[i] += angularVel[i] * dt;
|
||||||
|
|
||||||
// Update velocity and angular velocity
|
// Update velocity and angular velocity
|
||||||
// velocity += steering.liner * dt
|
// velocity += steering.liner * dt
|
||||||
// angularVelocity += steering.angular * dt
|
// angularVelocity += steering.angular * dt
|
||||||
velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i].linear, dt * 10));
|
velocity[i] = Vector2Add(velocity[i], Vector2Scale(steering[i].linear, dt * 10));
|
||||||
angularVel[i] += steering[i].angular;
|
angularVel[i] += steering[i].angular * dt * 10;
|
||||||
|
|
||||||
if (Vector2LengthSqr(steering[i].linear) == 0) {
|
if (Vector2LengthSqr(steering[i].linear) == 0) {
|
||||||
// Decay velocity
|
// Decay velocity
|
||||||
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
|
velocity[i] = Vector2Scale(velocity[i], 1 - (dt * 5.0f));
|
||||||
}
|
}
|
||||||
|
angularVel[i] *= 1 - (dt * 5.0f);
|
||||||
// Reset steering
|
// Reset steering
|
||||||
steering[i] = (SteeringOutput) {};
|
steering[i] = (SteeringOutput) {};
|
||||||
|
|
||||||
|
{
|
||||||
|
const InputState *input = ecs_singleton_get(ECS, InputState);
|
||||||
|
|
||||||
|
Vector2 mouse = input->mouseDownWorld;
|
||||||
|
f32 rot = Vector2Angle(position[i], mouse) + 270 * DEG2RAD;
|
||||||
|
rotation[i] = rot;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Check for speeding and clip
|
// Check for speeding and clip
|
||||||
const f32 maxSpeed = 15.0f;
|
const f32 maxSpeed = 15.0f;
|
||||||
if (Vector2Length(velocity[i]) > maxSpeed) {
|
if (Vector2Length(velocity[i]) > maxSpeed) {
|
||||||
@@ -100,7 +110,7 @@ void entityUpdateKinematic(ecs_iter_t *it) {
|
|||||||
ecs_entity_t entity = it->entities[i];
|
ecs_entity_t entity = it->entities[i];
|
||||||
if (ecs_has(it->world, entity, TextureRegion)) {
|
if (ecs_has(it->world, entity, TextureRegion)) {
|
||||||
TextureRegion *text = ecs_get_mut(it->world, entity, TextureRegion);
|
TextureRegion *text = ecs_get_mut(it->world, entity, TextureRegion);
|
||||||
text->flipX = velocity[i].x < 0.01f;
|
text->flipX = rotation[i] >= 0.0f * RAD2DEG && rotation[i] <= 180.0f * RAD2DEG;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,10 +126,17 @@ void entityFollowPath(ecs_iter_t *it) {
|
|||||||
|
|
||||||
for (i32 i = 0; i < it->count; i++) {
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
Position target = path[i].paths->waypoints[path[i].curWaypoint];
|
Position target = path[i].paths->waypoints[path[i].curWaypoint];
|
||||||
steering[i].linear.x = target.x - position[i].x;
|
steering[i].linear = Vector2Subtract(target, position[i]);
|
||||||
steering[i].linear.y = target.y - position[i].y;
|
|
||||||
|
|
||||||
f32 dst = Vector2LengthSqr(steering[i].linear);
|
f32 dst = Vector2LengthSqr(steering[i].linear);
|
||||||
|
f32 maxAccel = 10.0f;
|
||||||
|
steering[i].linear = Vector2Normalize(steering[i].linear);
|
||||||
|
steering[i].linear = Vector2Scale(steering[i].linear, maxAccel);
|
||||||
|
|
||||||
|
if (Vector2Length(velocity[i]) > 10.0f) {
|
||||||
|
f32 rot = Vector2Angle(position[i], target);
|
||||||
|
rotation[i] = rot;
|
||||||
|
}
|
||||||
|
|
||||||
if (dst < 8.0f) {
|
if (dst < 8.0f) {
|
||||||
path[i].curWaypoint++;
|
path[i].curWaypoint++;
|
||||||
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
if (path[i].curWaypoint >= path[i].paths->numWaypoints) {
|
||||||
@@ -130,10 +147,6 @@ void entityFollowPath(ecs_iter_t *it) {
|
|||||||
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
if (!path[i].paths) ecs_remove(ECS, it->entities[i], Path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
steering[i].linear = Vector2Normalize(steering[i].linear);
|
|
||||||
steering[i].linear = Vector2Scale(steering[i].linear, 10.0f);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,7 +196,7 @@ static void render(ecs_iter_t *it) {
|
|||||||
Rectangle src = t[i].rec;
|
Rectangle src = t[i].rec;
|
||||||
if (t[i].flipX) src.width *= -1.0f;
|
if (t[i].flipX) src.width *= -1.0f;
|
||||||
if (t[i].flipY) src.height *= -1.0f;
|
if (t[i].flipY) src.height *= -1.0f;
|
||||||
DrawTexturePro(t[i].texture, src, dst, origin, r[i], WHITE);
|
DrawTexturePro(t[i].texture, src, dst, origin, t[i].rotation, WHITE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +228,7 @@ void renderRotationDirection(ecs_iter_t *it) {
|
|||||||
Rotation *rot = ecs_field(it, Rotation, 2);
|
Rotation *rot = ecs_field(it, Rotation, 2);
|
||||||
|
|
||||||
for (i32 i = 0; i < it->count; i++) {
|
for (i32 i = 0; i < it->count; i++) {
|
||||||
Vector2 v = {0.0f, 4.0f};
|
Vector2 v = {10.0f, 0.0f};
|
||||||
v = Vector2Rotate(v, rot[i]);
|
v = Vector2Rotate(v, rot[i]);
|
||||||
v = Vector2Add(v, pos[i]);
|
v = Vector2Add(v, pos[i]);
|
||||||
DrawCircle(v.x, v.y, 1.0f, RED);
|
DrawCircle(v.x, v.y, 1.0f, RED);
|
||||||
|
|||||||
@@ -34,5 +34,11 @@
|
|||||||
<property name="animation" value="walk_3"/>
|
<property name="animation" value="walk_3"/>
|
||||||
</properties>
|
</properties>
|
||||||
</tile>
|
</tile>
|
||||||
<tile id="64" type="axe"/>
|
<tile id="64" type="axe">
|
||||||
|
<objectgroup draworder="index" id="2">
|
||||||
|
<object id="1" x="7" y="5" width="6" height="6">
|
||||||
|
<ellipse/>
|
||||||
|
</object>
|
||||||
|
</objectgroup>
|
||||||
|
</tile>
|
||||||
</tileset>
|
</tileset>
|
||||||
|
|||||||
Reference in New Issue
Block a user