Advertisement
Guest User

Player

a guest
Apr 8th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.72 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5.  
  6. //Player inherits from MovingObject, our base class for objects that can move, Enemy also inherits from this.
  7. public class Player : MovingObject
  8. {
  9.     public float restartLevelDelay = 1f;        //Delay time in seconds to restart level.
  10.     public int pointsPerFood = 5;               //Number of points to add to player food points when picking up a food object.
  11.     public int pointsPerSoda = 10;              //Number of points to add to player food points when picking up a soda object.
  12.     public int axeDamage = 1;                   //Amount of health the axe will take away from the enemies.
  13.     public int wallDamage = 1;                  //How much damage a player does to a wall when chopping it.
  14.     public Text foodText;
  15.     public Text moveText;
  16.     public Text floatText;
  17.     public Text dayText;
  18.     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.
  19.  
  20.  
  21.     public AudioClip moveSound1;                //1 of 2 Audio clips to play when player moves.
  22.     public AudioClip moveSound2;                //2 of 2 Audio clips to play when player moves.
  23.     public AudioClip eatSound1;                 //1 of 2 Audio clips to play when player collects a food object.
  24.     public AudioClip eatSound2;                 //2 of 2 Audio clips to play when player collects a food object.
  25.     public AudioClip drinkSound1;               //1 of 2 Audio clips to play when player collects a soda object.
  26.     public AudioClip drinkSound2;               //2 of 2 Audio clips to play when player collects a soda object.
  27.     public AudioClip gameOverSound;             //Audio clip to play when player dies.
  28.     public AudioClip chopSound1;
  29.     public AudioClip chopSound2;
  30.  
  31.     private Animator animator;                  //Used to store a reference to the Player's animator component.
  32.     private int food;                           //Used to store player food points total during level.
  33.     private int moveCount;                      //Used to store number of moves the player has made.
  34.     private int foodTemp;                       //Used to store a temporary variable for food when total will go over max.
  35.     private int foodMax = 100;                  //Used to store the maximum food the player can have.
  36.     private int floatCount = 0;                 //Used to hold a count in order to reset floatText every other turn.
  37.     private int level;                          //Current level number, expressed in game as "Day 1".
  38.  
  39.  
  40.     //Start overrides the Start function of MovingObject
  41.     protected override void Start ()
  42.     {
  43.         //Get a component reference to the Player's animator component
  44.         animator = GetComponent<Animator>();
  45.  
  46.         level = GameManager.instance.level;
  47.         //Get the current food point total stored in GameManager.instance between levels.
  48.         food = GameManager.instance.playerFoodPoints;
  49.  
  50.         foodText.text = "Food: " + food;
  51.  
  52.         moveCount = GameManager.instance.playerMovePoints;
  53.  
  54.         moveText.text = "Moves: " + moveCount;
  55.  
  56.         //Call the Start function of the MovingObject base class.
  57.         base.Start ();
  58.     }
  59.  
  60.     //This function is called when the behaviour becomes disabled or inactive.
  61.     private void OnDisable ()
  62.     {
  63.         //When Player object is disabled, store the current local food total in the GameManager so it can be re-loaded in next level.
  64.         GameManager.instance.playerFoodPoints = food;
  65.         //When Player object is disabled, store the current local move count in the GameManager so it can be re-loaded in next level.
  66.         GameManager.instance.playerMovePoints = moveCount;
  67.     }
  68.        
  69.  
  70.     private void Update ()
  71.     {
  72.         //Add one to the level UI counter.
  73.         dayText.text = "Day " + level;
  74.  
  75.         //If it's not the player's turn, exit the function.
  76.         if(!GameManager.instance.playersTurn) return;
  77.  
  78.         int horizontal = 0;     //Used to store the horizontal move direction.
  79.         int vertical = 0;       //Used to store the vertical move direction.
  80.  
  81.  
  82.         //Get input from the input manager, round it to an integer and store in horizontal to set x axis move direction
  83.         horizontal = (int) (Input.GetAxisRaw ("Horizontal"));
  84.  
  85.         //Get input from the input manager, round it to an integer and store in vertical to set y axis move direction
  86.         vertical = (int) (Input.GetAxisRaw ("Vertical"));
  87.  
  88.         //Check if moving horizontally, if so set vertical to zero.
  89.         if(horizontal != 0)
  90.         {
  91.             vertical = 0;
  92.         }
  93.  
  94.         //Check if we have a non-zero value for horizontal or vertical
  95.         if(horizontal != 0 || vertical != 0)
  96.         {
  97.             //Call AttemptMove passing in the generic parameter Wall, since that is what Player may interact with if they encounter one (by attacking it)
  98.             //Pass in horizontal and vertical as parameters to specify the direction to move Player in.
  99.             AttemptMove<Wall> (horizontal, vertical);
  100.         }
  101.  
  102.     }
  103.  
  104.     //AttemptMove overrides the AttemptMove function in the base class MovingObject
  105.     //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.
  106.     protected override void AttemptMove <T> (int xDir, int yDir)
  107.     {
  108.         //Every time player moves, add one to floatcount
  109.         floatCount++;
  110.  
  111.         //every two player turns, clear floatText and reset floatCount
  112.         if (floatCount >= 2)
  113.         {
  114.             floatText.text = "";
  115.             floatCount = 0;
  116.         }
  117.  
  118.         //Every time player moves, subtract from food points total.
  119.         food--;
  120.         foodText.text = "Food: " + food;
  121.  
  122.         //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
  123.         base.AttemptMove <T> (xDir, yDir);
  124.  
  125.         //Hit allows us to reference the result of the Linecast done in Move.
  126.         RaycastHit2D hit;
  127.  
  128.         //If Move returns true, meaning Player was able to move into an empty space.
  129.         if (Move (xDir, yDir, out hit))
  130.         {
  131.             SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);        //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
  132.         }
  133.  
  134.         //Since the player has moved and lost food points, check if the game has ended.
  135.         CheckIfGameOver ();
  136.  
  137.         //Set the playersTurn boolean of GameManager to false now that players turn is over.
  138.         GameManager.instance.playersTurn = false;
  139.  
  140.         //Add one to the move counter right after the player moves.
  141.         moveCount++;
  142.         moveText.text = "Moves: " + moveCount;
  143.     }
  144.  
  145.  
  146.     //OnCantMove overrides the abstract function OnCantMove in MovingObject.
  147.     //It takes a generic parameter T which in the case of Player is a Wall which the player can attack and destroy.
  148.     protected override void OnCantMove<T>(T component)
  149.     {
  150.         if(component.GetType() == typeof(Enemy))
  151.         {
  152.            
  153.             if (hasAxe == false)
  154.             {
  155.                 return;
  156.             }
  157.             else if (hasAxe == true)
  158.             {
  159.  
  160.  
  161.                 //Declare hitEnemy and set it to equal the encountered component.
  162.                 Enemy hitEnemy = component as Enemy;
  163.  
  164.                 //Call the EnemyHit function of hitEnemy passing it axeDamage, the amount of healthpoints to be subtracted.
  165.                 hitEnemy.LoseHealth(axeDamage);
  166.  
  167.                 //play the chop sound
  168.                 SoundManager.instance.RandomizeSfx (chopSound1, chopSound2);
  169.  
  170.                 //Play the axe animation
  171.                 animator.SetTrigger ("playerAxe"); 
  172.             }
  173.         }
  174.         else if(component.GetType() == typeof(Wall))
  175.         {
  176.             //Set hitWall to equal the component passed in as a parameter.
  177.             Wall hitWall = component as Wall;
  178.  
  179.             //Call the DamageWall function of the Wall we are hitting.
  180.             hitWall.DamageWall (wallDamage);
  181.  
  182.             if (hasAxe == false)
  183.             {
  184.                 //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
  185.                 animator.SetTrigger ("playerChop");
  186.             }
  187.  
  188.             else if (hasAxe == true)
  189.             {
  190.                 //Set the attack trigger of the player's animation controller in order to play the player's attack animation.
  191.                 animator.SetTrigger ("playerAxe");
  192.  
  193.             }
  194.         }
  195.     }
  196.        
  197.     //OnTriggerEnter2D is sent when another object enters a trigger collider attached to this object (2D physics only).
  198.     private void OnTriggerEnter2D (Collider2D other)
  199.     {
  200.         //Check if the tag of the trigger collided with is Exit.
  201.         if(other.tag == "Exit")
  202.         {
  203.             //Invoke the Restart function to start the next level with a delay of restartLevelDelay (default 1 second).
  204.             Invoke ("Restart", restartLevelDelay);
  205.  
  206.             //Disable the player object since level is over.
  207.             enabled = false;
  208.         }
  209.  
  210.         //Check if the tag of the trigger collided with is Food.
  211.         else if(other.tag == "Food")
  212.         {
  213.             if ((food + pointsPerFood) > foodMax)
  214.             {
  215.                 foodTemp = food;
  216.                 food = 200;
  217.                 floatText.text = "+" + (foodMax - foodTemp) + " Food";
  218.                 foodText.text = "Food: " + food;
  219.             }
  220.             else if ((food + pointsPerFood <= foodMax))
  221.             {
  222.                 //Add pointsPerFood to the players current food total.
  223.                 food += pointsPerFood;
  224.  
  225.                 floatText.text = "+" + pointsPerFood + "Food";
  226.                 foodText.text = "Food: " + food;
  227.                 SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);
  228.             }
  229.  
  230.             //Disable the food object the player collided with.
  231.             other.gameObject.SetActive (false);
  232.         }
  233.  
  234.         //Check if the tag of the trigger collided with is Soda.
  235.         else if(other.tag == "Soda")
  236.         {
  237.             if ((food + pointsPerSoda) > foodMax)
  238.             {
  239.                 foodTemp = food;
  240.                 food = 200;
  241.                 floatText.text = "+" + (foodMax - foodTemp) + " Food";
  242.                 foodText.text = "Food: " + food;
  243.             }
  244.             else if ((food + pointsPerSoda <= foodMax))
  245.             {
  246.                
  247.                 //Add pointsPerSoda to players food points total
  248.                 food += pointsPerSoda;
  249.  
  250.                 floatText.text = "+" + pointsPerSoda + "Food";
  251.                 foodText.text = "Food: " + food;
  252.                 SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);
  253.             }
  254.  
  255.             //Disable the soda object the player collided with.
  256.             other.gameObject.SetActive (false);
  257.         }
  258.  
  259.         else if(other.tag == "Axe")
  260.         {
  261.             hasAxe = true;
  262.             //axeDurability =
  263.             //Disable the axe object the player collided with.
  264.             other.gameObject.SetActive (false);
  265.         }
  266.            
  267.     }
  268.  
  269.  
  270.     //Restart reloads the scene when called.
  271.     private void Restart ()
  272.     {
  273.         //Load the last scene loaded, in this case Main, the only scene in the game.
  274.         Application.LoadLevel (Application.loadedLevel);
  275.     }
  276.  
  277.  
  278.     //LoseFood is called when an enemy attacks the player.
  279.     //It takes a parameter loss which specifies how many points to lose.
  280.     public void LoseFood (int loss)
  281.     {
  282.         //Set the trigger for the player animator to transition to the playerHit animation.
  283.         animator.SetTrigger ("playerHit");
  284.  
  285.         //Subtract lost food points from the players total.
  286.         food -= loss;
  287.  
  288.         floatText.text = "-" + loss + "Food";
  289.         foodText.text = "Food: " + food;
  290.  
  291.         //Check to see if game has ended.
  292.         CheckIfGameOver ();
  293.     }
  294.        
  295.     //CheckIfGameOver checks if the player is out of food points and if so, ends the game.
  296.     private void CheckIfGameOver ()
  297.     {
  298.         //Check if food point total is less than or equal to zero.
  299.         if (food <= 0)
  300.         {
  301.         SoundManager.instance.PlaySingle (gameOverSound);
  302.         SoundManager.instance.musicSource.Stop ();
  303.             //Call the GameOver function of GameManager.
  304.             GameManager.instance.GameOver ();
  305.         }
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement