Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- [RequireComponent(typeof(Controller2D))]
- public class Player : MonoBehaviour
- {
- //Jump variables
- public float maxJumpHeight = 3;
- public float minJumpHeight = 0.6f;
- public float timeToJumpApex = 0.6f;
- //float accelerationTimeGrounded = 0.03f;
- public float moveSpeed = 6;
- //Walljump variables
- public float wallSlideSpeedMax = 3.5f;
- public float wallJumpSpeed = 4;
- public float wallJumpTime = 0.5f;
- public float wallJumpFactor = 0.83f;
- public float doubleJumpFactor = 0.83f;
- public bool canMove = true;
- public bool canJump = true;
- public bool canDoubleJump = true;
- public bool facingLocked = false;
- public enum abilities
- {
- SlideKick, //0
- ChargeAttack, //1
- Airdash, //2
- DoubleJump, //3
- WallJump, //4
- BarrierKey, //5
- DoubleAirdash, //6
- SuperJump, //7
- FlashStep //8
- }
- public bool[] hasAbility;
- float gravity;
- float maxJumpVelocity;
- float minJumpVelocity;
- Vector3 velocity;
- Vector3 shiftVelocity;
- Vector3 spriteScale;
- public float facing = 1;
- //float velocityXSmoothing;
- float shiftTimer = 0;
- //TODO: Rewrite combat code
- /*
- *Basic setup:
- *Startup frames
- *Are there any wind-up active frames? If so, enter them, otherwise skip to swing active frames.
- *Go to swing active frames after wind-up active frames
- *Enter delay frames
- */
- //Combat/Shift related
- public float[] comboDamage; //Damage of the attack
- public float[] comboLength; //Minimum length of the attack
- public float[] comboShift; //shiftVelocity of the attack
- public float[] comboMove; //Duration of moving at comboShift during attack
- public float[] comboDelay; //For setting attackWindow
- public float[] windUpTime; //Wind-up of attack
- //public int[] hitboxes; //Number of hitboxes per attack
- public Vector2[] windUpHitboxSizes; //Size of hitboxes
- public Vector2[] swingHitboxSizes;
- public Vector2[] windUpHitboxPositions; //Positions of hitboxes
- public Vector2[] swingHitboxPositions;
- public bool canAttack = true;
- public int maxCombo = 3; //Number of combo hits - 1
- PlayerMelee meleeAttack; //For assigning our melee object
- //float[] swingTime; //Swing of attack
- float windUpTimer; //Time of wind-up, switch to swing after
- int comboPosition = 0; //Position in the combo
- int dashDoubleTap = 0;
- float doubleTapDir = 0; //Direction of double tap
- float attackWindow = 0; //Set to comboLength upon attacking
- float delayWindow = 0; //How long the player can delay an attack without ending the combo
- float lastXInput; //Since there's no GetAxisDown
- float lastFacing; //So we moonwalk instead of backstep
- float baseGravity; //So we have something constant to revert gravity to after airdashing
- float dashCooldown; //Added delay after dashing on the ground
- bool airAttacked; //Did the player attack in the air?
- bool airDodged; //Has the player dodged while airborne?
- bool backflipped; //Did the player backflip? In this case, we need to disable jump charging.
- bool isAttacking = false;
- //Dodge/dash move variables
- public bool canDodge = true;
- public float dodgeLength = 0.2f; //In seconds
- public float dodgeSpeed = 9;
- public float backflipSpeed = 7;
- public float backflipLength = 0.25f;
- public float backflipHeight = 7;
- public float airdashSpeed = 10;
- public float airdashLength = 0.2f;
- public float airdashCooldown = 0.05f;
- public float groundDashCooldown = 0.15f; //to curb mashing
- public bool onGround; //Are we on the ground?
- float dodgeAxis = 0.0f;
- public bool wallSliding = false;
- int wallDirX;
- Controller2D controller;
- // Use this for initialization
- void Start ()
- {
- controller = GetComponent<Controller2D>();
- gravity = -(2 * maxJumpHeight) / Mathf.Pow(timeToJumpApex, 2);
- maxJumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
- minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
- print("Gravity: " + gravity + " Jump Velocity: " + maxJumpVelocity);
- spriteScale = transform.localScale;
- baseGravity = gravity;
- //Probably not necessary anymore, but holding onto it just in case
- //Get swing times from wind-up times
- /*
- swingTime = new float[windUpTime.Length];
- for (int i = 0; i < windUpTime.Length; i++)
- {
- swingTime[i] = comboLength[i] - windUpTime[i];
- }*/
- meleeAttack = transform.Find ("PlayerMelee").GetComponent<PlayerMelee>();
- meleeAttack.gameObject.SetActive(false);
- }
- // Update is called once per frame
- void Update ()
- {
- //Get input axis
- Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
- //Check wall collision direction
- wallDirX = (controller.collisions.left) ? -1 : 1;
- //Check if on the ground
- onGround = controller.collisions.below;
- //If wall jumping can be done
- if (hasAbility[(int) abilities.WallJump] && (controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
- {
- wallSliding = true;
- if (velocity.y < -wallSlideSpeedMax)
- {
- velocity.y = -wallSlideSpeedMax;
- }
- }
- else wallSliding = false;
- if (controller.collisions.above || controller.collisions.below)
- {
- velocity.y = 0;
- //doubleTapDir = 0;
- }
- dodgeAxis = Input.GetAxis("Dodge");
- if (Input.GetButtonDown("Jump") /*&& controller.collisions.below*/ && canJump && input.y >= -0.5)
- {
- print ("Jump");
- if (wallSliding)
- {
- shiftTimer = wallJumpTime;
- shiftVelocity.x = wallJumpSpeed * -wallDirX * facing;
- //airDodged = false;
- velocity.y = maxJumpVelocity * wallJumpFactor;
- print(shiftVelocity.x);
- }
- if (controller.collisions.below)
- {
- velocity.y = maxJumpVelocity;
- //canDoubleJump = true;
- }
- if (!controller.collisions.below && !wallSliding && canDoubleJump && hasAbility[(int) abilities.DoubleJump])
- {
- velocity.y = maxJumpVelocity * doubleJumpFactor;
- canDoubleJump = false;
- }
- }
- if (Input.GetButtonUp("Jump"))
- {
- if (velocity.y > minJumpVelocity)
- {
- velocity.y = minJumpVelocity;
- }
- }
- //Hold button during ascent to jump higher
- /*
- if (Input.GetButton("Jump") && !controller.collisions.below && velocity.y > 0 && !backflipped)
- {
- //velocity.y += addJumpVelocity * Time.deltaTime;
- }*/
- //Old forward slide
- /*
- if (input.y < 0 && Input.GetButtonDown("Jump"))
- {
- if (controller.collisions.below)
- {
- print("Forward Slide");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = dodgeLength;
- shiftVelocity.x = dodgeSpeed;
- }
- }*/
- //Dash/dodge moves (all tied to canDodge)
- if (Input.GetButtonDown("Dodge") /*&& input.x != lastXInput*/ && canMove && canDodge)
- {
- //facingLocked = true;
- print(dodgeAxis);
- if (controller.collisions.below)
- {
- //Dodge code for ground
- //Slide kick if input is forward relative to facing
- if (facing * dodgeAxis < 0)
- {
- print("Forward Slide");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = dodgeLength;
- shiftVelocity.x = dodgeSpeed;
- }
- //Backstep if input is backward relative to facing
- else if (facing * dodgeAxis > 0)
- {
- print("Backstep");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = dodgeLength;
- shiftVelocity.x = -dodgeSpeed;
- }
- }
- else if (hasAbility[(int) abilities.Airdash] && !controller.collisions.below)
- {
- //Dodge code for air
- //Airdash if input is forward relative to facing
- if (facing * dodgeAxis < 0)
- {
- print("Airdash");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = airdashLength;
- shiftVelocity.x = airdashSpeed;
- gravity = 0;
- velocity.y = 0;
- airDodged = true;
- }
- //Backflip if input is backward relative to facing
- else if (facing * dodgeAxis > 0 && velocity.y > 0)
- {
- print("Backflip");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = backflipLength;
- shiftVelocity.x = -backflipSpeed;
- velocity.y = backflipHeight;
- backflipped = true;
- airDodged = true;
- }
- }
- }
- //Unlock facing if Shift isn't pressed
- /*
- if (!Input.GetKey(KeyCode.LeftShift))
- {
- facingLocked = false;
- } */
- //Attack input
- if (Input.GetButtonDown("Attack") && canAttack && comboPosition < maxCombo)
- {
- canDodge = false; //No dodge canceling attacks
- canAttack = false; //So you don't attack while attacking.
- isAttacking = true;
- meleeAttack.gameObject.SetActive(true);
- //Ground attack
- if (controller.collisions.below)
- {
- canMove = false; //Can't move during ground attacks, can during aerial
- canJump = false; //Can't break out of an attack with a jump
- shiftTimer = comboMove[comboPosition];
- shiftVelocity.x = comboShift[comboPosition];
- attackWindow = comboLength[comboPosition];
- delayWindow = comboDelay[comboPosition];
- meleeAttack.damage = comboDamage[comboPosition];
- meleeAttack.ResizeHitbox(windUpHitboxSizes[comboPosition]);
- meleeAttack.MoveHitbox(windUpHitboxPositions[comboPosition]);
- windUpTimer = windUpTime[comboPosition];
- }
- //Airborne attacks, ascending and descending
- else if (!controller.collisions.below && !airAttacked)
- {
- airAttacked = true;
- if (velocity.y > 0)
- {
- print("Ascending air attack");
- }
- else
- {
- print("Descending air attack");
- }
- }
- }
- //Backstep/Backflip
- /*
- if (Input.GetKeyDown(KeyCode.C) && canDodge)
- {
- if (controller.collisions.below)
- {
- print("Backstep");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = dodgeLength;
- shiftVelocity.x = -dodgeSpeed;
- }
- else if (!controller.collisions.below && velocity.y > 0)
- {
- print("Backflip");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = backflipLength;
- shiftVelocity.x = -backflipSpeed;
- velocity.y = backflipHeight;
- backflipped = true;
- airDodged = true;
- }
- }*/
- if (windUpTimer > 0)
- {
- windUpTimer -= Time.deltaTime;
- if (windUpTimer <= 0)
- {
- meleeAttack.ResizeHitbox(swingHitboxSizes[comboPosition]);
- meleeAttack.MoveHitbox(swingHitboxPositions[comboPosition]);
- }
- }
- //Attack window timer
- if (attackWindow > 0)
- {
- attackWindow -= Time.deltaTime;
- if (attackWindow <= 0)
- {
- meleeAttack.gameObject.SetActive(false);
- if (comboPosition < maxCombo)
- {
- comboPosition += 1;
- canAttack = true;
- }
- }
- }
- //Attack delay timer
- if (delayWindow > 0)
- {
- delayWindow -= Time.deltaTime;
- if (delayWindow <= 0)
- {
- //if (comboPosition == maxCombo)
- //{
- comboPosition = 0;
- canAttack = true;
- //}
- if (!canDodge)
- {
- canDodge = true;
- }
- canMove = true;
- canJump = true;
- isAttacking = false;
- }
- }
- //Shift timer countdown
- if (shiftTimer > 0)
- {
- shiftTimer -= Time.deltaTime;
- if (shiftTimer <= 0)
- {
- shiftVelocity.x = 0;
- if (!canDodge && controller.collisions.below)
- {
- dashCooldown = groundDashCooldown;
- }
- else if (!canDodge && !controller.collisions.below)
- {
- dashCooldown = airdashCooldown;
- }
- gravity = baseGravity;
- if (backflipped)
- {
- //velocity.y = 0;
- backflipped = false;
- }
- //canMove = true;
- }
- }
- if (dashCooldown > 0)
- {
- dashCooldown -= Time.deltaTime;
- if (dashCooldown <= 0 && !isAttacking)
- {
- print("Dash cooldown allowed movement again.");
- canMove = true;
- canJump = true;
- //print("You can move again.");
- if (!airDodged)
- {
- //print ("You can dodge again.");
- //canJump = true;
- canDodge = true;
- }
- }
- }
- //Airdash by double tap
- //Check if in air and analog stick/d-pad are pressed
- /*
- if (!controller.collisions.below && input.x != 0)
- {
- if (lastXInput == 0)
- {
- dashDoubleTap += 1;
- if (dashDoubleTap == 2)
- {
- print("Airdash");
- canDodge = false;
- canJump = false;
- canMove = false;
- shiftTimer = airdashLength;
- shiftVelocity.x = airdashSpeed;
- gravity = 0;
- velocity.y = 0;
- airDodged = true;
- dashDoubleTap = 0;
- }
- }
- }*/
- //If attack used in the air, re-enable dodging and attacking (temp?)
- if (airAttacked && controller.collisions.below)
- {
- canAttack = true;
- canDodge = true;
- airAttacked = false;
- //canJump = true;
- }
- //If airdash or backflip used in the air, re-enable dodging and movement
- if (airDodged && controller.collisions.below)
- {
- canMove = true;
- canJump = true;
- canDodge = true;
- airDodged = false;
- //print ("Landed from air dodge");
- }
- //Reset double jump upon landing
- if (!canDoubleJump && controller.collisions.below)
- {
- canDoubleJump = true;
- }
- //Instant movement if shifting isn't active
- if (shiftVelocity.x != 0)
- {
- velocity.x = shiftVelocity.x * facing;
- //Debug.Log("Shifting");
- }
- else
- {
- if (canMove)
- {
- if (input.x != 0)
- {
- velocity.x = Mathf.Sign(input.x) * moveSpeed;
- }
- else
- {
- velocity.x = input.x * moveSpeed;
- }
- }
- else
- {
- velocity.x = 0;
- }
- if (velocity.x != 0)
- {
- if (!facingLocked && Mathf.Abs(input.x) >= 0.25)
- {
- facing = Mathf.Sign(input.x);
- spriteScale.x = facing;
- transform.localScale = spriteScale;
- }
- }
- //print("Facing: " + facing);
- }
- //Smooth damping
- /*
- float targetVelocityX = input.x * moveSpeed;
- velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below)?accelerationTimeGrounded:accelerationTimeAirborne);
- */
- velocity.y += gravity * Time.deltaTime;
- controller.Move(velocity * Time.deltaTime, input);
- lastXInput = input.x;
- lastFacing = facing;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement