-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
361 lines (323 loc) · 7.01 KB
/
main.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
import "./style.css";
import * as PIXI from "pixi.js";
import { sound } from "@pixi/sound";
import { GlowFilter } from "@pixi/filter-glow";
// Objects
import SceneObject from "./scripts/sceneObject";
import { createParticles } from "./scripts/particles";
import MultiSceneObject from "./scripts/multiSceneObject";
import Cat from "./scripts/cat";
import Inventory from "./scripts/inventory";
/*
** Scene Setup
*/
const size = {
width: 500,
height: 500,
};
const app = new PIXI.Application({
width: size.width,
height: size.height,
backgroundAlpha: 0,
});
const appDiv = document.querySelector("#app");
appDiv.appendChild(app.view);
// Load Text
const shelterTitle = document.querySelector("#shelterTitle");
shelterTitle.textContent = "Buffalo Cat Shelter";
const moneyStat = document.querySelector("#moneyStat");
moneyStat.textContent = "$0";
const joyP = document.querySelector("#happyStat");
const hungerP = document.querySelector("#nourishStat");
const healthP = document.querySelector("#healthStat");
//Setup Overlay
const overlay = document.querySelector("#overDiv");
overlay.style.display = "none";
const overlayBtn = document.querySelector("#overButton");
const overText = document.querySelector("#overText");
overlayBtn.addEventListener("click", (e) => {
e.preventDefault;
overlay.style.display = "none";
overText.textContent = "Meow, meow, meow...";
});
// Sound
window.addEventListener("load", (e) => {
e.preventDefault;
sound.add("bg", "sounds/bg.mp3");
// sound.play("bg");
});
sound.add("meow", "sounds/meow.mp3");
/*
** Money Variable
*/
class money{
constructor(moneyStat){
this.moneyTotal = 200
this.moneyStat = moneyStat
}
updateMoney(newTotal){
if(newTotal <=0){
this.moneyTotal = 0
this.moneyStat.textContent = `Game Over`
}else{
this.moneyTotal = newTotal
this.moneyStat.textContent = `$${this.moneyTotal}`
}
}
addRev(){
setInterval(() => {
this.moneyTotal +=10
this.moneyStat.textContent= `$${this.moneyTotal}`
}, 10000);
}
}
const moneyData = new money(moneyStat)
moneyStat.textContent = `$${moneyData.moneyTotal}`
moneyData.addRev()
// Create and Update Cat Object
const cat1 = new Cat(joyP, hungerP, healthP);
cat1.mangageJoy();
cat1.manageHunger();
cat1.manageHealth();
/*
** Add Sprites/Containers
*/
// Particles
const cnt = new PIXI.ParticleContainer();
cnt.name = "particleCont";
app.stage.addChild(cnt);
cnt.position.x = 150;
cnt.position.y = 400;
PIXI.Assets.add("heart", "/assets/heart.png");
const heartRef = await PIXI.Assets.load("heart");
// Filter
const glowFilter = new GlowFilter({ distance: 15, outerStrength: 2 });
//Create Loader
PIXI.Assets.add("bowls", "/assets/bowls.png");
const bowlRef = await PIXI.Assets.load("bowls");
// Bowls
const bowls = new SceneObject(
app,
moneyData,
overlay,
overText,
true,
"bowls",
"bowls",
bowlRef,
30,
330,
0.5,
"static",
glowFilter,
"Thanks for the Din-Din 🐟",
2,
cat1,
2
);
// Food
const foodPos = [
[180, 45],
[180, 100],
[240, 45],
[240, 100],
[295, 45],
[295, 100],
];
PIXI.Assets.add("foodBox", "/assets/foodBox.png");
const foodBoxRef = await PIXI.Assets.load("foodBox");
const foodBox = new MultiSceneObject(
app,
moneyData,
overlay,
overText,
true,
"foodBox",
"foodBox",
foodBoxRef,
foodPos,
0.1,
"static",
glowFilter,
"I 😻 U",
20,
cat1,
7
);
// Treats
const treatsPos = [
[110, 25],
[110, 65],
[110, 110],
];
PIXI.Assets.add("treats", "/assets/treats.png");
const treatsRef = await PIXI.Assets.load("treats");
const treats = new MultiSceneObject(
app,
moneyData,
overlay,
overText,
true,
"treats",
"treats",
treatsRef,
treatsPos,
0.13,
"static",
glowFilter,
"Yummmm...🍭",
4,
cat1,
2
);
// Toys
PIXI.Assets.add("toys", "/assets/toys.png");
const toysRef = await PIXI.Assets.load("toys");
const toys = new SceneObject(
app,
moneyData,
overlay,
overText,
true,
"toys",
"toys",
toysRef,
220,
245,
0.28,
"static",
glowFilter,
"Mouse...mouse...mouse 🐭",
8,
cat1,
4
);
// Medicine
const medsPos = [
[120, 290],
[120, 370],
];
PIXI.Assets.add("meds", "/assets/meds.png");
const medsRef = await PIXI.Assets.load("meds");
const meds = new MultiSceneObject(
app,
moneyData,
overlay,
overText,
true,
"meds",
"meds",
medsRef,
medsPos,
0.13,
"static",
glowFilter,
"Feeling Much Better 💖",
24,
cat1,
8
);
// Animated Sprite
const catContainer = new PIXI.Container();
catContainer.name = "catCont";
const catImages = [
"/assets/cat/1.png",
"/assets/cat/2.png",
"/assets/cat/2.png",
"/assets/cat/2.png",
];
const catArray = [];
for (let i = 0; i < catImages.length; i++) {
const cat = PIXI.Texture.from(catImages[i]);
catArray.push(cat);
}
const animatedSprite = new PIXI.AnimatedSprite(catArray);
animatedSprite.position.x = 150;
animatedSprite.position.y = 400;
animatedSprite.scale.x = 0.1;
animatedSprite.scale.y = 0.1;
animatedSprite.play();
animatedSprite.animationSpeed = 0.005;
animatedSprite.filters = [glowFilter];
animatedSprite.eventMode = "static";
animatedSprite.on("pointerdown", catClick);
function catClick() {
sound.play("meow");
console.log("hi");
overlay.style.display = "flex";
overText.textContent = "I 😻 U";
}
catContainer.addChild(animatedSprite);
app.stage.addChild(catContainer);
// Create Inventory
const inventory = new Inventory(bowls, foodBox, treats, toys, meds);
// Loop through app children and grab isntances
const appChildren = app.stage.children;
const appChildrenRefOb = {};
const inventoryRefObj = {};
const inventoryNames = [
"bowlsCont",
"toysCont",
"FoodBoxCont",
"medsCont",
"treatsCont",
];
appChildren.forEach((e) => {
const name = e.name;
const childArr = e.children;
switch (name) {
case "bowlsCont":
// inventory.bowls = childArr;
inventoryRefObj[name] = childArr;
break;
case "toysCont":
// inventory.toys = childArr;
inventoryRefObj[name] = childArr;
break;
case "foodBoxCont":
// inventory.foodBox = childArr;
inventoryRefObj[name] = childArr;
break;
case "medsCont":
// inventory.meds = childArr;
inventoryRefObj[name] = childArr;
break;
case "treatsCont":
// inventory.treats = childArr;
inventoryRefObj[name] = childArr;
break;
default:
break;
}
appChildrenRefOb[name] = childArr;
});
// console.log(appChildrenRefOb);
// console.log(inventoryRefObj);
// Emitter Object
const emitter = await createParticles(cnt);
const displayPart = (delta) => {
emitter.update(delta);
};
let isEmitting = false
/*
** Game Loop/Ticker
*/
var elapsed = Date.now();
var update = function () {
requestAnimationFrame(update);
var now = Date.now();
if(cat1.joy>=6 && cat1.hunger>=6 && cat1.health >=6){
console.log("true")
emitter.emit = true;
}else{
isEmitting=false
emitter.emit = false
}
if (emitter) {
displayPart((now - elapsed) * 0.001);
}
elapsed = now;
};
update();
app.ticker.add(() => {});
export { cnt, app, heartRef };