diff --git a/src/entities/mage.js b/src/entities/mage.js index 820d763..4c79aec 100644 --- a/src/entities/mage.js +++ b/src/entities/mage.js @@ -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 { diff --git a/src/entities/player.js b/src/entities/player.js index 37007c3..9d7597a 100644 --- a/src/entities/player.js +++ b/src/entities/player.js @@ -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, diff --git a/src/entities/portal.js b/src/entities/portal.js index 634f07f..811d76f 100644 --- a/src/entities/portal.js +++ b/src/entities/portal.js @@ -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"); } diff --git a/src/game.js b/src/game.js index 15aad39..dc5ff7f 100644 --- a/src/game.js +++ b/src/game.js @@ -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 ********************************************************* */ diff --git a/src/levels/1-1 copy.js b/src/levels/1-1 copy.js index bc6b0bb..e1d08bd 100644 --- a/src/levels/1-1 copy.js +++ b/src/levels/1-1 copy.js @@ -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 + } ] }; diff --git a/src/levels/1-1.js b/src/levels/1-1.js index 254dbea..10e3243 100644 --- a/src/levels/1-1.js +++ b/src/levels/1-1.js @@ -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 + } ] };