Advertisement
gundambison

Player

Feb 22nd, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.23 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.UI;
  6.  
  7. public class Player : MovingObject
  8. {
  9.    
  10.     public int wallDamage = 1;
  11.    
  12.     public int pointsPerFood = 20;
  13.     public int pointsPerSoda = 10; //lebih make sense
  14.     public float restartLevelDelay = 1f;
  15.     public Text foodText;
  16.  
  17.     public AudioClip moveSound1;
  18.     public AudioClip moveSound2;
  19.     public AudioClip eatSound1;
  20.     public AudioClip eatSound2;
  21.     public AudioClip drinkSoup1;
  22.     public AudioClip drinkSoup2;
  23.     public AudioClip gameOverSound;
  24.  
  25.     private Animator animator;
  26.     private int food;
  27.    
  28.     // Start is called before the first frame update
  29.     protected override void Start()
  30.     {
  31.         animator  = GetComponent<Animator>();
  32.         food = GameManager.instance.playerFoodPoints;
  33.         foodText.text = "Food :" + food;
  34.         base.Start();
  35.     }
  36.    
  37.     private void OnDisable()
  38.     {
  39.         GameManager.instance.playerFoodPoints = food;
  40.     }
  41.  
  42.     // Update is called once per frame
  43.     void Update()
  44.     {
  45.         if( ! GameManager.instance.playersTurn) return ;
  46.        
  47.         int horizontal  =0;
  48.         int vertical    =0;
  49.        
  50.         horizontal = (int) Input.GetAxisRaw("Horizontal");
  51.         vertical = (int) Input.GetAxisRaw("Vertical");
  52.        
  53.         if( horizontal != 0) vertical =0;
  54.        
  55.         if( horizontal != 0 || vertical != 0){
  56.             AttemptMove<Wall> (horizontal, vertical);
  57.         }
  58.     }
  59.    
  60.     protected override void AttemptMove <T> (int xDir, int yDir)
  61.     {
  62.         food --;
  63.         foodText.text = "Food :" + food;
  64.  
  65.         base.AttemptMove <T> (xDir,yDir);
  66.        
  67.         RaycastHit2D hit;
  68.         if(Move(xDir, yDir, out hit))
  69.         {
  70.             SoundManager.instance.RandomizeSfx(moveSound1, moveSound2);
  71.         }
  72.         CheckIfGameOver();
  73.        
  74.         GameManager.instance.playersTurn = false;
  75.        
  76.     }
  77.    
  78.     private void OnTriggerEnter2D ( Collider2D other)
  79.     {
  80.         if(other.tag == "Exit"){
  81.             Invoke ("Restart", restartLevelDelay);
  82.             enabled = false;
  83.         }else if(other.tag == "Food"){
  84.             food += pointsPerFood;
  85.             foodText.text = "+"+ pointsPerFood+" Food :" + food;
  86.             SoundManager.instance.RandomizeSfx(eatSound1, eatSound2);
  87.             other.gameObject.SetActive(false);
  88.         }else if(other.tag == "Soda"){
  89.             food += pointsPerSoda;
  90.             foodText.text = "+" + pointsPerSoda + " Food :" + food;
  91.             SoundManager.instance.RandomizeSfx(drinkSoup1, drinkSoup2);
  92.             other.gameObject.SetActive(false);
  93.         }
  94.         else
  95.         {
  96.             Debug.Log(other.tag);
  97.  
  98.         }
  99.     }
  100.    
  101.     protected override void OnCantMove <T>(T component)
  102.     {
  103.         Wall hitWall = component as Wall;
  104.         hitWall.DamageWall(wallDamage);
  105.         animator.SetTrigger("PlayerChop");
  106.     }
  107.    
  108.     private void Restart()
  109.     {
  110.         Application.LoadLevel(Application.loadedLevel);
  111.         // 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene'
  112.     }
  113.    
  114.     public void LoseFood(int loss)
  115.     {
  116.         animator.SetTrigger("PlayerHit");
  117.         food -= loss;
  118.         foodText.text = "-" + loss  + " Food :" + food;
  119.         CheckIfGameOver();
  120.     }
  121.    
  122.     private void CheckIfGameOver()
  123.     {
  124.         if(food <= 0){
  125.             SoundManager.instance.PlaySingle( gameOverSound );
  126.             SoundManager.instance.musicSource.Stop();
  127.             GameManager.instance.GameOver();
  128.            
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement