-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummonable.cs
More file actions
60 lines (54 loc) · 2.11 KB
/
Summonable.cs
File metadata and controls
60 lines (54 loc) · 2.11 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
// summonable entity types that are bound to a player (pet, mount, ...)
using Mirror;
using UnityEngine;
namespace uMMORPG
{
public abstract class Summonable : Entity
{
[SyncVar, HideInInspector] public Player owner;
// sync with owner's item //////////////////////////////////////////////////
protected virtual ItemSlot SyncStateToItemSlot(ItemSlot slot)
{
slot.item.summonedHealth = health.current;
slot.item.summonedLevel = level.current;
// remove item if died?
if (((SummonableItem)slot.item.data).removeItemIfDied && health.current == 0)
--slot.amount;
return slot;
}
// find owner item index
// (avoid FindIndex for performance/GC)
public int GetOwnerItemIndex()
{
if (owner != null)
{
for (int i = 0; i < owner.inventory.slots.Count; ++i)
{
ItemSlot slot = owner.inventory.slots[i];
if (slot.amount > 0 && slot.item.summoned == netIdentity)
return i;
}
}
return -1;
}
// to save computations we don't sync to it all the time, it's enough to
// sync in:
// * OnDestroy when unsummoning the pet
// * On experience gain so that level ups and exp are saved properly
// * OnDeath so that people can't cheat around reviving pets
// => after a server crash the health/mana might not be exact, but that's a
// good price to pay to save computations in each Update tick
[Server]
public void SyncToOwnerItem()
{
// owner might be null if server shuts down and owner was destroyed before
if (owner != null)
{
// find the item (amount might be 0 already if a mount died, etc.)
int index = GetOwnerItemIndex();
if (index != -1)
owner.inventory.slots[index] = SyncStateToItemSlot(owner.inventory.slots[index]);
}
}
}
}