/
AmIDeadYet.cs
49 lines (40 loc) · 1.21 KB
/
AmIDeadYet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Unity C# Survival Guide - Challenge: Are You Alive?
/// </summary>
///
/// <remarks>
/// Instantiated By:
/// Attached To: Main Camera
///
/// Createa program that checks to see if the player is alive still. When you hit
/// the space bar, you should damage the player a random amount. If the Player is
/// dead, print out "The player has died!"
///
/// Bonus: Disable the damage function once a player is dead.
///
/// Things I discovered:
///
/// </remarks>
///
public class AmIDeadYet : MonoBehaviour
{
[SerializeField]
private int playerHealth = 100;
void Update()
{
if ( playerStillAlive()
&& AttackSuccessful() ) { DoDamage(); }
}
private bool playerStillAlive() { return playerHealth > 0; }
private bool AttackSuccessful() { return Input.GetKeyDown(KeyCode.Space); }
private void DoDamage()
{
playerHealth -= CalculateDamage();
if ( !playerStillAlive() ) { publishObituary(); }
}
private int CalculateDamage() { return Random.Range(0, playerHealth + 1); }
private void publishObituary() { Debug.Log("Player has died!"); }
}