private void SetNewPositions() { currentDirection = potentialDirection; Vector3 emptyPos; Vector3 targetPos = elements[0].position + potentialDirection; float maxDelay = iterationTime * (1 - waitingMovingRatio) * elasticityRatio; for (int i = 0; i < elements.Count; i++) { var item = elements[i]; emptyPos = item.position; float delay = maxDelay / elements.Count * i; var newAct = MovementAct.GetAct(); newAct.Destination = targetPos; newAct.StartPos = item.position; newAct.MovedObject = item; newAct.TimeInATrip = 0 - delay; newAct.TripDuration = iterationTime * (1 - waitingMovingRatio) - maxDelay; movements.Add(newAct); targetPos = emptyPos; } }
private IEnumerator MoveElements() { // needed to be stored for further lenghten of the snake if a fruit will get collected during an iteration var tailPos = elements[elements.Count - 1].position; while (movements.Count > 0) { for (int i = movements.Count - 1; i >= 0; i--) { var item = movements[i]; item.TimeInATrip += Time.deltaTime; float completeRatio = Mathf.Clamp01(item.TimeInATrip / item.TripDuration); float smoothedRatio = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f).Evaluate(completeRatio); item.MovedObject.position = Vector3.Lerp(item.StartPos, item.Destination, smoothedRatio); // rotation Quaternion newRotation = new Quaternion(); newRotation.SetLookRotation(item.Direction, Vector3.back); item.MovedObject.rotation = newRotation; // remove if reached destination if (completeRatio == 1) { MovementAct.Release(item); movements.RemoveAt(i); } } yield return(null); } // if a snake is shorter than should be due to a fruit collected or recent start - increase lenght by one element if (elements.Count < DesiredLenght) { var newElement = Instantiate(snakeElementPrefab, tailPos, Quaternion.identity); elements.Add(newElement.transform); } }
public static void Release(MovementAct movementAct) { _pool.Push(movementAct); }