Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- //Player inherits from MovingObject, our base class for objects that can move, Enemy also inherits from this.
- public class Player : MovingObject
- {
- public float restartLevelDelay = 1f; //Delay time in seconds to restart level.
- public int pointsPerFood = 5; //Number of points to add to player food points when picking up a food object.
- public int pointsPerSoda = 10; //Number of points to add to player food points when picking up a soda object.
- public int axeDamage = 1; //Amount of health the axe will take away from the enemies.
- public int wallDamage = 1; //How much damage a player does to a wall when chopping it.
- public Text foodText;
- public Text moveText;
- public Text floatText;
- public Text dayText;
- public bool hasAxe = false; //Used to tell if the player currently has the axe item equipped. Player must pick up axe, so default is false.
- public AudioClip moveSound1; //1 of 2 Audio clips to play when player moves.
- public AudioClip moveSound2; //2 of 2 Audio clips to play when player moves.
- public AudioClip eatSound1; //1 of 2 Audio clips to play when player collects a food object.
- public AudioClip eatSound2; //2 of 2 Audio clips to play when player collects a food object.
- public AudioClip drinkSound1; //1 of 2 Audio clips to play when player collects a soda object.
- public AudioClip drinkSound2; //2 of 2 Audio clips to play when player collects a soda object.
- public AudioClip gameOverSound; //Audio clip to play when player dies.
- public AudioClip chopSound1;
- public AudioClip chopSound2;
- private Animator animator; //Used to store a reference to the Player's animator component.
- private int food; //Used to store player food points total during level.
- private int moveCount; //Used to store number of moves the player has made.
- private int foodTemp; //Used to store a temporary variable for food when total will go over max.
- private int foodMax = 100; //Used to store the maximum food the player can have.
- private int floatCount = 0; //Used to hold a count in order to reset floatText every other turn.
- private int level; //Current level number, expressed in game as "Day 1".
- //Start overrides the Start function of MovingObject
- protected override void Start ()
- {
- //Get a component reference to the Player's animator component
- animator = GetComponent<Animator>();
- level = GameManager.instance.level;
- //Get the current food point total stored in GameManager.instance between levels.
- food = GameManager.instance.playerFoodPoints;
- foodText.text = "Food: " + food;
- moveCount = GameManager.instance.playerMovePoints;
- moveText.text = "Moves: " + moveCount;
- //Call the Start function of the MovingObject base class.
- base.Start ();
- }
- //This function is called when the behaviour becomes disabled or inactive.
- private void OnDisable ()
- {
- //When Player object is disabled, store the current local food total in the GameManager so it can be re-loaded in next level.
- GameManager.instance.playerFoodPoints = food;
- //When Player object is disabled, store the current local move count in the GameManager so it can be re-loaded in next level.
- GameManager.instance.playerMovePoints = moveCount;
- }
- private void Update ()
- {
- //Add one to the level UI counter.
- dayText.text = "Day " + level;
- //If it's not the player's turn, exit the function.
- if(!GameManager.instance.playersTurn) return;
- int horizontal = 0; //Used to store the horizontal move direction.
- int vertical = 0; //Used to store the vertical move direction.
- //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
- horizontal = (int) (Input.GetAxisRaw ("Horizontal"));
- //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
- vertical = (int) (Input.GetAxisRaw ("Vertical"));
- //Check if moving horizontally, if so set vertical to zero.
- if(horizontal != 0)
- {
- vertical = 0;
- }
- //Check if we have a non-zero value for horizontal or vertical
- if(horizontal != 0 || vertical != 0)
- {
- //Call AttemptMove passing in the generic parameter Wall, since that is what Player may interact with if they encounter one (by attacking it)
- //Pass in horizontal and vertical as parameters to specify the direction to move Player in.
- AttemptMove<Wall> (horizontal, vertical);
- }
- }
- //AttemptMove overrides the AttemptMove function in the base class MovingObject
- //AttemptMove takes a generic parameter T which for Player will be of the type Wall, it also takes integers for x and y direction to move in.
- protected override void AttemptMove <T> (int xDir, int yDir)
- {
- //Every time player moves, add one to floatcount
- floatCount++;
- //every two player turns, clear floatText and reset floatCount
- if (floatCount >= 2)
- {
- floatText.text = "";
- floatCount = 0;
- }
- //Every time player moves, subtract from food points total.
- food--;
- foodText.text = "Food: " + food;
- //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
- base.AttemptMove <T> (xDir, yDir);
- //Hit allows us to reference the result of the Linecast done in Move.
- RaycastHit2D hit;
- //If Move returns true, meaning Player was able to move into an empty space.
- if (Move (xDir, yDir, out hit))
- {
- SoundManager.instance.RandomizeSfx (moveSound1, moveSound2); //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
- }
- //Since the player has moved and lost food points, check if the game has ended.
- CheckIfGameOver ();
- //Set the playersTurn boolean of GameManager to false now that players turn is over.
- GameManager.instance.playersTurn = false;
- //Add one to the move counter right after the player moves.
- moveCount++;
- moveText.text = "Moves: " + moveCount;
- }
- //OnCantMove overrides the abstract function OnCantMove in MovingObject.
- //It takes a generic parameter T which in the case of Player is a Wall which the player can attack and destroy.
- protected override void OnCantMove<T>(T component)
- {
- if(component.GetType() == typeof(Enemy))
- {
- if (hasAxe == false)
- {
- return;
- }
- else if (hasAxe == true)
- {
- //Declare hitEnemy and set it to equal the encountered component.
- Enemy hitEnemy = component as Enemy;
- //Call the EnemyHit function of hitEnemy passing it axeDamage, the amount of healthpoints to be subtracted.
- hitEnemy.LoseHealth(axeDamage);
- //play the chop sound
- SoundManager.instance.RandomizeSfx (chopSound1, chopSound2);
- //Play the axe animation
- animator.SetTrigger ("playerAxe");
- }
- }
- else if(component.GetType() == typeof(Wall))
- {
- //Set hitWall to equal the component passed in as a parameter.
- Wall hitWall = component as Wall;
- //Call the DamageWall function of the Wall we are hitting.
- hitWall.DamageWall (wallDamage);
- if (hasAxe == false)
- {
- //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
- animator.SetTrigger ("playerChop");
- }
- else if (hasAxe == true)
- {
- //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
- animator.SetTrigger ("playerAxe");
- }
- }
- }
- //OnTriggerEnter2D is sent when another object enters a trigger collider attached to this object (2D physics only).
- private void OnTriggerEnter2D (Collider2D other)
- {
- //Check if the tag of the trigger collided with is Exit.
- if(other.tag == "Exit")
- {
- //Invoke the Restart function to start the next level with a delay of restartLevelDelay (default 1 second).
- Invoke ("Restart", restartLevelDelay);
- //Disable the player object since level is over.
- enabled = false;
- }
- //Check if the tag of the trigger collided with is Food.
- else if(other.tag == "Food")
- {
- if ((food + pointsPerFood) > foodMax)
- {
- foodTemp = food;
- food = 200;
- floatText.text = "+" + (foodMax - foodTemp) + " Food";
- foodText.text = "Food: " + food;
- }
- else if ((food + pointsPerFood <= foodMax))
- {
- //Add pointsPerFood to the players current food total.
- food += pointsPerFood;
- floatText.text = "+" + pointsPerFood + "Food";
- foodText.text = "Food: " + food;
- SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);
- }
- //Disable the food object the player collided with.
- other.gameObject.SetActive (false);
- }
- //Check if the tag of the trigger collided with is Soda.
- else if(other.tag == "Soda")
- {
- if ((food + pointsPerSoda) > foodMax)
- {
- foodTemp = food;
- food = 200;
- floatText.text = "+" + (foodMax - foodTemp) + " Food";
- foodText.text = "Food: " + food;
- }
- else if ((food + pointsPerSoda <= foodMax))
- {
- //Add pointsPerSoda to players food points total
- food += pointsPerSoda;
- floatText.text = "+" + pointsPerSoda + "Food";
- foodText.text = "Food: " + food;
- SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);
- }
- //Disable the soda object the player collided with.
- other.gameObject.SetActive (false);
- }
- else if(other.tag == "Axe")
- {
- hasAxe = true;
- //axeDurability =
- //Disable the axe object the player collided with.
- other.gameObject.SetActive (false);
- }
- }
- //Restart reloads the scene when called.
- private void Restart ()
- {
- //Load the last scene loaded, in this case Main, the only scene in the game.
- Application.LoadLevel (Application.loadedLevel);
- }
- //LoseFood is called when an enemy attacks the player.
- //It takes a parameter loss which specifies how many points to lose.
- public void LoseFood (int loss)
- {
- //Set the trigger for the player animator to transition to the playerHit animation.
- animator.SetTrigger ("playerHit");
- //Subtract lost food points from the players total.
- food -= loss;
- floatText.text = "-" + loss + "Food";
- foodText.text = "Food: " + food;
- //Check to see if game has ended.
- CheckIfGameOver ();
- }
- //CheckIfGameOver checks if the player is out of food points and if so, ends the game.
- private void CheckIfGameOver ()
- {
- //Check if food point total is less than or equal to zero.
- if (food <= 0)
- {
- SoundManager.instance.PlaySingle (gameOverSound);
- SoundManager.instance.musicSource.Stop ();
- //Call the GameOver function of GameManager.
- GameManager.instance.GameOver ();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement