Skip to content

Commit

Permalink
feat: Trigger- & CollisionSystem extended events
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Mar 2, 2024
1 parent 82b8e7f commit 69cb94a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 35 deletions.
1 change: 1 addition & 0 deletions .idea/.idea.MonoGame/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
Left = -30,
Top = -20,
TextAlign = TextHorizontalAlignment.Right,
Text = "Pre-alpha v0.2.2"
Text = "Pre-alpha v0.2.4"
}));
serviceRegistry.RegisterSingleton<Func<GameVersion>>(factory => factory.GetInstance<GameVersion>);

Expand Down
14 changes: 7 additions & 7 deletions src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ namespace Entities.Factories.Characters;
// But also, for the factory, ig, the view of the entities class will change.
// For now, Imma keep it as it is.
public class PlayerEntityFactory(
NameComponent nameComponent,
NameComponent name,
InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
CameraComponent cameraComponent,
RectangleColliderComponent rectangleCollider,
InventoryComponent inventoryComponent)
InventoryComponent inventory)

Check warning on line 21 in src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Parameter 'inventory' is unread.

Check warning on line 21 in src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Parameter 'inventory' is unread.

Check warning on line 21 in src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest)

Parameter 'inventory' is unread.

Check warning on line 21 in src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Parameter 'inventory' is unread.

Check warning on line 21 in src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Parameter 'inventory' is unread.

Check warning on line 21 in src/Libs/Entities/Factories/Characters/PlayerEntityFactory.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest)

Parameter 'inventory' is unread.
: EntityFactory
{
private readonly MovementAnimationsComponent _movementAnimations;
private readonly CharacterAnimatorComponent _characterAnimator;

public PlayerEntityFactory(
NameComponent nameComponent,
NameComponent name,
InputMovableComponent inputMovable,
MovableComponent movable,
TransformComponent transform,
CameraComponent cameraComponent,
RectangleColliderComponent rectangleCollider,
MovementAnimationsComponent movementAnimations,
CharacterAnimatorComponent characterAnimator,
InventoryComponent inventoryComponent) : this(nameComponent, inputMovable, movable, transform, cameraComponent,
rectangleCollider, inventoryComponent)
InventoryComponent inventory) : this(name, inputMovable, movable, transform, cameraComponent, rectangleCollider,
inventory)
{
_movementAnimations = movementAnimations;
_characterAnimator = characterAnimator;
Expand All @@ -49,10 +49,10 @@ protected override void AddTags(Entity e)

protected override void AddData(Entity e)
{
e.AddComponent(nameComponent);
e.AddComponent(name);
e.AddComponent(transform);
e.AddComponent(rectangleCollider);
e.AddComponent(inventoryComponent);
// e.AddComponent(inventory);
}

protected override void AddRender(Entity e)
Expand Down
57 changes: 43 additions & 14 deletions src/Libs/Systems/CollisionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,18 @@ public class CollisionSystem(World world) : IFixedSystem
{
public World World { get; set; } = world;

public delegate void TriggerHandler(Entity sender, CustomEventArgs args);
// TODO: Use graph instead (e.g. QuikGraph library), so will be handled the cases of relations like (combinatorics):
// { 1: [2, 3] }, { 2: [1, 2] }, { 3: [1] }, { 4: [] }
private (EntityId, EntityId)[] _activeIntersect = [];

public event TriggerHandler? RaiseTriggerIntersect;
public delegate void OutsideHandler(Entity sender, Entity with);

public delegate void InsideHandler(Entity sender, Entity with, bool isTriggerEvent);

// https://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp#:~:text=For%20completeness%27%20sake,fun%22%20NullReferenceException%20there.
public event InsideHandler? Entered;
public event InsideHandler? Stay;
public event OutsideHandler? Exited;

public void OnAwake()
{
Expand All @@ -43,22 +52,42 @@ public void OnUpdate(float deltaTime)
ref var rightTransform = ref other.GetComponent<TransformComponent>();
ref var rightCollider = ref other.GetComponent<RectangleColliderComponent>();

if (!Intersect(new(leftTransform, leftCollider),
new(rightTransform, rightCollider))) continue;

// TODO: OnEnter, OnStay, OnExit
if (leftCollider.IsTrigger || rightCollider.IsTrigger)
OnRaiseTriggerIntersect(other, new CustomEventArgs("Intersect"));
else OnCollision(ref leftTransform, ref rightTransform);
var intersect = Intersect(new(leftTransform, leftCollider),
new(rightTransform, rightCollider));
var entities = (e.ID, other.ID);
if (!intersect)
{
if (_activeIntersect.Contains(entities))
{
_activeIntersect = _activeIntersect
.TakeWhile(x => x != entities)
.ToArray();

Exited?.Invoke(e, other);
}

continue;
}

var isTriggerEvent = leftCollider.IsTrigger || rightCollider.IsTrigger;
if (!isTriggerEvent) OnCollision(ref leftTransform, ref rightTransform);

if (_activeIntersect.Contains(entities) || _activeIntersect.Contains((entities.Item2, entities.Item1)))
{
Stay?.Invoke(e, other, isTriggerEvent);
continue;
}

_activeIntersect = _activeIntersect
.TakeWhile(x => x != entities || x != (entities.Item2, entities.Item1))
.Append(entities)
.ToArray();

Entered?.Invoke(e, other, isTriggerEvent);
}
}
}

