Skip to content

Commit ab9c80d

Browse files
refactored event invoke conditions
1 parent 35f121a commit ab9c80d

File tree

12 files changed

+29
-56
lines changed

12 files changed

+29
-56
lines changed

Attributes/Health.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,18 @@ public void TakeDamage(GameObject instigator, float damage) {
6666
return;
6767
}
6868
healthPoints.value = Mathf.Max(healthPoints.value - damage, 0);
69-
takeDamage.Invoke(damage);
70-
if (onHealthUpdate != null) {
71-
onHealthUpdate();
72-
}
69+
takeDamage?.Invoke(damage);
70+
onHealthUpdate?.Invoke();
7371
if (healthPoints.value == 0) {
74-
onDie.Invoke(this);
72+
onDie?.Invoke(this);
7573
Die();
7674
AwardExperience(instigator);
7775
}
7876
}
7977

8078
public void Heal(float healthPercentToRestore) {
8179
healthPoints.value = Mathf.Min(healthPoints.value + (MaxHealthPoints * healthPercentToRestore / 100), MaxHealthPoints);
82-
if (onHealthUpdate != null) {
83-
onHealthUpdate();
84-
}
80+
onHealthUpdate?.Invoke();
8581
}
8682

8783
public object CaptureState() {
@@ -110,9 +106,7 @@ private bool RegenerateHealth() {
110106
}
111107
float regenHealthPoints = baseStats.GetStat(Stat.Health) * (regenerationPercentage / 100);
112108
healthPoints.value = Mathf.Max(healthPoints.value, regenHealthPoints);
113-
if (onHealthUpdate != null) {
114-
onHealthUpdate();
115-
}
109+
onHealthUpdate?.Invoke();
116110
return true;
117111
}
118112

Combat/Fighter.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,8 @@ private void Hit() {
143143
target.TakeDamage(gameObject, damage);
144144
}
145145
}
146-
if (updateTargetUI != null) {
147-
updateTargetUI();
148-
}
146+
updateTargetUI?.Invoke();
147+
149148
}
150149

151150
// Animation Trigger
@@ -181,9 +180,8 @@ public void Cancel() {
181180
StopAttack();
182181
target = null;
183182
mover.Cancel();
184-
if (updateTargetUI != null) {
185-
updateTargetUI();
186-
}
183+
updateTargetUI?.Invoke();
184+
187185
}
188186

189187
public object CaptureState() {

Combat/PickupBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected void Pickup(bool respawnable, float respawnTime) {
3838
} else {
3939
Destroy(gameObject);
4040
}
41-
onPickup.Invoke(this);
41+
onPickup?.Invoke(this);
4242
}
4343

4444
public bool HandleRaycast(GameObject callingObject) {

Combat/Projectile.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,16 @@ private void OnTriggerEnter(Collider other) {
8686
toDestroy.SetActive(false);
8787
}
8888

89-
onHit.Invoke();
90-
91-
if (updateUI != null) {
92-
updateUI();
93-
}
89+
onHit?.Invoke();
90+
updateUI?.Invoke();
9491

9592
StartCoroutine(ReturnToPoolWithDelay(afterHitTTL));
9693
}
9794

9895
private void PointTowardsTarget() {
9996
if (target != null) {
10097
transform.LookAt(GetAimLocation());
101-
onLaunch.Invoke();
98+
onLaunch?.Invoke();
10299
}
103100
}
104101

