caa-game/src/helpers/level.js

75 lines
2.3 KiB
JavaScript

var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileMap, Container, Sound } = asdf;
const texture = new Texture(__dirname + "/../../res/images/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");
const Sign = require("../entities/sign.js");
// const sounds = {
// bg: new Sound(__dirname + "/../../res/sounds/bg.wav", { volume: 0.05, loop: true })
// };
class Level extends TileMap {
constructor (level, keys, player, game) {
// 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;
this.game = game;
this.gameComplete = false;
this.switch = false;
// 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, this, () => { return e.action(this.player, this); });
break;
case "Portal":
e.entity = new Portal({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this.keys, this, e.texture, () => { return e.action(this.player, this); }, e.key);
break;
case "Sign":
e.entity = new Sign({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this.keys, this, e.text);
break;
}
this.children.push(e.entity);
}
this.entities = new Container();
this.children.push(this.entities);
this.textbox = new Container();
this.game.add(this.textbox);
// sounds.bg.play();
}
update(dt) {
super.update(dt);
}
}
module.exports = Level;