Skip to content

Commit

Permalink
feat: Add level complete and load next level
Browse files Browse the repository at this point in the history
  • Loading branch information
johnedvard committed Aug 22, 2022
1 parent 6843b19 commit 1cf79b0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/Game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { init, initPointer, initInput, GameLoop, onPointer } from 'kontra';
import { init, initPointer, initInput, GameLoop, onPointer, on } from 'kontra';
import { LEVEL_COMPLETE } from './gameEvents';
import { Level } from './Level';

export class Game {
Expand All @@ -16,7 +17,7 @@ export class Game {
initInput();
this.addPointerListeners();

this.loadLevel('level1');
this.loadLevel(1);

let loop = GameLoop({
update: function () {
Expand All @@ -28,9 +29,11 @@ export class Game {
});

loop.start(); // start the game
this.listenForGameEvents();
}

loadLevel(levelId) {
console.log('load level', levelId);
this.level = new Level(levelId, { game: this });
}

Expand All @@ -42,4 +45,12 @@ export class Game {
this.isDragging = false;
});
}

listenForGameEvents() {
on(LEVEL_COMPLETE, this.onLevelComplete);
}
onLevelComplete = () => {
this.level.destroy();
this.loadLevel(this.level.levelId + 1);
};
}
3 changes: 2 additions & 1 deletion src/Goal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { emit } from 'kontra';

import { isBoxCollision } from './utils';
import { GOAL_COLLISION } from './gameEvents';
import { GOAL_COLLISION, LEVEL_COMPLETE } from './gameEvents';

export class Goal {
level;
Expand Down Expand Up @@ -52,6 +52,7 @@ export class Goal {
if (this.radiusY <= 0) this.radiusY = 0;
if (this.radiusX <= 0 && this.radiusY <= 0) {
this.hasVanished = true;
emit(LEVEL_COMPLETE);
}
}
checkCollision() {
Expand Down
11 changes: 10 additions & 1 deletion src/Level.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { emit, on, off } from 'kontra';

import { GOAL_COLLISION, LEVEL_COMPLETE } from './gameEvents';
import { Goal } from './Goal';
import { Player } from './Player';
import { Saw } from './Saw';
Expand All @@ -8,9 +11,11 @@ export class Level {
saws = [];
goals = [];
isLevelLoaded = false;
levelId = -1;
constructor(levelId, { game }) {
this.game = game;
this.loadLevel(levelId).then((levelData) => {
this.levelId = levelId;
this.loadLevel('level' + levelId).then((levelData) => {
this.createPlayer(levelData);
this.createSaws(levelData);
this.createGoals(levelData);
Expand Down Expand Up @@ -89,4 +94,8 @@ export class Level {
});
}
}

destroy() {
console.log('destroy level');
}
}
1 change: 1 addition & 0 deletions src/gameEvents.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const GOAL_COLLISION = 'gc';
export const ARCADIAN_ADDED = 'aa';
export const LEVEL_COMPLETE = 'lc';

0 comments on commit 1cf79b0

Please sign in to comment.