-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.cs
More file actions
98 lines (87 loc) · 3.93 KB
/
Item.cs
File metadata and controls
98 lines (87 loc) · 3.93 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
// The Item struct only contains the dynamic item properties, so that the static
// properties can be read from the scriptable object.
//
// Items have to be structs in order to work with SyncLists.
//
// Use .Equals to compare two items. Comparing the name is NOT enough for cases
// where dynamic stats differ. E.g. two pets with different levels shouldn't be
// merged.
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using Mirror;
namespace uMMORPG
{
[Serializable]
public partial struct Item
{
// hashcode used to reference the real ScriptableItem (can't link to data
// directly because synclist only supports simple types). and syncing a
// string's hashcode instead of the string takes WAY less bandwidth.
public int hash;
// current durability
public int durability;
// dynamic stats (cooldowns etc. later)
public NetworkIdentity summoned; // summonable that's currently summoned
public int summonedHealth; // stored in item while summonable unsummoned
public int summonedLevel; // stored in item while summonable unsummoned
public long summonedExperience; // stored in item while summonable unsummoned
// constructors
public Item(ScriptableItem data)
{
hash = data.name.GetStableHashCode();
durability = data.maxDurability;
summoned = null;
summonedHealth = data is SummonableItem summonable ? summonable.summonPrefab.health.max : 0;
summonedLevel = data is SummonableItem ? 1 : 0;
summonedExperience = 0;
}
// wrappers for easier access
public ScriptableItem data
{
get
{
// show a useful error message if the key can't be found
// note: ScriptableItem.OnValidate 'is in resource folder' check
// causes Unity SendMessage warnings and false positives.
// this solution is a lot better.
if (!ScriptableItem.All.ContainsKey(hash))
throw new KeyNotFoundException("There is no ScriptableItem with hash=" + hash + ". Make sure that all ScriptableItems are in the Resources folder so they are loaded properly.");
return ScriptableItem.All[hash];
}
}
public string name => data.name;
public int maxStack => data.maxStack;
public int maxDurability => data.maxDurability;
public float DurabilityPercent()
{
return (durability != 0 && maxDurability != 0) ? (float)durability / (float)maxDurability : 0;
}
public long buyPrice => data.buyPrice;
public long sellPrice => data.sellPrice;
public long itemMallPrice => data.itemMallPrice;
public bool sellable => data.sellable;
public bool tradable => data.tradable;
public bool destroyable => data.destroyable;
public Sprite image => data.image;
// helper function to check for valid durability if a durability item
public bool CheckDurability() =>
maxDurability == 0 || durability > 0;
// tooltip
public string ToolTip()
{
// note: caching StringBuilder is worse for GC because .Clear frees the internal array and reallocates.
StringBuilder tip = new StringBuilder(data.ToolTip());
// show durability only if item has durability
if (maxDurability > 0)
tip.Replace("{DURABILITY}", (DurabilityPercent() * 100).ToString("F0"));
tip.Replace("{SUMMONEDHEALTH}", summonedHealth.ToString());
tip.Replace("{SUMMONEDLEVEL}", summonedLevel.ToString());
tip.Replace("{SUMMONEDEXPERIENCE}", summonedExperience.ToString());
// addon system hooks
Utils.InvokeMany(typeof(Item), this, "ToolTip_", tip);
return tip.ToString();
}
}
}