2020-03-02 22:06:16 +01:00
|
|
|
var asdf = require("asdf-games");
|
2020-03-29 15:40:57 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const { Texture, TileMap, entity } = asdf;
|
2020-03-02 22:06:16 +01:00
|
|
|
|
2020-03-22 15:54:55 +01:00
|
|
|
const texture = new Texture("./res/tilemap.png");
|
2020-04-06 12:25:12 +02:00
|
|
|
const tiles = require("../../res/tilemap.js");
|
2020-03-25 17:06:26 +01:00
|
|
|
const tileSize = 32;
|
|
|
|
|
2020-03-29 15:40:57 +02:00
|
|
|
const Mage = require("../entities/mage.js");
|
2020-03-31 10:33:44 +02:00
|
|
|
const Chest = require("../entities/chest.js");
|
2020-04-06 22:49:32 +02:00
|
|
|
const Portal = require("../entities/portal.js");
|
2020-03-02 22:06:16 +01:00
|
|
|
|
|
|
|
class Level extends TileMap {
|
2020-04-06 22:49:32 +02:00
|
|
|
constructor (level, keys, player) {
|
2020-03-31 10:33:44 +02:00
|
|
|
// Convert id array to tile array
|
|
|
|
var levelTiles = level.tiles.map(function(e) {
|
|
|
|
return { x: (tiles[e].x / tileSize), y: (tiles[e].y / tileSize), walkable: tiles[e].walkable };
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set level variables
|
|
|
|
super(levelTiles, level.size.w / tileSize, level.size.h / tileSize, tileSize, tileSize, texture);
|
2020-03-22 15:54:55 +01:00
|
|
|
this.pos = { x: 0, y: 0 };
|
2020-03-31 10:33:44 +02:00
|
|
|
this.startPos = level.startPos;
|
2020-03-23 20:55:39 +01:00
|
|
|
this.scale = { x: 1, y: 1 };
|
2020-03-31 10:33:44 +02:00
|
|
|
this.w = level.size.w;
|
|
|
|
this.h = level.size.h;
|
|
|
|
this.keys = keys;
|
2020-03-29 15:40:57 +02:00
|
|
|
this.player = player;
|
|
|
|
|
2020-03-31 10:33:44 +02:00
|
|
|
// Handle Entities
|
|
|
|
for (let index = 0; index < level.entities.length; index++) {
|
2020-04-06 22:49:32 +02:00
|
|
|
let e = level.entities[index];
|
2020-03-31 10:33:44 +02:00
|
|
|
switch (e.type) {
|
|
|
|
case "Mage":
|
2020-04-08 10:14:24 +02:00
|
|
|
e.entity = new Mage({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this);
|
2020-03-31 10:33:44 +02:00
|
|
|
break;
|
|
|
|
case "Chest":
|
2020-04-08 10:14:24 +02:00
|
|
|
e.entity = new Chest({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this.keys, () => { return e.action(this.player); });
|
2020-04-06 22:49:32 +02:00
|
|
|
break;
|
|
|
|
case "Portal":
|
2020-04-08 10:14:24 +02:00
|
|
|
e.entity = new Portal({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this.keys, e.texture, () => { return e.action(this.player, this); }, e.key);
|
2020-03-31 10:33:44 +02:00
|
|
|
break;
|
|
|
|
}
|
2020-04-06 22:49:32 +02:00
|
|
|
this.children.push(e.entity);
|
2020-03-29 15:40:57 +02:00
|
|
|
}
|
2020-03-31 10:33:44 +02:00
|
|
|
|
2020-03-29 15:40:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update(dt) {
|
|
|
|
super.update(dt);
|
2020-03-15 17:31:32 +01:00
|
|
|
}
|
2020-03-02 22:06:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Level;
|