Created
April 9, 2020 16:03
-
-
Save smkplus/b734e0db74c4f12b5703a87db0f517b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using UnityEngine.UI; | |
| public class GameManager : MonoBehaviour | |
| { | |
| public const int INITIAL_LIFE = 100; | |
| private const string SCORE_TXT = "SCORE: "; | |
| private const string LIFE_TXT = "LIFE: "; | |
| //UI | |
| [SerializeField] private Text scoreText; | |
| [SerializeField] private Text lifeText; | |
| [SerializeField] private Image gameOverOverlay; | |
| //Audio | |
| [SerializeField] private AudioClip attackSfx; | |
| [SerializeField] private AudioClip enemyDeathSfx; | |
| [SerializeField] private AudioClip playerDeathSfx; | |
| //Entities | |
| [SerializeField] private Player player; | |
| [SerializeField] private AudioSource audioSource; | |
| private void Start() | |
| { | |
| InitializeUI(); | |
| player.gameObject.SetActive(true); | |
| } | |
| private void InitializeUI() | |
| { | |
| gameOverOverlay.gameObject.SetActive(false); | |
| if (scoreText != null) | |
| { | |
| scoreText.text = SCORE_TXT + "0"; | |
| scoreText.gameObject.SetActive(true); | |
| } | |
| if (lifeText != null) | |
| { | |
| lifeText.text = LIFE_TXT + INITIAL_LIFE.ToString(); | |
| lifeText.gameObject.SetActive(true); | |
| } | |
| } | |
| private void DisableTextUI() | |
| { | |
| if (scoreText != null) | |
| { | |
| scoreText.gameObject.SetActive(false); | |
| } | |
| if (lifeText != null) | |
| { | |
| lifeText.gameObject.SetActive(false); | |
| } | |
| } | |
| public void UpdateScore(int score) | |
| { | |
| if(scoreText != null) | |
| { | |
| scoreText.text = SCORE_TXT + score.ToString(); | |
| } | |
| } | |
| public void UpdateLife(int life) | |
| { | |
| if(lifeText != null) | |
| { | |
| lifeText.text = LIFE_TXT + life.ToString(); | |
| } | |
| } | |
| public void PlayerAttacksEnemy(Enemy enemy) | |
| { | |
| if (player != null && enemy != null) | |
| { | |
| enemy.ReceiveDamage(player.Attack); | |
| PlayAttackSfx(); | |
| if (enemy.IsDead) | |
| { | |
| player.AddScore(enemy.TotalPoints); | |
| player.AddToTotalEnemiesKilled(); | |
| } | |
| } | |
| } | |
| public void EnemyAttacksPlayer(Enemy enemy) | |
| { | |
| if(enemy != null && player != null) | |
| { | |
| player.ReceiveDamage(enemy.Attack); | |
| PlayAttackSfx(); | |
| if (player.IsDead) | |
| { | |
| PlayPlayerDeathSfx(); | |
| gameOverOverlay.gameObject.SetActive(true); | |
| DisableTextUI(); | |
| player.gameObject.SetActive(false); | |
| } | |
| UpdateLife(player.Life); | |
| } | |
| } | |
| public void PlayAttackSfx() | |
| { | |
| if(audioSource != null && attackSfx != null) | |
| { | |
| audioSource.clip = attackSfx; | |
| audioSource.Play(); | |
| } | |
| } | |
| public void PlayEnemyDeathSfx() | |
| { | |
| if (audioSource != null && enemyDeathSfx!= null) | |
| { | |
| audioSource.clip = enemyDeathSfx; | |
| audioSource.Play(); | |
| } | |
| } | |
| public void PlayPlayerDeathSfx() | |
| { | |
| if (audioSource != null && playerDeathSfx != null) | |
| { | |
| audioSource.clip = playerDeathSfx; | |
| audioSource.Play(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment