IEnumerator idle(float idleDuration = 0) { // Debug.Log("idle"); sheepState = sheepBehaviors.idling; if (legs.getRunningState()) { // Debug.Log("stopping legs"); thisSheep.photonView.RPC("StopMoving", RpcTarget.All, true); } if (idleDuration > 0) { int roll = Random.Range(0, 11); if (roll > 3) { float direction = (Mathf.Pow(roll, 4) / 0.3f * roll) + thisSheep.facing; Vector2 runRise = new Vector2(Mathf.Sin(direction), Mathf.Cos(direction)); runRise *= (idleDuration / thisSheep.stats.speed) * 0.7f; Vector3 wayPoint = transform.position + (Vector3)runRise; if (legs.isNavigable(wayPoint)) { thisSheep.Move(wayPoint, -1, -1f, -1f); } } yield return(new WaitForSeconds(idleDuration)); } if (changeBehavior() == false) { StartCoroutine(idle(1)); } yield return(null); }
void PathEnded() { if (sheepState == sheepBehaviors.goingToSafety) { StopCoroutine("GoForSafety"); StartCoroutine(idle()); } else if (sheepState == sheepBehaviors.goingToFood) { StopCoroutine("WalkToFood"); Invoke("consume", eatTime); sheepState = sheepBehaviors.chewing; } }
IEnumerator WalkToFood() { thisSheep.Move(currentMostAppealingPatch, -1, -1f, -1f); // Vector2Int hereInt; // Vector2Int thereInt; int performantCycler = 0; bool patchObstructedNow = false; bool patchObstructedLastCycle = false; float startTime = Time.time; float distance = Vector2.Distance(transform.position, currentMostAppealingPatch); float ETA = 1.5f + 2 * distance / legs.GetSpeed(); sheepState = sheepBehaviors.goingToFood; // Debug.Log("Target patch " + distance + " away. ETA: " + ETA); while (Time.time - startTime < ETA) { // Debug.Log("On my way to " + currentMostAppealingPatch + ". " + (Time.time - startTime) + " elapsed. Now at " + (Vector2) transform.position); performantCycler = (performantCycler + 1) % 20; if (performantCycler % 2 == 0) { if (performantCycler == 0) { patchObstructedLastCycle = patchObstructedNow; patchObstructedNow = !legs.isNavigable(currentMostAppealingPatch); } if (gameState.getPatchValue(Mathf.FloorToInt(currentMostAppealingPatch.x), Mathf.FloorToInt(currentMostAppealingPatch.y)) < 1) { // Debug.Log((Vector2) transform.position + ": patch is baren"); break; } else if (patchObstructedLastCycle == true && patchObstructedNow == true) { // Debug.Log((Vector2) transform.position + ": patch obstructed"); break; } } yield return(new WaitForSeconds(0.1f)); } // Debug.Log("Timed out: " + Time.time + ", " + startTime); StartCoroutine(idle()); yield return(null); }
IEnumerator GoForSafety() { // Debug.Log("GoForSafety"); Vector2 randomOffset = new Vector2(Random.Range(-1 * flockSizeFactor, flockSizeFactor), Random.Range(-1 * flockSizeFactor, flockSizeFactor)); Vector2 safeSpotRelative = randomOffset + noDogsHere + (flockCenter - (Vector2)transform.position); if (shepherdMultiplier <= 1) { // this is equivalent to reducing the magnitude of safeSpotRelative by flocksizefactor safeSpotRelative *= Mathf.Clamp((safeSpotRelative.magnitude - flockSizeFactor) / safeSpotRelative.magnitude, 0, 1); } float toGo = safeSpotRelative.magnitude; float ETA = toGo / legs.GetSpeed(); Vector2 safeSpotAbsolute = safeSpotRelative + (Vector2)transform.position; // Debug.Log("running " + safeSpotRelative + "to safety. Expected to go " + toGo + " distance, at a speed of " + tempSpeed); float tempSpeed = Mathf.Clamp(2 + toGo * 0.1f, 2, 6); thisSheep.Move(safeSpotAbsolute, -1, tempSpeed, -1f); sheepState = sheepBehaviors.goingToSafety; yield return(new WaitForSeconds(ETA * 2)); StartCoroutine(idle()); yield return(null); }