/
entity.js
284 lines (237 loc) · 7.59 KB
/
entity.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
* entity.js
*
* TODO:
* -Handle inventory pickups and drops
* -Handle attack requests
*
* @class An entity structure. This can be a Mob, the Player, or Cow.
* @author Richard Habeeb, Addison Shaw
**/
function Entity(layer, r, c, target) {
var self = this;
this.health = 100;
this.max_health = 100;
this.row = r;
this.col = c;
this.x = (PX_PER_CELL*c)+PX_PER_CELL/2;
this.y = (PX_PER_CELL*r)+PX_PER_CELL/2;
this.heading = NORTH;
this.target = target;
this.attack_damage_bonus = 0;
this.level = 0;
this.type; //Player, Cow, Mob
this.move_time = 0.15; //time in tween animation in secs
this.items = new Array();
this.single_use_items = new Array();
this.current_equip; //Equiped item.
this.kills = 0;
this.is_taking_damage = false;
this.time_of_last_hit = (new Date()).getTime();
this.imageObj = new Image();
this.imageObj.src = 'images/Brain Jelly.png';
this.layer = layer;
this.anim = null;
this.loaded = false;
this.sprite = new Kinetic.Image({
x: this.x,
y: this.y,
width: PX_PER_CELL,
height: PX_PER_CELL,
offset: { x : PX_PER_CELL/2, y: PX_PER_CELL/2},
image: this.imageObj
});
/**
* This will fire once the imageObj has been loaded.
*/
this.imageObj.onload = function() {
self.layer.add(self.sprite);
self.layer.draw();
self.loaded = true;
};
/**
* This will return false if this entity is animating, or it's equip is animating.
* @return {bool}
*/
this.IsStopped = function() {
return (this.anim == null || (typeof this.anim === "object" && this.anim.tween._time >= this.anim.tween.duration)) &&
(this.current_equip == null || (typeof this.current_equip === "object" && !this.current_equip.is_animating));
};
/**
* Move and animate this entity in a given heading
* @param {string} heading
*/
this.Move = function(heading) {
if(this.loaded && this.IsStopped()) {
this.FaceHeading(heading);
switch(heading) {
case NORTH:
this.row -= 1;
this.y = Math.max(0, this.y-PX_PER_CELL);
break;
case EAST:
this.col += 1;
this.x = Math.min(WINDOW_WIDTH_PX, this.x+PX_PER_CELL);
break;
case SOUTH:
this.row += 1;
this.y = Math.min(WINDOW_HEIGHT_PX, this.y+PX_PER_CELL);
break;
case WEST:
this.col -= 1;
this.x = Math.max(0, this.x-PX_PER_CELL);
break;
}
this.sprite.setZIndex(this.y);
this.anim = new Kinetic.Tween({
node: this.sprite,
x: this.x,
y: this.y,
duration: this.move_time,
easing: Kinetic.Easings.Linear
});
this.anim.play();
}
}; //END Move
/**
* Change the heading field and flip this sprite, if necesary.
* @param {string} heading
*/
this.FaceHeading = function(heading) {
if(heading == WEST && heading != this.heading) {
this.sprite.setScaleX(-Math.abs(this.sprite.getScaleX()));
this.layer.draw();
} else if(heading == EAST && heading != this.heading) {
this.sprite.setScaleX(Math.abs(this.sprite.getScaleX()));
this.layer.draw();
}
this.heading = heading;
};
/**
* Attack. Calculate the cells affected and the damages and return them. Animate the equipped weapon.
* UNFINISHED!
* @return {Array({r: int, c: int, damage: int})}
*/
this.Attack = function() {
if(this.loaded && typeof this.current_equip != "undefined" && !this.current_equip.is_animating && (this.current_equip.type == ITEM_TYPES.EQUIP || this.current_equip.type == ITEM_TYPES.SINGLE_USE_WEAPON)) {
var cells_affected = new Array();
if(this.current_equip.type == ITEM_TYPES.EQUIP) {
if(this.current_equip.melee) {
if(this.current_equip.single_direction) {
var pos = {x:this.x, y:this.y};
if(this.heading == NORTH) {
pos.y -= this.current_equip.range*PX_PER_CELL;
for(var i = this.row-1; i >= Math.max(this.row-this.current_equip.range, 0); i--)
cells_affected.push({r:i, c:this.col, damage:this.current_equip.base_damage+this.attack_damage_bonus});
} else if(this.heading == EAST) {
pos.x += this.current_equip.range*PX_PER_CELL;
for(var i = this.col+1; i <= Math.min(this.col+this.current_equip.range, WINDOW_WIDTH_CELLS); i++)
cells_affected.push({r:this.row, c:i, damage:this.current_equip.base_damage+this.attack_damage_bonus});
} else if(this.heading == SOUTH) {
pos.y += this.current_equip.range*PX_PER_CELL;
for(var i = this.row+1; i <= Math.min(this.row+this.current_equip.range, WINDOW_HEIGHT_CELLS); i++)
cells_affected.push({r:i, c:this.col, damage:this.current_equip.base_damage+this.attack_damage_bonus});
} else if(this.heading == WEST) {
pos.x -= this.current_equip.range*PX_PER_CELL;
for(var i = this.col-1; i >= Math.max(this.col-this.current_equip.range, 0); i--)
cells_affected.push({r:this.row, c:i, damage:this.current_equip.base_damage+this.attack_damage_bonus});
}
this.current_equip.FaceHeading(this.heading);
this.current_equip.Animate(pos.x, pos.y);
} else {
}
} else {
}
}
return cells_affected;
}
return null;
}; //END Attack
/**
* Take damage and animate. return the remaining health. Target the attacker.
* @param {int} amount
* @param {Entity} from entity
* @return {int}
*/
this.TakeDamage = function(amount, from_entity) {
if(this.loaded && !this.is_taking_damage) {
this.health = Math.max(this.health-amount, 0);
var init_rotation = this.sprite.getRotation();
var init_opacity = this.sprite.getOpacity();
this.is_taking_damage = true;
this.anim = new Kinetic.Tween({
node: this.sprite,
opacity: 0.1,
rotation: (Math.random() > 0.5) ? Math.random() : -Math.random(),
duration: 0.1,
easing: Kinetic.Easings.EaseInOut,
onFinish: function() {
self.anim = new Kinetic.Tween({
node: self.sprite,
opacity: init_opacity,
rotation: init_rotation,
duration: 0.1,
easing: Kinetic.Easings.EaseInOut,
onFinish: function() { self.is_taking_damage = false;}
});
self.anim.play();
if(self.health <= 0) self.sprite.destroy();
}
});
this.anim.play();
if(this.type == MOB) {
this.target = from_entity;
}
}
return this.health;
}; //END TakeDamage
/**
* Use the queued up single use item. If the item causes damage, it returns an array of cells affected.
* UNFINISHED
* @return {Array({r: int, c: int, damage: int})}
*/
this.UseSingleUseItem = function() {
if (this.single_use_items.length > 0) {
if(this.single_use_items[0].type == ITEM_TYPES.SINGLE_USE_BUFF) {
if(this.single_use_items[0].buff_attribute.indexOf("health") !== -1) {
this.health = Math.min(this.health+this.single_use_items[0].buff_amount, this.max_health)
}
this.single_use_items[0].Animate(this.x, this.y);
}
this.single_use_items.splice(0, 1);
return null;
}
}
/**
* Adds the given item to inventory, if it is an equip, equip it.
* @param {Item} item
*/
this.AddItem = function(item)
{
if (item.type == ITEM_TYPES.EQUIP) {
this.items.push(item);
this.current_equip = item;
} else {
this.single_use_items.push(item);
}
}
/**
* Removes the given item from 'inventory'
* @param {Item} item
*/
this.RemoveItem = function(item)
{
if (array.indexOf(item) != -1)
{
this.items.splice(array.indexOf(item), 1);
}
}
/**
* Handle what items are dropped when this entity is dead.
* @return {Array({item})}
*/
this.HandleDrops = function()
{
return this.single_use_items;
}
}