Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using BecomeAKing.Mathematics;
  4. using BecomeAKing.Mathematics.Collisions;
  5.  
  6. namespace BecomeAKing {
  7.     public sealed partial class AdvancedRigidbody2D {
  8.         private const float FALL_START_Y_VELOCITY = -0.11F;
  9.         private const float FALL_FORCE_ACCELERATION_OFFSET = 0.28F;
  10.         private const float FALL_RECOVERY_START_IMPULSE = 32F;
  11.  
  12.         public delegate void FallAction ();
  13.         // Propagated when the fall process begins.
  14.         public event FallAction OnFallStart;
  15.         // Propagated during the whole fall process.
  16.         public event FallAction OnFallPersist;
  17.         // Propagated when the whole fall process ends.
  18.         public event FallAction OnFallEnd;
  19.         // Propagated when the body starts falling because of gravity.
  20.         public event FallAction OnFallForceStart;
  21.         // Propagated while the body is falling because of gravity.
  22.         public event FallAction OnFallForcePersist;
  23.         // Propagated when the body stops falling.
  24.         public event FallAction OnFallForceEnd;
  25.         // Propagated when the body starts recovering after the fall.
  26.         // The body starts recovering if the impulse of the fall is > FALL_RECOVERY_START_IMPULSE
  27.         // and if fallRecoveryTime > 0.
  28.         public event FallAction OnFallRecoveryStart;
  29.         // Propagated during the whole fall recovering process.
  30.         public event FallAction OnFallRecoveryPersist;
  31.         // Propagated when the body ends recovering.
  32.         public event FallAction OnFallRecoveryEnd;
  33.  
  34.         // Used as flag to enable fall detection.
  35.         private bool detectFall;
  36.         // Used as flag set to true during the whole fall process.
  37.         private bool isFalling;
  38.         // Used as flag set to true while the body has fall force.
  39.         private bool hasFallForce;
  40.         // Used as flag set to true during the whole fall recovery process.
  41.         private bool isFallRecovering;
  42.         // Used as flag set to true when the fall is recovered.
  43.         private bool isFallRecovered;
  44.         // Used as fall recovery time.
  45.         private float fallRecoveryTime;
  46.  
  47.         public bool DetectFall {
  48.             get {
  49.                 return this.detectFall;
  50.             }
  51.  
  52.             set {
  53.                 this.detectFall = value;
  54.             }
  55.         }
  56.  
  57.         public bool IsFalling {
  58.             get {
  59.                 return this.isFalling;
  60.             }
  61.         }
  62.  
  63.         public bool HasFallForce {
  64.             get {
  65.                 return this.hasFallForce;
  66.             }
  67.         }
  68.  
  69.         public bool IsFallRecovering {
  70.             get {
  71.                 return this.isFallRecovering;
  72.             }
  73.         }
  74.  
  75.         public float FallRecoveryTime {
  76.             get {
  77.                 return this.fallRecoveryTime;
  78.             }
  79.  
  80.             set {
  81.                 this.fallRecoveryTime = value;
  82.             }
  83.         }
  84.  
  85.         private void VerifyFall () {
  86.             if (this.detectFall) {
  87.                 if (this.body.velocity.y <= FALL_START_Y_VELOCITY) {
  88.                     if (!this.hasFallForce) {
  89.                         this.isFalling = true;
  90.  
  91.                         this.OnFallStart?.Invoke();
  92.  
  93.                         this.hasFallForce = true;
  94.  
  95.                         this.OnFallForceStart?.Invoke();
  96.  
  97.                         return; // Jump to next FixedUpdate.
  98.                     }
  99.                     else {
  100.                         this.VerticalForce -= FALL_FORCE_ACCELERATION_OFFSET + this.body.mass;
  101.  
  102.                         this.OnFallForcePersist?.Invoke();
  103.                     }
  104.                 }
  105.                 else if (
  106.                     this.hasFallForce &&
  107.                     this.colliderObserver.IsColliding(TagType.Platform)
  108.                 ) {
  109.                     float impulse = this.colliderObserver.FindCollision(TagType.Platform).GetImpulse();
  110.                     this.hasFallForce = false;
  111.  
  112.                     this.OnFallForceEnd?.Invoke();
  113.  
  114.                     if (this.fallRecoveryTime > 0F && impulse > FALL_RECOVERY_START_IMPULSE) {
  115.                         StartCoroutine(this.RecoverFall());
  116.  
  117.                         return; // Jump to next FixedUpdate.
  118.                     }
  119.                     else {
  120.                         this.isFallRecovered = true;
  121.                     }
  122.                 }
  123.  
  124.                 if (this.isFallRecovering) {
  125.                     this.OnFallRecoveryPersist?.Invoke();
  126.                 }
  127.                 else if (this.isFallRecovered) {
  128.                     this.isFallRecovered = false;
  129.                     this.isFalling = false;
  130.  
  131.                     this.OnFallEnd?.Invoke();
  132.                 }
  133.  
  134.                 if (this.isFalling) {
  135.                     this.OnFallPersist?.Invoke();
  136.                 }
  137.             }
  138.         }
  139.  
  140.         private IEnumerator RecoverFall () {
  141.             this.OnFallRecoveryStart?.Invoke();
  142.  
  143.             this.isFallRecovering = true;
  144.  
  145.             yield return new WaitForSeconds(this.fallRecoveryTime);
  146.  
  147.             this.isFallRecovering = false;
  148.             this.isFallRecovered = true;
  149.  
  150.             this.OnFallRecoveryEnd?.Invoke();
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement