Advertisement
Purianite

Clarity - New Player Class (WIP)

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