Skip to content

Commit

Permalink
chore: init rect collision feat & sys
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Oct 11, 2023
1 parent 46355ec commit 7ef5a50
Show file tree
Hide file tree
Showing 30 changed files with 191 additions and 34 deletions.
14 changes: 14 additions & 0 deletions MonoGame.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features", "src\Libs\Featur
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "src\Libs\Entities\Entities.csproj", "{8232C7AA-E217-465B-95A3-C42D1000FA3B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Systems.Debugging", "src\Libs\Systems.Debugging\Systems.Debugging.csproj", "{7AD01EBF-370B-4F35-9AEC-172A889D643F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Features.Debugging", "src\Libs\Features.Debugging\Features.Debugging.csproj", "{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -70,6 +74,14 @@ Global
{8232C7AA-E217-465B-95A3-C42D1000FA3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8232C7AA-E217-465B-95A3-C42D1000FA3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8232C7AA-E217-465B-95A3-C42D1000FA3B}.Release|Any CPU.Build.0 = Release|Any CPU
{7AD01EBF-370B-4F35-9AEC-172A889D643F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7AD01EBF-370B-4F35-9AEC-172A889D643F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7AD01EBF-370B-4F35-9AEC-172A889D643F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7AD01EBF-370B-4F35-9AEC-172A889D643F}.Release|Any CPU.Build.0 = Release|Any CPU
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5E3B6238-A92E-4703-8527-1C2410D7906A} = {F5258F7E-B0BA-466D-8CF8-4DAB84722BE3}
Expand All @@ -84,5 +96,7 @@ Global
{6EEBFA40-2814-43D0-A9DC-4A98F4837A1B} = {FE9CE932-6F98-4455-A80F-2A0CDB6DB46E}
{F4C957C8-3B10-4614-99F4-E2F2398F38ED} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
{8232C7AA-E217-465B-95A3-C42D1000FA3B} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
{7AD01EBF-370B-4F35-9AEC-172A889D643F} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
{8E6812DB-F7B8-411D-BD15-4BCFA2BF6C28} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using Components;
using Components.World;
using Components.Data;
using GameDesktop.Resources.Internal;
using LightInject;
using Microsoft.Xna.Framework;
Expand All @@ -10,7 +10,7 @@

namespace GameDesktop.CompositionRoots.Components;

