Skip to content

Commit e0547da

Browse files
committed
NPC events
1 parent b449efa commit e0547da

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

Assets/Components/NPC/NPC.cs

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,7 @@ protected void Awake()
2828

2929
private void OnDisable()
3030
{
31-
if(!npcController)
32-
{
33-
Debug.LogWarning($"[{gameObject.name}]: No NPCController found!");
34-
return;
35-
}
36-
37-
npcController.Unregister(this);
31+
npcController?.Unregister(this);
3832
}
3933

4034
protected void Start()
@@ -53,5 +47,12 @@ protected void SetTexture(Texture texture)
5347
renderer.material.mainTexture = texture;
5448
}
5549

50+
protected void NotifyEvent(string message)
51+
{
52+
npcController.NotifyEvent(this, message);
53+
}
54+
5655
public abstract void Interact();
56+
57+
public abstract void ReceiveMessage(string message);
5758
}

Assets/Components/NPC/NPC_Witch.cs

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private void SetMood(Mood mood)
3232
{
3333
currentMood = mood;
3434
SetTexture(textures[(int)mood % textures.Count]);
35+
NotifyEvent($"I am now {mood}");
3536
}
3637

3738
private void SetNextMood()
@@ -43,4 +44,9 @@ public override void Interact()
4344
{
4445
SetNextMood();
4546
}
47+
48+
public override void ReceiveMessage(string message)
49+
{
50+
Debug.Log($"[NPC_Witch] Received: {message}");
51+
}
4652
}

Assets/Controllers/NPCController.cs

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ private void Update()
1515
InteractRandomly();
1616
}
1717

18+
private void OnDisable()
19+
{
20+
npcs.Clear();
21+
}
22+
1823
private void InteractRandomly()
1924
{
2025
if (Time.frameCount % 60 != 0) return;
@@ -33,6 +38,13 @@ public void Register(NPC npc)
3338

3439
public void Unregister(NPC npc)
3540
{
41+
Debug.Log($"[NPCController]: Unregistering {npc.name}");
3642
npcs.Remove(npc);
3743
}
44+
45+
public void NotifyEvent(NPC npc, string message)
46+
{
47+
Debug.Log($"[NPCController]: ({npc.name}) says: {message}");
48+
npc.ReceiveMessage("GOTCHA!");
49+
}
3850
}

Assets/Scenes/Test_NPC.unity

+44
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,50 @@ PrefabInstance:
321321
objectReference: {fileID: 0}
322322
m_RemovedComponents: []
323323
m_SourcePrefab: {fileID: 100100000, guid: 2d2255df223f6d0478caba7808cbee99, type: 3}
324+
--- !u!1 &1098276951
325+
GameObject:
326+
m_ObjectHideFlags: 0
327+
m_CorrespondingSourceObject: {fileID: 0}
328+
m_PrefabInstance: {fileID: 0}
329+
m_PrefabAsset: {fileID: 0}
330+
serializedVersion: 6
331+
m_Component:
332+
- component: {fileID: 1098276953}
333+
- component: {fileID: 1098276952}
334+
m_Layer: 0
335+
m_Name: Main
336+
m_TagString: Untagged
337+
m_Icon: {fileID: 0}
338+
m_NavMeshLayer: 0
339+
m_StaticEditorFlags: 0
340+
m_IsActive: 1
341+
--- !u!114 &1098276952
342+
MonoBehaviour:
343+
m_ObjectHideFlags: 0
344+
m_CorrespondingSourceObject: {fileID: 0}
345+
m_PrefabInstance: {fileID: 0}
346+
m_PrefabAsset: {fileID: 0}
347+
m_GameObject: {fileID: 1098276951}
348+
m_Enabled: 1
349+
m_EditorHideFlags: 0
350+
m_Script: {fileID: 11500000, guid: f9e09136207ae48d3b7fbcf15660cd53, type: 3}
351+
m_Name:
352+
m_EditorClassIdentifier:
353+
--- !u!4 &1098276953
354+
Transform:
355+
m_ObjectHideFlags: 0
356+
m_CorrespondingSourceObject: {fileID: 0}
357+
m_PrefabInstance: {fileID: 0}
358+
m_PrefabAsset: {fileID: 0}
359+
m_GameObject: {fileID: 1098276951}
360+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
361+
m_LocalPosition: {x: -0.3906647, y: -14.688092, z: 7.7010303}
362+
m_LocalScale: {x: 1, y: 1, z: 1}
363+
m_ConstrainProportionsScale: 0
364+
m_Children: []
365+
m_Father: {fileID: 0}
366+
m_RootOrder: 4
367+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
324368
--- !u!4 &1234819821009000913
325369
Transform:
326370
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)