diff --git a/src/Level.js b/src/Level.js index 35b35af..cebb19d 100644 --- a/src/Level.js +++ b/src/Level.js @@ -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 })); diff --git a/src/Saw.js b/src/Saw.js index 6d24b6d..a817278 100644 --- a/src/Saw.js +++ b/src/Saw.js @@ -14,13 +14,14 @@ 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(); @@ -28,10 +29,10 @@ export class Saw { 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'; @@ -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(); diff --git a/src/level/level3.json b/src/level/level3.json new file mode 100644 index 0000000..e7201c0 --- /dev/null +++ b/src/level/level3.json @@ -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 }] +} diff --git a/src/sawBehavior.js b/src/sawBehavior.js index b7a983a..dd41ffd 100644 --- a/src/sawBehavior.js +++ b/src/sawBehavior.js @@ -1,2 +1,2 @@ -export const BACK_FORTH = 0; -export const UP_DOWN = 1; +export const BACK_FORTH = 'ew'; +export const UP_DOWN = 'ns';