Skip to content

Commit 1cf79b0

Browse files
committed
feat: Add level complete and load next level
1 parent 6843b19 commit 1cf79b0

4 files changed

Lines changed: 26 additions & 4 deletions

File tree

src/Game.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { init, initPointer, initInput, GameLoop, onPointer } from 'kontra';
1+
import { init, initPointer, initInput, GameLoop, onPointer, on } from 'kontra';
2+
import { LEVEL_COMPLETE } from './gameEvents';
23
import { Level } from './Level';
34

45
export class Game {
@@ -16,7 +17,7 @@ export class Game {
1617
initInput();
1718
this.addPointerListeners();
1819

19-
this.loadLevel('level1');
20+
this.loadLevel(1);
2021

2122
let loop = GameLoop({
2223
update: function () {
@@ -28,9 +29,11 @@ export class Game {
2829
});
2930

3031
loop.start(); // start the game
32+
this.listenForGameEvents();
3133
}
3234

3335
loadLevel(levelId) {
36+
console.log('load level', levelId);
3437
this.level = new Level(levelId, { game: this });
3538
}
3639

@@ -42,4 +45,12 @@ export class Game {
4245
this.isDragging = false;
4346
});
4447
}
48+
49+
listenForGameEvents() {
50+
on(LEVEL_COMPLETE, this.onLevelComplete);
51+
}
52+
onLevelComplete = () => {
53+
this.level.destroy();
54+
this.loadLevel(this.level.levelId + 1);
55+
};
4556
}

src/Goal.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { emit } from 'kontra';
22

33
import { isBoxCollision } from './utils';
4-
import { GOAL_COLLISION } from './gameEvents';
4+
import { GOAL_COLLISION, LEVEL_COMPLETE } from './gameEvents';
55

66
export class Goal {
77
level;
@@ -52,6 +52,7 @@ export class Goal {
5252
if (this.radiusY <= 0) this.radiusY = 0;
5353
if (this.radiusX <= 0 && this.radiusY <= 0) {
5454
this.hasVanished = true;
55+
emit(LEVEL_COMPLETE);
5556
}
5657
}
5758
checkCollision() {

src/Level.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { emit, on, off } from 'kontra';
2+
3+
import { GOAL_COLLISION, LEVEL_COMPLETE } from './gameEvents';
14
import { Goal } from './Goal';
25
import { Player } from './Player';
36
import { Saw } from './Saw';
@@ -8,9 +11,11 @@ export class Level {
811
saws = [];
912
goals = [];
1013
isLevelLoaded = false;
14+
levelId = -1;
1115
constructor(levelId, { game }) {
1216
this.game = game;
13-
this.loadLevel(levelId).then((levelData) => {
17+
this.levelId = levelId;
18+
this.loadLevel('level' + levelId).then((levelData) => {
1419
this.createPlayer(levelData);
1520
this.createSaws(levelData);
1621
this.createGoals(levelData);
@@ -89,4 +94,8 @@ export class Level {
8994
});
9095
}
9196
}
97+
98+
destroy() {
99+
console.log('destroy level');
100+
}
92101
}

src/gameEvents.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const GOAL_COLLISION = 'gc';
22
export const ARCADIAN_ADDED = 'aa';
3+
export const LEVEL_COMPLETE = 'lc';

0 commit comments

Comments
 (0)