Fixed some issues with level switching and items

This commit is contained in:
Arne van Iterson 2020-04-08 12:36:03 +02:00
parent 5a7573d9ae
commit 4f3468ed96
6 changed files with 79 additions and 31 deletions

View File

@ -60,7 +60,7 @@ class Mage extends TileSprite {
const yo = Math.sin(angle) * 100 * dt;
const r = wallslide.wallslide(this, this.level, xo, yo);
if (this.player.repellant) {
if (this.player.items.repellant) {
this.pos.x -= r.x;
this.pos.y -= r.y;
} else {

View File

@ -38,8 +38,10 @@ class Player extends TileSprite {
this.lives = 5;
this.key = false;
this.repellant = false;
this.items = {
key: false,
repellant: false
};
this.stamina = {
current: 5,

View File

@ -22,7 +22,7 @@ class Portal extends TileSprite {
x: 1,
y: 1,
w: 30,
h: 30
h: 45
};
break;
case "Ladder":
@ -50,8 +50,9 @@ class Portal extends TileSprite {
super.update(dt);
if (entity.hit(this, this.player) && this.keys.action) {
if (this.key) {
if (this.player.key) {
if (this.player.items.key) {
this.action();
this.player.items.key = false;
} else {
console.log("keyn't");
}

View File

@ -2,7 +2,7 @@ const { ipcRenderer, remote } = require("electron");
var asdf = require("asdf-games");
// eslint-disable-next-line no-unused-vars
const { Game, KeyControls, MouseControls, Camera, Lightsource } = asdf;
const { Game, KeyControls, MouseControls, Camera } = asdf;
const window = { w: 640, h: 320 };
@ -11,37 +11,36 @@ ipcRenderer.send("resize", window);
const { scene } = game;
var Player = require("./src/entities/player.js");
var Level = require("./src/levels/level.js");
const mouseAim = new MouseControls(document.getElementById("board"));
const keys = new KeyControls();
var Player = require("./src/entities/player.js");
var Level = require("./src/levels/level.js");
// Initialise first level 1-1.js at startPosition 0
var player = new Player(keys, window);
var level = new Level(require("./src/levels/1-1.js"), keys, player);
player.pos.x = level.startPos.x / 1;
player.pos.y = level.startPos.y / 1;
player.pos.x = level.startPos[0].x / 1;
player.pos.y = level.startPos[0].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);
const camera = new Camera(player, window, { w: level.w * 2, h: level.h * 2 });
scene.add(camera);
camera.add(level);
camera.add(player);
//camera.add(lightsource);
function switchLevel(module) {
function switchLevel(module, pos = 0) {
camera.map(function(e) {
camera.remove(e);
if (e instanceof Level) {
var items = player.items;
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.pos.x = level.startPos[pos].x / 1;
player.pos.y = level.startPos[pos].y / 1;
player.items = items;
player.level = e;
camera.add(e);
@ -52,22 +51,27 @@ function switchLevel(module) {
}
game.run(() => {
// Debugging tools
if (mouseAim.isDown) {
console.log("cliccccccccccc");
console.log(player.items);
}
// Switch to another level
if (level.switch) {
switchLevel(require(level.switch));
switchLevel(require(level.switch.module), level.switch.pos);
delete level.switch;
console.log(player);
}
// Refocus camera to player when player is moved by a portal
if (player.refocus) {
camera.setSubject(player);
player.refocus = false;
}
});
/* *********************************************************
Settings behavior
********************************************************* */

View File

@ -27,10 +27,31 @@ let level = {
y: 64
},
entities: [
{ 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}; player.refocus = true; }, key: true}
{
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.items.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

@ -22,15 +22,35 @@ let level = {
w: 960,
h: 480
},
startPos: {
x: 48,
y: 64
},
startPos: [
{ x: 48, y: 64 },
{ x: 48, y: 128 }
],
entities: [
{ 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, level) => { level.switch = "./src/levels/1-1 copy.js"; }, key: true}
{
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.items.key = true;
}
},
{
type: "Portal",
pos: { x: 8 * tileSize, y: 2 * tileSize },
texture: "Ladder",
action: (player, level) => {
level.switch = { module: "./src/levels/1-1 copy.js", pos: 0 };
},
key: true
}
]
};