Skip to content

Commit

Permalink
Merge pull request #106 from UoB-COMSM0166/gerod_branch
Browse files Browse the repository at this point in the history
#79 adding counter timer
  • Loading branch information
chiachia0120 authored Feb 27, 2025
2 parents d65db70 + 44dc23f commit 745a26f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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;
}
}

0 comments on commit 745a26f

Please sign in to comment.