-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerQuests.cs
More file actions
175 lines (161 loc) · 6.68 KB
/
PlayerQuests.cs
File metadata and controls
175 lines (161 loc) · 6.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using UnityEngine;
using Mirror;
namespace uMMORPG
{
[RequireComponent(typeof(PlayerInventory))]
[DisallowMultipleComponent]
public class PlayerQuests : NetworkBehaviour
{
[Header("Components")]
public Player player;
public PlayerInventory inventory;
[Header("Quests")] // contains active and completed quests (=all)
public int activeQuestLimit = 10;
public readonly SyncList<Quest> quests = new SyncList<Quest>();
// quests //////////////////////////////////////////////////////////////////
public int GetIndexByName(string questName)
{
// (avoid Linq because it is HEAVY(!) on GC and performance)
for (int i = 0; i < quests.Count; ++i)
if (quests[i].name == questName)
return i;
return -1;
}
// helper function to check if the player has completed a quest before
public bool HasCompleted(string questName)
{
// (avoid Linq because it is HEAVY(!) on GC and performance)
foreach (Quest quest in quests)
if (quest.name == questName && quest.completed)
return true;
return false;
}
// count the completed quests
public int CountIncomplete()
{
int count = 0;
foreach (Quest quest in quests)
if (!quest.completed)
++count;
return count;
}
// helper function to check if a player has an active (not completed) quest
public bool HasActive(string questName)
{
// (avoid Linq because it is HEAVY(!) on GC and performance)
foreach (Quest quest in quests)
if (quest.name == questName && !quest.completed)
return true;
return false;
}
// helper function to check if the player can accept a new quest
// note: no quest.completed check needed because we have a'not accepted yet'
// check
public bool CanAccept(ScriptableQuest quest)
{
// not too many quests yet?
// has required level?
// not accepted yet?
// has finished predecessor quest (if any)?
return CountIncomplete() < activeQuestLimit &&
player.level.current >= quest.requiredLevel && // has required level?
GetIndexByName(quest.name) == -1 && // not accepted yet?
(quest.predecessor == null || HasCompleted(quest.predecessor.name));
}
[Command]
public void CmdAccept(int npcQuestIndex)
{
// validate
// use collider point(s) to also work with big entities
if (player.state == "IDLE" &&
player.target != null &&
player.target.health.current > 0 &&
player.target is Npc npc &&
0 <= npcQuestIndex && npcQuestIndex < npc.quests.quests.Length &&
Utils.ClosestDistance(player, npc) <= player.interactionRange)
{
ScriptableQuestOffer npcQuest = npc.quests.quests[npcQuestIndex];
if (npcQuest.acceptHere && CanAccept(npcQuest.quest))
quests.Add(new Quest(npcQuest.quest));
}
}
// helper function to check if the player can complete a quest
public bool CanComplete(string questName)
{
// has the quest and not completed yet?
int index = GetIndexByName(questName);
if (index != -1 && !quests[index].completed)
{
// fulfilled?
Quest quest = quests[index];
if(quest.IsFulfilled(player))
{
// enough space for reward item (if any)?
return quest.rewardItem == null ||
inventory.CanAdd(new Item(quest.rewardItem), 1);
}
}
return false;
}
[Command]
public void CmdComplete(int npcQuestIndex)
{
// validate
// use collider point(s) to also work with big entities
if (player.state == "IDLE" &&
player.target != null &&
player.target.health.current > 0 &&
player.target is Npc npc &&
0 <= npcQuestIndex && npcQuestIndex < npc.quests.quests.Length &&
Utils.ClosestDistance(player, npc) <= player.interactionRange)
{
ScriptableQuestOffer npcQuest = npc.quests.quests[npcQuestIndex];
if (npcQuest.completeHere)
{
int index = GetIndexByName(npcQuest.quest.name);
if (index != -1)
{
// can complete it? (also checks inventory space for reward, if any)
Quest quest = quests[index];
if (CanComplete(quest.name))
{
// call quest.OnCompleted to remove quest items from
// inventory, etc.
quest.OnCompleted(player);
// gain rewards
player.gold += quest.rewardGold;
player.experience.current += quest.rewardExperience;
if (quest.rewardItem != null)
inventory.Add(new Item(quest.rewardItem), 1);
// complete quest
quest.completed = true;
quests[index] = quest;
}
}
}
}
}
// combat //////////////////////////////////////////////////////////////////
[Server]
public void OnKilledEnemy(Entity victim)
{
// call OnKilled in all active (not completed) quests
for (int i = 0; i < quests.Count; ++i)
if (!quests[i].completed)
quests[i].OnKilled(player, i, victim);
}
// ontrigger ///////////////////////////////////////////////////////////////
[ServerCallback]
void OnTriggerEnter(Collider col)
{
// quest location? then call OnLocation in active (not completed) quests
// (we use .CompareTag to avoid .tag allocations)
if (col.CompareTag("QuestLocation"))
{
for (int i = 0; i < quests.Count; ++i)
if (!quests[i].completed)
quests[i].OnLocation(player, i, col);
}
}
}
}