Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
Apply OCP P3
Browse files Browse the repository at this point in the history
NPC classes refactoring to enable player AI interactions extensions
  • Loading branch information
Kassout committed Apr 10, 2023
1 parent d51f2ca commit 2d1d2e8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
26 changes: 26 additions & 0 deletions SOLID/SOLID/Assets/Scripts/NPC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEngine;
using UnityEngine.Events;

public abstract class NPC : MonoBehaviour
{
public UnityEvent<string> OnSpeak;

[SerializeField]
private AudioSource audioSource;

private void Awake()
{
audioSource = GetComponent<AudioSource>();
}

public virtual void Interact()
{
OnSpeak?.Invoke(GetText());
audioSource.Play();
}

protected virtual string GetText()
{
return string.Empty;
}
}
11 changes: 11 additions & 0 deletions SOLID/SOLID/Assets/Scripts/NPC.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions SOLID/SOLID/Assets/Scripts/NPCEnemy.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using UnityEngine;
using UnityEngine.Events;

public class NPCEnemy : MonoBehaviour
public class NPCEnemy : NPC
{
public UnityEvent<string> OnSpeak;
public AudioSource audioSource;
public string text = "I deal 10 physical damage ( •̀ᴗ•́ )و ̑̑ ";

public void GetHit()
public override void Interact()
{
OnSpeak?.Invoke(text);
audioSource.Play();
base.Interact();

FindObjectOfType<Player>().ReceiveDamaged();
}

protected override string GetText()
{
return text;
}
}
13 changes: 3 additions & 10 deletions SOLID/SOLID/Assets/Scripts/NPCFriendly.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
using UnityEngine;
using UnityEngine.Events;

public class NPCFriendly : MonoBehaviour
public class NPCFriendly : NPC
{
public UnityEvent<string> OnSpeak;
public AudioSource audioSource;
public string text = "Hi there. Look out for that KOBOLD on the other side!";

public void Talk()
protected override string GetText()
{
OnSpeak?.Invoke(text);
audioSource.Play();

return text;
}
}
8 changes: 2 additions & 6 deletions SOLID/SOLID/Assets/Scripts/PlayerAIInteractions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ public void Interact(bool isSpriteFlipped)
RaycastHit2D hit = Physics2D.Raycast(raycastPoint.position, isSpriteFlipped ? Vector3.left : Vector3.right,1);
if(hit.collider != null)
{
if (hit.collider.GetComponent<NPCEnemy>())
if (hit.collider.TryGetComponent(out NPC npc))
{
hit.collider.GetComponent<NPCEnemy>().GetHit();
}
else if (hit.collider.GetComponent<NPCFriendly>())
{
hit.collider.GetComponent<NPCFriendly>().Talk();
npc.Interact();
}
}
}
Expand Down

0 comments on commit 2d1d2e8

Please sign in to comment.