Guest User

PsuedoJumper

a guest
Jun 12th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.35 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class PsuedoJumper : MonoBehaviour
  6. {
  7.     [Tooltip("The body that is being controlled to jump.")]
  8.     public PsuedoBody2D PsuedoBody2D;
  9.  
  10.     [Tooltip("When the body lands a jump, this event will be executed.")]
  11.     public GameEventController OnLandingEvent;
  12.  
  13.     [Tooltip("Normally, gravity is disabled during the jump and re-set to it's previous value upon finishing. If this is set, gravity will be turned enabled at landing regardless.")]
  14.     public bool ForceGravityOnAtLanding = false;
  15.  
  16.     [Header("_TESTING_")]
  17.     public bool TEST_Jump = false;
  18.     public Vector3 TEST_Target = Vector3.zero;
  19.     public float TEST_Speed;
  20.     public float TEST_ArcHeight;
  21.     public bool TEST_AllowJumpingInAir = true;
  22.     public float TEST_TargetHeight;
  23.  
  24.  
  25.     /// <summary>
  26.     /// Whether or not the PsuedoJumper is currently jumping.
  27.     /// </summary>
  28.     public bool IsJumping { get; private set; }
  29.  
  30.     /// <summary>
  31.     /// The height that the PsuedoBody2D is at.
  32.     /// </summary>
  33.     public float Height
  34.     {
  35.         get { return PsuedoBody2D.Height; }
  36.     }
  37.  
  38.     #region Private fields
  39.  
  40.     /// <summary>
  41.     /// The point in the world space that the body takes off from.
  42.     /// </summary>
  43.     private Vector3 _positionTakeOff;
  44.  
  45.     /// <summary>
  46.     /// The point in the world space that the body will land on.
  47.     /// </summary>
  48.     private Vector3 _positionLanding;
  49.  
  50.     /// <summary>
  51.     /// The total seconds the entire jump will take.
  52.     /// </summary>
  53.     private float _speed;
  54.  
  55.     /// <summary>
  56.     /// The time that the jump started.
  57.     /// </summary>
  58.     private float _startTime;
  59.  
  60.     /// <summary>
  61.     /// The parabolic function that represents the height of the body throughout the course of the jump.
  62.     /// </summary>
  63.     private Func<float, float> _parabolicFunction;
  64.  
  65.     /// <summary>
  66.     /// Whether gravity was enabled or not for the PsuedoBody2D wehn the jump started.
  67.     /// </summary>
  68.     private bool _gravitySetting;
  69.  
  70.     #endregion
  71.  
  72.     void Start ()
  73.     {
  74.         if (PsuedoBody2D == null)
  75.         {
  76.             PsuedoBody2D = GetComponent<PsuedoBody2D>();
  77.         }
  78.  
  79.         IsJumping = false;
  80.     }
  81.    
  82.     void Update ()
  83.     {
  84.         if (TEST_Jump)
  85.         {
  86.             TEST_Jump = false;
  87.             JumpToLocation(TEST_Target, TEST_ArcHeight, TEST_Speed, TEST_AllowJumpingInAir, TEST_TargetHeight);
  88.         }
  89.  
  90.         if (!IsJumping)
  91.         {
  92.             return;
  93.         }
  94.  
  95.         float t = _speed == 0 ? 1 : Mathf.Clamp01(((Time.time - _startTime)/_speed));
  96.  
  97.         Vector3 position = Vector3.Lerp(_positionTakeOff, _positionLanding, t);
  98.         PsuedoBody2D.GroundPoint.position = position;
  99.            
  100.         float height = _parabolicFunction(t);
  101.  
  102.         PsuedoBody2D.Height = height;
  103.            
  104.         IsJumping = t < 1;
  105.  
  106.         if (IsJumping)
  107.         {
  108.             PsuedoBody2D.EnableGravity = false;
  109.         }
  110.         else
  111.         {
  112.             PsuedoBody2D.EnableGravity = ForceGravityOnAtLanding || _gravitySetting;
  113.             GameEventController.Execute(OnLandingEvent);
  114.         }
  115.     }
  116.  
  117.     /// <summary>
  118.     /// Jumps the PsuedoBody to the target location in the given number of seconds.
  119.     /// </summary>
  120.     /// <param name="target">Where to jump to.</param>
  121.     /// <param name="arcHeightTip">The height of the arc used to jump.</param>
  122.     /// <param name="speedInSeconds">How long to take for the jump.</param>
  123.     /// <param name="allowAirborneJumping">If set to true, you can jump while already in the air, otherwise you must wait until the body is grounded.</param>
  124.     /// <param name="targetHeight">The height that the jumper is trying to reach.</param>
  125.     public void JumpToLocation(Vector3 target, float arcHeightTip, float speedInSeconds, bool allowAirborneJumping = false, float targetHeight = 0f)
  126.     {
  127.         if (PsuedoBody2D == null)
  128.         {
  129.             Debug.LogError("There is no PsuedoBody2D for this PsuedoJumper");
  130.             return;
  131.         }
  132.  
  133.         if(!allowAirborneJumping && !PsuedoBody2D.IsGrounded)
  134.         {
  135.             return;
  136.         }
  137.        
  138.         _speed = speedInSeconds;
  139.         _startTime = Time.time;
  140.         _positionLanding = target;
  141.         _positionTakeOff = PsuedoBody2D.GroundPoint.position;
  142.         if (PsuedoBody2D.IsGrounded)
  143.         {
  144.             _gravitySetting = PsuedoBody2D.EnableGravity;
  145.         }
  146.         _parabolicFunction = GetParabolicFunction(PsuedoBody2D.Height, arcHeightTip, targetHeight);
  147.  
  148.         IsJumping = true;
  149.     }
  150.  
  151.     /// <summary>
  152.     /// Creates a function that represents the height of the PsuedoBody2D at a given percentage of it's journey.
  153.     /// </summary>
  154.     /// <param name="heightTakeOff">The height the body is at during takeoff.</param>
  155.     /// <param name="heightArcTip">The tip of the arc for the travel.</param>
  156.     /// <param name="heightLanding">The height of the landing.</param>
  157.     /// <returns>Equation that when fed values from [0,1] returns the height the body should be at during that percentage of the journey.</returns>
  158.     Func<float, float> GetParabolicFunction(float heightTakeOff, float heightArcTip, float heightLanding)
  159.     {
  160.         float a = 2 * heightTakeOff - 4 * heightArcTip + 2 * heightLanding;
  161.         float b = -3 * heightTakeOff + 4 * heightArcTip - heightLanding;
  162.         float c = heightTakeOff;
  163.  
  164.         return (i) => (a * i * i + b * i + c);
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment