Skip to content

Commit

Permalink
Merge pull request #7 from Coborax/simple-aabb-physics
Browse files Browse the repository at this point in the history
feat: added simple physics system
  • Loading branch information
Coborax authored Nov 13, 2022
2 parents 8d90e8c + 40a51c1 commit 55f634f
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 30 deletions.
4 changes: 2 additions & 2 deletions SignE.Core/ECS/Components/Movement2DComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ namespace SignE.Core.ECS.Components
public class Movement2DComponent : IComponent
{
public float Speed { get; set; } = 50.0f;
public float VelX { get; set; }
public float VelY { get; set; }
public float JumpSpeed { get; set; } = 5.0f;

public void InitComponent()
{

Expand Down
13 changes: 13 additions & 0 deletions SignE.Core/ECS/Components/Physics/AABBComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace SignE.Core.ECS.Components.Physics
{
public class AABBComponent : IComponent
{
public int Width { get; set; }
public int Height { get; set; }

public void InitComponent()
{

}
}
}
15 changes: 15 additions & 0 deletions SignE.Core/ECS/Components/Physics/PhysicsMoverComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace SignE.Core.ECS.Components.Physics
{
public class PhysicsMoverComponent : IComponent
{
public float VelX { get; set; }
public float VelY { get; set; }

public float Gravity { get; set; } = 200.0f;

public void InitComponent()
{

}
}
}
1 change: 1 addition & 0 deletions SignE.Core/ECS/GameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public abstract class GameSystem
protected List<Entity> Entities;

public abstract void UpdateSystem();
public abstract void LateUpdateSystem();
public abstract void DrawSystem();
public abstract void GetEntities(World world);
}
Expand Down
5 changes: 5 additions & 0 deletions SignE.Core/ECS/Systems/Animator2DSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public override void UpdateSystem()
}
}

public override void LateUpdateSystem()
{

}

public override void DrawSystem()
{

Expand Down
5 changes: 5 additions & 0 deletions SignE.Core/ECS/Systems/Camera2DSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public override void UpdateSystem()
SignE.Graphics.Camera2D.Y = pos.Y;
}

public override void LateUpdateSystem()
{

}

public override void DrawSystem()
{

Expand Down
5 changes: 5 additions & 0 deletions SignE.Core/ECS/Systems/Draw2DSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public override void UpdateSystem()

}

public override void LateUpdateSystem()
{

}

