diff --git a/docs/Elevator.md b/docs/Elevator.md new file mode 100644 index 0000000..5da4ab1 --- /dev/null +++ b/docs/Elevator.md @@ -0,0 +1,46 @@ +# Elevator + +`Elevator` is a collection of `Line` [_entities_](Entity.md). +You can make the levels go up or down. + +## Properties + +### `x: number` + +`x` is the leftmost x-position of the elevator. + +### `y: number` + +`y` is the top y-position of the elevator. + +### `width: number` + +`width` is the total width of the elevator (and its levels) + +### `height: number` + +`height` is the total height of the elevator. + +### `speed: number` + +`speed` is the amount of pixels that the elevator will move per frame. + +### `levels: number` + +`levels` is the amount of lines created for the elevator. + +#### Example: Make an elevator and make it go up + +```ts +let elevator; + +tile.setup = function () { + elevator = tile.createElevator(100, 100, 150, 500, 5, 3); +}; + +tile.onTick = function () { + if (elevator !== undefined) { + elev.goUp(); + } +}; +``` diff --git a/src/Elevator.js b/src/Elevator.js new file mode 100644 index 0000000..1bae0d4 --- /dev/null +++ b/src/Elevator.js @@ -0,0 +1,45 @@ +import Line from "./Line.js"; + +class Elevator { + constructor(tile, x1, y1, width, height, speed, levels) { + this.levels = []; + this.x1 = x1; + this.y1 = y1; + this.width = width; + this.height = height; + this.x2 = x1 + width; + this.y2 = y1 + height; + this.speed = speed; + this.gap = height / levels; + for (let i = 0; i < levels; i++) { + y1 -= this.gap; + const level = new Line(tile, x1, y1, this.x2, y1, 2, false); + this.levels[i] = level.body; + } + Matter.Composite.add(tile.game.engine.world, levels); + } + + goUp() { + for (const body of this.levels) { + Matter.Body.translate(body, { x: 0, y: -this.speed }); + + if (body.position.y < this.y1) { + const nextY = this.y2 + (body.position.y - this.y1); + Matter.Body.translate(body, { x: 0, y: nextY - body.position.y }); + } + } + } + + goDown() { + for (const body of this.levels) { + Matter.Body.translate(body, { x: 0, y: -this.speed }); + + if (body.position.y < this.y1) { + const nextY = this.y1 + (body.position.y - this.y2); + Matter.Body.translate(body, { x: 0, y: body.position.y - nextY }); + } + } + } +} + +export default Elevator; \ No newline at end of file diff --git a/src/Tile.js b/src/Tile.js index 0b7d9bf..155e959 100644 --- a/src/Tile.js +++ b/src/Tile.js @@ -20,6 +20,7 @@ import Ramp from "./Ramp.js"; import Rope from "./Rope.js"; import Spring from "./Spring.js"; import Portal from "./Portal.js"; +import Elevator from "./Elevator.js"; /** * A tile in the game grid @@ -278,6 +279,19 @@ class Tile { new Rope(this, this.left + x, this.top + y, length, options); } + /** + * @method createElevator + * @param {number} x - the left x value + * @param {number} y - the top y value + * @param {number} width - width of elevator + * @param {number} height - height of elevator + * @param {number} speed - distance traveled per step + * @param {number} levels - how many levels are there + */ + createElevator(x, y, width, height, speed, levels) { + return new Elevator(this, this.left + x, this.top + y, width, height, speed, levels); + } + createZone(x, y, width, height, start, during, end, options = {}) { return new Zone(this, this.left + x, this.top + y, width, height, start, during, end, options); }