*
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public enum BehaviorStatus {
|
||||
Invalid,
|
||||
Success,
|
||||
Fail,
|
||||
Running
|
||||
};
|
||||
|
||||
public abstract class AiBehavior : MonoBehaviour {
|
||||
public string behaviorName;
|
||||
|
||||
protected virtual void OnBegin(AiBlackboard blackboard) { }
|
||||
protected virtual void OnEnd(AiBlackboard blackboard) { }
|
||||
protected abstract BehaviorStatus OnExecute(AiBlackboard blackboard);
|
||||
private BehaviorStatus _state = BehaviorStatus.Invalid;
|
||||
public BehaviorStatus State {
|
||||
get { return _state; }
|
||||
}
|
||||
|
||||
public bool Running {
|
||||
get { return _state == BehaviorStatus.Running; }
|
||||
}
|
||||
|
||||
public BehaviorStatus Tick(AiBlackboard blackboard) {
|
||||
if (_state != BehaviorStatus.Running) {
|
||||
OnBegin(blackboard);
|
||||
}
|
||||
|
||||
_state = OnExecute(blackboard);
|
||||
if (_state == BehaviorStatus.Running) {
|
||||
_state = BehaviorStatus.Success;
|
||||
}
|
||||
|
||||
if (_state != BehaviorStatus.Running) {
|
||||
OnEnd(blackboard);
|
||||
}
|
||||
|
||||
return _state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ac3489aca4eaca1438c296121ea6606f
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d08017e60048a74ca44adc931b5cea0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,47 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public static class BlackboardVariables {
|
||||
public static string LastEnemyPosition = "lastEnemyPosition";
|
||||
public static string LastSound = "lastSound";
|
||||
public static string LastSuspiciousTime = "lastSuspTime";
|
||||
public static string Enemy = "enemy";
|
||||
}
|
||||
|
||||
|
||||
public class AiBlackboard : MonoBehaviour {
|
||||
private Dictionary<string, object> _values = new Dictionary<string, object>();
|
||||
|
||||
public GameObject Obj {
|
||||
get {
|
||||
return (GameObject)_values["gameobject"];
|
||||
}
|
||||
}
|
||||
|
||||
public void SetValue(string key, object val) {
|
||||
_values[key] = val;
|
||||
}
|
||||
|
||||
/*public bool GetValue(string key, out object val) {
|
||||
if (_values.ContainsKey(key)) {
|
||||
val = _values[key];
|
||||
return true;
|
||||
}
|
||||
val = null;
|
||||
return false;
|
||||
}*/
|
||||
|
||||
public bool GetValue<T>(string key, out T obj) {
|
||||
if (_values.ContainsKey(key)) {
|
||||
obj = (T)_values[key];
|
||||
return true;
|
||||
}
|
||||
obj = default(T);
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool RemoveValue(string key) {
|
||||
return _values.Remove(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b64878cdefd0e5b409d5bb0cbd88f2b0
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiBlackboardDecorator : MonoBehaviour {
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fda99ed7265d9447a016da7ea552c0a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public abstract class AiComposite : AiBehavior {
|
||||
public AiBehavior[] children;
|
||||
|
||||
public void Add(AiBehavior behavior) {
|
||||
if (children == null || children.Length == 0) {
|
||||
children = new AiBehavior[1];
|
||||
children[0] = behavior;
|
||||
}
|
||||
else {
|
||||
Array.Resize(ref children, children.Length + 1);
|
||||
children[children.Length - 1] = behavior;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7ff17649812f35546970a12bf331b117
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public abstract class AiDecorator : AiBehavior {
|
||||
public AiBehavior child;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d5bf7cefe7649b64aa3bfba8a2240871
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,17 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiInverter : AiDecorator {
|
||||
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var result = child.Tick(blackboard);
|
||||
if (result == BehaviorStatus.Success) {
|
||||
return BehaviorStatus.Fail;
|
||||
} else if (result == BehaviorStatus.Fail) {
|
||||
return BehaviorStatus.Success;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 393988bfa4e415c498546e68a6e1ff46
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 50453c57d72f4764f91cac3e9da6775d, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public enum AiParallelPolicy {
|
||||
Any,
|
||||
All
|
||||
}
|
||||
|
||||
public class AiParallel : AiComposite {
|
||||
public AiParallelPolicy successPolicy = AiParallelPolicy.Any;
|
||||
public AiParallelPolicy failurePolicy = AiParallelPolicy.All;
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
int failCount = 0;
|
||||
int successCount = 0;
|
||||
foreach (var behavior in children) {
|
||||
//if (behavior.Running) {
|
||||
behavior.Tick(blackboard);
|
||||
//}
|
||||
|
||||
if (behavior.State == BehaviorStatus.Success) {
|
||||
++successCount;
|
||||
if (successPolicy == AiParallelPolicy.Any) {
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
else if (behavior.State == BehaviorStatus.Fail) {
|
||||
++failCount;
|
||||
if (failurePolicy == AiParallelPolicy.Any) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (failurePolicy == AiParallelPolicy.All && failCount == children.Length) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
if (successPolicy == AiParallelPolicy.All && successCount == children.Length) {
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e69523706cb2fa24d94ca0a2ba3cfc83
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: c9ec39ec3da62a64d941014c71e7f028, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiRepeatDecorator : AiDecorator {
|
||||
public int count = 1;
|
||||
private int _counter;
|
||||
public bool repeatForever;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
base.OnBegin(blackboard);
|
||||
_counter = 0;
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var state = child.Tick(blackboard);
|
||||
if (state == BehaviorStatus.Success) {
|
||||
if (repeatForever) {
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
else {
|
||||
_counter++;
|
||||
if (_counter == count) {
|
||||
return BehaviorStatus.Success;
|
||||
} else {
|
||||
return BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return state;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a02e2a8ad2091bd48b7acd45fff4b3c6
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6d5935485dc5991449b53931f8c2e312, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,11 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiReturnFailure : AiDecorator {
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return (childStatus == BehaviorStatus.Fail || childStatus == BehaviorStatus.Success)
|
||||
? BehaviorStatus.Fail
|
||||
: childStatus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d998bba05cd11ab4087e0ddf80721c8a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: b3ecdd4f2d60e5149ba02592143f517b, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,12 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiReturnSuccess : AiDecorator {
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return (childStatus == BehaviorStatus.Fail || childStatus == BehaviorStatus.Success)
|
||||
? BehaviorStatus.Success
|
||||
: childStatus;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94bfc6ef33c4fa74dbad429a88d561d9
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 72f177c027d95ac438d85c3a968f00e3, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiRoot : AiBehavior {
|
||||
public AiBehavior child;
|
||||
public AiBlackboard blackboard;
|
||||
private Health _health;
|
||||
|
||||
void Start() {
|
||||
blackboard.SetValue("gameobject", gameObject);
|
||||
_health = GetComponent<Health>();
|
||||
}
|
||||
|
||||
void FixedUpdate() {
|
||||
OnExecute(blackboard);
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (enabled && (_health == null || !_health.Dead)) {
|
||||
if (child == null) {
|
||||
Debug.LogWarning("AiRoot: child is null");
|
||||
return BehaviorStatus.Invalid;
|
||||
}
|
||||
return child.Tick(blackboard);
|
||||
}
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e18ffee70d70d64a8dd9750b2751d89
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: ded096a5301fa644e8891d1f6101b751, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,32 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiSelector : AiComposite {
|
||||
|
||||
private int _currentId = 0;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
_currentId = 0;
|
||||
}
|
||||
|
||||
protected override void OnEnd(AiBlackboard blackboard) {
|
||||
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (children == null || children.Length == 0) {
|
||||
Debug.LogError("Broken behavior! No children!");
|
||||
return BehaviorStatus.Invalid;
|
||||
}
|
||||
|
||||
while(_currentId < children.Length) {
|
||||
var current = children[_currentId];
|
||||
var result = current.Tick(blackboard);
|
||||
if (result != BehaviorStatus.Fail) {
|
||||
return result;
|
||||
}
|
||||
_currentId++;
|
||||
}
|
||||
return BehaviorStatus.Fail;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e5804a06425c3ad4a8d15d809813993a
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: bcabd2f1888c8f14ca23cb37f12fefb2, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiSequence : AiComposite {
|
||||
|
||||
private int _currentId = 0;
|
||||
|
||||
protected override void OnBegin(AiBlackboard blackboard) {
|
||||
_currentId = 0;
|
||||
}
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
if (children == null || children.Length == 0) {
|
||||
Debug.LogError("Broken behavior! No children!");
|
||||
return BehaviorStatus.Invalid;
|
||||
}
|
||||
|
||||
//Debug.Log("Sequence: " + behaviorName);
|
||||
|
||||
while(_currentId < children.Length) {
|
||||
var current = children[_currentId];
|
||||
var result = current.Tick(blackboard);
|
||||
if (result != BehaviorStatus.Success) {
|
||||
return result;
|
||||
}
|
||||
_currentId++;
|
||||
}
|
||||
|
||||
return BehaviorStatus.Success;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cbc50ac62b293274197389809d6f729c
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 8acfd1a7d74af514c9cfa3c24b4744a8, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiUntilFailure : AiDecorator {
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return childStatus == BehaviorStatus.Fail ? BehaviorStatus.Success : BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3d664e3d1f23ea647bbf57eb52207ad1
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: 6f3711f476005504c9f518040eab635a, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class AiUntilSuccess : AiDecorator {
|
||||
|
||||
protected override BehaviorStatus OnExecute(AiBlackboard blackboard) {
|
||||
var childStatus = child.Tick(blackboard);
|
||||
return childStatus == BehaviorStatus.Success ? BehaviorStatus.Success : BehaviorStatus.Running;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7156873ba7e0ded4282c752529bc401b
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {fileID: 2800000, guid: bbde1a7d7d1792a46937e629e84df4e0, type: 3}
|
||||
userData:
|
||||
assetBundleName:
|
||||
Reference in New Issue
Block a user