private void OnRaiseTriggerIntersect(Entity sender, CustomEventArgs customEventArgs)
{
customEventArgs.Message += $" at {DateTime.Now}";
RaiseTriggerIntersect?.Invoke(sender, customEventArgs);
}

private static void OnCollision(ref TransformComponent left, ref TransformComponent right)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Libs/Systems/InventorySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ public class InventorySystem(World world) : ISystem
{
public World World { get; set; } = world;

public delegate void InventoryHandler(Entity sender, ref InventoryComponent inventory);
public delegate void InitializeHandler(Entity sender, ref InventoryComponent inventory);

public event InventoryHandler? RaiseInventoryInitialized;
public event InitializeHandler? InventoryInitialized;

public void OnAwake()
{
var filter = World.Filter.With<InventoryComponent>().Build();

if (filter is null) return;
if (filter.IsEmpty()) return;

var entity = filter.First();
ref var inventory = ref entity.GetComponent<InventoryComponent>();

RaiseInventoryInitialized?.Invoke(entity, ref inventory);
InventoryInitialized?.Invoke(entity, ref inventory);
}

public void OnUpdate(float deltaTime)
Expand Down
15 changes: 12 additions & 3 deletions src/Libs/Systems/TriggerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ namespace Systems;
public class TriggerSystem : IFixedSystem
{
public World World { get; set; }
// TODO: List/Dictionary of intersecting colliders, so we can know who entered, staying, exited

public TriggerSystem(World world, CollisionSystem collisionSystem)
{
World = world;
collisionSystem.RaiseTriggerIntersect += OnTriggerIntersect;
collisionSystem.Entered += OnTriggerEntered;
collisionSystem.Stay += OnTriggerStay;
collisionSystem.Exited += OnTriggerExited;
}

private void OnTriggerIntersect(Entity sender, CustomEventArgs e)
private void OnTriggerEntered(Entity sender, Entity with, bool isTriggerEvent)
{
}

private void OnTriggerStay(Entity sender, Entity with, bool isTriggerEvent)
{
}

private void OnTriggerExited(Entity sender, Entity with)
{
}

Expand Down
5 changes: 5 additions & 0 deletions src/Libs/UI/Blocks/Counter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public Counter(Container container, Label counterLabel) : this(counterLabel) =>
container.Widgets.Add(_counterLabel);

public void UpdateText() => _counterLabel.Text = $"Count: {Count}";

// Test methods
public void SayEntered() => _counterLabel.Text = "Entered";

public void SayExited() => _counterLabel.Text = "Exited";
}
2 changes: 1 addition & 1 deletion src/Libs/UI/Blocks/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Inventory(Container container, InventorySystem inventorySystem)
_container = container;
_inventorySystem = inventorySystem;

_inventorySystem.RaiseInventoryInitialized += OnInventoryInitialized;
_inventorySystem.InventoryInitialized += OnInventoryInitialized;
}

private void OnInventoryInitialized(Entity sender, ref InventoryComponent inventory)
Expand Down
15 changes: 10 additions & 5 deletions src/Libs/UI/Factories/UIFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ public UIFactory AddTriggerCounter()

counter.UpdateText();

collisionSystem.RaiseTriggerIntersect += (_, _) =>
{
++counter.Count;
counter.UpdateText();
};
// collisionSystem.Stay += (_, _, isTriggerEvent) =>
// {
// if (!isTriggerEvent) return;
//
// ++counter.Count;
// counter.UpdateText();
// };

collisionSystem.Entered += (_, _, _) => counter.SayEntered();
collisionSystem.Exited += (_, _) => counter.SayExited();

return this;
}
Expand Down

0 comments on commit 69cb94a

Please sign in to comment.