Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docs/Elevator.md
Original file line number Diff line number Diff line change
@@ -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();
}
};
```
45 changes: 45 additions & 0 deletions src/Elevator.js
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions src/Tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down