Tile.cs 1.93 KB
Newer Older
Zac Olsen's avatar
Pathing  
Zac Olsen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Tile : MonoBehaviour {

	public bool passable = true;

	public Color canPas = new Color (255f, 255f, 255f);
	public Color cantPas = new Color (0f, 0f, 0f);

	private SpriteRenderer sprite;

	public int x;
	public int y;
	public Tile prev;

	public float goal = float.MaxValue;
	public float fitness = float.MaxValue;

	void Start () {

		sprite = GetComponent<SpriteRenderer> ();
	}

	void OnMouseDown () {

		passable = !passable;

		if (passable) {
			sprite.color = canPas;
		} else {
			sprite.color = cantPas;
		}
	}

	public List<Tile> GetNeighbors () {

		List<Tile> neighbors = new List<Tile> ();

		if (x < MapGen.map.GetLength(0) - 1 && MapGen.map[x + 1, y].passable) {
			neighbors.Add (MapGen.map[x + 1, y]);
		}

		if (x < MapGen.map.GetLength(0) - 1 && y < MapGen.map.GetLength(1) - 1 && MapGen.map[x + 1, y + 1].passable
			&& MapGen.map[x, y + 1].passable && MapGen.map[x + 1, y].passable) {
			neighbors.Add (MapGen.map[x + 1, y + 1]);
		}

		if (y < MapGen.map.GetLength (1) - 1 && MapGen.map [x, y + 1].passable) {
			neighbors.Add (MapGen.map[x, y + 1]);
		}

		if (x > 0 && y < MapGen.map.GetLength(1) - 1 && MapGen.map[x - 1, y + 1].passable
			&& MapGen.map[x, y + 1].passable && MapGen.map[x - 1, y].passable) {
			neighbors.Add (MapGen.map[x - 1, y + 1]);
		}

		if (x > 0 && MapGen.map[x - 1, y].passable) {
			neighbors.Add (MapGen.map[x - 1, y]);
		}

		if (x > 0 && y > 0 && MapGen.map[x - 1, y - 1].passable
			&& MapGen.map[x, y - 1].passable && MapGen.map[x - 1, y].passable) {
			neighbors.Add (MapGen.map[x - 1, y - 1]);
		}

		if (y > 0 && MapGen.map[x, y - 1].passable) {
			neighbors.Add (MapGen.map[x, y - 1]);
		}

		if (x < MapGen.map.GetLength(0) - 1 && y > 0 && MapGen.map[x + 1, y - 1].passable
			&& MapGen.map[x, y - 1].passable && MapGen.map[x + 1, y].passable) {
			neighbors.Add (MapGen.map[x + 1, y - 1]);
		}

		return neighbors;
	}
}