-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Hypercube.Client.Graphics.Events; | ||
using Hypercube.Client.Graphics.Rendering; | ||
using Hypercube.Math; | ||
using Hypercube.Math.Shapes; | ||
using Hypercube.Shared.Dependency; | ||
using Hypercube.Shared.Entities.Systems.Physics; | ||
using SharedPhysicsSystem = Hypercube.Shared.Entities.Systems.Physics.PhysicsSystem; | ||
|
||
namespace Hypercube.Client.Entities.Systems.Physics; | ||
|
||
public class PhysicsSystem : SharedPhysicsSystem | ||
{ | ||
[Dependency] private readonly IRenderer _renderer = default!; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
Subscribe<RenderDrawingEvent>(OnRenderDrawing); | ||
} | ||
|
||
private void OnRenderDrawing(ref RenderDrawingEvent args) | ||
{ | ||
foreach (var entity in GetEntities<PhysicsComponent>()) | ||
{ | ||
var shape = entity.Component.Shape; | ||
var circle = new Circle(shape.Position + entity.Component.Position, shape.Radius); | ||
|
||
_renderer.DrawCircle(circle, Color.Green); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,29 @@ | ||
using Hypercube.Client.Graphics.Texturing; | ||
using Hypercube.Math.Boxs; | ||
using Hypercube.Math.Shapes; | ||
using Hypercube.Math.Vectors; | ||
using Hypercube.Shared.Resources; | ||
|
||
namespace Hypercube.Client.Graphics.Realisation.OpenGL.Texturing; | ||
|
||
public readonly struct Texture(ResourcePath path, Vector2Int size, byte[] data) : ITexture | ||
public readonly struct Texture : ITexture | ||
{ | ||
public ResourcePath Path { get; } = path; | ||
public int Width { get; } = size.X; | ||
public int Height { get; } = size.Y; | ||
public byte[] Data { get; } = data; | ||
public ResourcePath Path { get; } | ||
public Vector2Int Size { get; } | ||
public byte[] Data { get; } | ||
|
||
public int Width => Size.X; | ||
public int Height => Size.Y; | ||
|
||
public Texture(ResourcePath path, Vector2Int size, byte[] data) | ||
{ | ||
Path = path; | ||
Size = size; | ||
Data = data; | ||
} | ||
|
||
public Box2 QuadCrateTranslated(Vector2 position) | ||
{ | ||
return new Box2(position, position + (Vector2)size); | ||
var size = (Vector2)Size / 2; | ||
return new Box2(position - size / 2, position + size / 2); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Hypercube.Math.Vectors; | ||
|
||
namespace Hypercube.Math.Shapes; | ||
|
||
public readonly struct Circle(Vector2 position, float radius) | ||
{ | ||
public readonly Vector2 Position = position; | ||
public readonly float Radius = radius; | ||
|
||
public float Area => Radius * Radius * HyperMathF.PI; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Hypercube.Math.Vectors; | ||
using Hypercube.Shared.Entities.Realisation.Components; | ||
using Hypercube.Shared.Entities.Systems.Transform; | ||
using Hypercube.Shared.Physics; | ||
using Hypercube.Shared.Physics.Shapes; | ||
|
||
namespace Hypercube.Shared.Entities.Systems.Physics; | ||
|
||
public sealed class PhysicsComponent : Component, IBody | ||
{ | ||
// Caching | ||
public TransformComponent TransformComponent; | ||
Check warning on line 12 in Hypercube.Shared/Entities/Systems/Physics/PhysicsComponent.cs GitHub Actions / Windows Build
Check warning on line 12 in Hypercube.Shared/Entities/Systems/Physics/PhysicsComponent.cs GitHub Actions / Windows Build
Check warning on line 12 in Hypercube.Shared/Entities/Systems/Physics/PhysicsComponent.cs GitHub Actions / Windows Build
|
||
public TransformSystem TransformSystem; | ||
Check warning on line 13 in Hypercube.Shared/Entities/Systems/Physics/PhysicsComponent.cs GitHub Actions / Windows Build
Check warning on line 13 in Hypercube.Shared/Entities/Systems/Physics/PhysicsComponent.cs GitHub Actions / Windows Build
Check warning on line 13 in Hypercube.Shared/Entities/Systems/Physics/PhysicsComponent.cs GitHub Actions / Windows Build
|
||
|
||
public CircleShape Shape { get; private set; } = new(); | ||
|
||
public Vector2 Velocity { get; private set; } | ||
|
||
public Vector2 Position | ||
{ | ||
get => TransformComponent.Transform.Position; | ||
set => TransformSystem.SetPosition(TransformComponent, value); | ||
} | ||
|
||
public Vector2 PreviousPosition { get; private set; } | ||
|
||
public void Move(Vector2 position) | ||
{ | ||
Position += position; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Hypercube.Shared.Dependency; | ||
using Hypercube.Shared.Entities.Realisation; | ||
using Hypercube.Shared.Entities.Realisation.Events; | ||
using Hypercube.Shared.Entities.Realisation.Systems; | ||
using Hypercube.Shared.Entities.Systems.Transform; | ||
using Hypercube.Shared.Physics; | ||
using Hypercube.Shared.Runtimes.Loop.Event; | ||
|
||
namespace Hypercube.Shared.Entities.Systems.Physics; | ||
|
||
public abstract class PhysicsSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly IPhysicsManager _physicsManager = default!; | ||
[Dependency] private readonly TransformSystem _transformSystem = default!; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
Subscribe<PhysicsComponent, ComponentAdded>(OnBodyAdded); | ||
Subscribe<PhysicsComponent, ComponentRemoved>(OnBodyRemoved); | ||
} | ||
|
||
public override void FrameUpdate(UpdateFrameEvent args) | ||
{ | ||
base.FrameUpdate(args); | ||
|
||
foreach (var physics in GetEntities<PhysicsComponent>()) | ||
{ | ||
_transformSystem.SetPosition(physics.Owner, physics.Component.Position); | ||
} | ||
} | ||
|
||
private void OnBodyAdded(Entity<PhysicsComponent> entity, ref ComponentAdded args) | ||
{ | ||
entity.Component.TransformComponent = GetComponent<TransformComponent>(entity); | ||
entity.Component.TransformSystem = _transformSystem; | ||
|
||
_physicsManager.AddBody(entity.Component); | ||
} | ||
|
||
private void OnBodyRemoved(Entity<PhysicsComponent> entity, ref ComponentRemoved args) | ||
{ | ||
_physicsManager.RemoveBody(entity.Component); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
using Hypercube.Shared.Entities.Realisation.Systems; | ||
using Hypercube.Math.Vectors; | ||
using Hypercube.Shared.Entities.Realisation; | ||
using Hypercube.Shared.Entities.Realisation.Systems; | ||
|
||
namespace Hypercube.Shared.Entities.Systems.Transform; | ||
|
||
public class TransformSystem : EntitySystem | ||
{ | ||
|
||
public void SetPosition(TransformComponent transform, Vector2 position) | ||
{ | ||
transform.Transform.SetPosition(position); | ||
} | ||
|
||
public void SetPosition(Entity<TransformComponent?> entity, Vector2 position) | ||
{ | ||
var transform = entity.Component ?? GetComponent<TransformComponent>(entity); | ||
transform.Transform.SetPosition(position); | ||
} | ||
} |