Advertisement
Guest User

ScoreView

a guest
Jul 19th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5.  
  6. public delegate void DamageRecieveHandler(int value);
  7.  
  8. public class ScoreView : MonoBehaviour
  9. {
  10.     public DamageRecieveHandler onDamageRecieve;
  11.  
  12.     private Text m_Score;
  13.     private int m_ScoreCounter;
  14.  
  15.     private void Awake()
  16.     {
  17.         m_Score = GetComponent<Text> ();
  18.     }
  19.  
  20.     private void Start()
  21.     {
  22.         TotalScore.RegisterObserver (this);
  23.     }
  24.  
  25.     private void OnDestroy()
  26.     {
  27.         onDamageRecieve = null;
  28.     }
  29.  
  30.     private void OnCollisionEnter2D(Collision2D targetHit)
  31.     {
  32.         if (targetHit.gameObject.name == "yoba_pechalnii")
  33.         {          
  34.             OnDamageRecieved ();
  35.         }
  36.     }
  37.  
  38.     private void OnDamageRecieved()
  39.     {
  40.         m_ScoreCounter += 1;
  41.         ShowScore (ScoresToString (m_ScoreCounter));
  42.  
  43.         if (onDamageRecieve != null)
  44.         {
  45.             onDamageRecieve (m_ScoreCounter);
  46.         }
  47.     }
  48.  
  49.     private void ShowScore(string value)
  50.     {
  51.         m_Score.text = value;
  52.     }
  53.  
  54.     private string ScoresToString(int value)
  55.     {
  56.         return string.Format ("Score: {0}", value);
  57.     }
  58.  
  59.     private TotalScoreView TotalScore { get { return TotalScoreView.Instance; } }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement