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; public static 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; 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); 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(); Kick(); Push(); Drink(); Dash(h); EnemyPushCheck(); EnemyDeadCheck(); 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; } } 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 = 0.5f; myAnimator.SetBool("knockBack", false); knockBack = false; } } } 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(); } 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 dashD = true; } 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 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 *0.5f; // 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.W) && 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.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); push = true; } else { myAnimator.SetBool("push", false); push = false; } } private void Block() { if (canBlock == true && Input.GetKey(KeyCode.E) && 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 || PlayerStats.stamina <= 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.UpArrow) && 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); } } } 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) { 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; } void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.tag == "ground") { canJump = true; } } }