Skip to content
Snippets Groups Projects
ArenaCombatControl.cs 13.7 KiB
Newer Older
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class ArenaCombatControl : MonoBehaviour {

    private Rigidbody2D Player;
    private Animator myAnimator;
Graham Solie's avatar
Graham Solie committed
    public Rigidbody2D Enemy;
    private PlayerStats stats; // to access stats/weapon
    public float original;
    [SerializeField]
    private float speed;
    private bool LookRight = true;
    private bool canMove = true;
Graham Solie's avatar
Graham Solie committed
    private bool canJump = false;
    private bool canBlock = true;
    private bool canRefill = false;
    public static float attackSpeedCooldown;
Graham Solie's avatar
Graham Solie committed
    private float blockCoolDown;
Graham Solie's avatar
Graham Solie committed
    public bool kicked = false;
    public static bool kick = false;
    public static bool attack = false;
Jordan's avatar
Jordan committed
	public static bool block = false;
Graham Solie's avatar
Graham Solie committed
    public static bool push = false;
    public int pushCount = 0;
    public float dashCoolDownD;
    public float dashCoolDownA;
    public int dashCountD;
    public int dashCountA;
    public bool canDashD = false;
    public bool canDashA = false;
    public bool knockBack = false;
    public float knockBackTimer;
    public float dashTimerD;
    public float dashTimerA;
    public bool dashD = false;
    public bool dashA = false;
    public static bool drinkStength = false;
    public static bool drinkAgility = false;
    public static bool drinkDefence = false;
    public static int winCount;
    // Use this for initialization
    void Start() {
        stats = GameObject.Find("Player").GetComponent<PlayerStats>();
        Player = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>(); //required for animation *******
        //SetWeaponStats();
        attackSpeedCooldown = stats.getWeapon().attackSpeed * 0.5f; // set cooldown based on equipped weapon (higher attack speed is quicker)
        Debug.Log(attackSpeedCooldown);
Graham Solie's avatar
Graham Solie committed
        blockCoolDown = 3;
        dashCoolDownD = 0.2f;
        dashCoolDownA = 0.2f;
        knockBackTimer = 0.5f;
        dashTimerD = 0.5f;
        dashTimerA = 0.5f;
    }

    // Update is called once per frame
    void Update() {
        float h = Input.GetAxis("Horizontal");
        float i = Input.GetAxis("Vertical");
        kicked = EnemyAI.isEnemyKicking;
        Movement(h);
        FlipPlayer(h);
        RotatePlayerZaxis(h);
        Attack();
        Block();
        Jump();
Graham Solie's avatar
Graham Solie committed
        Push();
Graham Solie's avatar
Graham Solie committed
        EnemyPushCheck();
        EnemyDeadCheck();
Jordan's avatar
Jordan committed
		PlayerDeadCheck ();
        if (canDashD == true) {
            dashCoolDownD -= Time.deltaTime;
        }

        if (canDashA == true) {
            dashCoolDownA -= Time.deltaTime;
        }

        if (dashD == true) {
            dashTimerD -= Time.deltaTime;
            Player.AddForce(new Vector2(350, Player.velocity.y * 5));
            if (dashTimerD <= 0) {
                dashD = false;
                dashTimerD = 0.5f;
            }
        }
        if (dashA == true) {
            dashTimerA -= Time.deltaTime;
            Player.AddForce(new Vector2(-350, Player.velocity.y * 5));
            if (dashTimerA <= 0) {
                dashA = false;
                dashTimerA = 0.5f;
            }
        }
Graham Solie's avatar
Graham Solie committed

        if (knockBack == true) {
            myAnimator.SetBool("knockBack", true);
            knockBackTimer -= Time.deltaTime;
Graham Solie's avatar
Graham Solie committed
            if (LookRight == false) {
                Player.AddForce(new Vector2(250, 10));
Graham Solie's avatar
Graham Solie committed
            }
            else {
                Player.AddForce(new Vector2(-250, 10));
            }
            if (knockBackTimer <= 0) {
                knockBackTimer = 0.5f;
                myAnimator.SetBool("knockBack", false);
                knockBack = false;
Graham Solie's avatar
Graham Solie committed
            }
Graham Solie's avatar
Graham Solie committed
        }
Jordan's avatar
Jordan committed
	private void PlayerDeadCheck (){
		if (stats.isPlayerDead ()) {
			myAnimator.SetBool ("death", true);
			canMove = false;
			canMove = false;
			canJump = false;
			canBlock = false;
			canRefill = false;
			canDashA = false;
			canDashD = false;
		}
	}
    private void EnemyDeadCheck() {
        if (EnemyAI.isEnemyDead == true) {
            myAnimator.SetBool("victory", true);

            if (PlayerStats.offenseBlessing == true) {
                PlayerStats.agility -= 5;
                PlayerStats.strength -= 5;
                PlayerStats.offenseBlessing = false;
            }
            if (PlayerStats.defenseBlessing == true) {
                PlayerStats.defense -= 5;
                PlayerStats.vitality -= 3;
                PlayerStats.defenseBlessing = false;
            }
            if (PlayerStats.luckBlessing == true) {
                PlayerStats.luck -= 3;
                PlayerStats.luckBlessing = false;
            }
            if (drinkStength == true) {
                StrengthBoostCoolDown();
            }
            if (drinkDefence == true) {
                DefenceBoostCoolDown();
            }
            if (drinkAgility == true) {
                AgilityBoostCoolDown();
            }
Michael LaFreniere's avatar
Michael LaFreniere committed
            if (PlayerStats.curHealth > PlayerStats.maxHealth) {
                PlayerStats.curHealth = PlayerStats.maxHealth; // for if you go over max.
            }
        else {
            myAnimator.SetBool("victory", false);
        }
    private void EnemyPushCheck() {

        if (EnemyAI.isEnemyPushing == true) {
            knockBack = true;
            Debug.Log("Getting Pushed");
        }
    }


    private void Dash(float horizontal) {
        if (Input.GetKeyUp(KeyCode.RightArrow)) {
            canDashD = true;
            dashCountD++;
            Debug.Log(dashCountD);
            if (dashCoolDownD >= 0 && dashCountD >= 2 && PlayerStats.stamina >= 1.5) {
                PlayerStats.stamina -= (float)2.5; // dash costs 2.5 stamina points
            }
            if (dashCoolDownD <= 0 || dashCountD >= 2) {
                dashCoolDownD = 0.5f;
                dashCountD = 0;
                canDashD = false;
            }
        }

        if (Input.GetKeyUp(KeyCode.LeftArrow)) {
            canDashA = true;
            dashCountA++;
            Debug.Log(dashCountA);
            if (dashCoolDownA >= 0 && dashCountA >= 2 && PlayerStats.stamina >= 1.5) {
                PlayerStats.stamina -= (float)2.5; // dash costs 2.5 stamina points
            if (dashCoolDownA <= 0 || dashCountA >= 2) {
                dashCoolDownA = 0.5f;
                dashCountA = 0;
                canDashA = false;
            }
        }

Graham Solie's avatar
Graham Solie committed
    //controlling the characters movement 
    private void Movement(float horizontal) {
        if (canMove) {
            Player.velocity = new Vector2(speed * horizontal, Player.velocity.y);
            myAnimator.SetFloat("speed", (Mathf.Abs(horizontal))); //required for animation *******
        }
    }


    private void Attack() {
        attackSpeedCooldown -= Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.Space) && attackSpeedCooldown <= 0) {
         //   attackSpeedCooldown = 3; // Base time, subtract attack speed from this to get the cool down. So a weapon with 0 attack speed will have 3 seconds between swings. 
Graham Solie's avatar
Graham Solie committed
            attackSpeedCooldown = stats.getWeapon().attackSpeed *0.5f; // set cooldown based on equipped weapon (higher attack speed is quicker)
            myAnimator.SetBool("attack", true);
        }
        else {
            attack = false;
            myAnimator.SetBool("attack", false);
        }
    }
Graham Solie's avatar
Graham Solie committed
    private void Kick() {
        if (Input.GetKeyDown(KeyCode.W) && PlayerStats.stamina >= 1) { // kick costs 1 stamina points
            PlayerStats.stamina -= 1; // kick costs 1 stamina points
            canMove = false;
Graham Solie's avatar
Graham Solie committed
            myAnimator.SetBool("kick", true);
        }
        else {
Graham Solie's avatar
Graham Solie committed
            myAnimator.SetBool("kick", false);
        }
    }
Graham Solie's avatar
Graham Solie committed
    private void Push() {
        if (Input.GetKeyDown(KeyCode.Q) && PlayerStats.stamina >= 2.5) { // costs 2.5 stamina to push
            PlayerStats.stamina -= 2.5f; // costs 2.5 stamina to push
            myAnimator.SetBool("push", true);
Graham Solie's avatar
Graham Solie committed
            push = true;
        }
        else {
            myAnimator.SetBool("push", false);

    private void Block() {
        if (canBlock == true && Input.GetKey(KeyCode.E) && PlayerStats.stamina > 0) {
            if (kicked == true) {
                PlayerStats.stamina -= 3; // stamina penalty of 3 for getting kicked with shield up
                if (PlayerStats.stamina < 0) {
                    PlayerStats.stamina = 0; // can't fall below 0
                }
                block = false;
            }
Graham Solie's avatar
Graham Solie committed
            Debug.Log("blocking");
            myAnimator.SetBool("block", true);
            Player.velocity = Vector2.zero;
            canMove = false;
Graham Solie's avatar
Graham Solie committed
            canRefill = false;
            blockCoolDown -= Time.deltaTime;
            PlayerStats.stamina -= 2*Time.deltaTime; // drains stamina while blocking ( 2x to counteract constant filling)
Graham Solie's avatar
Graham Solie committed
        }

        if (blockCoolDown < 0 || PlayerStats.stamina <= 0) {
Graham Solie's avatar
Graham Solie committed
            canRefill = true;
Graham Solie's avatar
Graham Solie committed
            canBlock = false;
        }

        if (canBlock == false) {
            Debug.Log("filling");
Graham Solie's avatar
Graham Solie committed
            myAnimator.SetBool("block", false);
            blockCoolDown += Time.deltaTime;
        }
Graham Solie's avatar
Graham Solie committed
        if (blockCoolDown > 3) {
            Debug.Log("true");
            canBlock = true;
Graham Solie's avatar
Graham Solie committed

        if (Input.GetKeyUp(KeyCode.E)) {
Graham Solie's avatar
Graham Solie committed
            Debug.Log("KeyOff");
            myAnimator.SetBool("block", false);
            canMove = true;
Graham Solie's avatar
Graham Solie committed
            canRefill = true;
        if (kicked == true) {
            block = false;
            Debug.Log("KeyOff");
            myAnimator.SetBool("block", false);
            canMove = true;
            canRefill = true;
            blockCoolDown = 0;
        }

Graham Solie's avatar
Graham Solie committed
        if (canRefill == true) {
            if (blockCoolDown < 3) {
                blockCoolDown += Time.deltaTime;
            }
        }
    }

    private void Jump() {
        if (Input.GetKeyDown(KeyCode.UpArrow) && canJump) {
Graham Solie's avatar
Graham Solie committed
            canJump = false;
            Player.AddForce(new Vector2(0, 300));
        }
    }



    //faces the characters body in the appropriate direction 
    private void FlipPlayer(float horizontal) {
        if (horizontal > 0 && !LookRight || horizontal < 0 && LookRight) {
            LookRight = !LookRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }


    //keeps the character from falling over
    private void RotatePlayerZaxis(float horizonal) {
        Vector3 myRotation = gameObject.transform.rotation.eulerAngles;
        Vector3 Pos = transform.localPosition;
        //if the player does not need to be climbing up an angle, the character will be fixed upright
        if (Pos.x < -4f || Pos.x > 5f) {
            transform.localRotation = Quaternion.Euler(0, 0, 0);
        }


        else {

            //allows the character to rotate but not past 90 degrees either direction when climbing 
            if (myRotation.z > 90) {
                myRotation.z = Mathf.Clamp(0, 0, 0);
                transform.rotation = Quaternion.Euler(myRotation);
            }

            if (myRotation.z < -90) {
                myRotation.z = Mathf.Clamp(0, 0, 0);
                transform.rotation = Quaternion.Euler(myRotation);
            }
        }
    }

    private void Drink() {
        //Can only drink the respected potion if the respected potion exists 
        if (Input.GetKeyDown(KeyCode.Alpha1) && PlayerStats.strengthPotions >= 1 && drinkStength !=true) {
            boostStrength();
            PlayerStats.strengthPotions--;
            drinkStength = true;
            myAnimator.SetBool("drink", true);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2) && PlayerStats.agilityPotions >= 1 && drinkAgility != true) {
            boostAgility();
            PlayerStats.agilityPotions--;
            drinkAgility = true;
            myAnimator.SetBool("drink", true);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3) && PlayerStats.defensePotions >= 1 && drinkDefence != true) {
            boostDefence();
            PlayerStats.defensePotions--;
            drinkDefence = true;
            myAnimator.SetBool("drink", true);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha4) && PlayerStats.healthPotions >= 1) {
Jordan's avatar
Jordan committed
			PlayerStats.curHealth += (int)(PlayerStats.maxHealth*0.5); //heal as soon as you drink the pot
			if (PlayerStats.curHealth > PlayerStats.maxHealth) {
				PlayerStats.curHealth = PlayerStats.maxHealth;
			}
            PlayerStats.healthPotions--;
            myAnimator.SetBool("drink", true);
        }

        else {
            myAnimator.SetBool("drink", false);
        }
    }

    //Apply the boost to the respected skill
    private void boostStrength() {
        PlayerStats.strength += 3;
        Invoke("StrengthBoostCoolDown", 150); //boost lasts 2.5 mins
    }

    private void boostDefence() {
        PlayerStats.defense += 3;
        Invoke("DefenceBoostCoolDown", 150); //boost lasts 2.5 mins
    }

    private void boostAgility() {
        PlayerStats.agility += 3;
        Invoke("AgilityBoostCoolDown", 150); //boost lasts 2.5 mins
    }

    //take boost off the respected skills
    private void StrengthBoostCoolDown() {
        drinkStength = false;
        PlayerStats.strength -= 3;
    }

    private void DefenceBoostCoolDown() {
        drinkDefence = false;
        PlayerStats.defense -= 3;
    }
    private void AgilityBoostCoolDown() {
        drinkAgility = false;
        PlayerStats.agility -= 3;
Graham Solie's avatar
Graham Solie committed
    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "ground") {
            canJump = true;
        }
    }