Subversion Repository Public Repository

Nextrek

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using UnityEngine;
using System.Collections.Generic;

/// <summary>
/// Since it would be incredibly tedious to create thousands of unique items by hand, a simple solution is needed.
/// Separating items into 2 parts is that solution. Base item contains stats that the item would have if it was max
/// level. All base items are created with their stats at max level. Game item, the second item class, has an
/// effective item level which is used to calculate effective item stats. Game items can be generated with a random
/// level (clamped within base item's min/max level range), and with random quality affecting the item's stats.
/// </summary>

[System.Serializable]
public class InvGameItem
{
	public enum Quality
	{
		Broken,
		Cursed,
		Damaged,
		Worn,
		Sturdy,			// Normal quality
		Polished,
		Improved,
		Crafted,
		Superior,
		Enchanted,
		Epic,
		Legendary,
		_LastDoNotUse,	// Flash export doesn't support Enum.GetNames :(
	}

	// ID of the base item used to create this game item
	[SerializeField] int mBaseItemID = 0;

	/// <summary>
	/// Item quality -- applies a penalty or bonus to all base stats.
	/// </summary>

	public Quality quality = Quality.Sturdy;

	/// <summary>
	/// Item's effective level.
	/// </summary>

	public int itemLevel = 1;

	// Cached for speed
	InvBaseItem mBaseItem;

	/// <summary>
	/// ID of the base item used to create this one.
	/// </summary>

	public int baseItemID { get { return mBaseItemID; } }

	/// <summary>
	/// Base item used by this game item.
	/// </summary>

	public InvBaseItem baseItem
	{
		get
		{
			if (mBaseItem == null)
			{
				mBaseItem = InvDatabase.FindByID(baseItemID);
			}
			return mBaseItem;
		}
	}

	/// <summary>
	/// Game item's name should prefix the quality
	/// </summary>

	public string name
	{
		get
		{
			if (baseItem == null) return null;
			return quality.ToString() + " " + baseItem.name;
		}
	}

	/// <summary>
	/// Put your formula for calculating the item stat modifier here.
	/// Simplest formula -- scale it with quality and item level.
	/// Since all stats on base items are specified at max item level,
	/// calculating the effective multiplier is as simple as itemLevel/maxLevel.
	/// </summary>

	public float statMultiplier
	{
		get
		{
			float mult = 0f;

			switch (quality)
			{
				case Quality.Cursed:	mult = -1f;		break;
				case Quality.Broken:	mult = 0f;		break;
				case Quality.Damaged:	mult = 0.25f;	break;
				case Quality.Worn:		mult = 0.9f;	break;
				case Quality.Sturdy:	mult = 1f;		break;
				case Quality.Polished:	mult = 1.1f;	break;
				case Quality.Improved:	mult = 1.25f;	break;
				case Quality.Crafted:	mult = 1.5f;	break;
				case Quality.Superior:	mult = 1.75f;	break;
				case Quality.Enchanted:	mult = 2f;		break;
				case Quality.Epic:		mult = 2.5f;	break;
				case Quality.Legendary:	mult = 3f;		break;
			}

			// Take item's level into account
			float linear = itemLevel / 50f;

			// Add a curve for more interesting results
			mult *= Mathf.Lerp(linear, linear * linear, 0.5f);
			return mult;
		}
	}

	/// <summary>
	/// Item's color based on quality. You will likely want to change this to your own colors.
	/// </summary>

	public Color color
	{
		get
		{
			Color c = Color.white;

			switch (quality)
			{
				case Quality.Cursed:	c = Color.red; break;
				case Quality.Broken:	c = new Color(0.4f, 0.2f, 0.2f); break;
				case Quality.Damaged:	c = new Color(0.4f, 0.4f, 0.4f); break;
				case Quality.Worn:		c = new Color(0.7f, 0.7f, 0.7f); break;
				case Quality.Sturdy:	c = new Color(1.0f, 1.0f, 1.0f); break;
				case Quality.Polished:	c = NGUIMath.HexToColor(0xe0ffbeff); break;
				case Quality.Improved:	c = NGUIMath.HexToColor(0x93d749ff); break;
				case Quality.Crafted:	c = NGUIMath.HexToColor(0x4eff00ff); break;
				case Quality.Superior:	c = NGUIMath.HexToColor(0x00baffff); break;
				case Quality.Enchanted: c = NGUIMath.HexToColor(0x7376fdff); break;
				case Quality.Epic:		c = NGUIMath.HexToColor(0x9600ffff); break;
				case Quality.Legendary: c = NGUIMath.HexToColor(0xff9000ff); break;
			}
			return c;
		}
	}

	/// <summary>
	/// Create a game item with the specified ID.
	/// </summary>

	public InvGameItem (int id) { mBaseItemID = id; }

	/// <summary>
	/// Create a game item with the specified ID and base item.
	/// </summary>

	public InvGameItem (int id, InvBaseItem bi) { mBaseItemID = id; mBaseItem = bi; }

	/// <summary>
	/// Calculate and return the list of effective stats based on item level and quality.
	/// </summary>

	public List<InvStat> CalculateStats ()
	{
		List<InvStat> stats = new List<InvStat>();

		if (baseItem != null)
		{
			float mult = statMultiplier;
			List<InvStat> baseStats = baseItem.stats;

			for (int i = 0, imax = baseStats.Count; i < imax; ++i)
			{
				InvStat bs = baseStats[i];
				int amount = Mathf.RoundToInt(mult * bs.amount);
				if (amount == 0) continue;

				bool found = false;

				for (int b = 0, bmax = stats.Count; b < bmax; ++b)
				{
					InvStat s = stats[b];

					if (s.id == bs.id && s.modifier == bs.modifier)
					{
						s.amount += amount;
						found = true;
						break;
					}
				}

				if (!found)
				{
					InvStat stat = new InvStat();
					stat.id = bs.id;
					stat.amount = amount;
					stat.modifier = bs.modifier;
					stats.Add(stat);
				}
			}

			// This would be the place to determine if it's a weapon or armor and sort stats accordingly
			stats.Sort(InvStat.CompareArmor);
		}
		return stats;
	}
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Examples/Scripts/InventorySystem/System/InvGameItem.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000