Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using BecomeAKing.Mathematics;
- using BecomeAKing.Mathematics.Collisions;
- namespace BecomeAKing {
- public sealed partial class AdvancedRigidbody2D {
- private const float FALL_START_Y_VELOCITY = -0.11F;
- private const float FALL_FORCE_ACCELERATION_OFFSET = 0.28F;
- private const float FALL_RECOVERY_START_IMPULSE = 32F;
- public delegate void FallAction ();
- // Propagated when the fall process begins.
- public event FallAction OnFallStart;
- // Propagated during the whole fall process.
- public event FallAction OnFallPersist;
- // Propagated when the whole fall process ends.
- public event FallAction OnFallEnd;
- // Propagated when the body starts falling because of gravity.
- public event FallAction OnFallForceStart;
- // Propagated while the body is falling because of gravity.
- public event FallAction OnFallForcePersist;
- // Propagated when the body stops falling.
- public event FallAction OnFallForceEnd;
- // Propagated when the body starts recovering after the fall.
- // The body starts recovering if the impulse of the fall is > FALL_RECOVERY_START_IMPULSE
- // and if fallRecoveryTime > 0.
- public event FallAction OnFallRecoveryStart;
- // Propagated during the whole fall recovering process.
- public event FallAction OnFallRecoveryPersist;
- // Propagated when the body ends recovering.
- public event FallAction OnFallRecoveryEnd;
- // Used as flag to enable fall detection.
- private bool detectFall;
- // Used as flag set to true during the whole fall process.
- private bool isFalling;
- // Used as flag set to true while the body has fall force.
- private bool hasFallForce;
- // Used as flag set to true during the whole fall recovery process.
- private bool isFallRecovering;
- // Used as flag set to true when the fall is recovered.
- private bool isFallRecovered;
- // Used as fall recovery time.
- private float fallRecoveryTime;
- public bool DetectFall {
- get {
- return this.detectFall;
- }
- set {
- this.detectFall = value;
- }
- }
- public bool IsFalling {
- get {
- return this.isFalling;
- }
- }
- public bool HasFallForce {
- get {
- return this.hasFallForce;
- }
- }
- public bool IsFallRecovering {
- get {
- return this.isFallRecovering;
- }
- }
- public float FallRecoveryTime {
- get {
- return this.fallRecoveryTime;
- }
- set {
- this.fallRecoveryTime = value;
- }
- }
- private void VerifyFall () {
- if (this.detectFall) {
- if (this.body.velocity.y <= FALL_START_Y_VELOCITY) {
- if (!this.hasFallForce) {
- this.isFalling = true;
- this.OnFallStart?.Invoke();
- this.hasFallForce = true;
- this.OnFallForceStart?.Invoke();
- return; // Jump to next FixedUpdate.
- }
- else {
- this.VerticalForce -= FALL_FORCE_ACCELERATION_OFFSET + this.body.mass;
- this.OnFallForcePersist?.Invoke();
- }
- }
- else if (
- this.hasFallForce &&
- this.colliderObserver.IsColliding(TagType.Platform)
- ) {
- float impulse = this.colliderObserver.FindCollision(TagType.Platform).GetImpulse();
- this.hasFallForce = false;
- this.OnFallForceEnd?.Invoke();
- if (this.fallRecoveryTime > 0F && impulse > FALL_RECOVERY_START_IMPULSE) {
- StartCoroutine(this.RecoverFall());
- return; // Jump to next FixedUpdate.
- }
- else {
- this.isFallRecovered = true;
- }
- }
- if (this.isFallRecovering) {
- this.OnFallRecoveryPersist?.Invoke();
- }
- else if (this.isFallRecovered) {
- this.isFallRecovered = false;
- this.isFalling = false;
- this.OnFallEnd?.Invoke();
- }
- if (this.isFalling) {
- this.OnFallPersist?.Invoke();
- }
- }
- }
- private IEnumerator RecoverFall () {
- this.OnFallRecoveryStart?.Invoke();
- this.isFallRecovering = true;
- yield return new WaitForSeconds(this.fallRecoveryTime);
- this.isFallRecovering = false;
- this.isFallRecovered = true;
- this.OnFallRecoveryEnd?.Invoke();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement