Skip to content

Commit 0d1b78c

Browse files
committed
Merge branch 'ObserverPattern'
2 parents 4346f79 + 9ee77dc commit 0d1b78c

File tree

5 files changed

+5069
-11
lines changed

5 files changed

+5069
-11
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Debugger.cs
2+
// 03-01-2022
3+
// James LaFritz
4+
5+
using System.Collections;
6+
using UnityEngine;
7+
8+
namespace Observer
9+
{
10+
[RequireComponent(typeof(Health), typeof(Level))]
11+
public class Debugger : MonoBehaviour
12+
{
13+
private IEnumerator Start()
14+
{
15+
Health health = GetComponent<Health>();
16+
Level level = GetComponent<Level>();
17+
while (true)
18+
{
19+
yield return new WaitForSeconds(1);
20+
Debug.Log($"Exp: {level!.ExperiencePoints}, Level: {level.CurrentLevel}, Health: {health!.CurrentHealth}");
21+
}
22+
}
23+
24+
private void OnEnable()
25+
{
26+
GetComponent<Level>().levelUp += ShowLevelUp;
27+
}
28+
29+
private void OnDisable()
30+
{
31+
GetComponent<Level>().levelUp -= ShowLevelUp;
32+
}
33+
34+
private void ShowLevelUp(int newLevel)
35+
{
36+
Debug.Log($"You have Leveled up: {newLevel}");
37+
}
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Health.cs
2+
// 03-01-2022
3+
// James LaFritz
4+
5+
using System.Collections;
6+
using UnityEngine;
7+
8+
namespace Observer
9+
{
10+
[RequireComponent(typeof(Level))]
11+
public class Health : MonoBehaviour
12+
{
13+
[SerializeField] private int m_fullHealth = 100;
14+
[SerializeField] private int m_drainPerSecond = 2;
15+
public float CurrentHealth { get; private set; } = 0;
16+
17+
private void Awake()
18+
{
19+
ResetHealth();
20+
StartCoroutine(HealthDrain());
21+
}
22+
23+
private void OnEnable()
24+
{
25+
GetComponent<Level>().levelUp += LevelUpHealth;
26+
}
27+
28+
private void OnDisable()
29+
{
30+
GetComponent<Level>().levelUp -= LevelUpHealth;
31+
}
32+
33+
private void ResetHealth()
34+
{
35+
CurrentHealth = m_fullHealth;
36+
}
37+
38+
private void LevelUpHealth(int currentLevel)
39+
{
40+
ResetHealth();
41+
}
42+
43+
private IEnumerator HealthDrain()
44+
{
45+
while (CurrentHealth > 0)
46+
{
47+
CurrentHealth -= m_drainPerSecond;
48+
yield return new WaitForSeconds(1);
49+
}
50+
}
51+
}
52+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Level.cs
2+
// 03-01-2022
3+
// James LaFritz
4+
5+
using System;
6+
using System.Collections;
7+
using UnityEngine;
8+
using UnityEngine.Events;
9+
10+
namespace Observer
11+
{
12+
public class Level : MonoBehaviour
13+
{
14+
[SerializeField] private UnityEvent m_levelUpEvent;
15+
[SerializeField] private int m_pointsPerLevel = 200;
16+
public int ExperiencePoints { get; private set; }
17+
18+
public int CurrentLevel => ExperiencePoints / m_pointsPerLevel;
19+
20+
public Action<int> levelUp;
21+
22+
private IEnumerator Start()
23+
{
24+
while (true)
25+
{
26+
yield return new WaitForSeconds(0.2f);
27+
GainExperience(10);
28+
}
29+
}
30+
31+
private void GainExperience(int amountToGain)
32+
{
33+
int previousLevel = CurrentLevel;
34+
ExperiencePoints += amountToGain;
35+
if (CurrentLevel <= previousLevel) return;
36+
37+
m_levelUpEvent?.Invoke();
38+
levelUp?.Invoke(CurrentLevel);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)