Skip to content
Snippets Groups Projects
  • Jordan's avatar
    4a49ab08
    More minor tweaks · 4a49ab08
    Jordan authored
    - Potions don't heal over now
    - Town doors are better
    - Readded name solution, not working 100% still
    - Sped up enemy
    4a49ab08
    History
    More minor tweaks
    Jordan authored
    - Potions don't heal over now
    - Town doors are better
    - Readded name solution, not working 100% still
    - Sped up enemy
PlayerMovement.cs 4.53 KiB
using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{

    private Rigidbody2D Player;
    private Animator myAnimator;

    [SerializeField]
    private float speed;
    private bool LookRight = true;
    private bool canMove = true;
    private float attackSpeedCooldown;

    // Use this for initialization
    void Start()
    {
        //weapons[currentWeapon].attackSpeed = 1f;
        Player = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>(); //required for animation *******
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float i = Input.GetAxis("Vertical");

        Movement(h, i);
        FlipPlayer(h);
        //RotatePlayerZaxis(h);
        attack();
        block();
        Drink();
    }

    //controlling the characters movement 
    private void Movement(float horizontal, float vertical)
    {

        if (canMove)
        {
            Player.velocity = new Vector2(speed * horizontal, speed * vertical);

            myAnimator.SetFloat("speed", (Mathf.Abs(horizontal) + Mathf.Abs(vertical))); //required for animation *******
            }
        }

    /*
     * Attack method for the player.  
     * Animations are handled here. See PlayerCollision.cs for interactions
     */ 
    private void attack() {
        attackSpeedCooldown -= Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.Space) && attackSpeedCooldown <= 0) {
            //attackSpeedCooldown = weapons[currentWeapon].attackSpeed;
			attackSpeedCooldown = 1;
            myAnimator.SetBool("attack", true);
        }
        else {
            myAnimator.SetBool("attack", false);
        }
    }

	/*
     * Block method for the player.  
     */ 
    private void block()
    {
        if (Input.GetKeyDown(KeyCode.E))
        {
            myAnimator.SetBool("block", true);
            Player.velocity = Vector2.zero;
            canMove = false;

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

        }

    }

	/*
     * Drink method for the player.  
     */ 
    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--;
            myAnimator.SetBool("drink", true);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha2) && PlayerStats.agilityPotions >= 1) {
            boostAgility();
            PlayerStats.agilityPotions--;
            myAnimator.SetBool("drink", true);
        }
        else if (Input.GetKeyDown(KeyCode.Alpha3) && PlayerStats.defensePotions >= 1) {
            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;
			}
            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() {
        PlayerStats.strength -= 15;
    }

    private void DefenceBoostCoolDown() {
        PlayerStats.defense -= 15;
    }
    private void AgilityBoostCoolDown() {
        PlayerStats.agility -= 15;
    }



	/*
     * 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;
			myAnimator.SetBool("block", false);
        }
    }
}