Advertisement
Purianite

Clarity - Old Player Class

Jan 2nd, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.78 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(Controller2D))]
  5. public class Player : MonoBehaviour
  6. {
  7.     //Jump variables
  8.     public float maxJumpHeight = 3;
  9.     public float minJumpHeight = 0.6f;
  10.     public float timeToJumpApex = 0.6f;
  11.     //float accelerationTimeGrounded = 0.03f;
  12.  
  13.     public float moveSpeed = 6;
  14.  
  15.     //Walljump variables
  16.     public float wallSlideSpeedMax = 3.5f;
  17.     public float wallJumpSpeed = 4;
  18.     public float wallJumpTime = 0.5f;
  19.     public float wallJumpFactor = 0.83f;
  20.     public float doubleJumpFactor = 0.83f;
  21.     public bool canMove = true;
  22.     public bool canJump = true;
  23.     public bool canDoubleJump = true;
  24.     public bool facingLocked = false;
  25.  
  26.     public enum abilities
  27.     {
  28.         SlideKick, //0
  29.         ChargeAttack, //1
  30.         Airdash, //2
  31.         DoubleJump, //3
  32.         WallJump, //4
  33.         BarrierKey, //5
  34.         DoubleAirdash, //6
  35.         SuperJump, //7
  36.         FlashStep //8
  37.     }
  38.  
  39.     public bool[] hasAbility;
  40.  
  41.     float gravity;
  42.     float maxJumpVelocity;
  43.     float minJumpVelocity;
  44.     Vector3 velocity;
  45.     Vector3 shiftVelocity;
  46.     Vector3 spriteScale;
  47.     public float facing = 1;
  48.     //float velocityXSmoothing;
  49.     float shiftTimer = 0;
  50.  
  51.     //TODO: Rewrite combat code
  52.     /*
  53.      *Basic setup:
  54.      *Startup frames
  55.      *Are there any wind-up active frames? If so, enter them, otherwise skip to swing active frames.
  56.      *Go to swing active frames after wind-up active frames
  57.      *Enter delay frames
  58.     */
  59.     //Combat/Shift related
  60.     public float[] comboDamage; //Damage of the attack
  61.     public float[] comboLength; //Minimum length of the attack
  62.     public float[] comboShift; //shiftVelocity of the attack
  63.     public float[] comboMove; //Duration of moving at comboShift during attack
  64.     public float[] comboDelay; //For setting attackWindow
  65.     public float[] windUpTime; //Wind-up of attack
  66.     //public int[] hitboxes; //Number of hitboxes per attack
  67.     public Vector2[] windUpHitboxSizes; //Size of hitboxes
  68.     public Vector2[] swingHitboxSizes;
  69.     public Vector2[] windUpHitboxPositions; //Positions of hitboxes
  70.     public Vector2[] swingHitboxPositions;
  71.     public bool canAttack = true;
  72.     public int maxCombo = 3; //Number of combo hits - 1
  73.     PlayerMelee meleeAttack; //For assigning our melee object
  74.     //float[] swingTime; //Swing of attack
  75.     float windUpTimer; //Time of wind-up, switch to swing after
  76.     int comboPosition = 0; //Position in the combo
  77.     int dashDoubleTap = 0;
  78.     float doubleTapDir = 0; //Direction of double tap
  79.     float attackWindow = 0; //Set to comboLength upon attacking
  80.     float delayWindow = 0; //How long the player can delay an attack without ending the combo
  81.     float lastXInput; //Since there's no GetAxisDown
  82.     float lastFacing; //So we moonwalk instead of backstep
  83.     float baseGravity; //So we have something constant to revert gravity to after airdashing
  84.     float dashCooldown; //Added delay after dashing on the ground
  85.     bool airAttacked; //Did the player attack in the air?
  86.     bool airDodged; //Has the player dodged while airborne?
  87.     bool backflipped; //Did the player backflip? In this case, we need to disable jump charging.
  88.     bool isAttacking = false;
  89.  
  90.     //Dodge/dash move variables
  91.     public bool canDodge = true;
  92.     public float dodgeLength = 0.2f; //In seconds
  93.     public float dodgeSpeed = 9;
  94.     public float backflipSpeed = 7;
  95.     public float backflipLength = 0.25f;
  96.     public float backflipHeight = 7;
  97.     public float airdashSpeed = 10;
  98.     public float airdashLength = 0.2f;
  99.     public float airdashCooldown = 0.05f;
  100.     public float groundDashCooldown = 0.15f; //to curb mashing
  101.     public bool onGround; //Are we on the ground?
  102.     float dodgeAxis = 0.0f;
  103.     public bool wallSliding = false;
  104.     int wallDirX;
  105.  
  106.     Controller2D controller;
  107.  
  108.     // Use this for initialization
  109.     void Start ()
  110.     {                
  111.         controller = GetComponent<Controller2D>();
  112.  
  113.         gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2);
  114.         maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
  115.         minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
  116.         print("Gravity: " + gravity + " Jump Velocity: " + maxJumpVelocity);
  117.         spriteScale = transform.localScale;
  118.         baseGravity = gravity;
  119.  
  120.         //Probably not necessary anymore, but holding onto it just in case
  121.         //Get swing times from wind-up times
  122.         /*
  123.         swingTime = new float[windUpTime.Length];
  124.         for (int i = 0; i < windUpTime.Length; i++)
  125.         {
  126.             swingTime[i] = comboLength[i] - windUpTime[i];
  127.         }*/
  128.  
  129.         meleeAttack = transform.Find ("PlayerMelee").GetComponent<PlayerMelee>();
  130.         meleeAttack.gameObject.SetActive(false);        
  131.     }
  132.    
  133.     // Update is called once per frame
  134.     void Update ()
  135.     {
  136.         //Get input axis
  137.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  138.  
  139.         //Check wall collision direction
  140.         wallDirX = (controller.collisions.left) ? -1 : 1;
  141.  
  142.         //Check if on the ground
  143.         onGround = controller.collisions.below;
  144.  
  145.         //If wall jumping can be done
  146.         if (hasAbility[(int) abilities.WallJump] && (controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
  147.         {
  148.             wallSliding = true;
  149.            
  150.             if (velocity.y < -wallSlideSpeedMax)
  151.             {
  152.                 velocity.y = -wallSlideSpeedMax;
  153.             }
  154.         }
  155.         else wallSliding = false;
  156.  
  157.         if (controller.collisions.above || controller.collisions.below)
  158.         {
  159.             velocity.y = 0;
  160.             //doubleTapDir = 0;
  161.         }
  162.        
  163.         dodgeAxis = Input.GetAxis("Dodge");
  164.  
  165.         if (Input.GetButtonDown("Jump") /*&& controller.collisions.below*/ && canJump && input.y >= -0.5)
  166.         {
  167.             print ("Jump");
  168.             if (wallSliding)
  169.             {
  170.                 shiftTimer = wallJumpTime;
  171.                 shiftVelocity.x = wallJumpSpeed * -wallDirX * facing;
  172.                 //airDodged = false;
  173.                 velocity.y = maxJumpVelocity * wallJumpFactor;
  174.                 print(shiftVelocity.x);
  175.             }
  176.             if (controller.collisions.below)
  177.             {
  178.                 velocity.y = maxJumpVelocity;
  179.                 //canDoubleJump = true;
  180.             }
  181.  
  182.             if (!controller.collisions.below && !wallSliding && canDoubleJump && hasAbility[(int) abilities.DoubleJump])
  183.             {
  184.                 velocity.y = maxJumpVelocity * doubleJumpFactor;
  185.                 canDoubleJump = false;
  186.             }
  187.         }
  188.  
  189.         if (Input.GetButtonUp("Jump"))
  190.         {
  191.             if (velocity.y > minJumpVelocity)
  192.             {
  193.                 velocity.y = minJumpVelocity;
  194.             }
  195.         }
  196.  
  197.         //Hold button during ascent to jump higher
  198.         /*
  199.         if (Input.GetButton("Jump") && !controller.collisions.below && velocity.y > 0 && !backflipped)
  200.         {
  201.             //velocity.y += addJumpVelocity * Time.deltaTime;
  202.         }*/
  203.  
  204.         //Old forward slide
  205.         /*
  206.         if (input.y < 0 && Input.GetButtonDown("Jump"))
  207.         {
  208.             if (controller.collisions.below)
  209.             {
  210.                 print("Forward Slide");
  211.                 canDodge = false;
  212.                 canJump = false;
  213.                 canMove = false;
  214.                 shiftTimer = dodgeLength;
  215.                 shiftVelocity.x = dodgeSpeed;
  216.             }
  217.         }*/
  218.  
  219.         //Dash/dodge moves (all tied to canDodge)
  220.  
  221.         if (Input.GetButtonDown("Dodge") /*&& input.x != lastXInput*/ && canMove && canDodge)
  222.         {
  223.             //facingLocked = true;
  224.             print(dodgeAxis);
  225.             if (controller.collisions.below)
  226.             {
  227.                 //Dodge code for ground
  228.                 //Slide kick if input is forward relative to facing
  229.                 if (facing * dodgeAxis < 0)
  230.                 {
  231.                     print("Forward Slide");
  232.                     canDodge = false;
  233.                     canJump = false;
  234.                     canMove = false;
  235.                     shiftTimer = dodgeLength;
  236.                     shiftVelocity.x = dodgeSpeed;
  237.                 }
  238.                 //Backstep if input is backward relative to facing
  239.                 else if (facing * dodgeAxis > 0)
  240.                 {
  241.                     print("Backstep");
  242.                     canDodge = false;
  243.                     canJump = false;
  244.                     canMove = false;
  245.                     shiftTimer = dodgeLength;
  246.                     shiftVelocity.x = -dodgeSpeed;
  247.                 }                
  248.             }
  249.             else if (hasAbility[(int) abilities.Airdash] && !controller.collisions.below)
  250.             {
  251.                 //Dodge code for air
  252.                 //Airdash if input is forward relative to facing
  253.                 if (facing * dodgeAxis < 0)
  254.                 {
  255.                     print("Airdash");
  256.                     canDodge = false;
  257.                     canJump = false;
  258.                     canMove = false;
  259.                     shiftTimer = airdashLength;
  260.                     shiftVelocity.x = airdashSpeed;
  261.                     gravity = 0;
  262.                     velocity.y = 0;
  263.                     airDodged = true;
  264.                 }
  265.                 //Backflip if input is backward relative to facing
  266.                 else if (facing * dodgeAxis > 0 && velocity.y > 0)
  267.                 {
  268.                     print("Backflip");
  269.                     canDodge = false;
  270.                     canJump = false;
  271.                     canMove = false;
  272.                     shiftTimer = backflipLength;
  273.                     shiftVelocity.x = -backflipSpeed;
  274.                     velocity.y = backflipHeight;
  275.                     backflipped = true;
  276.                     airDodged = true;
  277.                 }
  278.             }
  279.         }
  280.  
  281.         //Unlock facing if Shift isn't pressed
  282.         /*
  283.         if (!Input.GetKey(KeyCode.LeftShift))
  284.         {
  285.             facingLocked = false;
  286.         } */    
  287.  
  288.         //Attack input        
  289.         if (Input.GetButtonDown("Attack") && canAttack && comboPosition < maxCombo)
  290.         {
  291.             canDodge = false; //No dodge canceling attacks
  292.             canAttack = false; //So you don't attack while attacking.
  293.             isAttacking = true;
  294.             meleeAttack.gameObject.SetActive(true);
  295.  
  296.             //Ground attack
  297.             if (controller.collisions.below)
  298.             {
  299.                 canMove = false; //Can't move during ground attacks, can during aerial
  300.                 canJump = false; //Can't break out of an attack with a jump
  301.                 shiftTimer = comboMove[comboPosition];
  302.                 shiftVelocity.x = comboShift[comboPosition];
  303.                 attackWindow = comboLength[comboPosition];
  304.                 delayWindow = comboDelay[comboPosition];
  305.                 meleeAttack.damage = comboDamage[comboPosition];
  306.                 meleeAttack.ResizeHitbox(windUpHitboxSizes[comboPosition]);
  307.                 meleeAttack.MoveHitbox(windUpHitboxPositions[comboPosition]);
  308.                 windUpTimer = windUpTime[comboPosition];
  309.             }
  310.             //Airborne attacks, ascending and descending
  311.             else if (!controller.collisions.below && !airAttacked)
  312.             {
  313.                 airAttacked = true;
  314.                 if (velocity.y > 0)
  315.                 {
  316.                     print("Ascending air attack");                    
  317.                 }
  318.                 else
  319.                 {
  320.                     print("Descending air attack");                    
  321.                 }
  322.             }
  323.         }
  324.  
  325.         //Backstep/Backflip
  326.         /*
  327.         if (Input.GetKeyDown(KeyCode.C) && canDodge)
  328.         {
  329.             if (controller.collisions.below)
  330.             {
  331.                 print("Backstep");
  332.                 canDodge = false;
  333.                 canJump = false;
  334.                 canMove = false;
  335.                 shiftTimer = dodgeLength;
  336.                 shiftVelocity.x = -dodgeSpeed;
  337.             }
  338.             else if (!controller.collisions.below && velocity.y > 0)
  339.             {
  340.                 print("Backflip");
  341.                 canDodge = false;
  342.                 canJump = false;
  343.                 canMove = false;
  344.                 shiftTimer = backflipLength;
  345.                 shiftVelocity.x = -backflipSpeed;
  346.                 velocity.y = backflipHeight;
  347.                 backflipped = true;
  348.                 airDodged = true;
  349.             }
  350.         }*/
  351.  
  352.         if (windUpTimer > 0)
  353.         {
  354.             windUpTimer -= Time.deltaTime;
  355.             if (windUpTimer <= 0)
  356.             {
  357.                 meleeAttack.ResizeHitbox(swingHitboxSizes[comboPosition]);
  358.                 meleeAttack.MoveHitbox(swingHitboxPositions[comboPosition]);
  359.             }
  360.         }
  361.  
  362.         //Attack window timer
  363.         if (attackWindow > 0)
  364.         {
  365.             attackWindow -= Time.deltaTime;
  366.             if (attackWindow <= 0)
  367.             {
  368.                 meleeAttack.gameObject.SetActive(false);
  369.                 if (comboPosition < maxCombo)
  370.                 {
  371.                     comboPosition += 1;
  372.                     canAttack = true;
  373.                 }                                
  374.             }
  375.         }
  376.  
  377.         //Attack delay timer
  378.         if (delayWindow > 0)
  379.         {
  380.             delayWindow -= Time.deltaTime;
  381.             if (delayWindow <= 0)
  382.             {
  383.                 //if (comboPosition == maxCombo)
  384.                 //{
  385.                     comboPosition = 0;
  386.                     canAttack = true;
  387.                 //}  
  388.                 if (!canDodge)
  389.                 {
  390.                     canDodge = true;
  391.                 }
  392.                 canMove = true;
  393.                 canJump = true;
  394.                 isAttacking = false;
  395.             }
  396.         }
  397.  
  398.         //Shift timer countdown
  399.         if (shiftTimer > 0)
  400.         {
  401.             shiftTimer -= Time.deltaTime;
  402.             if (shiftTimer <= 0)
  403.             {
  404.                 shiftVelocity.x = 0;
  405.                 if (!canDodge && controller.collisions.below)
  406.                 {
  407.                     dashCooldown = groundDashCooldown;
  408.                 }
  409.                 else if (!canDodge && !controller.collisions.below)
  410.                 {
  411.                     dashCooldown = airdashCooldown;
  412.                 }
  413.                 gravity = baseGravity;
  414.                 if (backflipped)
  415.                 {
  416.                     //velocity.y = 0;
  417.                     backflipped = false;
  418.                 }
  419.                 //canMove = true;
  420.             }
  421.         }
  422.  
  423.         if (dashCooldown > 0)
  424.         {
  425.             dashCooldown -= Time.deltaTime;
  426.             if (dashCooldown <= 0 && !isAttacking)
  427.             {
  428.                 print("Dash cooldown allowed movement again.");
  429.                 canMove = true;
  430.                 canJump = true;
  431.                 //print("You can move again.");
  432.                 if (!airDodged)
  433.                 {
  434.                     //print ("You can dodge again.");
  435.                     //canJump = true;
  436.                     canDodge = true;
  437.                 }
  438.             }
  439.         }
  440.  
  441.         //Airdash by double tap
  442.         //Check if in air and analog stick/d-pad are pressed
  443.         /*
  444.         if (!controller.collisions.below && input.x != 0)
  445.         {
  446.             if (lastXInput == 0)
  447.             {
  448.                 dashDoubleTap += 1;
  449.                 if (dashDoubleTap == 2)
  450.                 {  
  451.                     print("Airdash");
  452.                     canDodge = false;
  453.                     canJump = false;
  454.                     canMove = false;
  455.                     shiftTimer = airdashLength;
  456.                     shiftVelocity.x = airdashSpeed;
  457.                     gravity = 0;
  458.                     velocity.y = 0;
  459.                     airDodged = true;
  460.                     dashDoubleTap = 0;
  461.                 }
  462.             }
  463.         }*/
  464.        
  465.         //If attack used in the air, re-enable dodging and attacking (temp?)
  466.         if (airAttacked && controller.collisions.below)
  467.         {
  468.             canAttack = true;
  469.             canDodge = true;
  470.             airAttacked = false;
  471.             //canJump = true;
  472.         }
  473.  
  474.         //If airdash or backflip used in the air, re-enable dodging and movement
  475.         if (airDodged && controller.collisions.below)
  476.         {
  477.             canMove = true;
  478.             canJump = true;
  479.             canDodge = true;
  480.             airDodged = false;
  481.             //print ("Landed from air dodge");
  482.         }
  483.  
  484.         //Reset double jump upon landing
  485.         if (!canDoubleJump && controller.collisions.below)
  486.         {
  487.             canDoubleJump = true;
  488.         }
  489.  
  490.         //Instant movement if shifting isn't active
  491.         if (shiftVelocity.x != 0)
  492.         {
  493.             velocity.x = shiftVelocity.x * facing;            
  494.  
  495.             //Debug.Log("Shifting");
  496.         }
  497.         else
  498.         {
  499.             if (canMove)
  500.             {
  501.                 if (input.x != 0)
  502.                 {
  503.                     velocity.x = Mathf.Sign(input.x) * moveSpeed;
  504.                 }
  505.                 else
  506.                 {
  507.                     velocity.x = input.x * moveSpeed;
  508.                 }
  509.             }
  510.             else
  511.             {
  512.                 velocity.x = 0;
  513.             }
  514.             if (velocity.x != 0)
  515.             {
  516.                 if (!facingLocked && Mathf.Abs(input.x) >= 0.25)
  517.                 {
  518.                     facing = Mathf.Sign(input.x);
  519.                     spriteScale.x = facing;
  520.                     transform.localScale = spriteScale;
  521.                 }
  522.             }
  523.             //print("Facing: " + facing);
  524.         }
  525.         //Smooth damping
  526.         /*
  527.         float targetVelocityX = input.x * moveSpeed;
  528.         velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
  529.         */
  530.         velocity.y += gravity * Time.deltaTime;
  531.         controller.Move(velocity * Time.deltaTime, input);        
  532.         lastXInput = input.x;      
  533.         lastFacing = facing;        
  534.     }
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement