Newer
Older
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ArenaCombatControl : MonoBehaviour {
private Rigidbody2D Player;
private Animator myAnimator;
private PlayerStats stats; // to access stats/weapon
[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 bool kick = false;
public static bool attack = false;
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;
// 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);
dashCoolDownD = 0.2f;
dashCoolDownA = 0.2f;
dashTimerD = 0.5f;
dashTimerA = 0.5f;
}
// Update is called once per frame
void Update() {
float h = Input.GetAxis("Horizontal");
float i = Input.GetAxis("Vertical");
Movement(h);
FlipPlayer(h);
RotatePlayerZaxis(h);
Attack();
Block();
Jump();
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;
Player.AddForce(new Vector2(250, 10));
Player.AddForce(new Vector2(-250, 10));
}
if (knockBackTimer <= 0) {
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.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
}
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;
}
}
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)
myAnimator.SetBool("attack", true);
}
else {
myAnimator.SetBool("attack", false);
}
}
if (Input.GetKeyDown(KeyCode.W) && PlayerStats.stamina >= 1) { // kick costs 1 stamina points
PlayerStats.stamina -= 1; // kick costs 1 stamina points
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);
myAnimator.SetBool("push", false);
if (canBlock == true && Input.GetKey(KeyCode.E) && PlayerStats.stamina > 0) {
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
}
myAnimator.SetBool("block", true);
Player.velocity = Vector2.zero;
canMove = false;
PlayerStats.stamina -= 2*Time.deltaTime; // drains stamina while blocking ( 2x to counteract constant filling)
if (blockCoolDown < 0 || PlayerStats.stamina <= 0) {
canBlock = false;
}
if (canBlock == false) {
Debug.Log("filling");
myAnimator.SetBool("block", false);
blockCoolDown += Time.deltaTime;
}
if (blockCoolDown > 3) {
Debug.Log("true");
canBlock = true;
if (Input.GetKeyUp(KeyCode.E)) {
myAnimator.SetBool("block", false);
canMove = 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;
}
}
if (Input.GetKeyDown(KeyCode.UpArrow) && canJump) {
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
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);
}
}
}
//Can only drink the respected potion if the respected potion exists
if (Input.GetKeyDown(KeyCode.Alpha1) && PlayerStats.strengthPotions >= 1 && drinkStength !=true) {
boostStrength();
PlayerStats.strengthPotions--;
myAnimator.SetBool("drink", true);
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && PlayerStats.agilityPotions >= 1 && drinkAgility != true) {
boostAgility();
PlayerStats.agilityPotions--;
myAnimator.SetBool("drink", true);
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && PlayerStats.defensePotions >= 1 && drinkDefence != true) {
boostDefence();
PlayerStats.defensePotions--;
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;
}
myAnimator.SetBool("drink", true);
}
else {
myAnimator.SetBool("drink", false);
}
}
//Apply the boost to the respected skill
private void boostStrength() {
Invoke("StrengthBoostCoolDown", 150); //boost lasts 2.5 mins
}
private void boostDefence() {
Invoke("DefenceBoostCoolDown", 150); //boost lasts 2.5 mins
}
private void boostAgility() {
Invoke("AgilityBoostCoolDown", 150); //boost lasts 2.5 mins
}
//take boost off the respected skills
private void StrengthBoostCoolDown() {
}
private void DefenceBoostCoolDown() {
}
private void AgilityBoostCoolDown() {
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "ground") {
canJump = true;
}
}