Created
March 29, 2017 20:57
-
-
Save hermesespinola/a933c6b45e887254d02ba03c7ea2b399 to your computer and use it in GitHub Desktop.
Unity Graph Agent
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// a.k.a. Enemy | |
public class PFTest : MonoBehaviour { | |
public Node start; | |
private Node end; | |
public float speed; | |
public float threshold; | |
public Node[] graph; | |
private List<Node> path; | |
private int currentNode; | |
State currentState; | |
private StateBehaviour currentBehaviour; | |
private Symbol balear; // start walking | |
private Symbol tocar; // walk faster | |
private Symbol relajar; // when not baleado, no camina | |
// Use this for initialization | |
void Start () { | |
balear = new Symbol("balear"); | |
tocar = new Symbol("tocar"); | |
relajar = new Symbol("relajar"); | |
// states | |
State relaxed = new State("relaxed", typeof(StateRelaxed)); // initial state | |
State tocado = new State("tocado", typeof(StateTocado); | |
State damaged = new State("damaged", typeof(StateDamaged)); | |
State dead = new State("dead", typeof(StateDead)); | |
// transition functions | |
damaged.AddNeighbor(relajar, relaxed); | |
damaged.AddNeighbor(tocar, tocado); | |
damaged.AddNeighbor(balear, dead); | |
tocado.AddNeighbor(relajar, relaxed); | |
tocado.AddNeighbor(balear, damaged); | |
tocado.AddNeighbor(tocar, damaged); | |
relaxed.AddNeighbor(relajar, relaxed); | |
relaxed.AddNeighbor(balear, damaged); | |
relaxed.AddNeighbor(tocar, tocado); | |
dead.AddNeighbor(relajar, dead); | |
dead.AddNeighbor(balear, /*re*/dead); | |
dead.AddNeighbor(tocar, dead); | |
currentState = relaxed; | |
currentBehaviour = (StateBehaviour) gameObject.AddComponent(currentState.Behaviour); | |
currentBehaviour.enemy = this; | |
FindPath (start); | |
} | |
IEnumerator CheckRelaxed(){ | |
while (true && currentState != dead) { | |
currentState.ApplySymbol(relajar); | |
if(temp != null && temp != currentState) { | |
currentState = temp; | |
// swap behaviour | |
Destroy(currentBehaviour); | |
currentBehaviour = (StateBehaviour) gameObject.AddComponent (currentState.Behaviour); | |
currentBehaviour.enemy = this; | |
} | |
yield return new WaitForSeconds(10); | |
} | |
} | |
void OnCollisionEnter(Collision col) { | |
State temp = null; | |
if (col.gameObject.tag = "Player") { | |
temp = currentState.ApplySymbol(tocar); | |
} else if (col.gameObject.tag = "Bullet") { | |
temp = currentState.ApplySymbol(balear); | |
Destroy(col.gameObject); | |
} | |
if (temp != null && temp != currentState) { | |
currentState = temp; | |
Destroy(currentBehaviour); | |
currentBehaviour = (StateBehaviour) gameObject.AddComponent(currentState.Behaviour); | |
currentBehaviour.enemy = this; | |
} | |
} | |
void FindPath(Node start) { | |
this.start = start; | |
end = graph [Random.Range (0, graph.Length - 1)]; | |
path = Pathfinding.Breadthwise (start, end); | |
for(int i = 0; i < path.Count; i++) { | |
print (path[i].transform.name); | |
} | |
currentNode = 0; | |
} | |
void FindPath(Node start, Node end) { | |
this.start = start; | |
this.end = end; | |
path = Pathfinding.Breadthwise (start, end); | |
currentNode = 0; | |
} | |
// Update is called once per frame | |
void Update () { | |
if (currentNode != path.Count) { | |
float distance = Vector3.Distance (transform.position, path [currentNode].transform.position); | |
transform.LookAt (path [currentNode].transform); | |
transform.Translate (transform.forward * Time.deltaTime * speed, Space.World); | |
if (distance < threshold) { | |
currentNode++; | |
} | |
} else { | |
FindPath (path [currentNode - 1]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment