Skip to content

Commit

Permalink
feat: Add saw behavior, east west, and level 3
Browse files Browse the repository at this point in the history
  • Loading branch information
johnedvard committed Aug 28, 2022
1 parent bb6319d commit 8e22d36
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Level.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@ export class Level {
createSaws(levelData) {
levelData.s.forEach((saw) => {
// TODO (johnedvard) Add actual saw behaviour
this.saws.push(new Saw(saw.x, saw.y, { level: this }));
this.saws.push(
new Saw(saw.x, saw.y, { level: this, behavior: saw.b, distance: saw.d })
);
});
}

createHearts(levelData) {
levelData.h.forEach((heart) => {
this.hearts.push(new Heart(heart.x, heart.y, { level: this }));
});
}

createBricks(levelData) {
levelData.b.forEach((brick) => {
this.bricks.push(new Brick(brick.x, brick.y, { level: this }));
Expand Down
28 changes: 25 additions & 3 deletions src/Saw.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ export class Saw {
scale = 4;
rotSpeed = 0.2;
level;
direction = 'e'; // n,s,e,w

constructor(x, y, { behavior, distance, level }) {
this.x = x;
this.y = y;
this.orgX = x;
this.orgY = y;
this.distance = distance;
this.distance = 200;
this.behavior = behavior;
this.level = level;
this.createSprite();
}

update() {
this.moveDistance(this.behavior, this.distance);
this.sprite.rotation += this.rotSpeed;
}
moveDistance(behavior, distance) {
let axis = '';
let multiplier = 1;
switch (behavior) {
case UP_DOWN:
axis = 'y';
Expand All @@ -42,9 +43,30 @@ export class Saw {
default:
}

this[axis] += this.speed;
switch (this.direction) {
case 'n':
multiplier = -1;
break;
case 's':
multiplier = 1;
break;
case 'e':
multiplier = 1;
if (this.orgX + distance < this.x) {
this.direction = 'w';
}
break;
case 'w':
multiplier = -1;
if (this.orgX - distance > this.x) {
this.direction = 'e';
}
break;
}
this[axis] += this.speed * multiplier;
this.sprite.x = this.x;
this.sprite.y = this.y;
this.sprite.rotation += this.rotSpeed * multiplier;
}
render(_ctx) {
this.sprite.render();
Expand Down
11 changes: 11 additions & 0 deletions src/level/level3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"p": { "x": 272, "y": 255 },
"r": 30,
"s": [{ "x": 464, "y": 350, "b": "ew", "d": 100 }],
"b": [{ "x": 260, "y": 220 }],
"g": [
{ "x": 368, "y": 700 },
{ "x": 600, "y": 700 }
],
"h": [{ "x": 550, "y": 400 }]
}
4 changes: 2 additions & 2 deletions src/sawBehavior.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const BACK_FORTH = 0;
export const UP_DOWN = 1;
export const BACK_FORTH = 'ew';
export const UP_DOWN = 'ns';

0 comments on commit 8e22d36

Please sign in to comment.