using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class ArenaCombatControl : MonoBehaviour { private Rigidbody2D Player; private Animator myAnimator; 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; 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; 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; // Use this for initialization void Start() { stats = GameObject.Find("Player").GetComponent<PlayerStats>(); Player = GetComponent<Rigidbody2D>(); myAnimator = GetComponent<Animator>(); //required for animation ******* //SetWeaponStats(); blockCoolDown = 3; dashCoolDownD = 0.2f; dashCoolDownA = 0.2f; knockBackTimer = 1; 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(); Kick(); Push(); Dash(h); EnemyPushCheck(); 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; } } if (knockBack == true) { myAnimator.SetBool("knockBack", true); knockBackTimer -= Time.deltaTime; if (LookRight == false) { Player.AddForce(new Vector2(250, 10)); } else { Player.AddForce(new Vector2(-250, 10)); } if (knockBackTimer <= 0) { knockBackTimer = 1; myAnimator.SetBool("knockBack", false); knockBack = false; } } } private void EnemyPushCheck() { if (EnemyAI.isEnemyPushing == true) { knockBack = true; Debug.Log("Getting Pushed"); } } private void Dash(float horizontal) { if (Input.GetKeyUp(KeyCode.D)) { 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 dashD = true; } if (dashCoolDownD <= 0 || dashCountD >= 2) { dashCoolDownD = 0.5f; dashCountD = 0; canDashD = false; } } if (Input.GetKeyUp(KeyCode.A)) { 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 dashA = true; } if (dashCoolDownA <= 0 || dashCountA >= 2) { dashCoolDownA = 0.5f; dashCountA = 0; canDashA = false; } } } //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. attackSpeedCooldown -= stats.getWeapon().attackSpeed; // set cooldown based on equipped weapon (higher attack speed is quicker) attack = true; myAnimator.SetBool("attack", true); } else { attack = false; myAnimator.SetBool("attack", false); } } private void Kick() { if (Input.GetKeyDown(KeyCode.K) && PlayerStats.stamina >= 1) { // kick costs 1 stamina points PlayerStats.stamina -= 1; // kick costs 1 stamina points canMove = false; kick = true; myAnimator.SetBool("kick", true); } else { canMove = true; kick = false; myAnimator.SetBool("kick", false); } } private void Push() { if (Input.GetKeyDown(KeyCode.L) && PlayerStats.stamina >= 3) { // costs 3 stamina to push PlayerStats.stamina -= 3; // costs 3 stamina to push myAnimator.SetBool("push", true); push = true; } else { myAnimator.SetBool("push", false); push = false; } } private void Block() { if (canBlock == true && Input.GetKey(KeyCode.J) && PlayerStats.stamina > 0) { block = true; 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; } Debug.Log("blocking"); myAnimator.SetBool("block", true); Player.velocity = Vector2.zero; canMove = false; canRefill = false; blockCoolDown -= Time.deltaTime; PlayerStats.stamina -= 2*Time.deltaTime; // drains stamina while blocking ( 2x to counteract constant filling) } 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.J)) { 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; } */ }