using Godot; using System; using System.Collections.Generic; using Godot.Collections; using GodotComponentTest.entities; using Array = System.Array; using NodePair = System.Tuple; public class InteractionSystem : Node { private List _interactionPairs; // Called when the node enters the scene tree for the first time. public override void _Ready() { _interactionPairs = new List(); } public override void _Process(float delta) { base._Process(delta); List invalidInteractionPairs = new List(); foreach (Tuple pair in _interactionPairs) { Node nodeA = pair.Item1; Node nodeB = pair.Item2; if (nodeA == null || nodeA.IsQueuedForDeletion() || nodeB == null || nodeB.IsQueuedForDeletion()) { invalidInteractionPairs.Add(pair); } if (nodeA == null || nodeA.IsQueuedForDeletion()) { IInteractionInterface interactableB = nodeB as IInteractionInterface; if (interactableB != null) { interactableB.OnInteractionEnd(); } } if (nodeB == null || nodeB.IsQueuedForDeletion()) { IInteractionInterface interactableA = nodeA as IInteractionInterface; if (interactableA != null) { interactableA.OnInteractionEnd(); } } } foreach (NodePair pair in invalidInteractionPairs) { _interactionPairs.Remove(pair); } } public void OnStartInteraction(Node nodeA, Node nodeB) { IInteractionInterface interactableA = nodeA as IInteractionInterface; if (interactableA != null) { interactableA.OnInteractionStart(); } IInteractionInterface interactableB = nodeB as IInteractionInterface; if (interactableB != null) { interactableB.OnInteractionStart(); } NodePair pair = new NodePair(nodeA, nodeB); _interactionPairs.Add(new NodePair(nodeA, nodeB)); } }