public class ComponentsCompositionRoot : ICompositionRoot
internal class ComponentsCompositionRoot : ICompositionRoot
{
private static readonly string PlayerSpriteSheetPath = System.IO.Path.Join(
Environment.GetEnvironmentVariable(EnvironmentVariable.AppBaseDirectory),
Expand All @@ -22,6 +22,7 @@ public void Compose(IServiceRegistry serviceRegistry)
RegisterMovementAnimationComponent(serviceRegistry);
RegisterCameraComponent(serviceRegistry);
RegisterTransformComponent(serviceRegistry);
RegisterRectangleCollisionComponent(serviceRegistry);
}

private static void RegisterSpriteComponent(IServiceRegistry serviceRegistry)
Expand Down Expand Up @@ -57,4 +58,9 @@ private static void RegisterTransformComponent(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterTransient<TransformComponent>();
}

private static void RegisterRectangleCollisionComponent(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterTransient(_ => new RectangleCollisionComponent { Size = new Rectangle(0, 0, 8, 8)});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Entitas;
using Features.Debugging;
using GameDesktop.Resources.Internal;
using LightInject;
using Serilog;
using Systems.Debugging;

namespace GameDesktop.CompositionRoots.DebugFeatures;

internal class DebugRootFeatureCompositionRoot : ICompositionRoot
{
private static readonly IMatcher<GameEntity>[] Matchers = { GameMatcher.RectangleCollision, GameMatcher.Transform };

public void Compose(IServiceRegistry serviceRegistry)
{
RegisterSystems(serviceRegistry);
RegisterFeature(serviceRegistry);
}

private static void RegisterSystems(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton(factory =>
{
var getGroup =
factory.GetInstance<Func<IMatcher<GameEntity>[], IGroup<GameEntity>>>(Matcher.AllOf);
IGroup<GameEntity> group = getGroup(Matchers);

return new DrawRectangleCollisionComponentsSystem(factory.GetInstance<Contexts>(), group,
factory.GetInstance<ILogger>());
});
}

private static void RegisterFeature(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterSingleton<DebugRootFeature>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GameDesktop.CompositionRoots.Entities;

public class PlayerEntityCompositionRoot : ICompositionRoot
internal class PlayerEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GameDesktop.CompositionRoots.Entities;

public class StaticEntityCompositionRoot : ICompositionRoot
internal class StaticEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using Entitas;
using Entitas.Extended;
using Features;
using GameDesktop.Resources.Internal;
using LightInject;
using Systems;

namespace GameDesktop.CompositionRoots.Features;

public class CameraFeatureCompositionRoot : ICompositionRoot
internal class CameraFeatureCompositionRoot : ICompositionRoot
{
private static readonly IMatcher<GameEntity>[] Matchers = { GameMatcher.Transform, GameMatcher.Drawable };

Expand All @@ -19,15 +20,15 @@ public void Compose(IServiceRegistry serviceRegistry)

private static void RegisterSystem(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterFallback((type, s) => true, request =>
serviceRegistry.RegisterSingleton<IDrawSystem>(factory =>
{
var getGroup =
request.ServiceFactory.GetInstance<Func<IMatcher<GameEntity>[], IGroup<GameEntity>>>(Matcher.AllOf);
factory.GetInstance<Func<IMatcher<GameEntity>[], IGroup<GameEntity>>>(Matcher.AllOf);
IGroup<GameEntity> group = getGroup(Matchers);

// return new CameraFollowingSystem(request.ServiceFactory.GetInstance<Contexts>(), group);
// return new CameraFollowingSystem(factory.GetInstance<Contexts>(), group);
return new DefaultDrawSystem(group);
}, new PerContainerLifetime());
});
}

private static void RegisterFeature(IServiceRegistry serviceRegistry) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace GameDesktop.CompositionRoots.Features;

public class InputFeatureCompositionRoot : ICompositionRoot
internal class InputFeatureCompositionRoot : ICompositionRoot
{
private static readonly IMatcher<GameEntity>[] Matchers =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace GameDesktop.CompositionRoots.Features;

public class MovementFeatureCompositionRoot : ICompositionRoot
internal class MovementFeatureCompositionRoot : ICompositionRoot
{
private static readonly IMatcher<GameEntity>[] MovableMatchers = { GameMatcher.Transform, GameMatcher.Movable };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Features;
using GameDesktop.CompositionRoots.Components;
using GameDesktop.CompositionRoots.DebugFeatures;
using GameDesktop.CompositionRoots.Entities;
using LightInject;

namespace GameDesktop.CompositionRoots.Features;

public class RootFeatureCompositionRoot : ICompositionRoot
internal class RootFeatureCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
Expand All @@ -23,8 +24,10 @@ public void Compose(IServiceRegistry serviceRegistry)

RegisterFeatures(serviceRegistry);

// Main entry point
serviceRegistry.RegisterSingleton<RootFeature>();
// Extra pre-entry point for debugging, might be safely excluded
serviceRegistry.RegisterFrom<DebugRootFeatureCompositionRoot>();

RegisterEntryPoint(serviceRegistry);
}

private static void RegisterFundamental(IServiceRegistry serviceRegistry)
Expand All @@ -50,4 +53,9 @@ private static void RegisterFeatures(IServiceRegistry serviceRegistry)
serviceRegistry.RegisterFrom<CameraFeatureCompositionRoot>();
serviceRegistry.RegisterFrom<MovementFeatureCompositionRoot>();
}

private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterSingleton<RootFeature>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GameDesktop.CompositionRoots.Features;

public class WorldInitializeFeatureCompositionRoot : ICompositionRoot
internal class WorldInitializeFeatureCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace GameDesktop.CompositionRoots;

public class FundamentalCompositionRoot : ICompositionRoot
internal class FundamentalCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GameDesktop.CompositionRoots;

public class GameCompositionRoot : ICompositionRoot
internal class GameCompositionRoot : ICompositionRoot
{
private const bool IsMouseVisible = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Entitas.CodeGeneration.Attributes;
using Microsoft.Xna.Framework;

namespace Components;
namespace Components.Data;

[Unique]
public class CameraComponent : IComponent
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Entitas;
using Components.Tags;
using Microsoft.Xna.Framework;
using MonoGame.Aseprite.Sprites;
using Services.Math;

namespace Components;
namespace Components.Data;

public class MovementAnimationComponent : DrawableComponent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Entitas;
using Microsoft.Xna.Framework;

namespace Components.World;
namespace Components.Data;

public class RectangleCollisionComponent : IComponent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Entitas;
using Components.Tags;
using MonoGame.Aseprite.Sprites;

namespace Components;
namespace Components.Data;

public class SpriteComponent : DrawableComponent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Entitas;
using Microsoft.Xna.Framework;

namespace Components.World;
namespace Components.Data;

public class TransformComponent : IComponent
{
Expand Down
7 changes: 6 additions & 1 deletion src/Libs/Components/GeneratedExtended/GameEntity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Components;
using Components.World;
using Components.Data;
using Microsoft.Xna.Framework;

public partial class GameEntity
Expand All @@ -25,4 +25,9 @@ public void AddSprite(SpriteComponent component)
isDrawable = true;
AddSprite(newSprite: component.Sprite);
}

public void AddRectangleCollision(RectangleCollisionComponent component)
{
AddRectangleCollision(newSize: component.Size);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Entitas;

namespace Components;
namespace Components.Tags;

public class DrawableComponent : IComponent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Entitas;

namespace Components.World;
namespace Components.Tags;

public class MovableComponent : IComponent
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Entitas;

namespace Components.World;
namespace Components.Tags;

public class PlayerComponent : IComponent
{
Expand Down
7 changes: 4 additions & 3 deletions src/Libs/Entities/PlayerEntity.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Components;
using Components.World;
using Components.Data;

namespace Entities;

Expand All @@ -8,7 +8,8 @@ public class PlayerEntity
public PlayerEntity(Contexts contexts,
MovementAnimationComponent movementAnimationComponent,
TransformComponent transformComponent,
CameraComponent cameraComponent)
CameraComponent cameraComponent,
RectangleCollisionComponent rectangleCollisionComponent)
{
GameEntity entity = contexts.game.CreateEntity();

Expand All @@ -17,6 +18,6 @@ public PlayerEntity(Contexts contexts,
entity.AddMovementAnimation(movementAnimationComponent);
entity.AddTransform(transformComponent);
entity.AddCamera(cameraComponent);
// e.AddRectangleCollision(new Rectangle(0, 0, 16, 16));
entity.AddRectangleCollision(rectangleCollisionComponent);
}
}
7 changes: 5 additions & 2 deletions src/Libs/Entities/StaticEntity.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using Components;
using Components.World;
using Components.Data;
using Microsoft.Xna.Framework;

namespace Entities;

public class StaticEntity
{
public StaticEntity(Contexts contexts,
TransformComponent transformComponent,
SpriteComponent spriteComponent)
SpriteComponent spriteComponent,
RectangleCollisionComponent rectangleCollision)
{
GameEntity e = contexts.game.CreateEntity();

e.AddTransform(transformComponent);
e.AddSprite(spriteComponent);
e.AddRectangleCollision(rectangleCollision);
}
}
11 changes: 11 additions & 0 deletions src/Libs/Features.Debugging/DebugRootFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Systems.Debugging;

namespace Features.Debugging;

public sealed class DebugRootFeature : Entitas.Extended.Feature
{
public DebugRootFeature(DrawRectangleCollisionComponentsSystem drawRectangleCollisionComponentsSystem)
{
Add(drawRectangleCollisionComponentsSystem);
}
}
Loading

0 comments on commit 7ef5a50

Please sign in to comment.