-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.js
213 lines (188 loc) · 6.23 KB
/
Item.js
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
213
import { Entity } from "./Entity.js";
import { Renderer } from "./Renderer.js";
import { Transform } from "./Transform.js";
export { Item, Weapon, Armor, Consumable };
class Item extends Entity
{
constructor(name = "", transform = new Transform(), renderer = new Renderer(), game = undefined)
{
super(name, transform, renderer, game);
}
update(delta)
{
this.renderer.playAnimation(1/6, true);
}
/**
* Checks if passed item is a weapon
* @param {*} item
* @returns boolean result of statement
*/
static isWeapon(item)
{
return item instanceof Weapon;
}
/**
* Checks if passed item is an armor set
* @param {*} item
* @returns boolean result of statement
*/
static isArmor(item)
{
return item instanceof Armor;
}
/**
* Checks if passed item is a consumable
* @param {*} item
* @returns boolean result of statement
*/
static isConsumable(item)
{
return item instanceof Consumable;
}
/**
* Take an object, checks if it is an item. If so, compares their names to check for equality
* @param {*} obj represents the object being compared
* @returns boolean result of comparison
*/
equals(obj)
{
if (!(obj instanceof Item))
return false;
if (this.itemName == obj.itemName)
return true;
}
}
class Weapon extends Item
{
lowDamage;
highDamage;
trace;
constructor(name, lowDamage, highDamage, trace, renderer, transform = new Transform(), game = undefined)
{
super(name, transform, renderer, game);
this.lowDamage = lowDamage;
this.highDamage = highDamage;
this.trace = trace;
}
/**
* Returns the amount of damage this weapon should deal, based on an external accuracy value.
* The accuracy number, which should be between 0 and 1, scales the damage from the weapons
* predefined minimum damage to its maximum damage.
*
* @param {Number} accuracy A number (float) between 0 and 1 that determines how much to scale the damage
* @returns Returns a number that represents the amount of damage to be dealt based on the given accuracy.
*/
damage(accuracy)
{
return accuracy * (highDamage - lowDamage) + lowDamage;
}
/**
* @returns trace pattern of this weapon
*/
getTrace()
{
return this.trace;
}
/**
* Serializes Weapon object to a string representation of a JSON object
* @returns the JSON string of this weapon
*/
serialize()
{
return '{ "type":"Weapon", "name":"' + this._Name +
'", "transform": ' + this.transform.serialize() + ', "renderer": ' + this.renderer.serialize()
+ '", "damage": { "low":' + this.lowDamage + ', "high":' + this.highDamage + '}'
+ ', "trace":"' + this.trace + '" }';
}
/**
* Deserializes the passed JSON to a new Weapon object
* @param {*} obj the JSON object representation
* @param {*} game the current game instance
* @returns a new Weapon object defined by the JSON object
*/
static deserialize(obj, game)
{
return new Weapon(obj.name, obj.damage.low, obj.damage.high, obj.trace, Renderer.deserialize(obj.renderer, game.stage), Transform.deserialize(obj.transform), game);
}
}
class Armor extends Item
{
lowProtection;
highProtection;
trace;
constructor(name, lowProtection, highProtection, trace, renderer, transform = new Transform(), game = undefined)
{
super(name, transform, renderer, game);
this.lowProtection = lowProtection;
this.highProtection = highProtection;
this.trace = trace;
}
/**
* Calculates the protection value of this armor due to an attack
* @param {Number} accuracy
* @returns the value of protection calculated via the accuracy of an associated attack
*/
protection(accuracy)
{
return accuracy * (highProtection - lowProtection) + lowProtection;
}
/**
* Serializes the current armor object into a JSON string
* @returns the JSON string of this Armor object
*/
serialize()
{
return '{ "type":"Armor", "name" : "' + this._Name
+ '", "transform": ' + this.transform.serialize() + ', "renderer": ' + this.renderer.serialize()
+ '", "protection":' + this.protection + '}';
}
/**
* Deserializes & establishes a new Armor object based on the passed JSON representation
* @param {*} obj the JSON object
* @param {*} game the current game instance
* @returns a new Armor object created from the passed JSON
*/
static deserialize(obj, game)
{
return new Armor(obj.name, obj.protection, Renderer.deserialize(obj.renderer), Transform.deserialize(obj.transform), game);
}
}
class Consumable extends Item
{
// Amount of items that can be held of the variety
stackCount;
constructor(name, renderer, count = 1, transform = new Transform(), game = undefined)
{
super(name, transform, renderer, game);
this.stackCount = count;
}
/**
* Stacks this item with another consumable, combining their stack counts.
*
* @param {Consumable} consumable The consumable to stack into this.
*/
stack(consumable)
{
this.stackCount += consumable.stackCount;
}
/**
* Serializes the current consumable object into a JSON string
* @returns the JSON string of this consumable object
*/
serialize()
{
return '{ "type":"Consumable", "name" : "' + this._Name
+ '", "transform": ' + this.transform.serialize() + ', "renderer": ' + this.renderer.serialize()
+ '", "count":' + this.stackCount + '}';
}
/**
* Deserializes & establishes a new consumable object based on the passed JSON representation
* @param {*} obj the JSON object
* @param {*} game the current game instance
* @returns a new consumable object created from the passed JSON
*/
static deserialize(obj, game)
{
return new Consumable(obj.name, Renderer.deserialize(obj.renderer), obj.count, Transform.deserialize(obj.transform), game);
}
}