Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BDeshiDev committed Feb 14, 2024
2 parents f606137 + a10e601 commit 9ecebfe
Show file tree
Hide file tree
Showing 10 changed files with 924 additions and 25 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,57 @@ public class DialogueChoicesNode : DialogueNodeBase
{
[Input]
public int Entry;

[Output]
[Output]
public int Choices;

private int _lastChoiceIndex = -1;

private List<DialogueChoiceNodeBase> _dialogueChoices;
public List<DialogueChoiceNodeBase> DialogueChoices=> _dialogueChoices;
protected List<DialogueChoiceNodeBase> _availableDialogueChoices;
public List<DialogueChoiceNodeBase> AvailableDialogueChoices => _availableDialogueChoices;

protected virtual void PrepareDialogueChoices()
{
GetAvailableChoices();
SetChoiceIndices();
}

protected virtual void GetAvailableChoices()
{
GetAllConnectedChoiceNodes();
RemoveUnavailableChoices();
}

void GetDialogueChoices()

protected virtual void GetAllConnectedChoiceNodes()
{
if (_dialogueChoices == null)
if (_availableDialogueChoices == null)
{
_dialogueChoices = new List<DialogueChoiceNodeBase>();
_availableDialogueChoices = new List<DialogueChoiceNodeBase>();
}
else
{
_dialogueChoices.Clear();
_availableDialogueChoices.Clear();
}

this.GetOutputNodesConnectedToPort("Choices", _dialogueChoices);
RemoveUnavailableChoices();
SetChoiceIndices();
this.GetOutputNodesConnectedToPort("Choices", _availableDialogueChoices);
}

private void SetChoiceIndices()
{
for (int i = 0; i < _dialogueChoices.Count; i++)
for (int i = 0; i < _availableDialogueChoices.Count; i++)
{
_dialogueChoices[i].DialogueChoiceIndex = i;
_availableDialogueChoices[i].DialogueChoiceIndex = i;
}
}

private void RemoveUnavailableChoices()
protected void RemoveUnavailableChoices()
{
for (int i = _dialogueChoices.Count-1; i >= 0; i--)
for (int i = _availableDialogueChoices.Count-1; i >= 0; i--)
{
var choice = _dialogueChoices[i];
var choice = _availableDialogueChoices[i];
if (!choice.CheckConditions())
{
_dialogueChoices.RemoveAt(i);
_availableDialogueChoices.RemoveAt(i);
}
}
}
Expand All @@ -65,29 +75,29 @@ public override void HandleDialogueAdvance()

public override DialogueNodeBase GetNextNode()
{
if (_lastChoiceIndex < 0 || _lastChoiceIndex >= _dialogueChoices.Count)
if (_lastChoiceIndex < 0 || _lastChoiceIndex >= _availableDialogueChoices.Count)
{
Debug.LogError($"INDEX {_lastChoiceIndex} OUT OF RANGE for dialogue choices {this}", this);
}
return _dialogueChoices[_lastChoiceIndex];
return _availableDialogueChoices[_lastChoiceIndex];
}

public override void HandleChoiceSelected(int choiceIndex)
{
//no value checks.
//we assume that the index is valid
_lastChoiceIndex = choiceIndex;
if (_lastChoiceIndex >= 0 && _lastChoiceIndex < _dialogueChoices.Count)
if (_lastChoiceIndex >= 0 && _lastChoiceIndex < _availableDialogueChoices.Count)
{
var pickedChoice = _dialogueChoices[_lastChoiceIndex];
var pickedChoice = _availableDialogueChoices[_lastChoiceIndex];
pickedChoice.HandleChoiceTaken();
}
}

public override async UniTask Play()
{
_lastChoiceIndex = -1;
GetDialogueChoices();
PrepareDialogueChoices();
Core.DialogueSystem.Instance.HandleDialogueChoiceStarted(this);

while (_lastChoiceIndex < 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Studio23.SS2.DialogueSystem.Utility;
using UnityEngine;

namespace Studio23.SS2.DialogueSystem.Data
{
/// <summary>
/// IF all normal choices are taken, shows another choice.
/// This is useful for dialogue trees that the player needs to fully exhaust
/// before being allowed to continue
/// </summary>
[CreateNodeMenu("Force Take All Choice Node"), NodeTint("#996600")]
public class ForceTakeAllChoiceNode:DialogueChoicesNode
{
[Output]
public int FinalChoice;

protected override void GetAvailableChoices()
{
GetAllConnectedChoiceNodes();
Debug.Log(_availableDialogueChoices.Count);
AddFinalChoiceIfAvailable();
Debug.Log(_availableDialogueChoices.Count);
RemoveUnavailableChoices();
Debug.Log("RemoveUnavailableChoices " + _availableDialogueChoices.Count);
}

private void AddFinalChoiceIfAvailable()
{
bool allNormalChoicesTaken = true;
foreach (var choice in AvailableDialogueChoices)
{
if (!choice.Taken)
{
allNormalChoicesTaken = false;
break;
}
}

if (allNormalChoicesTaken)
{
this.GetOutputNodesConnectedToPort("FinalChoice", _availableDialogueChoices);
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static void GetOutputNodesConnectedToPort<T>(this Node node, string field
{
//if the port is not an array, we can actually use connections.count
var port = node.GetOutputPort(fieldName);
if (port == null)
{
Debug.LogError($"{fieldName}no port" );
}
for (int i = 0; i < port.ConnectionCount; i++)
{
var connection = port.GetConnection(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private void HandleDialogueChoiceStarted(DialogueChoicesNode choicesNode)
private void SortChoices(DialogueChoicesNode choicesNode)
{
_sortedChoices.Clear();
_sortedChoices.AddRange(choicesNode.DialogueChoices);
_sortedChoices.AddRange(choicesNode.AvailableDialogueChoices);

_sortedChoices.Sort((choice1, choice2) =>
{
Expand Down
Loading

0 comments on commit 9ecebfe

Please sign in to comment.