Skip to content

Commit

Permalink
feat: Add level
Browse files Browse the repository at this point in the history
  • Loading branch information
johnedvard committed Aug 19, 2022
1 parent 651caad commit 1538ba6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
53 changes: 25 additions & 28 deletions src/Game.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
import { init, initPointer, initInput, GameLoop, onPointer } from 'kontra';
import { Player } from './Player';
import { Saw } from './Saw';
import { Level } from './Level';
import { lineIntersection } from './utils';
import { BACK_FORTH } from './sawBehavior';

export class Game {
canvas;
context;
player;
saw;
level;
constructor() {
const game = this;
let { canvas, context } = init();
this.canvas = canvas;
this.context = context;
initPointer();
initInput();
this.createPlayer();
this.createSaw();
this.addPointerListeners();

this.loadLevel('level1');

let loop = GameLoop({
update: function () {
game.player.update();
game.saw.update();
game.level.update();
game.checkCollisions();
},
render: function () {
game.player.render(context);
game.saw.render(context);
game.level.render(game.context);
},
});
loop.start(); // start the game
this.addPointerListeners();
}

createPlayer() {
this.player = new Player(40, 40, this);
}
createSaw() {
this.saw = new Saw(100, 200, { behavior: BACK_FORTH });
loadLevel(levelId) {
this.level = new Level(levelId, { game: this });
}

// TODO (johnedvard) Move collisions to own file?
checkCollisions() {
const rope = this.player.rope;
const rope = this.level.player.rope;
for (let i = 0; i < rope.length - 2; i++) {
if (
// TODO (johnedvard) add to y-axis if saw is up down
lineIntersection(
{ x: this.saw.x - 5, y: this.saw.y },
{ x: this.saw.x + 5, y: this.saw.y },
{ x: rope[i].x, y: rope[i].y },
{ x: rope[i + 1].x, y: rope[i + 1].y }
)
) {
console.log('intersection happened at index: ', i);
this.player.cutRope(i);
}
this.level.saws.forEach((saw) => {
if (
// TODO (johnedvard) add to y-axis if saw is up down
lineIntersection(
{ x: saw.x - 5, y: saw.y },
{ x: saw.x + 5, y: saw.y },
{ x: rope[i].x, y: rope[i].y },
{ x: rope[i + 1].x, y: rope[i + 1].y }
)
) {
this.level.player.cutRope(i);
}
});
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/Level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Player } from './Player';
import { Saw } from './Saw';
import { BACK_FORTH } from './sawBehavior';

export class Level {
player;
saws = [];
constructor(levelId, { game }) {
this.game = game;
this.createPlayer();
this.createSaw();
}

render(ctx) {
this.player.render(ctx);
this.saws.forEach((saw) => {
saw.render(ctx);
});
}

update() {
this.player.update();
this.saws.forEach((saw) => {
saw.update();
});
}
createPlayer() {
this.player = new Player(40, 40, { game: this.game });
}
createSaw() {
this.saws.push(new Saw(100, 200, { behavior: BACK_FORTH }));
}
}
6 changes: 3 additions & 3 deletions src/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class Player {
rope = []; // list of pointmasses
pointMass; // used to attach to the end of the rope
playerControls;
sprite = { render: () => {} }; // draw sprite on pointmass position
sprite = { render: () => {}, x: 0, y: 0 }; // draw sprite on pointmass position

constructor(x, y, game) {
constructor(x, y, { game }) {
this.game = game;
this.pointMass = new PointMass(x, y, { game, mass: 2 });
this.createRope();
Expand Down Expand Up @@ -64,7 +64,7 @@ export class Player {
p1.attachTo(p2);
this.rope.push(p2);
}
this.pointMass.x = this.rope[this.rope.length - 1];
// make player's pointmass attach to the rope
this.rope[this.rope.length - 1].attachTo(this.pointMass);
this.rope.push(this.pointMass);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PlayerControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export class PlayerControls {
}

initControls() {
onInput(['c'], () => this.player.cutRope(3));
onInput(['c'], () => this.player.cutRope(0));
}
}
12 changes: 12 additions & 0 deletions src/assets/level/level1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
id: 'level1',
player: {
x: 400,
y: 400,
},
objects: {
saws: [],
bricks: [{ x: 400, y: 400 }],
goals: [{ x: 400, y: 800 }],
},
};

0 comments on commit 1538ba6

Please sign in to comment.