Skip to content

Commit

Permalink
refactor: collision system split wit debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Oct 12, 2023
1 parent 39c2d6d commit 3b39073
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace GameDesktop.CompositionRoots.Features;

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
4 changes: 0 additions & 4 deletions src/Libs/Features/Features.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@
<PackageReference Include="LightInject" Version="6.6.4" />
</ItemGroup>

<ItemGroup>
<Folder Include="Debug\" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion src/Libs/Features/MovementFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ namespace Features;

public sealed class MovementFeature : Entitas.Extended.Feature
{
public MovementFeature(MovementSystem movementSystem,
public MovementFeature(
CollisionSystem collisionSystem,
MovementSystem movementSystem,
AnimatedMovementSystem animatedMovementSystem)
{
Add(collisionSystem);
Add(movementSystem);
Add(animatedMovementSystem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,31 @@ public DrawRectangleCollisionComponentsSystem(Contexts contexts, IGroup<GameEnti
}

// TODO: Move the logic in the release features
// TODO: Make sloped collision
// TODO: Pivot & anchors system
// TODO: Make circle colliders & relative system (sloped collision)
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
GameEntity[] entities = _group.GetEntities().OrderBy(x => x.transform.Position.Y).ToArray();
GameEntity[] entities = _group.GetEntities();

spriteBatch.Begin(samplerState: SamplerState.PointClamp);

// temp start
Color[] colors = { Color.Black, Color.White };
int k = 0;
// temp end

for (int i = 0; i < entities.Length; ++i)
foreach (GameEntity e in entities)
{
GameEntity first = entities[i];

for (int j = i + 1; j < entities.Length; ++j)
{
GameEntity second = entities[j];
if (AreIntersecting(first, second))
{
first.transform.Velocity = Vector2.Zero;
second.transform.Velocity = Vector2.Zero;
}
}

// temp start
var texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
texture.SetData(new[] { colors[k] });

++k;
if (k is 2)
if (k == colors.Length)
k = 0;
// temp end

spriteBatch.Draw(texture, first.transform.Position, sourceRectangle: first.rectangleCollision.Size,
spriteBatch.Draw(texture, e.transform.Position, sourceRectangle: e.rectangleCollision.Size,
Color.White);
}

spriteBatch.End();
}

private bool AreIntersecting(GameEntity first, GameEntity second)
{
Func<GameEntity, Rectangle> buildRectangle = new(x =>
new((int)(x.transform.Position.X + x.transform.Velocity.X),
(int)(x.transform.Position.Y + x.transform.Velocity.Y),
x.rectangleCollision.Size.Width,
x.rectangleCollision.Size.Height));

Rectangle firstRectangle = buildRectangle(first);
Rectangle secondRectangle = buildRectangle(second);

return Rectangle.Intersect(firstRectangle, secondRectangle).IsEmpty is false;
}
}
54 changes: 54 additions & 0 deletions src/Libs/Systems/CollisionSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Entitas;
using Entitas.Extended;
using Microsoft.Xna.Framework;
using Serilog;

namespace Systems;

public class CollisionSystem : IFixedExecuteSystem
{
private readonly IGroup<GameEntity> _group;
private readonly ILogger _logger;

public CollisionSystem(IGroup<GameEntity> group, ILogger logger)
{
_group = group;
_logger = logger;
}

// TODO: Optimize efficiency
public void FixedExecute(GameTime gameTime)
{
GameEntity[] entities = _group.GetEntities();

for (int i = 0; i < entities.Length; ++i)
{
GameEntity first = entities[i];

for (int j = i + 1; j < entities.Length; ++j)
{
GameEntity second = entities[j];

if (AreIntersecting(first, second))
{
first.transform.Velocity = Vector2.Zero;
second.transform.Velocity = Vector2.Zero;
}
}
}
}

private static bool AreIntersecting(GameEntity first, GameEntity second)
{
Rectangle firstRectangle = BuildRectangle(first);
Rectangle secondRectangle = BuildRectangle(second);

Rectangle intersect = Rectangle.Intersect(firstRectangle, secondRectangle);

return !intersect.IsEmpty;

Rectangle BuildRectangle(GameEntity x) => new((int)(x.transform.Position.X + x.transform.Velocity.X),
(int)(x.transform.Position.Y + x.transform.Velocity.Y), x.rectangleCollision.Size.Width,
x.rectangleCollision.Size.Height);
}
}

0 comments on commit 3b39073

Please sign in to comment.