-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpc.cs
More file actions
66 lines (58 loc) · 2.21 KB
/
Npc.cs
File metadata and controls
66 lines (58 loc) · 2.21 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// The Npc class is rather simple. It contains state Update functions that do
// nothing at the moment, because Npcs are supposed to stand around all day.
//
// Npcs first show the welcome text and then have offers for item trading and
// quests.
using Mirror;
using UnityEngine;
namespace uMMORPG
{
[RequireComponent(typeof(NavMeshMovement))]
[RequireComponent(typeof(NetworkNavMeshAgent))]
// offer components are optional. don't [RequireComponent] them!
public partial class Npc : Entity
{
[Header("Components")]
public NpcGuildManagement guildManagement;
public NpcQuests quests;
public NpcRevive revive;
public NpcTrading trading;
public NpcTeleport teleport;
[Header("Welcome Text")]
[TextArea(1, 30)] public string welcome;
// cache all NpcOffers
[HideInInspector] public NpcOffer[] offers;
void Awake()
{
offers = GetComponents<NpcOffer>();
}
// finite state machine states /////////////////////////////////////////////
[Server] protected override string UpdateServer() { return state; }
[Client]
protected override void UpdateClient()
{
UpdateFootsteps();
}
// attack //////////////////////////////////////////////////////////////////
public override bool CanAttack(Entity entity) { return false; }
// interaction /////////////////////////////////////////////////////////////
protected override void OnInteract()
{
Player player = Player.localPlayer;
// alive, close enough? then talk
// use collider point(s) to also work with big entities
if (health.current > 0 &&
Utils.ClosestDistance(player, this) <= player.interactionRange)
{
UINpcDialogue.singleton.Show();
}
// otherwise walk there
// use collider point(s) to also work with big entities
else
{
Vector3 destination = Utils.ClosestPoint(this, player.transform.position);
player.movement.Navigate(destination, player.interactionRange);
}
}
}
}