Skip to content

Commit

Permalink
feat(nodes): nodes now trigger a NodeEnter event when used
Browse files Browse the repository at this point in the history
This can be used to record the IDs of run nodes to track analytics information, change the playback
of a node a 2nd time, and other useful features that were previously unavailable.
  • Loading branch information
ashblue committed Jan 1, 2020
1 parent 7e422d5 commit 55ed467
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ MonoBehaviour:
- {fileID: -1807495471838374980}
- {fileID: -7347677369531188231}
root: {fileID: 426936237664981933}
scrollPosition: {x: 49972.676, y: 49968}
scrollPosition: {x: 51095.676, y: 50006}
--- !u!114 &373158185418196577
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ private void Awake () {
speakerContainer.SetActive(false);
});

_ctrl.Events.NodeEnter.AddListener((node) => {
Debug.Log($"Node Enter: {node.GetType()} - {node.UniqueId}");
});

_ctrl.Play(dialogue);
}

Expand Down
7 changes: 7 additions & 0 deletions Assets/com.fluid.dialogue/Runtime/DialogueController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CleverCrow.Fluid.Databases;
using CleverCrow.Fluid.Dialogues.Choices;
using CleverCrow.Fluid.Dialogues.Graphs;
using CleverCrow.Fluid.Dialogues.Nodes;

