Added level switching

This commit is contained in:
Arne van Iterson 2020-04-08 10:14:24 +02:00
parent 7832563728
commit 5a7573d9ae
5 changed files with 38 additions and 21 deletions

View File

@ -39,13 +39,15 @@ class Player extends TileSprite {
this.lives = 5;
this.key = false;
this.repellant = true;
this.repellant = false;
this.stamina = {
current: 5,
max: 5
};
this.refocus = false;
this.hitBox = {
x: 4,
y: 0,

View File

@ -20,11 +20,12 @@ const keys = new KeyControls();
var player = new Player(keys, window);
var level = new Level(require("./src/levels/1-1.js"), keys, player);
player.pos = level.startPos;
player.pos.x = level.startPos.x / 1;
player.pos.y = level.startPos.y / 1;
player.level = level;
// Lightsource makes the games extremely laggy for some reason, ill have to check the performance
//var lightsource = new Lightsource(player.pos.x, player.pos.y, level.pos.x, level.pos.y, level.w, level.h);
// var lightsource = new Lightsource(player.pos.x, player.pos.y, level.pos.x, level.pos.y, level.w, level.h);
const camera = new Camera(player, window, { w: level.w * 2, h: level.h * 2 });
@ -33,23 +34,37 @@ camera.add(level);
camera.add(player);
//camera.add(lightsource);
// function switchLevel(camera, module) {
// camera.map(function(e) {
// camera.remove(e);
// if (e instanceof Level) {
// e = new Level(module, keys, player);
// player.pos = level.startPos;
// player.level = level;
// camera.add(e);
// camera.add(player);
// }
// });
// }
function switchLevel(module) {
camera.map(function(e) {
camera.remove(e);
if (e instanceof Level) {
player = new Player(keys, window);
e = new Level(module, keys, player);
player.pos.x = level.startPos.x / 1;
player.pos.y = level.startPos.y / 1;
player.level = e;
camera.add(e);
camera.add(player);
camera.setSubject(player);
}
});
}
game.run(() => {
if (mouseAim.isDown) {
console.log("cliccccccccccc");
switchLevel(camera, require("./src/levels/1-1 copy.js"));
}
if (level.switch) {
switchLevel(require(level.switch));
delete level.switch;
console.log(player);
}
if (player.refocus) {
camera.setSubject(player);
player.refocus = false;
}
});

View File

@ -30,7 +30,7 @@ let level = {
{ 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.key = true; } },
{ type: "Portal", pos: { x: 8 * tileSize, y: 2 * tileSize }, texture: "Ladder", action: (player) => { player.pos = {x:512,y:256}; }, key: true}
{ type: "Portal", pos: { x: 8 * tileSize, y: 2 * tileSize }, texture: "Ladder", action: (player) => { player.pos = {x:512,y:256}; player.refocus = true; }, key: true}
]
};

View File

@ -30,7 +30,7 @@ let level = {
{ 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.key = true; } },
{ type: "Portal", pos: { x: 8 * tileSize, y: 2 * tileSize }, texture: "Ladder", action: (player) => { player.pos = {x:512,y:256}; }, key: true}
{ type: "Portal", pos: { x: 8 * tileSize, y: 2 * tileSize }, texture: "Ladder", action: (player, level) => { level.switch = "./src/levels/1-1 copy.js"; }, key: true}
]
};

View File

@ -32,13 +32,13 @@ class Level extends TileMap {
let e = level.entities[index];
switch (e.type) {
case "Mage":
e.entity = new Mage(e.pos, this.player, this);
e.entity = new Mage({ x: e.pos.x / 1, y: e.pos.y / 1 }, this.player, this);
break;
case "Chest":
e.entity = new Chest(e.pos, this.player, this.keys, () => { return e.action(this.player); });
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(e.pos, this.player, this.keys, e.texture, () => { return e.action(this.player); }, e.key);
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);