public void CellNeighbors(int row, int col, ForestCell l, ForestCell r, ForestCell t, ForestCell b) { this.left = l; this.right = r; this.top = t; this.bot = b; }
private ForestCell[,] InitForestModel() { var forest = new ForestCell[treesVertically, treesHorizontally]; for (int i = 0; i < treesVertically; i++) { for (int j = 0; j < treesHorizontally; j++) { forest[i, j] = new ForestCell(i, j); } } for (int i = 0; i < treesVertically; i++) { for (int j = 0; j < treesHorizontally; j++) { if (i == 0 && j == 0) { forest[i, j].CellNeighbors(i, j, null, forest[i, j + 1], null, forest[i + 1, j]); } else if (i == 0 && j == 99) { forest[i, j].CellNeighbors(i, j, forest[i, j - 1], null, null, forest[i + 1, j]); } else if (i == 99 && j == 0) { forest[i, j].CellNeighbors(i, j, null, forest[i, j + 1], forest[i - 1, j], null); } else if (i == 0) { forest[i, j].CellNeighbors(i, j, forest[i, j - 1], forest[i, j + 1], null, forest[i + 1, j]); } else if (j == 0) { forest[i, j].CellNeighbors(i, j, null, forest[i, j + 1], forest[i - 1, j], forest[i + 1, j]); } else if (i == 99 && j == 99) { forest[i, j].CellNeighbors(i, j, forest[i, j - 1], null, forest[i - 1, j], null); } else if (i == 99) { forest[i, j].CellNeighbors(i, j, forest[i, j - 1], forest[i, j + 1], forest[i - 1, j], null); } else if (j == 99) { forest[i, j].CellNeighbors(i, j, forest[i, j - 1], null, forest[i - 1, j], forest[i + 1, j]); } else { forest[i, j].CellNeighbors(i, j, forest[i, j - 1], forest[i, j + 1], forest[i - 1, j], forest[i + 1, j]); } } } return(forest); }
/// <summary> /// Throw rock on top cell. /// </summary> /// <param name="p_hHero"> Hero. </param> public static void ThrowRockTop(Hero p_hHero) { if (p_hHero.CurrentForestCell.ColumnIndex - 1 > 0) { ForestCell p_fcTarget = MainWindow.Forest[p_hHero.CurrentForestCell.LineIndex - 1, p_hHero.CurrentForestCell.ColumnIndex]; p_fcTarget.RemoveAlienOnCell(); Hero.Memory[p_fcTarget.LineIndex, p_fcTarget.ColumnIndex].HasNoAlien = 1; Hero.Memory[p_fcTarget.LineIndex, p_fcTarget.ColumnIndex].MayContainAlien = -1; p_hHero.CellsOK.Add(p_fcTarget); p_hHero.Score -= 10; } }
/// <summary> /// Throw rock on the left cell. /// </summary> /// <param name="p_hHero"> Hero. </param> public static void ThrowRockLeft(Hero p_hHero) { if (p_hHero.CurrentForestCell.LineIndex - 1 > 0) { // Set target ForestCell p_fcTarget = MainWindow.Forest[p_hHero.CurrentForestCell.LineIndex, p_hHero.CurrentForestCell.ColumnIndex - 1]; // Kill alien and remove radiation. p_fcTarget.RemoveAlienOnCell(); // Update memory Hero.Memory[p_fcTarget.LineIndex, p_fcTarget.ColumnIndex].HasNoAlien = 1; Hero.Memory[p_fcTarget.LineIndex, p_fcTarget.ColumnIndex].MayContainAlien = -1; // Mark cell as OK p_hHero.CellsOK.Add(p_fcTarget); p_hHero.Score -= 10; } }
/// <summary> /// Move the hero to a new cell. /// </summary> /// <param name="p_hHero"> Hero. </param> /// <param name="p_fcDestinationCell"> Destination cell. </param> /// <param name="cost"> Cost of the move. </param> public static void Move(Hero p_hHero, ForestCell p_fcDestinationCell, int cost) { // Update previous cell (used to know where we come and not throw rock there if we meet some radiation). p_hHero.PreviousForestCell = p_hHero.CurrentForestCell; // update hero current cell. p_hHero.CurrentForestCell = p_fcDestinationCell; // Update current MemoryCell. p_hHero.CurrentMemoryCell = Hero.Memory[p_fcDestinationCell.LineIndex, p_fcDestinationCell.ColumnIndex]; p_hHero.CurrentForestCell.AlreadyVisited = true; p_hHero.CurrentMemoryCell.IsSafe = 1; // Remove calculated cost from score. p_hHero.Score -= cost; // Throw new on move event. OnMove(p_hHero.PreviousForestCell, p_hHero.CurrentForestCell); }
/// <summary> /// Creates a cell at the specified coordinates /// </summary> /// <param name="xPos">The x coordinate</param> /// <param name="zPos">The z coordinate</param> /// <returns>The new cell</returns> private ForestCell CreateCell(int xPos, int zPos) { //Instantiate a new cell ForestCell newCell = Instantiate(cellPrefab); cells[xPos, zPos] = newCell; //Name the cell newCell.name = "Cell " + xPos + ", " + zPos; //Set the transform of the cell to be centered at the forest object newCell.transform.parent = transform; //Set the coordinates of the cell newCell.coordinates = new IntVector2(xPos, zPos); //Calculate the position of the cell in the forest Vector3 cellPos = new Vector3(xPos - sizeX * 0.5f + 0.5f, 0f, zPos - sizeZ * 0.5f + 0.5f); cellPos *= 2; newCell.transform.localPosition = cellPos; Material materials = newCell.GetComponentsInChildren <Renderer>()[0].material; Debug.Log(perlinTex.GetPixel(xPos, zPos).grayscale); if (materials != null) { if (perlinTex.GetPixel(xPos, zPos).grayscale <= 0.5f) { materials.mainTexture = tex1; } else { materials.mainTexture = tex2; } } newCell.GetComponent <Health>().manager = sManager; return(newCell); }
/// <summary> /// Method to spawn an animal into the world /// </summary> void SpawnAnimal() { // generate random ints xSpawn = Random.Range(0, forestGen.sizeX); ySpawn = Random.Range(0, forestGen.sizeX); // create the spawn position spawnPos = new IntVector2((int)xSpawn, (int)ySpawn); // get the cell in which the animal should spawn spawnCell = forestGen.GetCell(spawnPos); // check to make sure the position is unique for (int i = 0; i < animalSpawns.Count; i++) { if (spawnPos == animalSpawns[i]) { samePos = true; } } // add it to the list of spawnPositions animalSpawns.Add(spawnPos); if (samePos == false) { // check to make sure the tile isn't already on fire if (spawnCell.OnFire == false) { // get the world position of the forest cell Vector3 tempSpawn = new Vector3(spawnCell.transform.position.x, spawnCell.transform.position.y + 0.2f, spawnCell.transform.position.z); // instantiate the object in the world on the cell GameObject tempObj = Instantiate(animal, tempSpawn, Quaternion.identity); // increment the animal spawn counter animalCount++; } } }
/// <summary> /// Retrieves all the neighbors of this cell and adds them to the neighbors list /// </summary> private void GetNeighbors() { //The coordinates of this cell IntVector2 position = gameObject.GetComponent <ForestCell>().coordinates; //The forest this cell is in ForestGenerator forest = gameObject.GetComponentInParent <ForestGenerator>(); //Check every direction for (int i = 0; i < 4; i++) { //Calculate the coordinates of each direction IntVector2 cellToGet = position + ForestDirections.ToIntVector2((Direction)i); //If the coordinates calculated are part of the forest, retrieve that cell and add it to the list if (forest.ContainsCoordinates(cellToGet)) { ForestCell temp = forest.GetCell(cellToGet); neighbors.Add(temp); } } neighborsRetrieved = true; }
/// <summary> /// New move action. /// </summary> /// <param name="p_hHero"> Hero. </param> /// <param name="p_fcDestinationCell"> Destination cell. </param> /// <param name="p_iCost"> Cost </param> public Move(Hero p_hHero, ForestCell p_fcDestinationCell, int p_iCost) : base(p_hHero) { m_fcDestination = p_fcDestinationCell; m_iCost = p_iCost; }