Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#79 adding counter timer #106

Merged
merged 4 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/core/GameManager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Fruit } from '../models/Fruit.js';
import { Wall } from '../models/Wall.js';
import { Score } from '../models/Score.js';
import { Timer } from '../models/Timer.js';
import { checkCollision } from '../utils/CheckCollision.js';
import { ToolManager } from './ToolManager.js';
import { IncidentManager } from './IncidentManager.js';
Expand All @@ -9,6 +10,7 @@ export class Game {
constructor() {
this.fruits = [];
this.timer = 0;
this.counter = new Timer(120);
this.currentFruit = null;
this.gravity = 15;
this.walls = [];
Expand All @@ -24,6 +26,8 @@ export class Game {
world.gravity.y = this.gravity;

this.walls = Wall.createDefaultWalls();
//Start counter
this.counter.start();

this.currentFruit = new Fruit(0, 300, 25, 30);
let shuffleButton = createButton('Shake Tool');
Expand Down Expand Up @@ -68,6 +72,15 @@ export class Game {
this.incidentManager.update();

this.displayScore();
this.displayCounter();

// If counter is 0, end game
if (this.counter.getTimeLeft() <= 0) {
console.log("End of game because counter");
noLoop();
}


}

setCurrentFruit(fruit) {
Expand Down Expand Up @@ -133,6 +146,13 @@ export class Game {
text(`Score: ${this.score.getScore()}`, 10, 30);
}

displayCounter() {
fill(0);
textSize(16);
text(`Timer: ${this.counter.getTimeLeft()}s`, 300, 60);
}


isClickingUI(mx, my) {
let uiButtons = selectAll('button');
for (let btn of uiButtons) {
Expand Down
31 changes: 31 additions & 0 deletions docs/models/Timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export class Timer {
constructor(duration) {
this.duration = duration;
this.startTime = null;
this.running = false;
}

start() {
this.startTime = millis();
this.running = true;
}

getTimeLeft() {
if (!this.running) return this.duration;
const elapsed = (millis() - this.startTime) / 1000;
return Math.max(0, (this.duration - elapsed).toFixed(0));
}

addTime(seconds) {
this.startTime -= seconds * 1000;
}

reset() {
this.startTime = millis();
this.running = true;
}

stop() {
this.running = false;
}
}