// Use this for initialization
	void Start () {

		supportive = GetComponent<AudioSource>();

		// This is some instantiation.
		// We want it so that if the player is within a + direction from the particle, they will get hurt.
		GameObject player = GameObject.FindGameObjectWithTag("Player");
		int playerX = (int)player.transform.position.x;
		int playerY = (int)player.transform.position.y;

		Coord myPlace = new Coord((int)this.transform.position.x, (int)this.transform.position.y);
		Coord playerCoord = new Coord(playerX, playerY);
		Coord n, w, s, e;
		n = playerCoord.nextCoord (Direction.North);
		w = playerCoord.nextCoord (Direction.West);
		s = playerCoord.nextCoord (Direction.South);
		e = playerCoord.nextCoord (Direction.East);

		if( myPlace.isEqual (playerCoord) ||
		    myPlace.isEqual (n) ||
		    myPlace.isEqual (w) ||
		    myPlace.isEqual (s) ||
		    myPlace.isEqual (e) ) {
			PlayerMovement hitPlayer = player.GetComponent<PlayerMovement>();
			supportive.PlayOneShot(haha,1f);
			hitPlayer.LoseHealth(750); }

		Destroy (this.gameObject, 3);

	}
	// Update is called once per frame
	void Update () {
		if ( Input.GetButtonDown("Fire1") ) {

			// Do we have enough MP?
			PlayerMovement thatsMe = GetComponent<PlayerMovement>();
			if(thatsMe.getPlayerCurrentMP() > 0) {

				int thatsMyMP = thatsMe.getPlayerCurrentMP();
				thatsMyMP -= spellCost;

				if(thatsMyMP < 0) thatsMyMP = 0;

				thatsMe.setPlayerCurrentMP (thatsMyMP);

				source.pitch = Random.Range (pitchLowRange, pitchHighRange);
				float vol = Random.Range (volLowRange, volHighRange);
				source.PlayOneShot(magicSound,vol);

				Coord playerCoord = new Coord((int)thatsMe.transform.position.x, (int)thatsMe.transform.position.y);
				Coord rangeNorth, rangeSouth, rangeWest, rangeEast;

				// OK so we're going to figure out what enemies are in our range
				List<Enemy> newEnemyList = new List<Enemy>();
				List<Enemy> allEnemies = GameManager.instance.getListOfEnemies();
				for(int i = 0; i < allEnemies.Count; i++) {

					// Store coordinates for calculations
					Coord enemyCoord = new Coord((int)allEnemies[i].transform.position.x, (int)allEnemies[i].transform.position.y);
					bool sameSpot = false;
					if(enemyCoord.isEqual(playerCoord)) {
						newEnemyList.Add (allEnemies[i]);
						sameSpot = true;
					}
					rangeNorth = rangeSouth = rangeWest = rangeEast = playerCoord;
					// We'll have to travel down the particle's route
					// and see if the enemy lands squarely within that.
					// If so, then we're going to add it to the hit list and break
					// (we don't need to keep searching)
					for(int j = 1; j <= spellRadius && !sameSpot; j++) {
						rangeNorth = rangeNorth.nextCoord (Direction.North);
						rangeSouth = rangeSouth.nextCoord (Direction.South);
						rangeWest = rangeWest.nextCoord (Direction.West);
						rangeEast = rangeEast.nextCoord (Direction.East);
						if(enemyCoord.isEqual (rangeNorth) || 
						   enemyCoord.isEqual (rangeSouth) || 
						   enemyCoord.isEqual (rangeWest) || 
						   enemyCoord.isEqual (rangeEast) ) {
							newEnemyList.Add (allEnemies[i]);
							break;
						}

					}
				}

				// This is just particle effects
				rangeNorth = rangeSouth = rangeWest = rangeEast = playerCoord;
				for(int i = 1; i <= spellRadius; i++) {
					rangeNorth = rangeNorth.nextCoord (Direction.North);
					rangeSouth = rangeSouth.nextCoord (Direction.South);
					rangeWest = rangeWest.nextCoord (Direction.West);
					rangeEast = rangeEast.nextCoord (Direction.East);
					GameObject objectN = Instantiate(particleGfx, new Vector3 (rangeNorth.x, rangeNorth.y, 0), Quaternion.identity) as GameObject;
					GameObject objectS = Instantiate(particleGfx, new Vector3 (rangeSouth.x, rangeSouth.y, 0), Quaternion.identity) as GameObject;
					GameObject objectW = Instantiate(particleGfx, new Vector3 (rangeWest.x, rangeWest.y, 0), Quaternion.identity) as GameObject;
					GameObject objectE = Instantiate(particleGfx, new Vector3 (rangeEast.x, rangeEast.y, 0), Quaternion.identity) as GameObject;

					Destroy (objectN, 1f);
					Destroy (objectS, 1f);
					Destroy (objectW, 1f);
					Destroy (objectE, 1f);
				
					}

				// When we're here, it's time to inflict damage
				for(int i = 0; i < newEnemyList.Count; i++)
					newEnemyList[i].loseHealth (spellDamage);
			}
		}
	}
	public void FloodFillCheck(Tile[,] map, Coord current, Coord stop) {
		// The recursive algorithm. Starting at x and y, traverse down adjacent tiles and mark them if travelled, find the exit from the entrance
			int mapWidth = map.GetLength(0);
			int mapHeight = map.GetLength(1);

			int x = current.x;
			int y = current.y;

			//Debug.Log (x.ToString() + " and " + y.ToString () );

			if (map[x,y].mark != 0)
				// Base case. If the current tile is marked, then do nothing.
				return;

				// Change the current tile as marked
				map[x,y].mark = 1;

			if ( stop.isEqual (current) ) {
				Debug.Log ("Flood Fill found, flagging clearable: " + current.x.ToString() + " " + current.y.ToString ());
				clearable = true;
				return;
			}
					// Recursive calls. Make a recursive call as long as we are not on the
					// boundary (which would cause an Index Error.)
			if (x > 0 && !isSolid (map, current.nextCoord (Direction.West)) ) // left
				FloodFillCheck(map, current.nextCoord (Direction.West), stop);

			if (y > 0 && !isSolid (map, current.nextCoord (Direction.South)) ) // up
				FloodFillCheck(map, current.nextCoord (Direction.South), stop);

			if (x < mapWidth-1 && !isSolid (map, current.nextCoord (Direction.East)) ) // right
				FloodFillCheck(map, current.nextCoord (Direction.East), stop);

			if (y < mapHeight-1 && !isSolid (map, current.nextCoord (Direction.North)) ) // down
				FloodFillCheck(map, current.nextCoord (Direction.North), stop);

	}