Skip to content

Commit

Permalink
WIP Basic Systems
Browse files Browse the repository at this point in the history
  • Loading branch information
roo-oliv committed Oct 7, 2024
1 parent 1b149ea commit a6acf1e
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 50 deletions.
6 changes: 6 additions & 0 deletions MonoDreams.Examples/Component/PlayerState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MonoDreams.Examples.Component;

public class PlayerState
{

}
3 changes: 3 additions & 0 deletions MonoDreams.Examples/Level/LevelLoader.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using DefaultEcs;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using MonoDreams.Component.Debug;
using MonoDreams.Examples.Level.Levels;
using MonoDreams.Level;
using MonoDreams.Objects;
using MonoDreams.Renderer;

namespace MonoDreams.Examples.Level;
Expand Down
6 changes: 3 additions & 3 deletions MonoDreams.Examples/Level/Levels/Level0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class Level0(ContentManager content, ResolutionIndependentRenderer render
.*..*..*.....*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*..*.
.*..*..W........W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W........W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W..@.....W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W........W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W........W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W........W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W........W..W..W..W..W..W..W.....................................................W..W..W..W..W..W..W..W..W..W..W..W..W..*..*.
.*..*..W........W..W..W..W..W..W..W.....................................................W..W..W..W..........................W..*..*.
.*..*..W..@.....W..W..W..W..W..W..W.....................................................W..W..W..W..........................W..*..*.
.*..*..W..............W..W..W..W..W.....................................................W..W..W..W..........................W..*..*.
.*..*..W..............W..W..W..W..W.....................................................W..W..W..W..........................W..*..*.
.*..*..W..............W..W..W..W..W.....................................................W..W..W..W..........................W..*..*.
Expand Down Expand Up @@ -96,7 +96,7 @@ public void Load(World world)
}


// Player.Create(world, Constants.WorldGravity, _square, new Vector2(-900, 370), DrawLayer.Player);
// PlayerState.Create(world, Constants.WorldGravity, _square, new Vector2(-900, 370), DrawLayer.PlayerState);
// LoadTiles(world);
// LevelBoundaries.Create(world, _square, renderer);
}
Expand Down
2 changes: 1 addition & 1 deletion MonoDreams.Examples/Level/Levels/Level1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Load(World world)
}


// Player.Create(world, Constants.WorldGravity, _square, new Vector2(-900, 370));
// PlayerState.Create(world, Constants.WorldGravity, _square, new Vector2(-900, 370));
// LoadTiles(world);
// LevelBoundaries.Create(world, _square, renderer);
}
Expand Down
10 changes: 10 additions & 0 deletions MonoDreams.Examples/MonoDreams.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
<PackageReference Include="MonoGame.Extended.Content.Pipeline" Version="3.8.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="MonoGame.ImGuiNet" Version="1.0.5" />
</ItemGroup>

<ItemGroup>
<Reference Include="ImGui.NET">
<HintPath>bin\Debug\net8.0\ImGui.NET.dll</HintPath>
</Reference>
<Reference Include="Monogame.ImGuiNet">
<HintPath>..\MonoDreams\bin\Debug\net8.0\Monogame.ImGuiNet.dll</HintPath>
</Reference>
</ItemGroup>

<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
Expand Down
2 changes: 2 additions & 0 deletions MonoDreams.Examples/Objects/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MonoDreams.Component.Collision;
using MonoDreams.Component.Input;
using MonoDreams.Component.Physics;
using MonoDreams.Examples.Component;
using Position = MonoDreams.Component.Position;

