Skip to content

Commit

Permalink
[fix, refactor, feat] Added DescantIfNodes, and fixed a bunch of smal…
Browse files Browse the repository at this point in the history
…l bugs that arose during their implementation
  • Loading branch information
Owmacohe committed May 22, 2024
1 parent 1c53fda commit cb7cc9e
Show file tree
Hide file tree
Showing 33 changed files with 649 additions and 185 deletions.
17 changes: 12 additions & 5 deletions Components/DescantComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public override string ToString()
}

/// <summary>
/// A DescantNode class attribute to indicate the maximum number of
/// A DescantComponent class attribute to indicate the maximum number of
/// Components of the attached type that can be added to a DescantNode
/// </summary>
public class MaxQuantityAttribute : Attribute
Expand All @@ -103,23 +103,30 @@ public class MaxQuantityAttribute : Attribute
}

/// <summary>
/// A DescantNode class attribute to indicate which type(s) of DescantNodes that the attached type can be added to
/// A DescantComponent class attribute to indicate which type(s) of
/// DescantNodes that the attached type can be added to
/// </summary>
public class NodeTypeAttribute : Attribute
{
public readonly DescantNodeType Type;

public NodeTypeAttribute(DescantNodeType type) => Type = type;
}

/// <summary>
/// A DescantComponent class attribute to indicate whether to show the
/// component in the DescantNodes' component dropdowns
/// </summary>
public class DontShowInEditorAttribute : Attribute { }

/// <summary>
/// A DescantNode property attribute to indicate that the attached parameter
/// A DescantComponent property attribute to indicate that the attached parameter
/// should be rendered inline with the name in the Descant Graph GUI
/// </summary>
public class InlineAttribute : Attribute { }

/// <summary>
/// A DescantNode property attribute to indicate to which group/row
/// A DescantComponent property attribute to indicate to which group/row
/// the attached parameter should be rendered in the Descant Graph GUI
/// </summary>
public class ParameterGroupAttribute : Attribute
Expand All @@ -130,7 +137,7 @@ public class ParameterGroupAttribute : Attribute
}

/// <summary>
/// A DescantNode property attribute to indicate that the attached
/// A DescantComponent property attribute to indicate that the attached
/// parameter should not be filtered in the Descant Graph GUI
/// </summary>
public class NoFilteringAttribute : Attribute { }
Expand Down
36 changes: 36 additions & 0 deletions Components/IfComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using Descant.Utilities;

namespace Descant.Components
{
/// <summary>
/// Custom Component used for displaying the DescantIfNode's check
/// (should never be added as an actual node component as it doesn't do anything)
/// </summary>
[Serializable, MaxQuantity(0), NodeType(DescantNodeType.If), DontShowInEditor]
public class IfComponent : DescantComponent
{
public DescantActor Actor;

[ParameterGroup("Variable to check")] public VariableType VariableType;
[ParameterGroup("Variable to check")] public string VariableName;

[ParameterGroup("Comparison to make")] public ComparisonType ComparisonType;
[ParameterGroup("Comparison to make")] public float Comparison;

/// <summary>
/// Method to make the comparison
/// </summary>
/// <returns>Whether the DescantIfNode's comparison passes or fails</returns>
public bool Check()
{
return (VariableType.Equals(VariableType.Statistic) && DescantComponentUtilities.CompareVariable(
Actor.Statistics[VariableName], Comparison, ComparisonType)) ||
(VariableType.Equals(VariableType.Topic) && Actor.Topics.Contains(VariableName)) ||
(VariableType.Equals(VariableType.Relationship) && DescantComponentUtilities.CompareVariable(
Actor.Relationships[VariableName], Comparison, ComparisonType)) ||
(VariableType.Equals(VariableType.DialogueAttempts) && DescantComponentUtilities.CompareVariable(
Actor.DialogueAttempts, Comparison, ComparisonType));
}
}
}
3 changes: 3 additions & 0 deletions Components/IfComponent.cs.meta

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

7 changes: 0 additions & 7 deletions Descant-1.2.0.unitypackage.meta

This file was deleted.

Binary file removed Descant-1.2.0.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions Descant-1.2.0.zip.meta

This file was deleted.

4 changes: 2 additions & 2 deletions Documentation/todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
## Hard

