Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
Uploading the source code
  • Loading branch information
lourenco-pedro committed Apr 6, 2021
1 parent 7f087f3 commit 15d47c5
Show file tree
Hide file tree
Showing 52 changed files with 2,250 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Components.meta

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

8 changes: 8 additions & 0 deletions Components/Dependencies.meta

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

15 changes: 15 additions & 0 deletions Components/Dependencies/PTreeActor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PTreeBehaviour
{
[System.Serializable]
public class PTreeActor
{

public void SetNPC()
{
}
}
}
11 changes: 11 additions & 0 deletions Components/Dependencies/PTreeActor.cs.meta

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

12 changes: 12 additions & 0 deletions Components/Dependencies/PTreeTargeting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace PTreeBehaviour
{
[System.Serializable]
public class PTreeTargeting
{
}
}
11 changes: 11 additions & 0 deletions Components/Dependencies/PTreeTargeting.cs.meta

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

161 changes: 161 additions & 0 deletions Components/PTreeBehaviourComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PTreeBehaviour
{
public class PTreeBehaviourComponent : MonoBehaviour
{
public PTreeBehaviour Behaviour => _behaviour;
public bool Enabled
{
get => _enabled;
set => _enabled = value;
}

public BehaviourNode CurrentNode
{
get
{
if (string.IsNullOrEmpty(_runningNode))
{
return null;
}

if (IsRoot(_runningNode))
{
return Behaviour.RootNode;
}

return Behaviour.GetBaseNode(_runningNode);
}
}

public Dictionary<string, RuntimeNodeState> RuntimeNodes => _runtimeNodes;

public PTreeActor Actor;
public PTreeTargeting Target;

[Space]

[SerializeField]
private PTreeBehaviour _behaviour;
[SerializeField]
private bool _enabled = true;

private string _runningNode;
private string _toNextNode;
private Dictionary<string, RuntimeNodeState> _runtimeNodes;

public void Run()
{
if (!_enabled)
{
return;
}

if (string.IsNullOrEmpty(_toNextNode))
{
_toNextNode = Behaviour.RootNode.NodeID;
SetAllNodeStates(RuntimeNodeState.NONE);
}

while (!string.IsNullOrEmpty(_toNextNode))
{
if (IsCurrentRuntimeNodeFinished())
{
_runningNode = _toNextNode;
_toNextNode = string.Empty;
}

switch (CurrentNode.BehaviourNodeType)
{
case BehaviourNodeType.Root:
{
RootBehaviourNode rootNode = CurrentNode as RootBehaviourNode;
_toNextNode = rootNode.OutputId;
SetCurrentRuntimeNodeState(RuntimeNodeState.FINISHED);
}
break;
case BehaviourNodeType.Action:
{
ActionBehaviourNode actionNode = CurrentNode as ActionBehaviourNode;
actionNode.Execute(this);
SetCurrentRuntimeNodeState(RuntimeNodeState.FINISHED);
_toNextNode = string.Empty;
}
break;
case BehaviourNodeType.Condition:
{
ConditionBehaviourNode conditionNode = CurrentNode as ConditionBehaviourNode;
_toNextNode = ((bool)conditionNode.Execute(this)) ? conditionNode.TrueNode : conditionNode.FalseNode;
SetCurrentRuntimeNodeState(RuntimeNodeState.FINISHED);
}
break;
case BehaviourNodeType.Sequence:
{
SequenceBehaviourNode sequenceBehaviourNode = CurrentNode as SequenceBehaviourNode;
foreach (var nodeId in sequenceBehaviourNode.Outputs)
{
BehaviourNode node = Behaviour.GetBaseNode(nodeId) as ActionBehaviourNode;
node.Execute(this);
}
_toNextNode = string.Empty;
}
break;
}
}
}

public void SetBehaviour(PTreeBehaviour behaviour)
{
_behaviour = behaviour;

_runtimeNodes = new Dictionary<string, RuntimeNodeState>();

_runtimeNodes.Add(behaviour.RootNode.NodeID, RuntimeNodeState.NONE);

foreach (var node in behaviour.AllNodes)
{
_runtimeNodes.Add(node.NodeID, RuntimeNodeState.NONE);
}

_runningNode = Behaviour.RootNode.NodeID;
}

public void SetAllNodeStates(RuntimeNodeState state, bool onlyFinishedNodes = true)
{

var keys = new string[_runtimeNodes.Keys.Count];
_runtimeNodes.Keys.CopyTo(keys, 0);

foreach (var id in keys)
{
if ((onlyFinishedNodes && _runtimeNodes[id] == RuntimeNodeState.RUNNING) || !onlyFinishedNodes)
{
_runtimeNodes[id] = state;
}
}
}

private bool IsRoot(string nodeId)
{
return Behaviour.RootNode.NodeID == nodeId;
}

private bool IsCurrentRuntimeNodeFinished()
{
return _runtimeNodes[_runningNode] == RuntimeNodeState.FINISHED;
}

private bool IsCurrentRuntimeNodeState(RuntimeNodeState state)
{
return _runtimeNodes[_runningNode] == state;
}

private void SetCurrentRuntimeNodeState(RuntimeNodeState state)
{
_runtimeNodes[_runningNode] = state;
}
}
}
11 changes: 11 additions & 0 deletions Components/PTreeBehaviourComponent.cs.meta

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

8 changes: 8 additions & 0 deletions Editor.meta

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

8 changes: 8 additions & 0 deletions Editor/NodeGUI.meta

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

Loading

0 comments on commit 15d47c5

Please sign in to comment.