Added portals and doors

This commit is contained in:
Arne van Iterson 2020-04-06 22:49:32 +02:00
parent 02c8e5587c
commit fe5e8cf993
3 changed files with 68 additions and 10 deletions

54
src/entities/portal.js Normal file
View File

@ -0,0 +1,54 @@
var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Texture, TileSprite, entity } = asdf;
var texture = new Texture("./res/tilemap.png");
class Portal extends TileSprite {
constructor(pos, player, type, action) {
super(texture, 32, 32);
this.pos = pos;
this.scale = { x: 1, y: 1 };
this.player = player;
this.action = action;
switch (type) {
case "Door":
this.frame = { x: 1, y: 7 };
this.hitBox = {
x: 1,
y: 1,
w: 30,
h: 30
};
break;
case "Ladder":
this.frame = { x: 2, y: 7 };
this.hitBox = {
x: 4,
y: 0,
w: 24,
h: 32
};
break;
case "Stairs":
this.frame = { x: 4, y: 7 };
this.hitBox = {
x: 1,
y: 1,
w: 30,
h: 30
};
break;
}
}
update(dt) {
super.update(dt);
if (entity.hit(this, this.player)) {
this.action();
}
}
}
module.exports = Portal;

View File

@ -1,6 +1,6 @@
const tileSize = 32;
var level = {
let level = {
tiles: [
1, 2, 2, 2, 2, 6, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 6,
4, 21, 22, 23, 21, 7, 0, 4, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 7,
@ -27,9 +27,10 @@ var level = {
y: 64
},
entities: [
{ type: "Mage", pos: { x: 8 * tileSize, y: 2 * tileSize }},
{ type: "Mage", pos: { x: 9 * tileSize, y: 2 * tileSize }},
{ type: "Mage", pos: { x: 1 * tileSize, y: 8 * tileSize }},
{ type: "Chest", pos: { x: 4 * tileSize, y: 2 * tileSize }, action: (player) => { player.pos = {x:512,y:256}; } }
{ type: "Chest", pos: { x: 4 * tileSize, y: 2 * tileSize }, action: (player) => { player.pos = {x:512,y:256}; } },
{ type: "Portal", pos: { x: 8 * tileSize, y: 2 * tileSize }, texture: "Ladder", action: (player) => { player.pos = {x:512,y:256}; }}
]
};

View File

@ -8,9 +8,11 @@ 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) {
console.log(level);
// Convert id array to tile array
var levelTiles = level.tiles.map(function(e) {
@ -29,18 +31,19 @@ class Level extends TileMap {
// Handle Entities
for (let index = 0; index < level.entities.length; index++) {
const e = level.entities[index];
var entity;
let e = level.entities[index];
switch (e.type) {
case "Mage":
entity = new Mage(e.pos, this.player, this);
e.entity = new Mage(e.pos, this.player, this);
break;
case "Chest":
entity = new Chest(e.pos, this.player, this.keys, () => { return e.action(this.player); });
e.entity = new Chest(e.pos, this.player, this.keys, () => { return e.action(this.player); });
break;
case "Portal":
e.entity = new Portal(e.pos, this.player, e.texture, () => { return e.action(this.player); });
break;
}
console.log(e);
this.children.push(entity);
this.children.push(e.entity);
}
}