- Create a proper video tutorial
- Add `DescantIfNode`
- ~~Add `DescantIfNode`~~
- ~~Make script calling easier~~
- ~~Make autosave better (removed temporarily)~~
- ~~Create log system~~
Expand All @@ -40,7 +40,7 @@
- Allow ~~actors~~ and other data to be dragged and dropped
- Show thumbnails
- Indicate that scripts and methods are there when typing in the components
- Add If Node
- ~~Add If Node~~
- Make a video tutorial
- ~~Make `RandomizedNode` automatically last~~
- ~~Create a search functionality in the graph editor~~
Expand Down
30 changes: 23 additions & 7 deletions Editor/Data/DescantGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public class DescantGraph : ScriptableObject
/// A unique ID number that will be applied to the next-created ResponseNode
/// </summary>
[HideInInspector] public int ResponseNodeID;

/// <summary>
/// A unique ID number that will be applied to the next-created IfNode
/// </summary>
[HideInInspector] public int IfNodeID;

/// <summary>
/// A unique ID number that will be applied to the next-created EndNode
Expand All @@ -74,6 +79,11 @@ public class DescantGraph : ScriptableObject
/// The ResponseNodes in the graph
/// </summary>
public List<DescantResponseNodeData> ResponseNodes;

/// <summary>
/// The IfNodes in the graph
/// </summary>
public List<DescantIfNodeData> IfNodes;

/// <summary>
/// The StartNode in the graph
Expand Down Expand Up @@ -112,6 +122,7 @@ public DescantGraph()
TypewriterSpeed = 1;
ChoiceNodes = new List<DescantChoiceNodeData>();
ResponseNodes = new List<DescantResponseNodeData>();
IfNodes = new List<DescantIfNodeData>();

// We assume that we'll replace this later, but just in case we don't, we create a default StartNode
StartNode = new DescantStartNodeData(
Expand Down Expand Up @@ -162,6 +173,7 @@ public override int GetHashCode()
hashCode.Add(GroupID);
hashCode.Add(ChoiceNodes);
hashCode.Add(ResponseNodes);
hashCode.Add(IfNodes);
hashCode.Add(StartNode);
hashCode.Add(EndNodes);
hashCode.Add(Groups);
Expand Down Expand Up @@ -193,17 +205,20 @@ public override string ToString()

foreach (var j in ResponseNodes)
temp += "\n\t" + j;

temp += "\n\t" + StartNode;

foreach (var k in EndNodes)
foreach (var k in IfNodes)
temp += "\n\t" + k;

temp += "\n\t" + StartNode;

foreach (var l in Groups)
foreach (var l in EndNodes)
temp += "\n\t" + l;

foreach (var m in Connections)
foreach (var m in Groups)
temp += "\n\t" + m;

foreach (var n in Connections)
temp += "\n\t" + n;

return GetType() + " (" + name + " " + Autosave + " " + Advanced + " " + Typewriter + " " + TypewriterSpeed + " " +
ChoiceNodeID + " " + ResponseNodeID + " " + EndNodeID + " " + GroupID + ")" +
Expand All @@ -221,8 +236,9 @@ public void CleanUpConnections(int checksToPerform = 2)
for (int j = i + 1; j < Connections.Count; j++)
{
if (Connections[j].Equals(Connections[i]) ||
Connections[j].IllegalChoiceFrom() ||
Connections[j].ToItself())
Connections[j].IllegalChoiceOrIf() ||
Connections[j].ToItself() ||
Connections[j].To.Equals("null"))
Connections.RemoveAt(j);
}
}
Expand Down
87 changes: 87 additions & 0 deletions Editor/Data/DescantNodesData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,93 @@ public override string ToString()

#endregion

#region DescantIfNodeData

