Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MG-34: Collision #12

Merged
merged 6 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -55,6 +56,19 @@ private static void RegisterCameraComponent(IServiceRegistry serviceRegistry) =>

private static void RegisterTransformComponent(IServiceRegistry serviceRegistry)
{
serviceRegistry.RegisterTransient<TransformComponent>();
serviceRegistry.RegisterSingleton(_ =>
{
return new TransformComponent { Position = new(316, 116) };
}, "Player");

serviceRegistry.RegisterSingleton(_ =>
{
return new TransformComponent { Position = new(300, 100) };
}, "StaticEntity");
}

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
@@ -1,15 +1,19 @@
using Entities;
using Components.Data;
using Entities;
using LightInject;

namespace GameDesktop.CompositionRoots.Entities;

public class PlayerEntityCompositionRoot : ICompositionRoot
internal class PlayerEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
RegisterEntity(serviceRegistry);
}

private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient<PlayerEntity>();
serviceRegistry.RegisterTransient(factory => new PlayerEntity(factory.GetInstance<Contexts>(),
factory.GetInstance<MovementAnimationComponent>(),
factory.GetInstance<TransformComponent>("Player"), factory.GetInstance<CameraComponent>(),
factory.GetInstance<RectangleCollisionComponent>()));
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Entities;
using Components.Data;
using Entities;
using LightInject;

namespace GameDesktop.CompositionRoots.Entities;

public class StaticEntityCompositionRoot : ICompositionRoot
internal class StaticEntityCompositionRoot : ICompositionRoot
{
public void Compose(IServiceRegistry serviceRegistry)
{
Expand All @@ -12,5 +13,8 @@ public void Compose(IServiceRegistry serviceRegistry)


private static void RegisterEntity(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterTransient<StaticEntity>();
serviceRegistry.RegisterTransient(factory => new StaticEntity(factory.GetInstance<Contexts>(),
factory.GetInstance<TransformComponent>("StaticEntity"),
factory.GetInstance<SpriteComponent>(),
factory.GetInstance<RectangleCollisionComponent>()));
}
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,8 +10,13 @@

namespace GameDesktop.CompositionRoots.Features;

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

private static readonly IMatcher<GameEntity>[] MovableMatchers = { GameMatcher.Transform, GameMatcher.Movable };

private static readonly IMatcher<GameEntity>[] AnimatedMovableMatchers =
Expand All @@ -31,6 +36,16 @@ private static void RegisterImpl(IServiceRegistry 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(CollisionMatchers);

var logger = factory.GetInstance<ILogger>();

return new CollisionSystem(group, logger);
});

serviceRegistry.RegisterSingleton(factory =>
{
var movement = factory.GetInstance<IMovement>();
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,19 +24,18 @@ public void Compose(IServiceRegistry serviceRegistry)

RegisterFeatures(serviceRegistry);

// Main entry point
serviceRegistry.RegisterSingleton<RootFeature>();
#if DEBUG
serviceRegistry.RegisterFrom<DebugRootFeatureCompositionRoot>();
#endif

RegisterEntryPoint(serviceRegistry);
}

private static void RegisterFundamental(IServiceRegistry serviceRegistry)
{
private static void RegisterFundamental(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterFrom<FundamentalCompositionRoot>();
}

private static void RegisterComponents(IServiceRegistry serviceRegistry)
{
private static void RegisterComponents(IServiceRegistry serviceRegistry) =>
serviceRegistry.RegisterFrom<ComponentsCompositionRoot>();
}

private static void RegisterEntities(IServiceRegistry serviceRegistry)
{
Expand All @@ -50,4 +50,7 @@ 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
7 changes: 5 additions & 2 deletions src/Apps/GameDesktop/CompositionRoots/GameCompositionRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

namespace GameDesktop.CompositionRoots;

public class GameCompositionRoot : ICompositionRoot
internal class GameCompositionRoot : ICompositionRoot
{
private const float TargetFramesPerSecond = 120.0f;
private const bool IsMouseVisible = true;

public void Compose(IServiceRegistry serviceRegistry)
{
serviceRegistry.Register(factory =>
{
Game game = new(factory.GetInstance<ILogger>(), factory.GetInstance<IServiceContainer>())
Game game = new(factory.GetInstance<ILogger>(),
factory.GetInstance<IServiceContainer>(),
TargetFramesPerSecond)
{
IsMouseVisible = IsMouseVisible, Content = { RootDirectory = AppVariable.ContentRootDirectory, }
};
Expand Down
21 changes: 17 additions & 4 deletions src/Apps/GameDesktop/Game.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Features;
using System;
using Features;
using GameDesktop.CompositionRoots.Features;
using LightInject;
using Microsoft.Xna.Framework;
Expand All @@ -9,17 +10,21 @@ namespace GameDesktop;

public class Game : Microsoft.Xna.Framework.Game
{
private readonly float _targetTimeStep;

private readonly ILogger _logger;
private readonly IServiceContainer _container;

private Entitas.Extended.Feature _rootFeature;
private SpriteBatch _spriteBatch;
private float _accumulatedTime;


public Game(ILogger logger, IServiceContainer container)
public Game(ILogger logger, IServiceContainer container, float targetFramesPerSecond)
{
_logger = logger;
_container = container;
_targetTimeStep = 1 / targetFramesPerSecond;

_logger.ForContext<Game>().Verbose("ctor");
}
Expand Down Expand Up @@ -75,11 +80,19 @@ protected override void EndRun()
_logger.ForContext<Game>().Verbose("Ended");
}

private void FixedUpdate(GameTime fixedGameTime) => _rootFeature.FixedExecute(fixedGameTime);
// maybe fixed update is incorrect. needs review in the future
private void FixedUpdate(GameTime gameTime) => _rootFeature.FixedExecute(gameTime);

protected override void Update(GameTime gameTime)
{
FixedUpdate(gameTime);
_accumulatedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;

while (_accumulatedTime >= _targetTimeStep)
{
FixedUpdate(gameTime);

_accumulatedTime -= _targetTimeStep;
}

base.Update(gameTime);
_rootFeature.Execute(gameTime);
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
Loading
Loading