void Start() { this.SetDefaultFromThis(ref audioSource); var sub1 = container.content.ObserveCountChanged(notifyCurrentCount: true) .Subscribe(count => { if (state == FiloState.Gathering && count >= maxCount) { state = FiloState.SettleBeforeSwitchingDown; Observable.Timer(TimeSpan.FromSeconds(switchDownSoundDelay)) .Subscribe((l) => { audioSource.PlayOneShot(audioClip); }); } }); var sub2 = container.content.ObserveCountChanged(notifyCurrentCount: true) .Subscribe(count => { if (count == 0 && state == FiloState.Releasing) { state = FiloState.SwitchingUp; startTime = PausableTime.Instance.Time; Observable.Timer(TimeSpan.FromSeconds(switchUpSoundDelay)) .Subscribe((l) => { audioSource.PlayOneShot(audioClip); }); } }); AddSubscriptions(sub1, sub2); }
void FixedUpdate() { if (PausableTime.Instance.IsPaused) { return; } if (state == FiloState.SettleBeforeSwitchingDown) { if (container.content.All(item => item.GetComponent <Rigidbody>().IsSleeping())) { state = FiloState.SwitchingDown; eulerAngles = transform.localEulerAngles; startTime = PausableTime.Instance.Time; } } if (state == FiloState.SwitchingDown) { SetRotation(Mathf.LerpAngle(upPositionAngle, downPositionAngle, Mathf.Pow(TimeRatio, 2))); if (CurrentTime >= switchTime) { SetRotation(downPositionAngle); state = FiloState.Releasing; } } if (state == FiloState.SwitchingUp) { SetRotation(Mathf.Lerp(downPositionAngle, upPositionAngle, Mathf.Pow(TimeRatio, 2))); if (CurrentTime >= switchTime) { SetRotation(upPositionAngle); state = FiloState.Gathering; } } if (state == FiloState.Releasing) { foreach (var item in container.content.Take(container.content.Count - 1)) { var body = item.GetComponent <Rigidbody>(); body.AddForce(-body.velocity * slowDownFactor, ForceMode.VelocityChange); } } }