-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Split from my own Core repository.
- Loading branch information
0 parents
commit 74bcabe
Showing
16 changed files
with
874 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.meta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using BT; | ||
|
||
public class IntervalPrecondition : BTPrecondition { | ||
private float _interval; | ||
private float _minInterval; | ||
private float _maxInterval; | ||
private float _time; | ||
|
||
public IntervalPrecondition (float interval) : this (interval, interval) { | ||
|
||
} | ||
|
||
public IntervalPrecondition (float minInterval, float maxInterval) { | ||
_minInterval = minInterval; | ||
_maxInterval = maxInterval; | ||
_time = Random.Range(minInterval, maxInterval); | ||
} | ||
|
||
public override bool Check () { | ||
_time -= Time.deltaTime; | ||
if (_time <= 0) { | ||
_time = Random.Range(_minInterval, _maxInterval); | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using BT; | ||
|
||
public class ResetData<T> : BTAction { | ||
private string _dataToReset; | ||
private T _defaultData; | ||
private string _targetData; | ||
private bool _shouldClear; | ||
|
||
|
||
public ResetData (string dataToReset, T defaultData, bool shouldClear = false) : base (null) { | ||
_dataToReset = dataToReset; | ||
_defaultData = defaultData; | ||
_targetData = Jargon.Invalid; | ||
_shouldClear = shouldClear; | ||
} | ||
|
||
public ResetData (string dataToReset, string targetData, bool shouldClear = false) : base (null) { | ||
_dataToReset = dataToReset; | ||
_targetData = targetData; | ||
_shouldClear = shouldClear; | ||
} | ||
|
||
protected override BTResult Execute () { | ||
if (_targetData.Equals(Jargon.Invalid)) { | ||
database.SetData<T>(_dataToReset, _defaultData); | ||
} | ||
else { | ||
T data = database.GetData<T>(_targetData); | ||
database.SetData<T>(_dataToReset, data); | ||
} | ||
|
||
return BTResult.Ended; | ||
} | ||
|
||
public override void Clear () { | ||
base.Clear (); | ||
if (_shouldClear) { | ||
Execute(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using BT; | ||
|
||
// How to use: | ||
// 1. Initiate values in the database for the children to use. | ||
// 2. Initiate BT root | ||
// 3. Some actions & preconditions that will be used later | ||
// 4. Add children nodes | ||
// 5. Activate the root, including the children nodes' initialization | ||
|
||
public class BTTree : MonoBehaviour { | ||
protected BTNode root; | ||
[HideInInspector] | ||
public Database database; | ||
|
||
[HideInInspector] | ||
public bool isRunning = true; | ||
|
||
protected int _resetDataId; | ||
|
||
void Awake () { | ||
Init(); | ||
|
||
root.Activate(database); | ||
} | ||
void Update () { | ||
if (!isRunning) return; | ||
|
||
if (database.GetData<bool>(Jargon.ShouldReset)) { | ||
Reset(); | ||
database.SetData<bool>(Jargon.ShouldReset, false); | ||
} | ||
if (root.Evaluate()) { | ||
root.Tick(); | ||
} | ||
} | ||
|
||
void OnDestroy () { | ||
if (root != null) { | ||
root.Clear(); | ||
} | ||
} | ||
|
||
// Need to be called at the initialization code in the children. | ||
protected virtual void Init () { | ||
database = GetComponent<Database>(); | ||
if (database == null) { | ||
database = gameObject.AddComponent<Database>(); | ||
} | ||
|
||
_resetDataId = database.GetDataId(Jargon.ShouldReset); | ||
database.SetData<bool>(_resetDataId, false); | ||
} | ||
|
||
protected void Reset () { | ||
root.Clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace BT { | ||
public class BTAction : BTNode { | ||
private BTActionStatus status = BTActionStatus.Ready; | ||
|
||
|
||
public BTAction (BTPrecondition precondition = null) : base (precondition) {} | ||
|
||
// protected virtual void Enter () {Debug.Log("Enter "+name);} | ||
// protected virtual void Exit () {Debug.Log("Exit "+name);} | ||
protected virtual void Enter () {} | ||
protected virtual void Exit () {} | ||
protected virtual BTResult Execute () { | ||
return BTResult.Running; | ||
} | ||
|
||
public override void Clear () { | ||
if (status != BTActionStatus.Ready) { // not cleared yet | ||
Exit(); | ||
status = BTActionStatus.Ready; | ||
} | ||
} | ||
|
||
public override BTResult Tick () { | ||
BTResult result = BTResult.Ended; | ||
if (status == BTActionStatus.Ready) { | ||
Enter(); | ||
status = BTActionStatus.Running; | ||
} | ||
if (status == BTActionStatus.Running) { // not using else so that the status changes reflect instantly | ||
result = Execute(); | ||
if (result != BTResult.Running) { | ||
Exit(); | ||
status = BTActionStatus.Ready; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
|
||
private enum BTActionStatus { | ||
Ready = 1, | ||
Running = 2, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace BT { | ||
|
||
public abstract class BTNode { | ||
public List<BTNode> children; | ||
public BTPrecondition precondition; | ||
public Database database; | ||
|
||
private BTNode _parent; | ||
public BTNode parent { | ||
get{return _parent;} | ||
set{_parent = value;} | ||
} | ||
|
||
public float interval = 0; | ||
private float _lastTimeEvaluated = 0; | ||
|
||
private bool _activated; | ||
public bool activated {get{return _activated;}} | ||
|
||
|
||
public BTNode () : this (null) {} | ||
|
||
public BTNode (BTPrecondition precondition) { | ||
this.precondition = precondition; | ||
} | ||
|
||
// to use with BTNode's constructor to provide initialization delay | ||
public virtual void Init () {} | ||
public bool Evaluate () { | ||
bool timerOk = (Time.time - _lastTimeEvaluated) > interval; | ||
if (timerOk) { | ||
_lastTimeEvaluated = Time.time; | ||
} | ||
return timerOk && (precondition == null || precondition.Check()) && DoEvaluate(); | ||
} | ||
public virtual BTResult Tick () {return BTResult.Ended;} | ||
public virtual void Clear () {} | ||
|
||
protected virtual bool DoEvaluate () {return true;} | ||
|
||
public virtual void AddChild (BTNode aNode) { | ||
if (children == null) { | ||
children = new List<BTNode>(); | ||
} | ||
if (aNode != null) { | ||
children.Add(aNode); | ||
aNode.parent = this; | ||
} | ||
} | ||
|
||
public virtual void Activate (Database database) { | ||
if (_activated) return ; | ||
|
||
this.database = database; | ||
Init(); | ||
|
||
if (precondition != null) { | ||
precondition.Activate(database); | ||
} | ||
if (children != null) { | ||
foreach (BTNode child in children) { | ||
child.Activate(database); | ||
} | ||
} | ||
|
||
_activated = true; | ||
} | ||
} | ||
|
||
|
||
public enum BTResult { | ||
Ended = 1, | ||
Running = 2, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using BT; | ||
|
||
namespace BT { | ||
|
||
public class BTParallel : BTNode { | ||
protected List<BTResult> results; | ||
protected ParallelFunction func; | ||
|
||
public BTParallel (ParallelFunction func) : this (func, null) {} | ||
|
||
public BTParallel (ParallelFunction func, BTPrecondition precondition) : base (precondition) { | ||
results = new List<BTResult>(); | ||
this.func = func; | ||
} | ||
|
||
protected override bool DoEvaluate () { | ||
foreach (BTNode child in children) { | ||
if (!child.Evaluate()) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public override BTResult Tick () { | ||
int endingResultCount = 0; | ||
|
||
for (int i=0; i<children.Count; i++) { | ||
|
||
if (func == ParallelFunction.And) { | ||
if (results[i] == BTResult.Running) { | ||
results[i] = children[i].Tick(); | ||
} | ||
if (results[i] != BTResult.Running) { | ||
endingResultCount++; | ||
} | ||
} | ||
else { | ||
if (results[i] == BTResult.Running) { | ||
results[i] = children[i].Tick(); | ||
} | ||
if (results[i] != BTResult.Running) { | ||
BTResult result = results[i]; | ||
ResetResults(); | ||
return result; | ||
} | ||
} | ||
} | ||
if (endingResultCount == children.Count) { // only apply to AND func | ||
ResetResults(); | ||
return BTResult.Ended; | ||
} | ||
return BTResult.Running; | ||
} | ||
|
||
public override void Clear () { | ||
ResetResults(); | ||
|
||
foreach (BTNode child in children) { | ||
child.Clear(); | ||
} | ||
} | ||
|
||
public override void AddChild (BTNode aNode) { | ||
base.AddChild (aNode); | ||
results.Add(BTResult.Running); | ||
} | ||
|
||
private void ResetResults () { | ||
for (int i=0; i<results.Count; i++) { | ||
results[i] = BTResult.Running; | ||
} | ||
} | ||
|
||
public enum ParallelFunction { | ||
And = 1, // returns Ended when all results are not running | ||
Or = 2, // returns Ended when any result is not running | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.