-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.js
328 lines (313 loc) · 10.7 KB
/
Player.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
function Player(area) {
powerupjs.AnimatedGameObject.call(this, 1, ID.layer_background_1, ID.player);
this.lastDirection = "front";
this.animated = true;
this.loadAnimation(sprites.main_character["idle"].front, "idle_front", true);
this.loadAnimation(sprites.main_character["idle"].back, "idle_back", true);
this.loadAnimation(sprites.main_character["idle"].right, "idle_right", true);
this.loadAnimation(sprites.main_character["idle"].left, "idle_left", true);
this.loadAnimation(sprites.main_character["idle_sword"].front, "sword_idle_front", true);
this.loadAnimation(sprites.main_character["idle_sword"].back, "sword_idle_back", true);
this.loadAnimation(sprites.main_character["idle_sword"].right, "sword_idle_right", true);
this.loadAnimation(sprites.main_character["idle_sword"].left, "sword_idle_left", true);
this.mode = "walking";
this.loadAnimation(
sprites.main_character["run"].front,
"run_front",
true,
0.08
);
this.loadAnimation(
sprites.main_character["run"].back,
"run_back",
true,
0.08
);
this.loadAnimation(
sprites.main_character["run"].right,
"run_right",
true,
0.08
);
this.loadAnimation(
sprites.main_character["run"].left,
"run_left",
true,
0.08
);
this.loadAnimation(
sprites.main_character["tend"].front,
"tend_front",
false,
0.09
);
this.position = new powerupjs.Vector2(200, 300);
this.areaIndex = area;
}
Player.prototype = Object.create(powerupjs.AnimatedGameObject.prototype);
Player.prototype.update = function (delta) {
powerupjs.AnimatedGameObject.prototype.update.call(this, delta);
if (
powerupjs.Game.gameWorld.map.mode !== "playing" ||
powerupjs.Game.gameWorld.inventory.open
)
return;
if (powerupjs.Keyboard.down(38)) {
if (this.velocity.x === 0) this.playAnimation("run_back");
this.lastDirection = "back";
this.velocity.y = -250;
this.mode = "walking";
} else if (powerupjs.Keyboard.down(40)) {
if (this.velocity.x === 0) this.playAnimation("run_front");
this.lastDirection = "front";
this.velocity.y = 250;
this.mode = "walking";
} else {
this.velocity.y = 0;
}
if (powerupjs.Keyboard.down(37)) {
this.playAnimation("run_left");
this.lastDirection = "left";
this.velocity.x = -250;
this.mode = "walking";
} else if (powerupjs.Keyboard.down(39)) {
this.playAnimation("run_right");
this.lastDirection = "right";
this.velocity.x = 250;
this.mode = "walking";
} else {
this.velocity.x = 0;
}
if (
this.velocity.x === 0 &&
this.velocity.y === 0 &&
this.mode === "walking"
) {
// if (powerupjs.Game.gameWorld.currentTool === 'none')
this.playAnimation("idle_" + this.lastDirection);
// else if (powerupjs.Game.gameWorld.currentTool === 'sword'){
// this.playAnimation("sword_idle_" + this.lastDirection)
// }
}
var objects = powerupjs.Game.gameWorld.map.areas[
powerupjs.Game.gameWorld.map.currentAreaIndex
].find(ID.objects);
var tiles = powerupjs.Game.gameWorld.map.areas[
powerupjs.Game.gameWorld.map.currentAreaIndex
].find(ID.tiles);
for (var i = 0; i < objects.listLength; i++) {
var object = objects.at(i);
var inventory = powerupjs.Game.gameWorld.inventory; // Pick up effects
if (powerupjs.Keyboard.pressed(32)) {
if (this.mode === "tending") return;
if (
object.type === "plant" &&
object.velocity.y === 0 &&
object.velocity.x === 0 &&
this.boundingBox.intersects(object.boundingBox) && // Dropped fruit
object.visible &&
inventory.itemGrid.gameObjects.length < 27
) {
for (var k = 0; k < inventory.itemGrid.gridLength; k++) {
// Loop through all the item slots
var col = Math.floor(k / inventory.itemGrid.columns);
var row = k % inventory.itemGrid.columns;
if (inventory.itemGrid.at(row, col) === null) {
// If its empty
object.visible = false;
inventory.itemGrid.addAt(
new powerupjs.SpriteGameObject(
sprites.items[object.sprite_type],
1,
0,
ID.layer_overlays
),
row,
col
);
break; // So it doesn't fill the whole inventory
}
}
} else if (
object.type === "seeds" &&
this.boundingBox.intersects(object.boundingBox) && // Seeds packets
object.visible &&
inventory.itemGrid.gameObjects.length < 27
) {
console.log(inventory.itemGrid.gameObjects.length);
for (var k = 0; k < inventory.itemGrid.gridLength; k++) {
var col = Math.floor(k / inventory.itemGrid.columns);
var row = k % inventory.itemGrid.columns;
if (inventory.itemGrid.at(row, col) === null) {
object.visible = false;
inventory.itemGrid.addAt(
new powerupjs.SpriteGameObject(
sprites.items[object.sprite_type],
1,
0,
ID.layer_overlays
),
row,
col
);
break;
}
}
} else if (
object.type === "crops" &&
object.ready &&
this.boundingBox.intersects(object.boundingBox) &&
object.visible &&
inventory.itemGrid.gameObjects.length < 27
) {
object.visible = false;
objects.remove(object);
var tileIndex = new powerupjs.Vector2(
object.position.x / 32,
object.position.y / 32
);
tiles.at(tileIndex.x, tileIndex.y).containsCrops = "false"; // Free up the tile
for (var k = 0; k < inventory.itemGrid.gridLength; k++) {
var col = Math.floor(k / inventory.itemGrid.columns);
var row = k % inventory.itemGrid.columns;
if (inventory.itemGrid.at(row, col) === null) {
inventory.itemGrid.addAt(
new powerupjs.SpriteGameObject(
sprites.items[object.sprite_type + "_plant"],
1,
0,
ID.layer_overlays
),
row,
col
);
break;
}
}
powerupjs.Game.gameWorld.map.areas[
powerupjs.Game.gameWorld.map.currentAreaIndex
].terrainEditor.save(); // Save the freed up tile
powerupjs.Game.gameWorld.saveObjects(); // Save the crops
break;
} else if (object.type === "building" && object.visible) {
if (this.boundingBox.intersects(object.doorHitbox)) {
for (var i = 0; i < powerupjs.Game.gameWorld.interiors.length; i++) {
if (
powerupjs.Game.gameWorld.interiors[i].type === object.building_type
) {
powerupjs.Game.gameWorld.interiors[i].player.position =
powerupjs.Game.gameWorld.interiors[i].spawnPosition.copy();
}
}
powerupjs.Game.gameWorld.currentInteriorArea = object.building_type;
}
break;
} else if (object.type === "market" && object.visible) {
if (this.boundingBox.intersects(object.doorHitbox)) {
powerupjs.Game.gameWorld.selling = true
}
break;
}
}
}
var bounds = powerupjs.Game.gameWorld.map.areas[
powerupjs.Game.gameWorld.map.currentAreaIndex
].find(ID.boundaries);
if (powerupjs.Game.gameWorld.currentInteriorArea === 'none') {
for (var i = 0; i < bounds.gameObjects.length; i++) {
// Boundary effects
if (bounds.gameObjects[i] === undefined) continue;
var boundary = bounds.gameObjects[i];
var boundingBox = new powerupjs.Rectangle(
this.boundingBox.x,
this.boundingBox.y,
this.boundingBox.width,
this.boundingBox.height
);
boundingBox.y = this.boundingBox.y + 20;
boundingBox.height = this.boundingBox.height - 20;
if (boundingBox.intersects(boundary.boundingBox)) {
var depth = boundingBox.calculateIntersectionDepth(boundary.boundingBox);
if (Math.abs(depth.y) > Math.abs(depth.x)) {
this.position.x += depth.x;
} else {
this.position.y += depth.y;
}
}
}
}
var interior_bounds = powerupjs.Game.gameWorld.interiors[0].find(
ID.interior_boundaries
);
if (powerupjs.Game.gameWorld.currentInteriorArea !== 'none') {
for (var i = 0; i < interior_bounds.gameObjects.length; i++) {
// Boundary effects
if (interior_bounds.gameObjects[i] === undefined) continue;
var boundary = interior_bounds.gameObjects[i];
var boundingBox = new powerupjs.Rectangle(
this.boundingBox.x,
this.boundingBox.y,
this.boundingBox.width,
this.boundingBox.height
);
boundingBox.y = this.boundingBox.y + 20;
boundingBox.height = this.boundingBox.height - 20;
if (boundingBox.intersects(boundary.boundingBox)) {
var depth = boundingBox.calculateIntersectionDepth(boundary.boundingBox);
if (Math.abs(depth.y) > Math.abs(depth.x)) {
this.position.x += depth.x;
} else {
this.position.y += depth.y;
}
}
}
}
var map = powerupjs.Game.gameWorld.map;
if (this.position.y < 0) {
// Up an area
if (map.currentAreaIndex > 6) {
map.currentAreaIndex -= 6;
this.position.y = 700;
map.areas[map.currentAreaIndex].player.position = map.playerPosition;
map.areas[map.currentAreaIndex].player.lastDirection =
map.playerAnimation;
} else {
this.position.y = 10;
}
}
if (this.position.y > 730) {
// Down an area
map.currentAreaIndex += 6;
this.position.y = 20;
map.areas[map.currentAreaIndex].player.position = map.playerPosition;
map.areas[map.currentAreaIndex].player.lastDirection = map.playerAnimation;
}
if (this.position.x > 1440) {
// Right of an area
map.currentAreaIndex += 1;
this.position.x = 60;
map.areas[map.currentAreaIndex].player.position = map.playerPosition;
map.areas[map.currentAreaIndex].player.lastDirection = map.playerAnimation;
}
if (this.position.x < 0) {
// Left of an area
if (map.currentAreaIndex > 0) {
map.currentAreaIndex -= 1;
this.position.x = 1400;
map.areas[map.currentAreaIndex].player.position = map.playerPosition;
map.areas[map.currentAreaIndex].player.lastDirection =
map.playerAnimation;
} else {
this.position.x = 10;
}
}
if (this.animationEnded()) {
// if (powerupjs.Game.gameWorld.currentTool === 'none')
this.playAnimation("idle_" + this.lastDirection);
// else if (powerupjs.Game.gameWorld.currentTool === 'sword'){
// this.playAnimation("sword_idle_" + this.lastDirection)
// }
}
powerupjs.Game.gameWorld.map.playerAnimation = this.lastDirection;
powerupjs.Game.gameWorld.map.playerPosition = this.position.copy();
};