-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: collision system split wit debugging
- Loading branch information
Showing
5 changed files
with
80 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |