-
Michael LaFreniere authoredMichael LaFreniere authored
ArenaCombatControl.cs 14.58 KiB
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 -= 1;
PlayerStats.strength -= 1;
PlayerStats.offenseBlessing = false;
}
if (PlayerStats.defenseBlessing == true) {
PlayerStats.defense -= 1;
PlayerStats.vitality -= 1;
PlayerStats.defenseBlessing = false;
}
if (PlayerStats.luckBlessing == true) {
PlayerStats.luck -= 2;
PlayerStats.luckBlessing = false;
}
if (drinkStength == true) {
StrengthBoostCoolDown();
}
if (drinkDefence == true) {
DefenceBoostCoolDown();
}
if (drinkAgility == true) {
AgilityBoostCoolDown();
}
PlayerStats.curHealth += 0.1f * PlayerStats.curHealth; // + 10% health after fight - fixing the poverty stricken players
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) {
boostStrength();
PlayerStats.strengthPotions--;
drinkStength = true;
myAnimator.SetBool("drink", true);
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && PlayerStats.agilityPotions >= 1) {
boostAgility();
PlayerStats.agilityPotions--;
drinkAgility = true;
myAnimator.SetBool("drink", true);
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && PlayerStats.defensePotions >= 1) {
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 += 15;
Invoke("StrengthBoostCoolDown", 150); //boost lasts 2.5 mins
}
private void boostDefence() {
PlayerStats.strength += 15;
Invoke("DefenceBoostCoolDown", 150); //boost lasts 2.5 mins
}
private void boostAgility() {
PlayerStats.strength += 15;
Invoke("AgilityBoostCoolDown", 150); //boost lasts 2.5 mins
}
//take boost off the respected skills
private void StrengthBoostCoolDown() {
drinkStength = false;
PlayerStats.strength -= 15;
}
private void DefenceBoostCoolDown() {
drinkDefence = false;
PlayerStats.defense -= 15;
}
private void AgilityBoostCoolDown() {
drinkAgility = false;
PlayerStats.agility -= 15;
}
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;
}
*/
}