public override void DrawSystem()
{
DrawCircles();
Expand Down
33 changes: 16 additions & 17 deletions SignE.Core/ECS/Systems/Movement2DSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using SignE.Core.ECS.Components;
using SignE.Core.ECS.Components.Physics;
using SignE.Core.Extensions;
using SignE.Core.Input;

Expand All @@ -11,29 +12,27 @@ public override void UpdateSystem()
{
foreach (var entity in Entities)
{
var pos = entity.GetComponent<Position2DComponent>();
var mover = entity.GetComponent<PhysicsMoverComponent>();
var movement = entity.GetComponent<Movement2DComponent>();

movement.VelX = 0;
movement.VelY = 0;

if (SignE.Input.IsKeyDown(Key.W))
movement.VelY = -(movement.Speed * SignE.Graphics.DeltaTime);

if (SignE.Input.IsKeyDown(Key.S))
movement.VelY = (movement.Speed * SignE.Graphics.DeltaTime);

mover.VelX = 0;

if (SignE.Input.IsKeyDown(Key.D))
movement.VelX = (movement.Speed * SignE.Graphics.DeltaTime);

if (SignE.Input.IsKeyDown(Key.A))
movement.VelX = -(movement.Speed * SignE.Graphics.DeltaTime);
mover.VelX = movement.Speed;

pos.X += movement.VelX;
pos.Y += movement.VelY;
if (SignE.Input.IsKeyDown(Key.A))
mover.VelX = -movement.Speed;

if (SignE.Input.IsKeyPressed(Key.SPACE))
mover.VelY -= movement.JumpSpeed;
}
}

public override void LateUpdateSystem()
{

}

public override void DrawSystem()
{

Expand All @@ -42,7 +41,7 @@ public override void DrawSystem()
public override void GetEntities(World world)
{
Entities = world.Entities
.WithComponent<Position2DComponent>()
.WithComponent<PhysicsMoverComponent>()
.WithComponent<Movement2DComponent>()
.ToList();
}
Expand Down
93 changes: 93 additions & 0 deletions SignE.Core/ECS/Systems/Physics/SimplePhysicsSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SignE.Core.ECS.Components;
using SignE.Core.ECS.Components.Physics;
using SignE.Core.Extensions;

namespace SignE.Core.ECS.Systems.Physics
{
public class SimplePhysicsSystem : GameSystem
{

private List<Entity> _simpleMovers;

public override void UpdateSystem()
{

}

public override void LateUpdateSystem()
{
foreach (var entity in _simpleMovers)
{
var mover = entity.GetComponent<PhysicsMoverComponent>();
var entityPos = entity.GetComponent<Position2DComponent>();
var aabb = entity.GetComponent<AABBComponent>();

mover.VelY += mover.Gravity;

var posX = new Position2DComponent
{
X = entityPos.X + mover.VelX * SignE.Graphics.DeltaTime,
Y = entityPos.Y
};

var posY = new Position2DComponent
{
X = entityPos.X,
Y = entityPos.Y + mover.VelY * SignE.Graphics.DeltaTime
};

foreach (var other in Entities)
{
if (entity == other)
continue;

var otherPos = other.GetComponent<Position2DComponent>();
var otherAabb = other.GetComponent<AABBComponent>();

if (CheckCollision(posX, aabb, otherPos, otherAabb))
mover.VelX = 0;

if (CheckCollision(posY, aabb, otherPos, otherAabb))
mover.VelY = 0;
}

entityPos.X += mover.VelX * SignE.Graphics.DeltaTime;
entityPos.Y += mover.VelY * SignE.Graphics.DeltaTime;
}
}

private static bool CheckCollision(Position2DComponent aPos, AABBComponent aAabb, Position2DComponent bPos, AABBComponent bAabb)
{
var aIsToTheRightOfB = aPos.X - aAabb.Width / 2 > bPos.X + bAabb.Width / 2;
var aIsToTheLeftOfB = aPos.X + aAabb.Width / 2 < bPos.X - bAabb.Width / 2;
var aIsAboveB = aPos.Y + aAabb.Height / 2 < bPos.Y - bAabb.Height / 2;
var aIsBelowB = aPos.Y - aAabb.Height / 2 > bPos.Y + bAabb.Height / 2;

return !(aIsToTheRightOfB
|| aIsToTheLeftOfB
|| aIsAboveB
|| aIsBelowB);
}

public override void DrawSystem()
{
foreach (var entity in Entities)
{
var pos = entity.GetComponent<Position2DComponent>();
var aabb = entity.GetComponent<AABBComponent>();

SignE.Graphics.DrawRectangle(pos.X, pos.Y, aabb.Width, aabb.Height, false);
}
}

public override void GetEntities(World world)
{
_simpleMovers = world.Entities.WithComponent<Position2DComponent>().WithComponent<AABBComponent>()
.WithComponent<PhysicsMoverComponent>().ToList();
Entities = world.Entities.WithComponent<Position2DComponent>().WithComponent<AABBComponent>().ToList();
}
}
}
5 changes: 5 additions & 0 deletions SignE.Core/ECS/Systems/YSortSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public override void UpdateSystem()
}
}

public override void LateUpdateSystem()
{

}

public override void DrawSystem()
{

Expand Down
10 changes: 10 additions & 0 deletions SignE.Core/ECS/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public void UpdateSystems()
}
}

public void LateUpdateSystems()
{
foreach (var gameSystem in _systems)
{
gameSystem.LateUpdateSystem();
}
}

