caa-game/src/levels/level.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-03-02 22:06:16 +01:00
var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileMap, entity } = asdf;
2020-03-02 22:06:16 +01:00
const texture = new Texture("./res/tilemap.png");
2020-04-06 12:25:12 +02:00
const tiles = require("../../res/tilemap.js");
const tileSize = 32;
const Mage = require("../entities/mage.js");
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) {
// 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);
this.pos = { x: 0, y: 0 };
this.startPos = level.startPos;
this.scale = { x: 1, y: 1 };
this.w = level.size.w;
this.h = level.size.h;
this.keys = keys;
this.player = player;
// Handle Entities
for (let index = 0; index < level.entities.length; index++) {
2020-04-06 22:49:32 +02:00
let e = level.entities[index];
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);
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);
break;
}
2020-04-06 22:49:32 +02:00
this.children.push(e.entity);
}
}
update(dt) {
super.update(dt);
}
2020-03-02 22:06:16 +01:00
}
module.exports = Level;