Skip to content

Commit

Permalink
feat(control): Slide to collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Huvier committed Aug 16, 2023
1 parent 234af7d commit c6f32e9
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions SharpEngine.Core/Component/ControlComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,22 @@ public override void Update(float delta)
Direction = new Vec2(posX, posY).Normalized();
var newPos = new Vec2(_transform.Position.X + Direction.X * Speed * delta,
_transform.Position.Y + Direction.Y * Speed * delta);
var newPosX = new Vec2(_transform.Position.X + Speed * delta * (Direction.X < 0 ? -1 : 1 ), _transform.Position.Y);
var newPosY = new Vec2(_transform.Position.X, _transform.Position.Y + Speed * delta * (Direction.Y < 0 ? -1 : 1 ));
if (_basicPhysics == null || _basicPhysics.CanGo(newPos))
_transform.Position = newPos;
else if (Direction.X != 0 && _basicPhysics.CanGo(newPosX))
{
_transform.Position = newPosX;
Direction = new Vec2(Direction.X < 0 ? -1 : 1, 0);
}
else if (Direction.Y != 0 && _basicPhysics.CanGo(newPosY))
{
_transform.Position = newPosY;
Direction = new Vec2(0, Direction.Y < 0 ? -1 : 1);
}
else
IsMoving = false;

}
}

0 comments on commit c6f32e9

Please sign in to comment.