public void DrawSystems()
{
foreach (var gameSystem in _systems)
Expand All @@ -70,5 +78,7 @@ public List<GameSystem> GetAllSystems()
{
return _systems;
}


}
}
12 changes: 9 additions & 3 deletions SignE.ExampleGame/ECS/Systems/PlayerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using SignE.Core.ECS;
using SignE.Core.ECS.Components;
using SignE.Core.ECS.Components.Physics;
using SignE.Core.ECS.Systems;
using SignE.Core.Extensions;
using SignE.Core.Input;
Expand All @@ -16,7 +17,7 @@ public override void UpdateSystem()
foreach (var entity in Entities)
{
var player = entity.GetComponent<PlayerComponent>();
var movement = entity.GetComponent<Movement2DComponent>();
var movement = entity.GetComponent<PhysicsMoverComponent>();
var sprite = entity.GetComponent<SpriteComponent>();
var animator = entity.GetComponent<Animator2DComponent>();

Expand Down Expand Up @@ -47,7 +48,12 @@ public override void UpdateSystem()
}
}

private void UpdateIdleRunning(Movement2DComponent movement, PlayerComponent player)
public override void LateUpdateSystem()
{

}

private void UpdateIdleRunning(PhysicsMoverComponent movement, PlayerComponent player)
{
if (movement.VelX != 0 || movement.VelY != 0)
player.PlayerState = PlayerState.Running;
Expand Down Expand Up @@ -86,7 +92,7 @@ public override void GetEntities(World world)
{
Entities = world.Entities
.WithComponent<SpriteComponent>()
.WithComponent<Movement2DComponent>()
.WithComponent<PhysicsMoverComponent>()
.WithComponent<Animator2DComponent>()
.WithComponent<PlayerComponent>().ToList();
}
Expand Down
5 changes: 5 additions & 0 deletions SignE.ExampleGame/ECS/Systems/SmoothFollowSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public override void UpdateSystem()
}
}

public override void LateUpdateSystem()
{

}

public override void DrawSystem()
{

Expand Down
47 changes: 46 additions & 1 deletion SignE.ExampleGame/Resources/Levels/sandbox.level
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,18 @@
{
"$type": "SignE.Core.ECS.Components.Movement2DComponent, SignE.Core",
"Speed": 100.0,
"JumpSpeed": 300.0
},
{
"$type": "SignE.Core.ECS.Components.Physics.AABBComponent, SignE.Core",
"Width": 30,
"Height": 60
},
{
"$type": "SignE.Core.ECS.Components.Physics.PhysicsMoverComponent, SignE.Core",
"VelX": 0.0,
"VelY": 0.0
"VelY": 0.0,
"Gravity": 0.5
},
{
"$type": "SignE.ExampleGame.ECS.Components.PlayerComponent, SignE.ExampleGame",
Expand Down Expand Up @@ -297,6 +307,32 @@
"FlipX": false,
"FlipY": false,
"IsSpritesheet": false
},
{
"$type": "SignE.Core.ECS.Components.Physics.AABBComponent, SignE.Core",
"Width": 500,
"Height": 360
}
]
},
{
"Id": "3982be64-3340-42db-97c1-f631e417adbc",
"Components": [
{
"$type": "SignE.Core.ECS.Components.Position2DComponent, SignE.Core",
"Y": 0.0,
"X": 0.0
},
{
"$type": "SignE.Core.ECS.Components.Camera2DComponent, SignE.Core"
},
{
"$type": "SignE.ExampleGame.ECS.Components.SmoothFollowComponent, SignE.ExampleGame",
"Target": {
"Id": "1c870102-4b69-426b-9377-e9e0dd6281c1"
},
"Smooth": 0.001,
"NewPropertyHihi": 0.0
}
]
}
Expand All @@ -313,6 +349,15 @@
},
{
"$type": "SignE.ExampleGame.ECS.Systems.PlayerSystem, SignE.ExampleGame"
},
{
"$type": "SignE.ExampleGame.ECS.Systems.SmoothFollowSystem, SignE.ExampleGame"
},
{
"$type": "SignE.Core.ECS.Systems.Physics.SimplePhysicsSystem, SignE.Core"
},
{
"$type": "SignE.Core.ECS.Systems.Camera2DSystem, SignE.Core"
}
]
}
Loading

0 comments on commit 55f634f

Please sign in to comment.