private IEnumerator MoveCoroutine() { // Randomise the distance walked (if NPCreature is not hungry) int movePoints; bool reproduce = false; if (Hungry() || Horny()) { movePoints = speed; } else { movePoints = Mathf.RoundToInt(Random.Range(0f, speed)); } // Start walking while (movePoints > 0) { UnitPosition = PlayGrid.getUnitCoordinates((Vector2)gameObject.GetComponent <Transform>().transform.position); directionVector = Vector2.zero; // Check if the NPCreature is hungry. If yes, determine in which direction there is a plant with the most nutrition if (Hungry()) { directionVector = FindPlant(); //Debug.Log("Highest nutriotional plant at Vector [" + directionVector.x + "," + directionVector.y + "]"); } else if (Horny()) { directionVector = findMate(); } if (directionVector == Vector2.zero) //If the directionVector has remained zero (i.e., plant or mate not found) { directionVector = chooseRandomWalkingDirection(); } // Take a step forward (checking for collision with other NPCreatures) GameObject NPCreatureObstacle = checkforNPColission(directionVector); if (NPCreatureObstacle == null) { stepForward(directionVector); } else if (Horny() && NPCreatureObstacle.GetComponent <NPCreature>().Horny()) { // collision between two horny NPCreatures. //Debug.Log("Horny NPCreatures collided"); reproduce = true; } yield return(null); // Use Player overlap script to munch away plant if there is any in the vicinity //check for a collider overlap Collider2D[] encounters = new Collider2D[2]; boxcol.OverlapCollider(contactfilter, encounters); foreach (Collider2D encounter in encounters) { if (encounter != null) { if (encounter.gameObject.tag == "Plant" && satiation > 0) { satiation += encounter.gameObject.GetComponent <Plant>().GetEaten(); satiation = Mathf.Clamp(satiation, 0f, satiationMax); } else if (encounter.gameObject.tag == "NPCreature") // if NPCreatures accidentally step into the same space (which was empty), they will reproduce //Debug.Log("NPCreatures overlapping at [" + PlayGrid.getUnitCoordinates(gameObject.transform.position).x + "," + PlayGrid.getUnitCoordinates(gameObject.transform.position).y + "]"); { if (Horny() && encounter.gameObject.GetComponent <NPCreature>().Horny()) { reproduce = true; } } } } if (reproduce) { Reproduce(); } movePoints--; } }
private IEnumerator MoveCoroutine() { int movePoints = speed; while (movePoints > 0) { Vector2 directionVector = Vector2.zero; int NutritionalValue = 0; UnitPosition = PlayGrid.getUnitCoordinates((Vector2)gameObject.GetComponent <Transform>().transform.position); // If (omnivore && hungry) --> find highest satiation value between observed plants and if (Hungry()) { if (herbivore > 0) // If (herbivore && hungry) -> find plant { FindPlant(out int NutritionalValue_observed, out Vector2 selectedDirectionVector_observed); if (NutritionalValue_observed * herbivore > NutritionalValue) { NutritionalValue = NutritionalValue_observed * herbivore; directionVector = selectedDirectionVector_observed; } } if (carnivore > 0) // If (carnivore && hungry) --> find meat { FindMeat(out int NutritionalValue_observed, out Vector2 selectedDirectionVector_observed); if (NutritionalValue_observed * carnivore > NutritionalValue) { NutritionalValue = NutritionalValue_observed * carnivore; directionVector = selectedDirectionVector_observed; } } } if (directionVector == Vector2.zero) //If the directionVector has remained zero (i.e., plant or mate not found) { directionVector = chooseRandomWalkingDirection(); } // Take a step forward (checking for collision with other PlayCreatures) GameObject PlayCreatureObstacle = checkforPlayCreatureColission(directionVector); if (PlayCreatureObstacle == null) { stepForward(directionVector); } yield return(null); //check for a collider overlap Collider2D[] encounters = new Collider2D[2]; boxcol.OverlapCollider(contactfilter, encounters); foreach (Collider2D encounter in encounters) { if (encounter != null) { if (encounter.gameObject.tag == "Plant" && herbivore > 0) { satiation += encounter.gameObject.GetComponent <Plant>().GetEaten() * herbivore; } else if (encounter.gameObject.tag == "NPCreature" && carnivore > 0) { satiation += encounter.gameObject.GetComponent <NPCreature>().GetEaten() * carnivore; } satiation = Mathf.Clamp(satiation, 0f, satiationMax); } } movePoints--; } }