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

public class ArenaCombatControl : MonoBehaviour {

    private Rigidbody2D Player;
    private Animator myAnimator;
    public Rigidbody2D Enemy;

    public float original;
    [SerializeField]
    private float speed;
    private bool LookRight = true;
    private bool canMove = true;
    private bool canJump = false;
    private bool canBlock = true;
    private bool canRefill = false;
    private float attackSpeedCooldown;
    private float blockCoolDown;
    public bool kicked = false;
    public static bool kick = false;
    public static bool attack = false;
    public static bool block = false;
    public static bool push = false;
    public int pushCount = 0;
    // fields to modify player stats based on weapons -- now in CharacterLook script!! *****
    /*
    public WeaponStats[] weapons;
    public int currentWeapon;
    */

    // Use this for initialization
    void Start() {

        //weapons[currentWeapon].attackSpeed = 1f;
        Player = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>(); //required for animation *******
        //SetWeaponStats();
        blockCoolDown = 3;
    }

    // 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();
        Kick();
        Push();
        EnemyPushCheck();
    }

    private void EnemyPushCheck() {

        if (EnemyAI.isEnemyPushing == true) {
            Debug.Log("Getting Pushed");
            if (LookRight == false) {
                Player.AddForce(new Vector2(500, 0));
            }
            else {
                Player.AddForce(new Vector2(-500, 0));
            }
        }
    }



    //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 = weapons[currentWeapon].attackSpeed;
            attack = true;
            myAnimator.SetBool("attack", true);
        }
        else {
            attack = false;
            myAnimator.SetBool("attack", false);
        }
    }

    private void Kick() {
        if (Input.GetKeyDown(KeyCode.F)) {
            kick = true;
            myAnimator.SetBool("kick", true);
        }
        else {
            kick = false;
            myAnimator.SetBool("kick", false);
        }
    }

    private void Push() {
        if (Input.GetKeyDown(KeyCode.Q)) {
            myAnimator.SetBool("push", true);
            push = true;
        }
        else {
            myAnimator.SetBool("push", false);
            push = false;
        }
    }





    private void Block() {

        if (canBlock == true && Input.GetKey(KeyCode.E)) {
            block = true;
            if (kicked == true) {
                block = false;
            }
            Debug.Log("blocking");
            myAnimator.SetBool("block", true);
            Player.velocity = Vector2.zero;
            canMove = false;
            canRefill = false;
            blockCoolDown -= Time.deltaTime;
        }

        if (blockCoolDown < 0) {
            block = false;
            canRefill = true;
            canBlock = false;
        }

        if (canBlock == false) {
            Debug.Log("filling");
            block = false;
            myAnimator.SetBool("block", false);
            blockCoolDown += Time.deltaTime;
        }

        if (blockCoolDown > 3) {
            Debug.Log("true");
            canBlock = true;
        }

        if (Input.GetKeyUp(KeyCode.E)) {
            block = false;
            Debug.Log("KeyOff");
            myAnimator.SetBool("block", false);
            canMove = true;
            canRefill = true;
        }

        if (kicked == true) {
            block = false;
            Debug.Log("KeyOff");
            myAnimator.SetBool("block", false);
            canMove = true;
            canRefill = true;
            blockCoolDown = 0;
        }

        if (canRefill == true) {
            if (blockCoolDown < 3) {
                blockCoolDown += Time.deltaTime;
            }
        }
    }

    private void Jump() {
        if (Input.GetKeyDown(KeyCode.W) && canJump) {
            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);
            }
        }
    }

    void OnCollisionEnter2D(Collision2D coll) {
        if (coll.gameObject.tag == "ground") {
            canJump = true;
        }
    }


    /* can change stats simply in the inspector for dynamic purposes :)
        //a few proposed weapon stats
    private void SetWeaponStats()
    {
        weapons[0].damageMin = 1;
        weapons[0].damageMax = 5;
        weapons[0].attackSpeed = 1;

        weapons[1].damageMin = 2;
        weapons[1].damageMax = 5;
        weapons[1].attackSpeed = 2;

        weapons[2].damageMin = 3;
        weapons[2].damageMax = 6;
        weapons[2].attackSpeed = 2;

        weapons[3].damageMin = 3;
        weapons[3].damageMax = 4;
        weapons[3].attackSpeed = 4;

        weapons[4].damageMin = 6;
        weapons[4].damageMax = 12;
        weapons[4].attackSpeed = 1;

        weapons[5].damageMin = 5;
        weapons[5].damageMax = 10;
        weapons[5].attackSpeed = 3;

        

    }
    */
}