Combat/Weapon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class Weapon : MonoBehaviour {
88
[SerializeField] private GameObject hitEffect = null;
99

1010
public void OnHit(Health target) {
11-
onHit.Invoke();
11+
onHit?.Invoke();
1212

1313
if (hitEffect != null) {
1414
GameObject impactObj = Instantiate(hitEffect);

Conversing/DialogueManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ public void CloseDialogue() {
5353
Debug.Log("End of conversation with.");
5454
animator.SetBool(IS_OPEN_TRIGGER, false);
5555
dialogLines.Clear();
56-
if (onDialogueClose != null) {
57-
onDialogueClose();
58-
}
56+
onDialogueClose?.Invoke();
5957
}
6058
}
6159

Events/EventSystem.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,18 @@ public class EventSystem : MonoBehaviour {
1717
public static event DialogueInitiatedHandler OnTalkedToNPC;
1818

1919
public void EnemyDied(Health enemy) {
20-
if (OnEnemyDeath != null) {
21-
Debug.Log($"Enemy died: {enemy.name}");
22-
OnEnemyDeath(enemy);
23-
}
20+
Debug.Log($"Enemy died: {enemy.name}");
21+
OnEnemyDeath?.Invoke(enemy);
2422
}
2523

2624
public void ItemPickedUp(PickupBase pickup) {
27-
if (OnItemPickedUp != null) {
28-
Debug.Log($"Item picked up: {pickup.name}");
29-
OnItemPickedUp(pickup);
30-
}
25+
Debug.Log($"Item picked up: {pickup.name}");
26+
OnItemPickedUp?.Invoke(pickup);
3127
}
3228

3329
public void InitiatedDialogue(DialogueInitiator npc) {
34-
if (OnTalkedToNPC != null) {
35-
OnTalkedToNPC(npc);
36-
GetComponent<ActionScheduler>().CancelCurrentAction();
37-
}
30+
OnTalkedToNPC?.Invoke(npc);
31+
GetComponent<ActionScheduler>().CancelCurrentAction();
3832
}
3933
}
4034
}

NPC/DialogueInitiator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void Interact() {
3636

3737
public void StartDialogue(Dialogue dialogue) {
3838
dialogueManager.StartDialogue(dialogue);
39-
onDialogeInitiated.Invoke(this);
39+
onDialogeInitiated?.Invoke(this);
4040
isInteracting = true;
4141
}
4242
}

Questing/QuestGiver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private void AssignQuest() {
3535
private void CheckQuest() {
3636
if (quest != null) {
3737
Debug.Log("Checking quest");
38-
onDialogeInitiated.Invoke(this);
38+
onDialogeInitiated?.Invoke(this);
3939
if (quest.Completed) {
4040
Debug.Log("Quest completed");
4141
quest.CompleteQuest();

Questing/QuestManager.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,14 @@ private void OnDisable() {
3939

4040
private void UpdateUIOnCompletedGoal(Stage stage, Goal goal) {
4141
goal.onComplete -= UpdateUIOnCompletedGoal;
42-
if (onQuestAdded != null) {
43-
onQuestAdded(stage);
44-
}
42+
onQuestAdded?.Invoke(stage);
43+
4544
}
4645

4746
private void UpdateUIOnCompletedStage(Stage stage) {
4847
stage.onActive -= UpdateUIOnCompletedStage;
4948
latestStage = stage;
50-
if (onQuestAdded != null) {
51-
onQuestAdded(stage);
52-
}
49+
onQuestAdded?.Invoke(stage);
5350
}
5451

5552
public Quest AddQuest(string questGiver, string quest) {
@@ -69,10 +66,7 @@ public Quest AddQuest(string questGiver, string quest) {
6966
latestQuest.Stages[i].Goals[j].onComplete += UpdateUIOnCompletedGoal;
7067
}
7168
}
72-
73-
if (onQuestAdded != null) {
74-
onQuestAdded(latestStage);
75-
}
69+
onQuestAdded?.Invoke(latestStage);
7670

7771
return latestQuest;
7872
}

Stats/BaseStats.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private void UpdateLevel() {
4141
int newLevel = CalculateLevel();
4242
if (newLevel > currentLevel.value) {
4343
currentLevel.value = newLevel;
44-
if (onLevelUp != null && onLevelUp()) {
44+
if (onLevelUp?.Invoke() == true) {
4545
LevelUpEffect();
4646
}
4747
}

Stats/Experience.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ public class Experience : MonoBehaviour, ISaveable {
1212

1313
public void GainExperience(float xp) {
1414
experiencePoints += xp;
15-
if (onExperienceGained != null) {
16-
onExperienceGained();
17-
}
15+
onExperienceGained?.Invoke();
1816
}
1917
public object CaptureState() {
2018
return experiencePoints;

0 commit comments

Comments
 (0)