/// <summary>
/// Serializable class to hold the data for saving and loading Descant if nodes
/// </summary>
[Serializable]
public class DescantIfNodeData : DescantNodeData
{
/// <summary>
/// The component used to determine which path the dialogue will take after the node
/// </summary>
[SerializeReference] public IfComponent IfComponent;

/// <summary>
/// Parameterized constructor
/// </summary>
/// <param name="name">The custom name of the node</param>
/// <param name="type">The unique identifier ID for the node</param>
/// <param name="id">The type of this node</param>
/// <param name="position">The node's current position</param>
/// <param name="ifComponent">
/// The component used to determine which path the dialogue will take after the node
/// </param>
/// <param name="nodeComponents">The list of Components attached to the node</param>
/// <param name="comments">The comments associated with this node</param>
public DescantIfNodeData(
string name,
string type,
int id,
Vector2 position,
IfComponent ifComponent,
List<DescantComponent> nodeComponents,
string comments)
: base(name, type, id, position, nodeComponents, comments)
{
IfComponent = ifComponent;
}

/// <summary>
/// Overridden Equals method
/// </summary>
/// <param name="other">The object being compared against</param>
/// <returns>Whether the other object has the same data as this one</returns>
public override bool Equals(object other)
{
try
{
_ = (DescantIfNodeData) other;
}
catch
{
return false;
}

return Equals((DescantIfNodeData)other);
}

public override int GetHashCode()
{
return HashCode.Combine(base.GetHashCode(), IfComponent.GetHashCode());
}

#if UNITY_EDITOR
/// <summary>
/// Custom Equals method
/// </summary>
/// <param name="other">The data object being compared against</param>
/// <returns>Whether the other DescantIfNodeData has the same data as this one</returns>
public bool Equals(DescantIfNodeData other)
{
return
base.Equals(other) &&
IfComponent.Equals(other.IfComponent);
}
#endif

/// <summary>
/// Overridden ToString method
/// </summary>
public override string ToString()
{
return base.ToString() + " (" + IfComponent + ")";
}
}

#endregion

#region DescantStartNodeData and DescantEndNodeData

/// <summary>
Expand Down
33 changes: 16 additions & 17 deletions Editor/Data/DescantOtherData.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;

namespace Descant.Editor
{
Expand Down Expand Up @@ -139,6 +140,11 @@ public class DescantConnectionData
/// </summary>
public int FromID;

/// <summary>
/// The index of the port that this connection is coming from (base 1)
/// </summary>
public int FromIndex;

/// <summary>
/// The name of the node the connection is going to
/// </summary>
Expand All @@ -148,11 +154,6 @@ public class DescantConnectionData
/// The ID of the node the connection is going to
/// </summary>
public int ToID;

/// <summary>
/// The index of the port that this connection is coming from (base 1 for ChoiceNodes and ResponseNodes)
/// </summary>
public int ChoiceIndex;

/// <summary>
/// Parameterized constructor
Expand All @@ -161,31 +162,29 @@ public class DescantConnectionData
/// <param name="fromID">The ID of the node the connection is coming from</param>
/// <param name="to">The name of the node the connection is going to</param>
/// <param name="toID">The ID of the node the connection is going to</param>
/// <param name="choiceIndex">
/// <param name="fromIndex">
/// The index of the port that this connection is coming from (base 1 for ChoiceNodes and ResponseNodes)
/// </param>
public DescantConnectionData(string from, int fromID, string to, int toID, int choiceIndex = 0)
public DescantConnectionData(string from, int fromID, string to, int toID, int fromIndex = 0)
{
From = from;
FromID = fromID;
FromIndex = fromIndex;
To = to;
ToID = toID;
ChoiceIndex = choiceIndex;
}

/// <summary>
/// Checks to make sure that the connection isn't an illegal one coming from a Choice node's input port
/// Checks to make sure that the connection isn't an illegal one coming from a Choice or If node's input port
/// </summary>
/// <returns>Whether this connection is illegally coming from a Choice node's input port</returns>
public bool IllegalChoiceFrom()
public bool IllegalChoiceOrIf()
{
return From.Trim() == "Choice" && ChoiceIndex == 0;
return (From.Equals("Choice") || From.Equals("If")) && FromIndex == 0;
}

/// <summary>
/// Determines whether the connection points to itself
/// </summary>
/// <returns></returns>
public bool ToItself()
{
return From == To && FromID == ToID;
Expand All @@ -212,7 +211,7 @@ public override bool Equals(object other)

public override int GetHashCode()
{
return HashCode.Combine(From, FromID, To, ToID, ChoiceIndex);
return HashCode.Combine(From, FromID, FromIndex, To, ToID);
}

#if UNITY_EDITOR
Expand All @@ -225,8 +224,8 @@ public bool Equals(DescantConnectionData other)
{
return
From == other.From && FromID == other.FromID &&
To == other.To && ToID == other.ToID &&
ChoiceIndex == other.ChoiceIndex;
FromIndex == other.FromIndex &&
To == other.To && ToID == other.ToID;
}
#endif

Expand All @@ -235,7 +234,7 @@ public bool Equals(DescantConnectionData other)
/// </summary>
public override string ToString()
{
return GetType() + " (" + FromID + From + " " + ToID + To + " " + ChoiceIndex + ")";
return GetType() + " (" + FromID + From + " " + FromIndex + " " + ToID + To + ")";
}
}

Expand Down
Loading

0 comments on commit cb7cc9e

Please sign in to comment.