/// <summary> /// Causes an ant to attempt to eat from the block it is currently on. This will restore its health to full /// unless the block contians less food than the amount trying to be eaten. If eat causes the block food to hit zero, /// the block will turn to dirt /// </summary> /// <param name="ant"></param> public void Eat(Ant ant) { //First reduce our health if (!ReduceHealth(ant)) { return; } //Get the type of the voxel Type atType = Field[ant.X, ant.Z].GetType(); //If not a food voxel if (atType != new FoodVoxel().GetType()) { Debug.Log("Ants cannot eat dirt or nest"); return; } //else, decrement resource FoodVoxel vox = Field[ant.X, ant.Z] as FoodVoxel; if (vox.Resource > (AntStartingHealth - ant.health)) { vox.Resource -= (AntStartingHealth - ant.health); ant.health += (AntStartingHealth - ant.health); } //If we have diminished ALL the food else { ant.health += vox.Resource; vox.Resource = 0; //Turn food source to dirt if (vox.Resource <= 0) { //get old coord int yIndex = vox.Y - 1; int xIndex = vox.X; int zIndex = vox.Z; //Create new dirt voxel DirtVoxel dirt = vox.gameObject.AddComponent <DirtVoxel>(); //Delete old voxel GameObject.Destroy(vox); //place it in our field Field[xIndex, yIndex] = dirt; //set new voxel coords dirt.X = xIndex; dirt.Y = yIndex; dirt.Z = zIndex; //Set new neighbours if (dirt.Z + 1 < zWidth) { dirt.N = Field[dirt.X, dirt.Z + 1]; Field[dirt.X, dirt.Z + 1].S = dirt; } if (dirt.X + 1 < xWidth) { dirt.E = Field[dirt.X + 1, dirt.Z]; Field[dirt.X + 1, dirt.Z].W = dirt; } if (dirt.Z - 1 >= 0) { dirt.S = Field[dirt.X, dirt.Z - 1]; Field[dirt.X, dirt.Z - 1].N = dirt; } if (dirt.X - 1 >= 0) { dirt.W = Field[dirt.X - 1, dirt.Z]; Field[dirt.X - 1, dirt.Z].E = dirt; } //init mesh creation dirt.Init(); } } }
/// <summary> /// Causes an ant to dig the block in front of it, removing it from the field. Can only dig blocks within a height difference of 1 from the ant /// </summary> /// <param name="ant"></param> /// <returns>Returns true if the dig was sucessful, false otherwise</returns> public bool Dig(Ant ant) { //First reduce our health if (!ReduceHealth(ant)) { return(false); } //If we are already carrying a voxel if (ant.voxelCarried) { Debug.Log("Ants cannot carry more than one block at a time"); return(false); } //Get the coordinates of where we are, and where we are trying to dig Voxel from; Voxel to; GetVoxelFacing(ant, out from, out to); //If we are unable to dig if (to == null) { Debug.Log("Cannot dig outside of the board"); return(false); } //If the height difference is 1 if (!IsValidMove(from, to)) { Debug.Log("Cannot dig more than one block height"); return(false); } //get old coord int yIndex = to.Y - 1; int xIndex = to.X; int zIndex = to.Z; //Create new dirt voxel DirtVoxel dirt = to.gameObject.AddComponent <DirtVoxel>(); //Delete old voxel GameObject.Destroy(to); //place it in our field Field[xIndex, yIndex] = dirt; //set new voxel coords dirt.X = xIndex; dirt.Y = yIndex; dirt.Z = zIndex; //Set new neighbours if (dirt.Z + 1 < zWidth) { dirt.N = Field[dirt.X, dirt.Z + 1]; Field[dirt.X, dirt.Z + 1].S = dirt; } if (dirt.X + 1 < xWidth) { dirt.E = Field[dirt.X + 1, dirt.Z]; Field[dirt.X + 1, dirt.Z].W = dirt; } if (dirt.Z - 1 >= 0) { dirt.S = Field[dirt.X, dirt.Z - 1]; Field[dirt.X, dirt.Z - 1].N = dirt; } if (dirt.X - 1 >= 0) { dirt.W = Field[dirt.X - 1, dirt.Z]; Field[dirt.X - 1, dirt.Z].E = dirt; } //init mesh creation dirt.Init(); return(true); }