using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Main : MonoBehaviour {

	public Transform lilyPrefab;
	public Transform endPadPrefab;
	public Transform playerPrefab;
	public Transform[] footPrefabs;
	public Transform rockPrefab;
	public Transform honeyPrefab;
	public Transform redPadPrefab;
	public Transform purplePadPrefab;
	public Transform yellowPadPrefab;

	public Transform redFishPrefab;
	public Transform purpleFishPrefab;
	public Transform yellowFishPrefab;

	public Transform turtlePrefab;

	public Transform bannerPrefab;

	private Transform player;

	private Communicator communicator;

	public const int numLevels = 10;
	private int[,,,] levels = {
		{
			{ // baby chow
				{0,0,0, 0,0,0,0, 0,0},
				{0,0,0, 0,0,0,0, 0,0},
				{0,0,12,0,0,0,0, 0,0},
				{0,0,7, 1,1,4,0, 0,0},
				{0,0,1, 0,0,1,0, 0,0},
				{0,0,1, 0,0,1,0, 0,0},
				{0,0,3, 1,1,6,13,0,0},
				{0,0,0, 0,0,0,0, 0,0},
				{0,0,0, 0,0,0,0, 0,0}
			},{
				{0,0,0, 0,0,0,0, 0,0},
				{0,0,0, 0,0,0,0, 0,0},
				{0,0,22,0,0,0,0, 0,0},
				{0,0,5, 1,1,4,0, 0,0},
				{0,0,1, 0,0,1,0, 0,0},
				{0,0,1, 0,0,1,0, 0,0},
				{0,0,3, 1,1,7,23,0,0},
				{0,0,0, 0,0,0,0, 0,0},
				{0,0,0, 0,0,0,0, 0,0}
			}
		},{
			{ // sonic
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,22,0,0,0,0},
				{0,0,0,1,4,1,0,0,0},
				{0,0,0,0,5,0,0,0,0},
				{0,0,0,0,1,0,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,1,4,1,0,0,0},
				{0,0,0,0,6,13,0,0,0},
				{0,0,0,0,1,0,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // turtle intro level
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,1,15,1,0,4,0,0},
				{0,3,1,0,1,0,1,0,0},
				{0,0,0,0,1,1,1,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,1,1,1,0,4,0,0},
				{0,3,1,0,1,0,1,0,0},
				{0,0,0,0,1,16,1,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // level tadpol
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,0,0,0,1,0,0,0,0},
				{0,0,0,1,16,1,0,0,0},
				{0,0,0,1,1,9,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,1,15,1,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // !?
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,1,0,0,0,0},
				{0,0,0,0,5,0,0,0,0},
				{0,0,0,0,15,0,0,0,0},
				{0,0,0,0,6,0,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,3,1,1,23,0,0},
				{0,0,0,0,0,1,13,0,0},
				{0,0,0,0,16,1,13,0,0},
				{0,0,0,1,1,23,0,0,0},
				{0,0,0,1,0,0,0,0,0},
				{0,0,0,4,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // honey
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,1,9,4,9,1,23,0},
				{0,0,0,5,1,7,0,0,0},
				{0,0,0,0,7,0,0,0,0},
				{0,0,0,1,3,1,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,12,0,32,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,1,6,9,6,1,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // level swagwife
				{0,0,0,0,12,0,0,0,0},
				{0,0,0,1,0,1,0,0,0},
				{0,0,0,6,6,6,0,0,0},
				{0,0,0,6,15,6,0,0,0},
				{0,0,0,1,7,1,0,0,0},
				{0,0,0,7,15,7,0,0,0},
				{0,0,0,7,7,7,0,0,0},
				{0,0,0,4,0,3,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,32,1,22,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,1,16,1,33,0,0},
				{0,0,0,1,0,1,0,0,0},
				{0,0,0,5,16,5,0,0,0},
				{0,0,0,1,0,1,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,0,0,0,1,0,0,0,0}
			}
		},{
			{ // level mac
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,5,0,5,0,0,0},
				{0,0,0,1,4,1,0,0,0},
				{0,0,0,5,0,5,0,0,0},
				{0,0,0,1,1,1,0,0,0},
				{0,0,0,20,0,20,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,12,0,12,0,0,0},
				{0,0,0,6,1,6,0,0,0},
				{0,0,0,6,0,6,0,0,0},
				{0,0,0,1,4,1,0,0,0},
				{0,0,0,6,0,6,0,0,0},
				{0,0,0,6,1,6,0,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // tommy lau
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,12,0,0,0,0},
				{0,0,6,6,1,6,6,0,0},
				{0,0,6,6,16,6,6,0,0},
				{0,11,1,2,3,2,1,13,0},
				{0,0,6,6,2,6,6,0,0},
				{0,0,6,6,4,6,6,0,0},
				{0,0,0,0,10,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,0,0,22,0,0},
				{0,21,1,15,1,15,1,0,0},
				{0,0,1,2,5,2,1,0,0},
				{0,0,1,5,4,5,1,0,0},
				{0,0,1,2,5,2,1,0,0},
				{0,0,1,15,3,15,1,23,0},
				{0,0,20,0,0,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{
			{ // satay
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,21,1,1,1,1,1,13,0},
				{0,11,1,1,1,1,1,33,0},
				{0,31,1,21,1,13,1,33,0},
				{0,31,1,1,1,1,1,23,0},
				{0,21,1,1,1,1,1,13,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			},{
				{0,0,0,0,0,0,0,0,0},
				{0,0,0,0,4,0,0,0,0},
				{0,0,6,6,7,6,7,0,0},
				{0,0,6,7,7,5,6,0,0},
				{0,0,5,5,6,5,6,0,0},
				{0,0,7,7,6,5,7,0,0},
				{0,0,6,5,7,5,6,0,0},
				{0,0,0,0,3,0,0,0,0},
				{0,0,0,0,0,0,0,0,0}
			}
		},{ // misc test
			{ 
				{1,1,1,1,1,1,1,1,1},
				{0,0,0,1,1,1,0,0,0},
				{0,1,1,1,1,1,1,0,0},
				{0,1,1,9,1,4,1,0,0},
				{0,1,1,1,1,1,1,1,0},
				{1,1,1,3,1,15,1,1,1},
				{0,0,1,1,1,1,1,1,0},
				{0,0,0,1,0,1,1,0,0},
				{0,0,0,0,0,1,0,0,0}
			},{
				{1,1,1,1,1,1,1,1,1},
				{0,0,0,1,1,1,0,0,0},
				{0,1,1,1,1,1,1,0,0},
				{0,1,1,1,1,4,1,0,0},
				{1,1,1,1,1,9,1,1,1},
				{0,1,1,3,1,16,1,1,1},
				{0,0,1,1,1,1,1,1,0},
				{0,0,0,1,0,1,1,0,0},
				{0,0,0,0,0,1,0,0,0}
			}
		}
	};

	private Transform[,] transformGrid;
	private List<Transform> transforms;

	private Vector2 playerPos;
	private float playerSpeed = 3;
	private int moves = 3;
	public int level = 0;
	private float delay = 0;
	public int gameIndex = 0;
	private float animCount = 0;
	private float deadCount = 0;

	private Vector2 actionPos;
	private List<Vector2> actions;
	private List<Transform> feet;
	private List<int> dirs;

	public bool ready{
		get {
			return actions.Count == moves;
		}
	}
	public bool playback;
	public bool atEnd = false;
	
	// Use this for initialization
	void Start () {
		playback = false;
		transforms = new List<Transform>();
		feet = new List<Transform>();
		GameObject obj = GameObject.FindGameObjectWithTag("Communicator");
		communicator = obj.GetComponent<Communicator>();
	}

	void clearLevel() {
		foreach (Transform transform in transforms) {
			GameObject.Destroy(transform.gameObject);
 		}
		transforms.Clear();
	}

	public void die() {
		deadCount = 1;
		//reset();
		playSound(0);
	}

	public void reset() {
		playback = false;
		clearLevel();
		loadLevel();
	}

	public void loadLevel() {
		clearLevel ();
		CreateTiles();
		player = instantiateAt(playerPrefab, playerPos);
		player.Translate(new Vector3(0, 0.28f, 0));
		player.Rotate(new Vector3(90, 0, 0));
		transforms.Add(player);
		actionPos = playerPos;
		actions = new List<Vector2>();
		dirs = new List<int>();
	}

	void CreateTiles() {
		transformGrid = new Transform[9,9];
		Random.seed = 1234 * gameIndex + 12123 * level + 13422341;
		for (int x = 0; x < 9; x ++) {
			for (int y = 0; y < 9; y ++) {
				Vector2 pos = new Vector2(x, y);
				int tile = levels[level, gameIndex, y, x];
				switch (tile) {
				case 3:
					playerPos = new Vector2(x, y);
					break;
				case 9: // honey
					Transform honey = instantiateAt(honeyPrefab, pos);
					honey.Translate(new Vector3(0, 0.30f, 0));
					honey.Rotate(new Vector3(90, Random.Range(0, 360), 0));
					transforms.Add(honey);
					break;
				}

				switch (tile) {
				case 1:
				case 3:
				case 8:
				case 9:
					Transform lily = instantiateAt(lilyPrefab, pos);
					lily.Rotate(Vector3.up, Random.Range(0, 360));
					transforms.Add(lily);
					break;
				case 2:
					Transform rock = instantiateAt(rockPrefab, pos);
					rock.Rotate(Vector3.up, Random.Range(0, 360));
					transforms.Add(rock);
					break;
				case 4:
					Transform endPad = instantiateAt(endPadPrefab, pos);
					endPad.Rotate(Vector3.up, Random.Range(0, 360));
					transforms.Add(endPad);
					break;
				case 5:
					Transform redPad = instantiateAt(redPadPrefab, pos);
					redPad.Rotate(Vector3.up, Random.Range(0, 360));
					transforms.Add(redPad);
					break;
				case 6:
					Transform purplePad = instantiateAt(purplePadPrefab, pos);
					purplePad.Rotate(Vector3.up, Random.Range(0, 360));
					transforms.Add(purplePad);
					break;
				case 7:
					Transform yellowPad = instantiateAt(yellowPadPrefab, pos);
					yellowPad.Rotate(Vector3.up, Random.Range(0, 360));
					transforms.Add(yellowPad);
					break;
				case 15: // turtle
					Transform turtle = instantiateAt(turtlePrefab, pos);
					turtle.Rotate(Vector3.up, 60 + 90 * (int)Random.Range(0, 4));
					transforms.Add(turtle);
					transformGrid[x, y] = turtle;
					break;
				case 16:
					Transform turtleDown = instantiateAt(turtlePrefab, pos);
					turtleDown.Rotate(Vector3.up, 60 + 90 * (int)Random.Range(0, 4));
					turtleDown.Translate(new Vector3(0, -0.25f, 0));
					Turtle turtleScript = (Turtle)turtleDown.gameObject.GetComponent(typeof(Turtle));
					turtleScript.Down = true;
					transforms.Add(turtleDown);
					transformGrid[x, y] = turtleDown;
					break;
				case 10:
				case 11:
				case 12:
				case 13:
				case 20:
				case 21:
				case 22:
				case 23:
				case 30:
				case 31:
				case 32:
				case 33:
				case 40:
				case 41:
				case 42:
				case 43:
					Transform fish;
					switch (tile / 10) {
					case 1:
					default:
						fish = instantiateAt(redFishPrefab, pos);
						break;
					case 2:
						fish = instantiateAt(purpleFishPrefab, pos);
						break;
					case 3:
						fish = instantiateAt(yellowFishPrefab, pos);
						break;
					}

					// this is high quality code used to shift back the fish
					switch (tile % 10) {
					case 0:
					default:
						fish.Translate(new Vector3(0, -0.015f, -2));
						break;
					case 1:
						fish.Translate(new Vector3(-2, -0.015f, 0));
						break;
					case 2:
						fish.Translate(new Vector3(0, -0.015f, 2));
						break;
					case 3:
						fish.Translate(new Vector3(2, -0.015f, 0));
						break;
					}
						//fish = instantiateAt(redFishPrefrab, pos);
					fish.Rotate(Vector3.up, ((tile % 10) + 1) * 90);
					transformGrid[x, y] = fish;
					transforms.Add(fish);
					break;
				}
			}
		}
	}

	// Not more update! This must be called by the communicator.
	public void DoLogic () {
		if (deadCount > 0) {
			deadCount -= Time.deltaTime;
			if (deadCount <= 0) {
				reset ();
			}
			return;
		}
		if (playback) {
			handleAnimation();
			return;
		}
		if (getResetButtonDown()) {
			actionPos = playerPos;
			clearFeet();
			actions.Clear();
			communicator.SetGameNotReady(gameIndex);
			return;
		}
		if (actions.Count == moves) {
			return;
		}
//		if (actions.Count == moves) {
//			if (Input.GetButtonDown("Go")) {
//				playback = true;
//				clearFeet();
//				delay = 0.001f;
//			}
//			return;
//		}

		float xdir = getHorizontalDown();
		float ydir = getVerticalDown();

		if (xdir <= -1) {
			if (canStep(actionPos + new Vector2(-1, 0))) {
				//player.Translate(2 * Vector3.left);
				actionPos.x -= 1;
				actions.Add(actionPos);
				dirs.Add(3);
				updateColorsAndTurtles(actionPos);
				checkHoney(actionPos);

				feet.Add(makeFootAt(actionPos, actions.Count - 1));
			}
		}
		else if (xdir >= 1) {
			if (canStep(actionPos + new Vector2(1, 0))) {
				//player.Translate(2 * Vector3.right);
				actionPos.x += 1;
				actions.Add(actionPos);
				dirs.Add(1);
				updateColorsAndTurtles(actionPos);
				checkHoney(actionPos);

				feet.Add(makeFootAt(actionPos, actions.Count - 1));
			}
		}
		else if (ydir <= -1) {
			if (canStep(actionPos + new Vector2(0, -1))) {
				//player.Translate(2 * Vector3.down);
				actionPos.y -= 1;
				actions.Add(actionPos);
				dirs.Add(0);
				updateColorsAndTurtles(actionPos);
				checkHoney(actionPos);
				
				feet.Add(makeFootAt(actionPos, actions.Count - 1));
			}
		}
		else if (ydir >= 1) {
			if (canStep(actionPos + new Vector2(0, 1))) {
				//player.Translate(2 * Vector3.up);
				actionPos.y += 1;
				actions.Add(actionPos);
				dirs.Add(2);
				updateColorsAndTurtles(actionPos);
				checkHoney(actionPos);
				
				feet.Add(makeFootAt(actionPos, actions.Count - 1));
			}
		}

		if (actions.Count == moves) {
			communicator.SetGameReady(gameIndex);
		}
	}

	private void checkHoney(Vector2 pos) {
		int tile = levels[level, gameIndex, (int)pos.y, (int)pos.x];
		if (tile == 9) {
			while (actions.Count < 3) {
				actions.Add(actions[actions.Count-1]);
				dirs.Add(dirs[dirs.Count-1]);
			}
		}
	}

	private void updateColorsAndTurtles(Vector2 actionPos) {
		int actionIndex = actions.Count - 1;
		for (int i = 0; i < 4; i ++) {
			communicator.colorsActive[gameIndex, actionIndex, i] = false;
		}
		communicator.turtleActive[gameIndex, actionIndex] = false;
		int tile = levels[level, gameIndex, (int) actionPos.y, (int) actionPos.x];
		switch (tile) {
		case 5:
		case 6:
		case 7:
		case 8:
			communicator.colorsActive[gameIndex, actionIndex, tile - 5] = true;
			break;
		case 15:
			communicator.turtleActive[gameIndex, actionIndex] = true;
			break;
		}
	}

	private float getHorizontalDown() {
		if (communicator.tutorialing) {
			return 0;
		}
		if (gameIndex == 0) {
			return Input.GetButtonDown("Horizontal1") ? Input.GetAxis("Horizontal1") : 0;
		}
		else {
			return Input.GetButtonDown("Horizontal2") ? Input.GetAxis("Horizontal2") : 0;
		}
	}
	
	private float getVerticalDown() {
		if (communicator.tutorialing) {
			return 0;
		}
		if (gameIndex == 0) {
			return Input.GetButtonDown("Vertical1") ? Input.GetAxis("Vertical1") : 0;
		}
		else {
			return Input.GetButtonDown("Vertical2") ? Input.GetAxis("Vertical2") : 0;
		}
	}

	private bool getResetButtonDown() {
		if (communicator.tutorialing) {
			return false;
		}
		if (gameIndex == 0) {
			return Input.GetButtonDown("Reset1");
		}
		else {
			return Input.GetButtonDown("Reset2");
		}
	}
	
	public void startPlayback() {
		playback = true;
		clearFeet();
		delay = 0.001f;
		actionPos = playerPos;
	}

	private void clearFeet() {
		foreach (Transform foot in feet) {
			Destroy(foot.gameObject);
		}
		feet.Clear();
	}

	private void handleAnimation() {
		Vector3 vec;
		Vector3 vec2;
		if (delay > 0) {
			delay -= Time.deltaTime;
			if (delay <= 0) {
				// ============================== Start each action here ==============================
				vec = getPosition(actions[0]);
				vec.y = player.position.y;
				animCount = 0;

				setTurtleDown(true);

				Frog frogScript = player.GetComponent<Frog>();
				frogScript.Hop();
				//switch (dirs[0]) {
				//case 0:
				//rotateDir(player, dirs[0]);
				player.rotation = Quaternion.Euler(new Vector3(90, dirs[0] * 90, 0));
				playSound(gameIndex + 2);
			}
			return;
		}
		// ============================== Animation ==============================
		int moveIndex = moves - actions.Count;

		animCount += playerSpeed * Time.deltaTime / 2;
		animCount = Mathf.Pow(animCount, 0.7f);
		if (animCount > 1) {
			animCount = 1;
		}
		vec = getPosition(actions[0]);
		vec.y = player.position.y;
		vec2 = getPosition(actionPos);
		vec2.y = player.position.y;

		player.position = Vector3.Lerp (vec2, vec, animCount);
		if (animCount == 1) {
			fishFire(moveIndex);
			// ============================== check death ==============================
			int tile = levels[level, gameIndex, (int)actions[0].y, (int)actions[0].x];
			atEnd = false;
			// check colors
			for (int i = 0; i < 4; i ++) {
				if (communicator.colorsActive[1-gameIndex, moveIndex, i]) {
					//check fish
					if (inLineWithFish(actions[0], i)) {
						communicator.dead = true;
						return;
					}
				}
			}

			if (communicator.turtleActive[1-gameIndex, moveIndex]) {
				setTurtleDown(false);
				playSound(5);
			}
			else if (tile == 16) {
				communicator.dead = true;
				return;
			}
			if (actions.Count == 1) {
				playback = false;
				playerPos = actions[0];
				if (tile == 4) {
					atEnd = true;
				}
			}
			else {
				//if (tile == 16 || tile == 15) {
				//	delay = 0.6f;
				//}
				//else {
					delay = 0.2f;
				//}
			}
			actionPos = actions[0];
			actions.RemoveAt(0);
			dirs.RemoveAt(0);
		}
	}

	private void fishFire(int moveIndex) {
		bool fishFired = false;
		for (int x = 0; x < 9; x ++) {
			for (int y = 0; y < 9; y ++) {
				int tile = levels[level, gameIndex, y, x];
				int colour = (tile / 10) - 1;
				if (colour >= 0 && (tile % 10) >= 0 && (tile % 10) <= 3) {
					if (communicator.colorsActive[1-gameIndex, moveIndex, colour]) {
						Transform fishTrfm = transformGrid[x,y];
						Fish fish = (Fish) fishTrfm.gameObject.GetComponent(typeof(Fish));
						fish.Fire();
						fishFired = true;
					}
				}
			}
		}
		if (fishFired) {
			playSound(4);
		}
	}

	private void setTurtleDown(bool value) {
		for (int x = 0; x < 9; x ++) {
			for (int y = 0; y < 9; y ++) {
				int tile = levels[level, gameIndex, y, x];
				if (tile == 16) {
					Transform turtleTrfm = transformGrid[x,y];
					Turtle turtle = (Turtle) turtleTrfm.gameObject.GetComponent(typeof(Turtle));
					turtle.Down = value;
				}
			}
		}
	}

	private bool inLineWithFish(Vector2 pos, int color) {
		// to the right
		int x = (int) pos.x;
		int y = (int) pos.y;
		for (int x2 = x; x2 < 9; x2 ++) {
			if (levels[level, gameIndex, y, x2] == 10 * (color + 1) + 3) {
				return true;
			}
		}
		for (int x2 = x; x2 >= 0; x2 --) {
			if (levels[level, gameIndex, y, x2] == 10 * (color + 1) + 1) {
				return true;
			}
		}
		for (int y2 = y; y2 < 9; y2 ++) {
			if (levels[level, gameIndex, y2, x] == 10 * (color + 1) + 0) {
				return true;
			}
		}
		for (int y2 = y; y2 >= 0; y2 --) {
			if (levels[level, gameIndex, y2, x] == 10 * (color + 1) + 2) {
				return true;
			}
		}
		return false;
	}

	private Transform makeFootAt(Vector2 pos, int index) {
		Transform foot = instantiateAt(footPrefabs[index], pos);
		foot.localScale = new Vector3(0.1f, 0.1f, 0.1f);
		foot.Translate(new Vector3(0, 0.30f + 0.1f * index, 0));
		foot.Rotate(new Vector3(90, 0, 0));
		return foot;
	}
	
	private Transform instantiateAt(Transform prefab, Vector2 pos) {
		return (Transform)Instantiate(prefab, getPosition(pos), Quaternion.identity);
	}

	private Vector3 getPosition(Vector2 pos) {
		return transform.position + new Vector3(2*(pos.x-4), 0, 2*(4-pos.y));
	}
	
	private bool canStep(Vector2 pos) {
		int x = (int) pos.x;
		int y = (int) pos.y;
		if (x < 0 || x >= 9 ||
		    y < 0 || y >= 9) {
			return false;
		}
		switch (levels[level, gameIndex, y, x]) {
		case 1:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 15:
		case 16:
			return true;
		}
		return false;
	}


	public void addVictory() {
		Transform banner = (Transform)Instantiate(bannerPrefab, new Vector3(0, 1, 10), Quaternion.Euler(90, 0, 0));
		BannerScript bannerScript = banner.GetComponent<BannerScript>();
		if (gameIndex == 0) {
			bannerScript.dir = Vector3.right;
		}
		else {
			bannerScript.dir = Vector3.left;
		}
		bannerScript.startPos = transform.position + new Vector3(0, 2, 0);
		bannerScript.SetAway();
		transforms.Add(banner);
	}

	public void playSound(int index) {
		GameObject camera = GameObject.FindGameObjectWithTag("MainCamera");
		AudioSource[] audioSources = camera.GetComponents<AudioSource>();
		audioSources[index].Stop();
		audioSources[index].Play();
	}
	
}
