-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventoryBoxer.js
375 lines (351 loc) · 15.3 KB
/
inventoryBoxer.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//Purpose : extends the Canvas class to include all functions pertaining to inventory collection,quest tracking,and health tracking.
//Future Addition : There is an area for hunger tracking and food consumption that I never made due to time constraints
//All instantiations : StatsCanvas
class InventoryCanvas extends Canvas
{
constructor(canvasID, width, height)
{
super (canvasID, width,height)
this.ctx = null;
this.canvasElement = null;
//Boolean values that tell you whether or not a slot is filled
this.inventoryArray = [0,0,0,0,0,0,0,0,0]
//once you get a food item, youre going to stack it in that inventory slot
// note: the name and item of it is always candy
//this holds the index number of the food slot
this.foodSlot = null;
this.foodCounter = 0;
//Updates your progress bar
this.progressCounter = 0;
//Updates if you completed the dragging task so you dont complete multiple times
// when complete, the array will include "chairQuest", "dollQuest"
this.completedDraggingQuestCount = [];
//Updates your health counter
this.health = 5;
}
//Purpose: removes hearts on hit and sends to the endgame event if you die
updateHearts()
{
switch (this.health)
{
case 0:
this.endGame();
heartCover.xCoord = [126,164, 199, 234, 269 ];
heartCover.yCoord = Array(5).fill(70) ;
//not using the inbult draw function because im doing weird stuff with the height and width
for (let i =0; i< heartCover.xCoord.length; i++)
{
document.getElementById("informationAndStats").getContext('2d').drawImage(heartCover.imageElement,heartCover.xCoord[i], heartCover.yCoord[i], heartCover.height, heartCover.width);
}
break;
case 1:
//start warning the player they're about to die
//TODO warning
//TODO flash the player area with a red line and the heart section with a red line
heartCover.xCoord = [126,164, 199, 234 ];
heartCover.yCoord = Array(4).fill(70) ;
//not using the inbult draw function because im doing weird stuff with the height and width
for (let i =0; i< heartCover.xCoord.length; i++)
{
document.getElementById("informationAndStats").getContext('2d').drawImage(heartCover.imageElement,heartCover.xCoord[i], heartCover.yCoord[i], heartCover.height, heartCover.width);
}
break;
case 2:
heartCover.xCoord = [126,164, 199];
heartCover.yCoord = Array(3).fill(70) ;
//not using the inbult draw function because im doing weird stuff with the height and width
for (let i =0; i< heartCover.xCoord.length; i++)
{
document.getElementById("informationAndStats").getContext('2d').drawImage(heartCover.imageElement,heartCover.xCoord[i], heartCover.yCoord[i], heartCover.height, heartCover.width);
}
break;
case 3:
heartCover.xCoord = [126,164];
heartCover.yCoord = Array(2).fill(70) ;
//not using the inbult draw function because im doing weird stuff with the height and width
for (let i =0; i< heartCover.xCoord.length; i++)
{
document.getElementById("informationAndStats").getContext('2d').drawImage(heartCover.imageElement,heartCover.xCoord[i], heartCover.yCoord[i], heartCover.height, heartCover.width);
}
break;
case 4:
heartCover.xCoord = [126];
heartCover.yCoord = Array(1).fill(70) ;
document.getElementById("informationAndStats").getContext('2d').drawImage(heartCover.imageElement,heartCover.xCoord, heartCover.yCoord, heartCover.height, heartCover.width);
break;
case 5:
fullHeart.xCoord = [124, 159, 194, 229,264] ;
fullHeart.yCoord = Array(5).fill(66) ;
fullHeart.drawImage();
break;
}
}
addInventoryItem(furniture)
{
//Purpose: helps canvas animation loop through where to put new inventory items
const inventoryBoxesCoords =
{
"0" : [64,192],
"1" : [160,192],
"2" : [256, 192],
"3" : [64,288],
"4" : [160,288],
"5" : [256,288],
"6" : [64,384],
"7" : [160,384],
"8" : [256,384]
}
//Execute
let itemArray = Object.values(furniture.itemsInside); // it is an array but filter refused to work so idk
if (this.foodSlot !== null)
{
if (itemArray.includes("candy"))
{
this.foodCounter++;
this.ctx.fillStyle = "rgb(189, 177, 143)";
this.ctx.fillRect((inventoryBoxesCoords[this.foodSlot][0] - 10), (inventoryBoxesCoords[this.foodSlot][1]- 15), 20,20)
this.ctx.fillStyle = "black";
this.ctx.fillText(this.foodCounter,(inventoryBoxesCoords[this.foodSlot][0] - 3), (inventoryBoxesCoords[this.foodSlot][1] - 3));
itemArray = itemArray.filter(function(item) {
return item !== "candy"
})
}
if (itemArray.length > 0)
{
for (let i = 0; i< itemArray.length; i++)
{
for (let j =0; j < this.inventoryArray.length; j++)
{
if (this.inventoryArray[j] == 0)
{
let imageElementToAdd = allInventoryItems[itemArray[i]].imageElement;
this.ctx.drawImage(imageElementToAdd, inventoryBoxesCoords[j][0], inventoryBoxesCoords[j][1]);
this.inventoryArray[j] = 1;
break;
}
}
}
}
}
else
{
for (let i = 0; i< itemArray.length; i++)
{
for (let j =0; j < this.inventoryArray.length; j++)
{
if (this.inventoryArray[j] == 0)
{
if (itemArray[i] == "candy")
{
this.foodSlot = j;
this.foodCounter++
}
let imageElementToAdd = allInventoryItems[itemArray[i]].imageElement;
this.ctx.drawImage(imageElementToAdd, inventoryBoxesCoords[j][0], inventoryBoxesCoords[j][1]);
this.inventoryArray[j] = 1;
break;
}
}
}
}
//now rewrite the textArea
//for the sake of quest progression, this will only fire the first time you pick up a candle
if (itemArray.includes("candle"))
{
//update your progress bar
this.progressCounter++;
this.updateProgressBar();
//bookquest
TextCanvas.currentTextKey = "bookQuest";
// -1 not 0 to make the rewriteText work correctly
TextCanvas.currentTextArrayIndex = -1;
TextCanvas.totalArrayIndex = allTexts[TextCanvas.currentTextKey].length;
//to make rewriting the text work
TextCanvas.previousText = allTexts[TextCanvas.currentTextKey][0];
button.status = "progress";
//setTimeout(questCompleteSoundElement.play(),1000);
TextCanvas.rewriteText();
//to stop the darkening of your character now that you have the candle
userSprite.hasCandle = true;
}
else if (itemArray.includes("book"))
{
//play the added to inventory noise
questCompleteSoundElement.play();
//update your progress bar
this.progressCounter++;
this.updateProgressBar();
TextCanvas.currentTextKey = "chairQuest";
// -1 not 0 to make the rewriteText work correctly
TextCanvas.currentTextArrayIndex = -1;
TextCanvas.totalArrayIndex = allTexts[TextCanvas.currentTextKey].length;
//to make rewriting the text work
TextCanvas.previousText = allTexts[TextCanvas.currentTextKey][0];
button.status = "progress";
TextCanvas.rewriteText();
//TODO do i need a hasbook boolean?
}
else if (itemArray.includes("mace"))
{
//update your progress bar
this.progressCounter++;
this.updateProgressBar();
TextCanvas.currentTextKey = "smashMirrorQuest";
// -1 not 0 to make the rewriteText work correctly
TextCanvas.currentTextArrayIndex = -1;
TextCanvas.totalArrayIndex = allTexts[TextCanvas.currentTextKey].length;
//to make rewriting the text work
TextCanvas.previousText = allTexts[TextCanvas.currentTextKey][0];
button.status = "progress";
TextCanvas.rewriteText();
userSprite.hasWeapon = true;
//TODO do i need a hasbook boolean?
}
else if (itemArray.includes("paint"))
{
if (this.progressCounter == 11)
{
//update your progress bar
this.progressCounter++;
this.updateProgressBar();
//go to the ending screen
playerAreaCanvas.floor = "transition";
playerAreaCanvas.transitionFloor = "won";
}
}
else
{
TextCanvas.rewriteText("returntoText")
}
//now remove the items from the furniture
furniture.itemsInside = [];
//youre no longer interacting with a furniture item
TextCanvas.furnitureInteractingWith = null;
}
// Purpose: On load and on quest completion, update the green quest progress bar
// Logic: on case 0, draw all 6 32x32 sprites. subsequently, update the location with the half or full progress bar
updateProgressBar()
{
//Purpose: holds the bar coordinates for easier redrawing
const barCoords =
{
'0' : [92,33],
'1' : [123.1,33],
'2' : [164.1, 33.5],
'3' : [194.1,33.5],
'4' : [224.1, 33.5],
'5' : [254.1,33.5],
}
//Execute
switch(this.progressCounter)
{
//TODO the rest
case 1:
//replace the 0th index with the half bar
halfbar.xCoord = barCoords[0][0];
halfbar.yCoord = barCoords[0][1];
halfbar.drawImage();
break;
case 2:
//note: we have some sprite size differences because I suck at cropping. I tried to crop correctly but couldnt
//so im doing weird jiggling of the coordinate and scaling here
fullbar.xCoord = barCoords[0][0]+ 4;
fullbar.yCoord = barCoords[0][1] + 7;
fullbar.drawImage();
break;
case 3:
halfbar.xCoord = barCoords[1][0] +2;
halfbar.yCoord = barCoords[1][1];
halfbar.drawImage();
break;
case 4:
fullbar.xCoord = barCoords[1][0] + 5;
fullbar.yCoord = barCoords[1][1] + 7;
fullbar.drawImage();
break;
case 5:
halfbar.xCoord = barCoords[2][0] -5;
halfbar.yCoord = barCoords[2][1] - 0.5;
halfbar.drawImage();
break;
case 6:
fullbar.xCoord = barCoords[2][0] -2.5;
fullbar.yCoord = barCoords[2][1] + 6.5;
fullbar.drawImage();
break;
case 7:
halfbar.xCoord = barCoords[3][0] -5;
halfbar.yCoord = barCoords[3][1] -0.5;
halfbar.drawImage();
break;
case 8:
fullbar.xCoord = barCoords[3][0];
fullbar.yCoord = barCoords[3][1] +6.25;;
fullbar.drawImage();
break;
case 9:
halfbar.xCoord = barCoords[4][0] -1;
halfbar.yCoord = barCoords[4][1] - 0.5;
halfbar.drawImage();
break;
case 10:
fullbar.xCoord = barCoords[4][0] + 2.2;
fullbar.yCoord = barCoords[4][1] + 6.25;
fullbar.drawImage();
break;
case 11:
halfbar.xCoord = barCoords[5][0] -0.25;
halfbar.yCoord = barCoords[5][1] - 0.75;
halfbar.drawImage();
break;
case 12:
fullbar.xCoord = barCoords[5][0] +2;
fullbar.yCoord = barCoords[5][1] + 6.5;
fullbar.drawImage();
break;
}
}
//Purpose: Your hearts ran to 0, you lost! So sad :((
endGame()
{
playerAreaCanvas.floor = "transition"
playerAreaCanvas.transitionFloor = "lost";
}
}
const statsCanvas = new InventoryCanvas("informationAndStats", widthAddition, fullAreaHeight);
statsCanvas.getCanvasMade();
const statsBackground = new ImageClass("assets/informationandStats/interface.png",0,0,widthAddition,fullAreaHeight,"informationAndStats");
statsBackground.createImageElement();
//add in items that are found in desks and therefore only show up in the inventory
const candle = new ImageClass ("assets/questItems/candle.png",null,null, 32,32, "informationAndStats");
candle.createImageElement();
const candy = new ImageClass("assets/questItems/candy.png", null,null,32,32, "informationAndStats");
candy.createImageElement();
const mace = new ImageClass ("assets/questItems/mace.png",null,null,32,32, "informationAndStats");
mace.createImageElement();
const paint = new ImageClass ("assets/questItems/paint.png",null,null,32,32, "informationAndStats");
paint.createImageElement();
//Purpose: helps with getting items out of dressers etc
const allInventoryItems =
{
"candle" : candle,
"candy" : candy,
"book" : book,
"mace" : mace,
"paint" : paint
}
//add in images for progress bar, health, etc
const halfbar = new ImageClass('assets/informationandStats/halfbar.png',null,null,32,32,"informationAndStats");
halfbar.createImageElement();
const fullbar = new ImageClass('assets/informationandStats/fullbar.png',null,null,32,32,"informationAndStats");
fullbar.createImageElement();
const fullHeart = new ImageClass('assets/informationandStats/heart.png',null,null,32,32,"informationAndStats");
fullHeart.createImageElement();
const heartCover = new ImageClass('assets/informationandStats/coverHeart.png',null,null,25,21,"informationAndStats");
heartCover.createImageElement();
function updateStatsArea()
{
statsBackground.drawImage();
statsCanvas.updateProgressBar();
statsCanvas.updateHearts();
}