From 187dac2d85319e85ebbebec7e60443e11a392dc5 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Fri, 10 Nov 2023 16:26:16 +0100 Subject: [PATCH] Interaction now working with streamed entities. Yay! --- entities/Chest.cs | 28 ++++++++------------- entities/Tree.cs | 20 ++++++--------- scenes/Game.cs | 2 ++ scenes/World.cs | 10 ++++++-- systems/InteractionSystem.cs | 49 ++++++++++++++++++------------------ 5 files changed, 53 insertions(+), 56 deletions(-) diff --git a/entities/Chest.cs b/entities/Chest.cs index fcbf940..a0c24f0 100644 --- a/entities/Chest.cs +++ b/entities/Chest.cs @@ -1,13 +1,12 @@ -using Godot; -using System; using System.Linq; +using Godot; using GodotComponentTest.components; using GodotComponentTest.entities; public class Chest : Entity, IInteractionInterface { // resources - private PackedScene _goldBarScene = GD.Load("res://entities/GoldBar.tscn"); + private readonly PackedScene _goldBarScene = GD.Load("res://entities/GoldBar.tscn"); public enum LidState { @@ -16,23 +15,18 @@ public class Chest : Entity, IInteractionInterface } public LidState State = LidState.Closed; - public bool IsMouseOver = false; - + public bool IsMouseOver; + private MeshInstance _mesh; private AnimationPlayer _animationPlayer; - private InteractionComponent _interactionComponent; - public InteractionComponent InteractionComponent - { - get => _interactionComponent; - set => _interactionComponent = value; - } - - [Signal] - delegate void EntityClicked(Entity entity); + public InteractionComponent InteractionComponent { get; set; } [Signal] - delegate void ChestOpened(Entity entity); + private delegate void EntityClicked(Entity entity); + + [Signal] + private delegate void ChestOpened(Entity entity); // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -114,9 +108,9 @@ public class Chest : Entity, IInteractionInterface GetParent().AddChild(bar); } - if (_interactionComponent != null) + if (InteractionComponent != null) { - _interactionComponent.EmitSignal("InteractionEnd"); + InteractionComponent.EndInteraction(); } } } \ No newline at end of file diff --git a/entities/Tree.cs b/entities/Tree.cs index f754484..2c8c728 100644 --- a/entities/Tree.cs +++ b/entities/Tree.cs @@ -1,6 +1,6 @@ -using Godot; using System; using System.Diagnostics; +using Godot; using GodotComponentTest.components; using GodotComponentTest.entities; @@ -8,22 +8,16 @@ public class Tree : StaticBody, IInteractionInterface { [Export] public float ChopDuration = 2; public bool IsMouseOver; - + public InteractionComponent InteractionComponent { get; set; } + private MeshInstance _geometry; private AnimationPlayer _animationPlayer; private float _health = 100; private bool _isBeingChopped; - - private InteractionComponent _interactionComponent; - public InteractionComponent InteractionComponent - { - get => _interactionComponent; - set => _interactionComponent = value; - } - + [Signal] - delegate void EntityClicked(Entity entity); - + public delegate void EntityClicked(Entity entity); + // Called when the node enters the scene tree for the first time. public override void _Ready() { @@ -51,6 +45,8 @@ public class Tree : StaticBody, IInteractionInterface if (_health == 0) { + InteractionComponent.EndInteraction(); + InteractionComponent.TargetEntity = null; QueueFree(); } } diff --git a/scenes/Game.cs b/scenes/Game.cs index 60df2b4..475a425 100644 --- a/scenes/Game.cs +++ b/scenes/Game.cs @@ -114,6 +114,8 @@ public class Game : Spatial if (node.HasSignal("EntityClicked")) node.Connect("EntityClicked", this, nameof(OnEntityClicked)); + _world.Connect("EntityClicked", this, nameof(OnEntityClicked)); + // perform dependency injection //_streamContainer.SetWorld(_tileWorld);Clicked var worldInfoComponent = _player.GetNode("WorldInfo"); diff --git a/scenes/World.cs b/scenes/World.cs index 91d7402..8cb460d 100644 --- a/scenes/World.cs +++ b/scenes/World.cs @@ -218,8 +218,14 @@ public class World : Spatial var grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.15); if (grassAsset != null) chunk.Entities.AddChild(grassAsset); - var treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.05); - if (treeAsset != null) chunk.Entities.AddChild(treeAsset); + Tree treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.05) as Tree; + if (treeAsset != null) + { + chunk.Entities.AddChild(treeAsset); + treeAsset.Connect("EntityClicked", this, nameof(OnEntityClicked)); + } + + // TODO: MarkCellUnwalkable(cell); // else if (environmentRandom.NextDouble() < 0.01) // { diff --git a/systems/InteractionSystem.cs b/systems/InteractionSystem.cs index fa7aecd..01b2710 100644 --- a/systems/InteractionSystem.cs +++ b/systems/InteractionSystem.cs @@ -9,54 +9,54 @@ using NodePair = System.Tuple; public class InteractionSystem : Node { - private List _interactionPairs; + private List _activeInteractions; // Called when the node enters the scene tree for the first time. public override void _Ready() { - _interactionPairs = new List(); + _activeInteractions = new List(); } - public override void _Process(float delta) { base._Process(delta); List invalidInteractionPairs = new List(); + List endedInteractions = new List(); - foreach (Tuple pair in _interactionPairs) + foreach (InteractionComponent interaction in _activeInteractions) { - Node nodeA = pair.Item1; - Node nodeB = pair.Item2; + Spatial owningEntity = interaction.OwningEntity; + Spatial targetEntity = interaction.TargetEntity; - if (nodeA == null || nodeA.IsQueuedForDeletion() || nodeB == null || nodeB.IsQueuedForDeletion()) { - invalidInteractionPairs.Add(pair); + if (owningEntity == null || owningEntity.IsQueuedForDeletion() || targetEntity == null || targetEntity.IsQueuedForDeletion()) + { + interaction.hasStopped = true; } - if (nodeA == null || nodeA.IsQueuedForDeletion()) + if (interaction.hasStopped) { - IInteractionInterface interactableB = nodeB as IInteractionInterface; - if (interactableB != null) - { - interactableB.OnInteractionEnd(); - interactableB.InteractionComponent = null; - } - } - - if (nodeB == null || nodeB.IsQueuedForDeletion()) - { - IInteractionInterface interactableA = nodeA as IInteractionInterface; + IInteractionInterface interactableA = owningEntity as IInteractionInterface; if (interactableA != null) { interactableA.OnInteractionEnd(); interactableA.InteractionComponent = null; } + + IInteractionInterface interactableB = targetEntity as IInteractionInterface; + if (interactableB != null) + { + interactableB.OnInteractionEnd(); + interactableB.InteractionComponent = null; + } + + endedInteractions.Add(interaction); } } - - foreach (NodePair pair in invalidInteractionPairs) + + foreach (InteractionComponent interaction in endedInteractions) { - _interactionPairs.Remove(pair); + _activeInteractions.Remove(interaction); } } @@ -71,8 +71,7 @@ public class InteractionSystem : Node interactionComponent.EmitSignal("InteractionStart"); - NodePair pair = new NodePair(owningEntity, targetEntity); - _interactionPairs.Add(new NodePair(owningEntity, targetEntity)); + _activeInteractions.Add(interactionComponent); } private static void ConnectInteractionSignals(Entity entity, InteractionComponent interactionComponent)