namespace MonoDreams.Examples.Objects;
Expand All @@ -14,6 +15,7 @@ public static class Player
public static Entity Create(World world, Texture2D texture, Vector2 position, Enum? drawLayer = null)
{
var entity = world.CreateEntity();
entity.Set(new PlayerState());
entity.Set(new InputControlled());
entity.Set(new Position(position));
entity.Set(new BoxCollider(new Rectangle(Point.Zero, Constants.PlayerSize)));
Expand Down
8 changes: 7 additions & 1 deletion MonoDreams.Examples/Screens/DreamGameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using MonoDreams.Component;
using MonoDreams.Examples.Level;
using MonoDreams.Examples.System;
using MonoDreams.Examples.System.InGameDebug;
using MonoDreams.Objects;
using MonoDreams.Renderer;
using MonoDreams.Screen;
using MonoDreams.State;
Expand Down Expand Up @@ -48,20 +50,24 @@ public DreamGameScreen(Game game, ContentManager content, Camera camera, Resolut
public void Load(ScreenController screenController, ContentManager content)
{
_levelLoader.LoadLevel(0);
InGameDebug.Create(_world, _spriteBatch, _renderer);
}

private SequentialSystem<GameState> CreateSystem()
{
return new SequentialSystem<GameState>(
new DebugSystem(_world, _game, _spriteBatch),
new InputHandlingSystem(),
new MovementSystem(_world, _parallelRunner),
new GravitySystem(_world, _parallelRunner, Constants.WorldGravity, Constants.MaxFallVelocity),
new VelocitySystem(_world, _parallelRunner),
new CollisionDetectionSystem(_world, _parallelRunner),
new CollisionResolutionSystem(_world),
new PositionSystem(_world, _parallelRunner),
new BeginDrawSystem(_spriteBatch, _renderer, _camera),
new DrawSystem(_world, _spriteBatch, _parallelRunner),
new EndDrawSystem(_spriteBatch)
new EndDrawSystem(_spriteBatch),
new DrawDebugSystem(_world, _spriteBatch, _renderer)
);
}

Expand Down
119 changes: 119 additions & 0 deletions MonoDreams.Examples/System/InGameDebug/DebugSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using DefaultEcs;
using DefaultEcs.System;
using ImGuiNET;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoDreams.Component;
using MonoDreams.Component.Debug;
using MonoDreams.Examples.Component;
using MonoDreams.LegacyComponents;
using MonoDreams.Message;
using MonoDreams.State;
using MonoGame.ImGuiNet;
using Position = MonoDreams.Component.Position;

namespace MonoDreams.Examples.System.InGameDebug;

public class DebugSystem : AComponentSystem<GameState, DebugUI>
{
private readonly ImGuiRenderer _guiRenderer;
private List<CollisionMessage> _collisions = [];
// private List<PlayerTouchMessage> _touches = [];
private readonly World _world;
private readonly Game _game;
private readonly SpriteBatch _batch;

public DebugSystem(World world, Game game, SpriteBatch batch) : base(world)
{
_world = world;
_game = game;
_batch = batch;
_guiRenderer = new ImGuiRenderer(game);
_world.Subscribe(this);
}

[Subscribe]
private void On(in CollisionMessage message) => _collisions.Add(message);

// [Subscribe]
// private void On(in PlayerTouchMessage message) => _touches.Add(message);

protected override void Update(GameState state, ref DebugUI debugUI)
{
_guiRenderer.RebuildFontAtlas();
_batch.GraphicsDevice.SetRenderTarget(debugUI.DebugRenderTarget);
_batch.GraphicsDevice.Clear(Color.Transparent);
_guiRenderer.BeforeLayout(state.GameTime.current);

_game.IsMouseVisible = true;

var player = _world.GetEntities().With<PlayerState>().AsEnumerable().First();
ImGui.Begin("PlayerState", ImGuiWindowFlags.Modal);

ImGui.Text($"Position: {player.Get<Position>().Current}");
// ImGui.Text($"Bounds: {player.Get<Collidable>().Bounds}");

// ImGui.SetNextItemOpen(true);
// if (ImGui.TreeNode("State"))
// {
// var playerState = player.Get<PlayerState>();
// ImGui.Text($"Movement: {playerState.Movement}");
// ImGui.Text($"Grabbing: {playerState.Grabbing}");
// ImGui.Text($"Riding: {playerState.Riding}");
// ImGui.TreePop();
// }
//
// ImGui.SetNextItemOpen(true);
// if (ImGui.TreeNode("Dynamic Body"))
// {
// var dynamicBody = player.Get<DynamicBody>();
// ImGui.Text($"Gravity: {dynamicBody.Gravity}");
// ImGui.Text($"IsJumping: {dynamicBody.IsJumping}");
// ImGui.Text($"IsRiding: {dynamicBody.IsRiding}");
// ImGui.Text($"IsSliding: {dynamicBody.IsSliding}");
// ImGui.TreePop();
// }
//
// ImGui.SetNextItemOpen(true);
// if (ImGui.TreeNode("Movement Controller"))
// {
// var movementController = player.Get<MovementController>();
// ImGui.Text($"Velocity: {movementController.Velocity}");
// ImGui.TreePop();
// }

ImGui.End();

ImGui.Begin("Collision Messages", ImGuiWindowFlags.Modal);
foreach (var collision in _collisions)
{
ImGui.SetNextItemOpen(true);
if (ImGui.TreeNode("Collision"))
{
ImGui.Text($"Base Entity: {collision.BaseEntity}");
ImGui.Text($"Colliding Entity: {collision.CollidingEntity}");
ImGui.Text($"Contact Time: {collision.ContactTime}");
ImGui.TreePop();
}
}
ImGui.End();

// ImGui.Begin("PlayerTouch Messages", ImGuiWindowFlags.Modal);
// foreach (var touch in _touches)
// {
// ImGui.SetNextItemOpen(true);
// if (ImGui.TreeNode("Touch"))
// {
// ImGui.Text($"TouchingEntity: {touch.TouchingEntity}");
// ImGui.Text($"Side: {touch.Side}");
// ImGui.TreePop();
// }
// }
// ImGui.End();

_guiRenderer.AfterLayout();
_batch.GraphicsDevice.SetRenderTarget(null);
_collisions.Clear();
// _touches.Clear();
}
}
24 changes: 24 additions & 0 deletions MonoDreams.Examples/System/InGameDebug/DrawDebugSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using DefaultEcs;
using DefaultEcs.System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoDreams.Component.Debug;
using MonoDreams.Renderer;
using MonoDreams.State;

namespace MonoDreams.Examples.System.InGameDebug;

public class DrawDebugSystem(World world, SpriteBatch batch, ResolutionIndependentRenderer renderer) : AComponentSystem<GameState, DebugUI>(world)
{
protected override void Update(GameState state, ref DebugUI debugUI)
{
batch.Begin(
SpriteSortMode.Deferred,
BlendState.AlphaBlend,
SamplerState.PointClamp,
DepthStencilState.None,
RasterizerState.CullNone);
batch.Draw(debugUI.DebugRenderTarget, Vector2.Zero, new Rectangle(0, 0, renderer.ScreenWidth, renderer.ScreenHeight), Color.White);
batch.End();
}
}
6 changes: 5 additions & 1 deletion MonoDreams.Examples/System/MovementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
namespace MonoDreams.Examples.System;

public class MovementSystem(World world, IParallelRunner parallelRunner)
: AEntitySetSystem<GameState>(world.GetEntities().With<Position>().With<InputControlled>().AsSet(), parallelRunner)
: AEntitySetSystem<GameState>(world.GetEntities().With<Position>().With<Velocity>().With<InputControlled>().AsSet(), parallelRunner)
{
protected override void Update(GameState state, in Entity entity)
{
if (InputState.Left.Pressed(state)) entity.Get<Position>().Current.X -= Constants.MaxWalkVelocity * state.Time;
if (InputState.Right.Pressed(state)) entity.Get<Position>().Current.X += Constants.MaxWalkVelocity * state.Time;
if (InputState.Jump.Pressed(state))
{
entity.Get<Velocity>().Current.Y = Constants.JumpVelocity;
}
}
}
3 changes: 3 additions & 0 deletions MonoDreams/MonoDreams.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
<ItemGroup>
<ClCompile Include="Effect\MultiTextureEffect.fx" />
</ItemGroup>
<ItemGroup>
<Content Include="bin\Debug\net8.0\Monogame.ImGuiNet.dll" />
</ItemGroup>
<Target Name="RestoreDotnetTools" BeforeTargets="Restore">
<Message Text="Restoring dotnet tools" Importance="High" />
<Exec Command="dotnet tool restore" />
Expand Down
16 changes: 16 additions & 0 deletions MonoDreams/Objects/InGameDebug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DefaultEcs;
using Microsoft.Xna.Framework.Graphics;
using MonoDreams.Component.Debug;

Check failure on line 3 in MonoDreams/Objects/InGameDebug.cs

View workflow job for this annotation

GitHub Actions / create_nuget

The type or namespace name 'Debug' does not exist in the namespace 'MonoDreams.Component' (are you missing an assembly reference?)
using MonoDreams.Renderer;

namespace MonoDreams.Objects;

public class InGameDebug
{
public static Entity Create(World world, SpriteBatch batch, ResolutionIndependentRenderer renderer)
{
var entity = world.CreateEntity();
entity.Set(new DebugUI(new RenderTarget2D(batch.GraphicsDevice, renderer.ScreenWidth, renderer.ScreenHeight)));
return entity;
}
}
26 changes: 21 additions & 5 deletions MonoDreams/System/Collision/CollisionResolutionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DefaultEcs;
using DefaultEcs.System;
using DefaultEcs.Threading;
using Microsoft.Xna.Framework;
using MonoDreams.Component;
using MonoDreams.Component.Collision;
using MonoDreams.Component.Physics;
Expand All @@ -13,9 +14,10 @@

namespace MonoDreams.System.Collision;

public class CollisionResolutionSystem<TCollidableComponent, TPositionComponent> : ISystem<GameState>
public class CollisionResolutionSystem<TCollidableComponent, TPositionComponent, TVelocityComponent> : ISystem<GameState>
where TCollidableComponent : BoxCollider, ICollider
where TPositionComponent : Position
where TVelocityComponent : Velocity
{
private readonly World _world;
private readonly List<CollisionMessage> _collisions;
Expand Down Expand Up @@ -63,6 +65,12 @@ private void ResolveCollision(CollisionMessage collision)
if (contactNormal.X != 0)
{
position.Current.X = contactPoint.X - dynamicRect.Width / 2f;

if (entity.Has<TVelocityComponent>())
{
var velocity = entity.Get<TVelocityComponent>();
velocity.Current.X = 0;
}

if ((int)Math.Abs(contactPoint.X + dynamicRect.Width / 2f - targetRect.Left) == 0)
{
Expand All @@ -77,6 +85,13 @@ private void ResolveCollision(CollisionMessage collision)
if (contactNormal.Y != 0)
{
position.Current.Y = contactPoint.Y - dynamicRect.Height / 2f;

if (entity.Has<TVelocityComponent>())
{
var velocity = entity.Get<TVelocityComponent>();
velocity.Current.Y = 0;
}

if ((int)Math.Abs(contactPoint.Y + dynamicRect.Height / 2f - targetRect.Top) == 0)
{
_world.Publish(new RigidBodyTouchMessage(collidingEntity, RelativeReferential.Top));
Expand All @@ -89,10 +104,11 @@ private void ResolveCollision(CollisionMessage collision)
}
}

public class CollisionResolutionSystem<TPosition>(World world)
: CollisionResolutionSystem<BoxCollider, TPosition>(world)
where TPosition : Position;
public class CollisionResolutionSystem<TPosition, TVelocity>(World world)
: CollisionResolutionSystem<BoxCollider, TPosition, TVelocity>(world)
where TPosition : Position
where TVelocity : Velocity;

public class CollisionResolutionSystem(World world)
: CollisionResolutionSystem<BoxCollider, Position>(world);
: CollisionResolutionSystem<BoxCollider, Position, Velocity>(world);

Loading

0 comments on commit a6acf1e

Please sign in to comment.