ThetaStar.cs 3.3 KB
Newer Older
Zac Olsen's avatar
Zac Olsen committed
1 2 3 4
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

Zac Olsen's avatar
Zac Olsen committed
5
public class ThetaStar : MonoBehaviour {
Zac Olsen's avatar
Zac Olsen committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

	public int rows = 6;
	public int cols = 6;

	public GameObject tile;
	public GameObject enemy;
	public static Tile[,] map;

	public Tile startPos;
	public Tile endPos;

	public Heap open;

	void Start () {

		map = new Tile[rows, cols];

		float startx = -(float)rows / 2f * tile.transform.lossyScale.x;
		float starty = (float)cols / 2f * tile.transform.lossyScale.y;

		for (int y = 0; y < cols; y++) {
			for (int x = 0; x < rows; x++) {

				map[x, y] = ((GameObject) Instantiate (tile, new Vector2(startx + x * tile.GetComponent<BoxCollider2D>().size.x,
Zac Olsen's avatar
Zac Olsen committed
30
					starty - y * tile.GetComponent<BoxCollider2D>().size.y), Quaternion.identity)).GetComponent<Tile> ();
Zac Olsen's avatar
Zac Olsen committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

				map [x, y].x = x;
				map [x, y].y = y;
			}
		}

		open = new Heap (rows * cols);

		enemy = (GameObject)(Instantiate (enemy, map[0, 0].transform.position, Quaternion.identity));
	}

	void FixedUpdate () {

		if (map[rows - 1, cols - 1].prev != null) {

			Tile current = map [rows - 1, cols - 1];
			while (current.prev != null) {
				Debug.DrawLine (current.transform.position, current.prev.transform.position, Color.red);
				current = current.prev;
			}
		}
	}

	public void BeginPath () {
		Path (map[0, 0], map[rows - 1, cols - 1]);
	}

	void Path (Tile start, Tile finish) {

		start.goal = 0;
Zac Olsen's avatar
Zac Olsen committed
61

Zac Olsen's avatar
Zac Olsen committed
62
		start.fitness = EstimateHeuristic (start, finish);
Zac Olsen's avatar
Zac Olsen committed
63
		open.Add (start);
Zac Olsen's avatar
Zac Olsen committed
64

Zac Olsen's avatar
Zac Olsen committed
65
		while (open.Count > 0) {
Zac Olsen's avatar
Zac Olsen committed
66 67

			Tile current = open.RemoveFirst ();
Zac Olsen's avatar
Zac Olsen committed
68 69 70 71
			if (current == finish) {
				break;
			}

Zac Olsen's avatar
Zac Olsen committed
72 73
			current.closed = true;

Zac Olsen's avatar
Zac Olsen committed
74
			List<Tile> neighbors = current.GetNeighbors ();
Zac Olsen's avatar
Zac Olsen committed
75 76
			for (int i = 0; i < neighbors.Count; i++) {

Zac Olsen's avatar
Zac Olsen committed
77 78 79 80 81 82
				if (!neighbors[i].closed) {
					if (!open.Contains(neighbors[i])) {

						neighbors [i].goal = float.MaxValue;
						neighbors [i].prev = null;
					}
Zac Olsen's avatar
Zac Olsen committed
83

Zac Olsen's avatar
Zac Olsen committed
84
					UpdateVertices (current, neighbors[i], finish);
Zac Olsen's avatar
Zac Olsen committed
85 86 87 88
				}
			}
		}

Zac Olsen's avatar
Zac Olsen committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
		PrintPathRetrace (finish);
		if (finish.prev != null) {
			enemy.GetComponent<TraversePath> ().BeginTraversingPath (CreatePath (finish));
		}
	}

	void UpdateVertices (Tile current, Tile neighbor, Tile finish) {

		Debug.Log (neighbor.goal);

		if (current.goal + DistBetween(current, neighbor) < neighbor.goal) {

			neighbor.goal = current.goal + DistBetween (current, neighbor);
			neighbor.prev = current;

			if (open.Contains (neighbor)) {
				open.Remove (neighbor);
			}

			neighbor.fitness = neighbor.goal + EstimateHeuristic(neighbor, finish);
			open.Add (neighbor);
		}

		Debug.Log (neighbor.goal);
	}

	void ComputeCosts (Tile current, Tile neighbor) {

		if (current.goal + DistBetween(current, neighbor) + neighbor.weight < neighbor.goal) {
			neighbor.prev = current;
			neighbor.goal = current.goal + DistBetween(current, neighbor) + neighbor.weight;
		}
Zac Olsen's avatar
Zac Olsen committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	}

	float EstimateHeuristic (Tile current, Tile finish) {
		return Mathf.Abs (current.x - finish.x) + Mathf.Abs (current.y - finish.y);
	}

	float DistBetween (Tile current, Tile neighbor) {
		return Mathf.Pow(current.x - neighbor.x, 2) + Mathf.Pow(current.y - neighbor.y, 2);
	}

	void PrintPathRetrace (Tile end) {

		while (end != null) {
			Debug.Log (end.x + ", " + end.y);
			end = end.prev;
		}
	}

	Path CreatePath (Tile finish) {

		Path path = new Path ();

		while (finish != null) {
			path.Push (finish);
			finish = finish.prev;
		}

		return path;
	}
}