namespace CleverCrow.Fluid.Dialogues {
public interface IDialogueController {
Expand All @@ -28,6 +29,7 @@ public void Play (IDialoguePlayback playback) {

playback.Events.Speak.AddListener(TriggerSpeak);
playback.Events.Choice.AddListener(TriggerChoice);
playback.Events.NodeEnter.AddListener(TriggerEnterNode);
playback.Events.Begin.AddListener(TriggerBegin);
playback.Events.End.AddListener(TriggerEnd);

Expand All @@ -52,6 +54,7 @@ public void PlayChild (IDialoguePlayback playback) {
});
playback.Events.Speak.AddListener(TriggerSpeak);
playback.Events.Choice.AddListener(TriggerChoice);
playback.Events.NodeEnter.AddListener(TriggerEnterNode);

_activeDialogue.Push(playback);
playback.Play();
Expand Down Expand Up @@ -79,6 +82,10 @@ private void TriggerChoice (IActor actor, string text, List<IChoice> choices) {
Events.Choice.Invoke(actor, text, choices);
}

private void TriggerEnterNode (INode node) {
Events.NodeEnter.Invoke(node);
}

public void Next () {
ActiveDialogue?.Next();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using CleverCrow.Fluid.Dialogues.Choices;
using CleverCrow.Fluid.Dialogues.Nodes;
using CleverCrow.Fluid.Utilities.UnityEvents;

namespace CleverCrow.Fluid.Dialogues {
Expand All @@ -8,5 +9,6 @@ public class DialogueEvents : IDialogueEvents {
public IUnityEvent End { get; } = new UnityEventPlus();
public IUnityEvent<IActor, string> Speak { get; } = new UnityEventPlus<IActor, string>();
public IUnityEvent<IActor, string, List<IChoice>> Choice { get; } = new UnityEventPlus<IActor, string, List<IChoice>>();
public IUnityEvent<INode> NodeEnter { get; } = new UnityEventPlus<INode>();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using CleverCrow.Fluid.Dialogues.Choices;
using CleverCrow.Fluid.Dialogues.Nodes;
using CleverCrow.Fluid.Utilities.UnityEvents;

namespace CleverCrow.Fluid.Dialogues {
Expand All @@ -8,5 +9,6 @@ public interface IDialogueEvents {
IUnityEvent End { get; }
IUnityEvent<IActor, string> Speak { get; }
IUnityEvent<IActor, string, List<IChoice>> Choice { get; }
IUnityEvent<INode> NodeEnter { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class NodeChoiceHubData : NodeDataChoiceBase {
public override INode GetRuntime (IGraph graphRuntime, IDialogueController dialogue) {
var runtimeChoices = choices.Select(c => c.GetRuntime(graphRuntime, dialogue)).ToList();
return new NodeChoiceHub(
null,
UniqueId,
runtimeChoices,
conditions.Select(c => c.GetRuntime(graphRuntime, dialogue)).ToList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ public NodeDialogue (
_choices = choices;
}

private List<IChoice> GetValidChoices () {
private List<IChoice> GetValidChoices (IDialoguePlayback playback) {
var child = Next();
if (_choices.Count == 0 && child?.HubChoices != null && child.HubChoices.Count > 0) {
playback.Events.NodeEnter.Invoke(child);
return child.HubChoices;
}

return _choices.Where(c => c.IsValid).ToList();
}

public override void Play (IDialoguePlayback playback) {
_emittedChoices = GetValidChoices();
protected override void OnPlay (IDialoguePlayback playback) {
_emittedChoices = GetValidChoices(playback);
if (_emittedChoices.Count > 0) {
playback.Events.Choice.Invoke(_actor, _dialogue, _emittedChoices);
return;
Expand Down
7 changes: 6 additions & 1 deletion Assets/com.fluid.dialogue/Runtime/Nodes/NodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public INode Next () {
return Children.Find(n => n.IsValid);
}

public virtual void Play (IDialoguePlayback playback) {
public void Play (IDialoguePlayback playback) {
playback.Events.NodeEnter.Invoke(this);
OnPlay(playback);
}

protected virtual void OnPlay (IDialoguePlayback playback) {
playback.Next();
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/com.fluid.dialogue/Runtime/Nodes/NodeDataBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IConnectionChildCollection {
}

public abstract class NodeDataBase : ScriptableObject, INodeData {
[HideInInspector]
// [HideInInspector]
[SerializeField]
private string _uniqueId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public NodePlayGraph (
_graph = graph;
}

public override void Play (IDialoguePlayback playback) {
protected override void OnPlay (IDialoguePlayback playback) {
playback.ParentCtrl.PlayChild(_graph);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ public void It_should_trigger_a_Begin_event () {
[Test]
public void It_should_trigger_a_speak_event_with_the_root_child_dialogue () {
var node = A.Node.Build();
node.Play(Arg.Do<IDialoguePlayback>(p => p.Events.Speak.Invoke(null, null)));

_graph = A.Graph
.WithNextResult(node)
.Build();
_playback = new DialoguePlayback(_graph, null, _events);

_playback.Play();

node.Received(1).Play(_playback);
_playback.Events.Speak.ReceivedWithAnyArgs(1).Invoke(null, null);
}

[Test]
Expand Down
28 changes: 28 additions & 0 deletions Assets/com.fluid.dialogue/Tests/Editor/Nodes/NodeDialogueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ public void It_should_trigger_a_speak_event () {
}
}

public class NodeEnterEvents : NodeDialogueTest {
[Test]
public void It_should_trigger_a_NodeEnter_event () {
var node = CreateNodeDialogue();
var playback = Substitute.For<IDialoguePlayback>();

node.Play(playback);

playback.Events.NodeEnter.Received(1).Invoke(node);
}
}

public class ChoiceEvents {
public class InternalChoices : NodeDialogueTest {
[Test]
Expand Down Expand Up @@ -156,6 +168,22 @@ public void It_should_use_choices_from_next_child_if_its_a_choice_hub () {
playback.Events.Choice.Received(1).Invoke(_actor, DIALOGUE, choiceHub.HubChoices);
}

[Test]
public void It_should_trigger_enter_node_on_a_used_choice_hub () {
var playback = Substitute.For<IDialoguePlayback>();
var choice = A.Choice.Build();
var choiceHub = A.Node
.WithHubChoice(choice)
.Build();
var choiceHubData = A.NodeData.WithNode(choiceHub).Build();
_children.Add(choiceHubData);

var node = CreateNodeDialogue();
node.Play(playback);

playback.Events.NodeEnter.Received(1).Invoke(choiceHub);
}

[Test]
public void It_should_not_use_a_choice_hub_if_choices_are_present () {
var playback = Substitute.For<IDialoguePlayback>();
Expand Down

0 comments on commit 55ed467

Please sign in to comment.