Interaction now working with streamed entities. Yay!

WorldChunkRefactoring
Martin Felis 2023-11-10 16:26:16 +01:00
parent b968f9b3b2
commit 187dac2d85
5 changed files with 53 additions and 56 deletions

View File

@ -1,13 +1,12 @@
using Godot;
using System;
using System.Linq; using System.Linq;
using Godot;
using GodotComponentTest.components; using GodotComponentTest.components;
using GodotComponentTest.entities; using GodotComponentTest.entities;
public class Chest : Entity, IInteractionInterface public class Chest : Entity, IInteractionInterface
{ {
// resources // resources
private PackedScene _goldBarScene = GD.Load<PackedScene>("res://entities/GoldBar.tscn"); private readonly PackedScene _goldBarScene = GD.Load<PackedScene>("res://entities/GoldBar.tscn");
public enum LidState public enum LidState
{ {
@ -16,23 +15,18 @@ public class Chest : Entity, IInteractionInterface
} }
public LidState State = LidState.Closed; public LidState State = LidState.Closed;
public bool IsMouseOver = false; public bool IsMouseOver;
private MeshInstance _mesh; private MeshInstance _mesh;
private AnimationPlayer _animationPlayer; private AnimationPlayer _animationPlayer;
private InteractionComponent _interactionComponent; public InteractionComponent InteractionComponent { get; set; }
public InteractionComponent InteractionComponent
{
get => _interactionComponent;
set => _interactionComponent = value;
}
[Signal]
delegate void EntityClicked(Entity entity);
[Signal] [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. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
@ -114,9 +108,9 @@ public class Chest : Entity, IInteractionInterface
GetParent().AddChild(bar); GetParent().AddChild(bar);
} }
if (_interactionComponent != null) if (InteractionComponent != null)
{ {
_interactionComponent.EmitSignal("InteractionEnd"); InteractionComponent.EndInteraction();
} }
} }
} }

View File

@ -1,6 +1,6 @@
using Godot;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using Godot;
using GodotComponentTest.components; using GodotComponentTest.components;
using GodotComponentTest.entities; using GodotComponentTest.entities;
@ -8,22 +8,16 @@ public class Tree : StaticBody, IInteractionInterface
{ {
[Export] public float ChopDuration = 2; [Export] public float ChopDuration = 2;
public bool IsMouseOver; public bool IsMouseOver;
public InteractionComponent InteractionComponent { get; set; }
private MeshInstance _geometry; private MeshInstance _geometry;
private AnimationPlayer _animationPlayer; private AnimationPlayer _animationPlayer;
private float _health = 100; private float _health = 100;
private bool _isBeingChopped; private bool _isBeingChopped;
private InteractionComponent _interactionComponent;
public InteractionComponent InteractionComponent
{
get => _interactionComponent;
set => _interactionComponent = value;
}
[Signal] [Signal]
delegate void EntityClicked(Entity entity); public delegate void EntityClicked(Entity entity);
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
@ -51,6 +45,8 @@ public class Tree : StaticBody, IInteractionInterface
if (_health == 0) if (_health == 0)
{ {
InteractionComponent.EndInteraction();
InteractionComponent.TargetEntity = null;
QueueFree(); QueueFree();
} }
} }

View File

@ -114,6 +114,8 @@ public class Game : Spatial
if (node.HasSignal("EntityClicked")) if (node.HasSignal("EntityClicked"))
node.Connect("EntityClicked", this, nameof(OnEntityClicked)); node.Connect("EntityClicked", this, nameof(OnEntityClicked));
_world.Connect("EntityClicked", this, nameof(OnEntityClicked));
// perform dependency injection // perform dependency injection
//_streamContainer.SetWorld(_tileWorld);Clicked //_streamContainer.SetWorld(_tileWorld);Clicked
var worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo"); var worldInfoComponent = _player.GetNode<WorldInfoComponent>("WorldInfo");

View File

@ -218,8 +218,14 @@ public class World : Spatial
var grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.15); var grassAsset = SelectAsset(offsetCoord, _grassAssets, environmentRandom, 0.15);
if (grassAsset != null) chunk.Entities.AddChild(grassAsset); if (grassAsset != null) chunk.Entities.AddChild(grassAsset);
var treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.05); Tree treeAsset = SelectAsset(offsetCoord, _treeAssets, environmentRandom, 0.05) as Tree;
if (treeAsset != null) chunk.Entities.AddChild(treeAsset); if (treeAsset != null)
{
chunk.Entities.AddChild(treeAsset);
treeAsset.Connect("EntityClicked", this, nameof(OnEntityClicked));
}
// TODO: MarkCellUnwalkable(cell); // TODO: MarkCellUnwalkable(cell);
// else if (environmentRandom.NextDouble() < 0.01) // else if (environmentRandom.NextDouble() < 0.01)
// { // {

View File

@ -9,54 +9,54 @@ using NodePair = System.Tuple<Godot.Node, Godot.Node>;
public class InteractionSystem : Node public class InteractionSystem : Node
{ {
private List<NodePair> _interactionPairs; private List<InteractionComponent> _activeInteractions;
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()
{ {
_interactionPairs = new List<NodePair>(); _activeInteractions = new List<InteractionComponent>();
} }
public override void _Process(float delta) public override void _Process(float delta)
{ {
base._Process(delta); base._Process(delta);
List<NodePair> invalidInteractionPairs = new List<NodePair>(); List<NodePair> invalidInteractionPairs = new List<NodePair>();
List<InteractionComponent> endedInteractions = new List<InteractionComponent>();
foreach (Tuple<Node, Node> pair in _interactionPairs) foreach (InteractionComponent interaction in _activeInteractions)
{ {
Node nodeA = pair.Item1; Spatial owningEntity = interaction.OwningEntity;
Node nodeB = pair.Item2; Spatial targetEntity = interaction.TargetEntity;
if (nodeA == null || nodeA.IsQueuedForDeletion() || nodeB == null || nodeB.IsQueuedForDeletion()) { if (owningEntity == null || owningEntity.IsQueuedForDeletion() || targetEntity == null || targetEntity.IsQueuedForDeletion())
invalidInteractionPairs.Add(pair); {
interaction.hasStopped = true;
} }
if (nodeA == null || nodeA.IsQueuedForDeletion()) if (interaction.hasStopped)
{ {
IInteractionInterface interactableB = nodeB as IInteractionInterface; IInteractionInterface interactableA = owningEntity as IInteractionInterface;
if (interactableB != null)
{
interactableB.OnInteractionEnd();
interactableB.InteractionComponent = null;
}
}
if (nodeB == null || nodeB.IsQueuedForDeletion())
{
IInteractionInterface interactableA = nodeA as IInteractionInterface;
if (interactableA != null) if (interactableA != null)
{ {
interactableA.OnInteractionEnd(); interactableA.OnInteractionEnd();
interactableA.InteractionComponent = null; 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"); interactionComponent.EmitSignal("InteractionStart");
NodePair pair = new NodePair(owningEntity, targetEntity); _activeInteractions.Add(interactionComponent);
_interactionPairs.Add(new NodePair(owningEntity, targetEntity));
} }
private static void ConnectInteractionSignals(Entity entity, InteractionComponent interactionComponent) private static void ConnectInteractionSignals(Entity entity, InteractionComponent interactionComponent)