-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
684 lines (580 loc) · 19.8 KB
/
sketch.js
File metadata and controls
684 lines (580 loc) · 19.8 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
let myPet;
let myBorder;
let feed;
let backgrounds;
let interactHandler;
let currency;
let lastPlaytimeReward = 0;
let house;
let shop;
let inventory;
let gameStorage;
let tutorial; // New tutorial object
// Global flag for showing the backgrounds menu
let backgroundMenuVisible = false;
// Global flag for checking for game over
let gameOver = false;
// Settings button properties
let settingsButtonSize = 40;
let settingsButtonPadding = 10;
// Global flag for showing the settings menu
let settingsMenuVisible = false;
// Global flag for showing the instructions dialog
let instructionsDialogVisible = false;
function setup() {
createCanvas(windowWidth, windowHeight);
// Force 60 FPS
frameRate(60);
// Initialize storage system
gameStorage = new GameStorage();
// Request device orientation permission on devices that require it (e.g., iOS)
if (typeof DeviceOrientationEvent !== 'undefined' &&
typeof DeviceOrientationEvent.requestPermission === 'function') {
// Try to request permission immediately
DeviceOrientationEvent.requestPermission()
.then(response => {
if (response === "granted") {
console.log("Gyroscope permission granted.");
} else {
console.log("Gyroscope permission denied.");
}
})
.catch(error => {
console.error("Gyroscope permission error:", error);
// If failed (likely needs user gesture), create a temporary permission dialog
let permissionDialog = createDiv("This app needs access to device motion. Tap anywhere to continue.");
permissionDialog.position(0, 0);
permissionDialog.style("width", "100%");
permissionDialog.style("height", "100%");
permissionDialog.style("background-color", "rgba(0, 0, 0, 0.8)");
permissionDialog.style("color", "white");
permissionDialog.style("text-align", "center");
permissionDialog.style("padding-top", "40%");
permissionDialog.style("font-size", "18px");
permissionDialog.style("position", "absolute");
permissionDialog.style("z-index", "1000");
permissionDialog.mousePressed(() => {
DeviceOrientationEvent.requestPermission()
.then(response => {
if (response === "granted") {
console.log("Gyroscope permission granted.");
} else {
console.log("Gyroscope permission denied.");
}
permissionDialog.remove();
});
});
});
}
// Create currency with initial balance
currency = new Currency(900);
// Create inventory
inventory = new Inventory();
// Setup currency callbacks if needed
currency.setOnBalanceChangeCallback((balance, amount, type, reason) => {
console.log(`Currency ${type}: ${amount} (${reason}). New balance: ${balance}`);
});
// Create backgrounds manager
backgrounds = new Backgrounds();
// Create and set up the border (bottom panel)
myBorder = new Border();
myBorder.setThickness(min(width, height) * 0.1);
// Get playable area
let playArea = myBorder.getPlayableArea();
// Create feed and position it in the bottom left
feed = new Feed();
feed.foodServings = 5;
// Create shop
shop = new Shop();
shop.setReferences(inventory, currency);
// Create pet at the center of the playable area
myPet = new Pet(playArea.width / 2, playArea.height / 2);
myPet.setBoundaries(0, 0, playArea.width, playArea.height);
// Create house and position it in the bottom right
house = new House();
house.setPosition(playArea);
house.setPet(myPet);
// Initialize interaction handler
interactHandler = new InteractHandler();
interactHandler.setReferences(myPet, myBorder, playArea, house);
// Set up interaction callbacks
setupInteractionCallbacks();
// Initialize tutorial with references to game objects
tutorial = new Tutorial({
pet: myPet,
border: myBorder,
feed: feed,
house: house
});
// Try to load saved game data
loadGameData();
// If no saved game exists and tutorial hasn't been seen, start tutorial
if (!gameStorage.hasSavedGame() && !tutorial.hasSeenTutorial) {
tutorial.start();
}
}
function setupInteractionCallbacks() {
interactHandler.setCallbacks({
onPetInteract: (x, y) => {
console.log("Pet interaction detected!");
// Award a small coin bonus for interacting with pet
currency.awardInteractionBonus(1);
},
onBorderInteract: (x, y) => {
console.log("Border interaction detected");
},
onDrag: (x, y, dx, dy) => {
// If dragging near the pet (active), make it follow the drag.
let d = dist(x, y, myPet.x, myPet.y);
if (d < myPet.size) {
myPet.x = constrain(x, myPet.minX, myPet.maxX);
myPet.y = constrain(y, myPet.minY, myPet.maxY);
myPet.targetX = myPet.x;
myPet.targetY = myPet.y;
}
}
});
}
function draw() {
// Get playable area
let playArea = myBorder.getPlayableArea();
// Draw the current background theme
backgrounds.draw(playArea);
// Draw the food table (Feed) in the bottom left of the playable area.
feed.display(playArea);
// Draw the house (should appear on top of the background)
house.display(playArea);
// Show house hover effects as before…
if (house.contains(mouseX, mouseY)) {
house.setHover(true);
cursor(HAND);
} else {
house.setHover(false);
cursor(ARROW);
}
// Update and draw pet if not resting
myPet.update();
myPet.display();
// Display the border
myBorder.display();
// Draw settings button in top-right corner
drawSettingsButton();
// If tutorial is active, display it on top of everything
if (tutorial.active) {
tutorial.display();
return; // Skip other UI elements when tutorial is active
}
// If the instructions dialog should be visible, draw it
if (instructionsDialogVisible) {
displayInstructionsDialog();
}
// If the settings menu should be visible, draw it
else if (settingsMenuVisible) {
drawSettingsMenu();
}
// If the backgrounds menu should be visible, draw it on top.
else if (backgroundMenuVisible) {
drawBackgroundMenu();
}
else if (shop.visible) {
shop.display();
}
else if (inventory.visible) {
inventory.display();
}
}
// Draw settings button in top-right corner (gear icon)
function drawSettingsButton() {
const btnX = width - settingsButtonSize - settingsButtonPadding;
const btnY = settingsButtonPadding;
push();
// Button background
fill(50, 100, 200);
stroke(255);
strokeWeight(2);
rect(btnX, btnY, settingsButtonSize, settingsButtonSize, 5);
// Draw gear icon
fill(255);
noStroke();
// Draw gear center
const centerX = btnX + settingsButtonSize / 2;
const centerY = btnY + settingsButtonSize / 2;
const outerRadius = settingsButtonSize * 0.35;
const innerRadius = settingsButtonSize * 0.2;
// Draw center circle
ellipse(centerX, centerY, innerRadius * 2);
// Draw gear teeth
const teethCount = 8;
for (let i = 0; i < teethCount; i++) {
const angle = TWO_PI * i / teethCount;
const tipX = centerX + cos(angle) * outerRadius;
const tipY = centerY + sin(angle) * outerRadius;
const baseX1 = centerX + cos(angle - 0.2) * innerRadius;
const baseY1 = centerY + sin(angle - 0.2) * innerRadius;
const baseX2 = centerX + cos(angle + 0.2) * innerRadius;
const baseY2 = centerY + sin(angle + 0.2) * innerRadius;
// Draw each tooth as a triangle
triangle(tipX, tipY, baseX1, baseY1, baseX2, baseY2);
}
pop();
}
// Check if settings button was clicked
function isSettingsButtonPressed(x, y) {
const btnX = width - settingsButtonSize - settingsButtonPadding;
const btnY = settingsButtonPadding;
return (x > btnX && x < btnX + settingsButtonSize &&
y > btnY && y < btnY + settingsButtonSize);
}
// Draw settings menu with options
function drawSettingsMenu() {
push();
// Semi-transparent dark overlay covering the whole screen
fill(0, 150);
noStroke();
rect(0, 0, width, height);
// Menu panel dimensions
const menuWidth = min(width * 0.8, 400);
const buttonHeight = 40;
const spacing = 15;
const buttonsCount = 3; // Reduced from 4 to 3
const menuHeight = buttonHeight * buttonsCount + spacing * (buttonsCount + 2);
// Menu position
const menuX = width / 2 - menuWidth / 2;
const menuY = height / 2 - menuHeight / 2;
// Draw menu background
fill(240);
stroke(50);
strokeWeight(2);
rect(menuX, menuY - spacing, menuWidth, menuHeight, 10);
// Title
textAlign(CENTER, TOP);
fill(50, 80, 150);
noStroke();
textSize(24);
text("Settings", width / 2, menuY - spacing * 0.25);
// Draw menu buttons - removed Instructions button
const buttonOptions = [
{ label: "Save Game", color: color(50, 150, 50) },
{ label: "Clear Save", color: color(200, 100, 50) },
{ label: "Start New Game", color: color(70, 120, 200) }
];
for (let i = 0; i < buttonOptions.length; i++) {
const btnY = menuY + spacing * (i + 1.5) + buttonHeight * i;
const btnWidth = menuWidth * 0.8;
const btnX = width / 2 - btnWidth / 2;
// Button background
fill(buttonOptions[i].color);
stroke(30);
strokeWeight(2);
rect(btnX, btnY, btnWidth, buttonHeight, 8);
// Button text
textAlign(CENTER, CENTER);
fill(255);
noStroke();
textSize(20);
text(buttonOptions[i].label, btnX + btnWidth / 2, btnY + buttonHeight / 2);
}
pop();
}
// Check if any settings menu button was pressed and handle it
function checkSettingsMenuButtons(x, y) {
if (!settingsMenuVisible) return false;
const menuWidth = min(width * 0.8, 400);
const buttonHeight = 40;
const spacing = 15;
const buttonsCount = 3; // Reduced from 4 to 3
const menuHeight = buttonHeight * buttonsCount + spacing * (buttonsCount + 2);
const menuX = width / 2 - menuWidth / 2;
const menuY = height / 2 - menuHeight / 2;
const btnWidth = menuWidth * 0.8;
const btnX = width / 2 - btnWidth / 2;
for (let i = 0; i < 3; i++) { // Loop through 3 buttons
const btnY = menuY + spacing * (i + 1.5) + buttonHeight * i;
if (x > btnX && x < btnX + btnWidth &&
y > btnY && y < btnY + buttonHeight) {
switch(i) {
case 0: // Save Game
saveGameData();
break;
case 1: // Clear Save
if (confirm("Are you sure you want to delete your saved game?")) {
gameStorage.clearSavedGame();
// Show confirmation message
let msg = createDiv("Saved game deleted!");
msg.position(width/2, height/2);
msg.style('background-color', 'rgba(200, 50, 50, 0.8)');
msg.style('color', 'white');
msg.style('padding', '10px 20px');
msg.style('border-radius', '5px');
msg.style('font-size', '20px');
msg.style('position', 'absolute');
msg.style('transform', 'translate(-50%, -50%)');
msg.style('z-index', '1000');
setTimeout(() => msg.remove(), 1500);
}
break;
case 2: // Start New Game
if (confirm("Are you sure you want to start a new game? This will reset your current progress.")) {
// Reset game state
currency.resetBalance(900);
inventory.reset();
shop.reset();
let playArea = myBorder.getPlayableArea();
myPet = new Pet(playArea.width / 2, playArea.height / 2);
myPet.setBoundaries(0, 0, playArea.width, playArea.height);
house.setPet(myPet);
interactHandler.setReferences(myPet, myBorder, playArea, house);
// Show confirmation message
let msg = createDiv("New game started!");
msg.position(width/2, height/2);
msg.style('background-color', 'rgba(70, 120, 200, 0.8)');
msg.style('color', 'white');
msg.style('padding', '10px 20px');
msg.style('border-radius', '5px');
msg.style('font-size', '20px');
msg.style('position', 'absolute');
msg.style('transform', 'translate(-50%, -50%)');
msg.style('z-index', '1000');
setTimeout(() => {
msg.remove();
// Only start tutorial if it hasn't been seen before
if (!tutorial.hasSeenTutorial()) {
startTutorial();
}
}, 1500);
}
break;
}
// Close the settings menu after action
settingsMenuVisible = false;
return true;
}
}
// Check if click is outside the menu to close it
if (x < menuX || x > menuX + menuWidth ||
y < menuY || y > menuY + menuHeight) {
settingsMenuVisible = false;
return true;
}
return false;
}
function loadGameData() {
if (gameStorage.hasSavedGame()) {
const savedData = gameStorage.loadGame();
if (savedData) {
// Load pet data
if (savedData.pet) {
myPet.health = savedData.pet.health;
myPet.energy = savedData.pet.energy;
myPet.happiness = savedData.pet.happiness;
myPet.lastFed = savedData.pet.lastFed;
myPet.color = savedData.pet.color;
}
// Load currency data
if (savedData.currency) {
currency.resetBalance(savedData.currency.balance);
}
// Load inventory data
if (savedData.inventory && savedData.inventory.items) {
inventory.items = savedData.inventory.items;
}
// Load background theme
if (savedData.backgrounds && savedData.backgrounds.currentTheme) {
backgrounds.setTheme(savedData.backgrounds.currentTheme);
}
// Load shop data (purchased backgrounds)
if (savedData.shop && savedData.shop.backgroundItems) {
shop.backgroundItems = savedData.shop.backgroundItems;
}
// Load tutorial flag
if (savedData.hasOwnProperty('seenTutorial')) {
tutorial.hasSeenTutorial = savedData.seenTutorial;
} else {
tutorial.hasSeenTutorial = true; // Assume already seen for existing saves
}
console.log("Game data loaded successfully!");
}
}
}
function saveGameData() {
const gameData = {
pet: myPet,
currency: currency,
inventory: inventory,
backgrounds: backgrounds,
shop: shop,
seenTutorial: tutorial.hasSeenTutorial // Save tutorial flag
};
const success = gameStorage.saveGame(gameData);
if (success) {
// Show a temporary save confirmation message
let saveMsg = createDiv("Game Saved!");
saveMsg.position(width/2, height/2);
saveMsg.style('background-color', 'rgba(50, 200, 50, 0.8)');
saveMsg.style('color', 'white');
saveMsg.style('padding', '10px 20px');
saveMsg.style('border-radius', '5px');
saveMsg.style('font-size', '20px');
saveMsg.style('position', 'absolute');
saveMsg.style('transform', 'translate(-50%, -50%)');
saveMsg.style('z-index', '1000');
// Remove the message after 1.5 seconds
setTimeout(() => {
saveMsg.remove();
}, 1500);
}
}
function mousePressed() {
// During tutorial, only handle tutorial navigation buttons
if (tutorial.active) {
// Button handling is managed by the p5 buttons
return false;
}
if (gameOver) {
return false;
}
// Check if instructions dialog is visible and handle clicks
if (instructionsDialogVisible) {
handleInstructionsDialogClick(mouseX, mouseY);
return false;
}
// Check if settings button was pressed
if (isSettingsButtonPressed(mouseX, mouseY)) {
settingsMenuVisible = !settingsMenuVisible;
return false;
}
// Check if any settings menu button was pressed
if (settingsMenuVisible) {
checkSettingsMenuButtons(mouseX, mouseY);
return false;
}
if (backgroundMenuVisible) {
let themeKeys = Object.keys(backgrounds.themes);
let buttonW = width * 0.3;
let buttonH = 40;
let startY = height / 2 - 50;
// Check the inventory to see which backgrounds are unlocked.
let unlockedBackgrounds = inventory.getItems("backgrounds");
for (let i = 0; i < themeKeys.length; i++) {
let btnX = width / 2 - buttonW / 2;
let btnY = startY + i * (buttonH + 10);
if (
mouseX > btnX && mouseX < btnX + buttonW &&
mouseY > btnY && mouseY < btnY + buttonH
) {
// Check if the background is unlocked by looking into the inventory.
let unlocked = false;
for (let j = 0; j < unlockedBackgrounds.length; j++) {
if (unlockedBackgrounds[j].id === themeKeys[i]) {
unlocked = true;
break;
}
}
if (!unlocked) {
console.log(`Theme "${themeKeys[i]}" is locked.`);
} else {
backgrounds.setTheme(themeKeys[i]);
console.log(`Background set to: ${themeKeys[i]}`);
backgroundMenuVisible = false;
}
return false;
}
}
// If click outside buttons, close the menu.
backgroundMenuVisible = false;
return false;
}
else if (shop.visible) {
shop.handleMousePressed(mouseX, mouseY);
return false;
}
else if (inventory.visible) {
inventory.handleMousePressed(mouseX, mouseY);
return false;
}
interactHandler.handleMousePressed(mouseX, mouseY);
return false; // Prevent default behavior
}
function touchStarted() {
// Call mousePressed() when touch starts
mousePressed();
}
function touchMoved() {
if (gameOver) {
return false;
}
if (touches.length > 0) {
interactHandler.handleTouchMove(touches[0].x, touches[0].y);
}
return false; // Prevent default
}
function touchEnded() {
if (touches.length > 0) {
interactHandler.handleTouchEnd(touches[0].x, touches[0].y);
}
return false; // Prevent default
}
// Draw the overlay menu for background themes.
function drawBackgroundMenu() {
push();
// Draw semi-transparent overlay
fill(0, 150);
noStroke();
rect(0, 0, width, height);
// Title
fill(255);
textAlign(CENTER, CENTER);
textSize(24);
text("Select Background Theme", width / 2, height / 2 - 100);
// Get available themes from the backgrounds object.
let themeKeys = Object.keys(backgrounds.themes);
let buttonW = width * 0.3;
let buttonH = 40;
let startY = height / 2 - 50;
// Get the user's unlocked background themes from inventory.
let unlockedBackgrounds = inventory.getItems("backgrounds"); // array of backgrounds
for (let i = 0; i < themeKeys.length; i++) {
let btnX = width / 2 - buttonW / 2;
let btnY = startY + i * (buttonH + 10);
// Draw button base
fill(200);
rect(btnX, btnY, buttonW, buttonH, 5);
// Check if this theme is unlocked:
let unlocked = false;
for (let j = 0; j < unlockedBackgrounds.length; j++) {
if (unlockedBackgrounds[j].id === themeKeys[i]) {
unlocked = true;
break;
}
}
if (!unlocked) {
// Overlay with grey and lock symbol if not unlocked.
fill(120, 120, 120, 150);
rect(btnX, btnY, buttonW, buttonH, 5);
fill(255);
textSize(30);
text("🔒", width / 2, btnY + buttonH / 2);
} else {
fill(0);
textSize(20);
text(themeKeys[i], width / 2, btnY + buttonH / 2);
}
}
pop();
}
// Update the windowResized function to include tutorial resizing
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
// Update border
myBorder.resize();
// Update pet boundaries
let playArea = myBorder.getPlayableArea();
myPet.setBoundaries(0, 0, playArea.width, playArea.height);
myPet.constrainPosition();
// Update house position
house.resize(playArea);
// Update backgrounds
backgrounds.resize();
// Update tutorial buttons if active
tutorial.resize();
}