GodotComponentTest/systems/InteractionSystem.cs

79 lines
2.2 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using Godot.Collections;
using GodotComponentTest.entities;
using Array = System.Array;
using NodePair = System.Tuple<Godot.Node, Godot.Node>;
public class InteractionSystem : Node
{
private List<NodePair> _interactionPairs;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_interactionPairs = new List<NodePair>();
}
public override void _Process(float delta)
{
base._Process(delta);
List<NodePair> invalidInteractionPairs = new List<NodePair>();
foreach (Tuple<Node, Node> 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));
}
}