caa-game/src/levels/level.js

54 lines
1.7 KiB
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileMap, entity } = asdf;
const texture = new Texture("./res/tilemap.png");
const tiles = require("../../res/tilemap.js");
const tileSize = 32;
const Mage = require("../entities/mage.js");
const Chest = require("../entities/chest.js");
const Portal = require("../entities/portal.js");
class Level extends TileMap {
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++) {
let e = level.entities[index];
switch (e.type) {
case "Mage":
e.entity = new Mage({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this);
break;
case "Chest":
e.entity = new Chest({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this.keys, () => { return e.action(this.player); });
break;
case "Portal":
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;
}
this.children.push(e.entity);
}
}
update(dt) {
super.update(dt);
}
}
module.exports = Level;