diff --git a/package-lock.json b/package-lock.json index 7bfc675..fc7db5c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -151,9 +151,9 @@ } }, "asdf-games": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/asdf-games/-/asdf-games-1.0.8.tgz", - "integrity": "sha512-l6wE9BpbjzoJ9EetUWuglfw60UcbgA1JEC968+SnRCZlNGG92LzV+kPSbiAyLuz6+4pkuBgWJnSI/gbWZ3bzLA==" + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/asdf-games/-/asdf-games-1.0.11.tgz", + "integrity": "sha512-PXk+PKlm932HHMEsiaKsWwiC/+ZnbBSISqy80Pin8p8TM51P4Sec6iBjyIJ38SbCiDOqZqCc/fGDf0dSQoBHVg==" }, "astral-regex": { "version": "1.0.0", diff --git a/package.json b/package.json index e282931..4e1ff40 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "author": "McArn", "license": "ISC", "dependencies": { - "asdf-games": "^1.0.8" + "asdf-games": "^1.0.11" }, "devDependencies": { "electron": "^8.0.2", diff --git a/src/entities/chest.js b/src/entities/chest.js new file mode 100644 index 0000000..7abb59d --- /dev/null +++ b/src/entities/chest.js @@ -0,0 +1,52 @@ +var asdf = require("asdf-games"); +// eslint-disable-next-line no-unused-vars +const { Texture, TileSprite, AnimManager, entity } = asdf; + +var texture = new Texture("./res/tilemap.png"); + +const state = { + open: 0, + closed: 1 +}; + +class Chest extends TileSprite { + constructor(pos, player, keys, action) { + super(texture, 32, 32); + this.pos = pos; + this.scale = { x: 1, y: 1 }; + + this.anims = new AnimManager(this); + this.anims.add("open", [{ x: 3, y: 6 }], 0.1); + this.anims.add("closed", [{ x: 2, y: 6 }], 0.1); + + this.anims.play("closed"); + + this.state = state.closed; + this.player = player; + this.keys = keys; + this.action = action; + + this.hitBox = { + x: 1, + y: 1, + w: 28, + h: 28 + }; + } + + update(dt) { + super.update(dt); + if (entity.hit(this, this.player) && this.keys.action && this.state == state.closed) { + this.state = state.open; + this.action(); + } + + if (this.state == state.closed) { + this.anims.play("closed"); + } else if (this.state == state.open) { + this.anims.play("open"); + } + } +} + +module.exports = Chest; \ No newline at end of file diff --git a/src/entities/mage.js b/src/entities/mage.js index 36e628d..2dc23c3 100644 --- a/src/entities/mage.js +++ b/src/entities/mage.js @@ -46,6 +46,12 @@ class Mage extends TileSprite { update(dt) { super.update(dt); + if (entity.distance(this.player, this) < 100) { + this.state = states.attack; + } else { + this.state = states.idle; + } + if (this.state != states.idle) { // Move diff --git a/src/entities/player.js b/src/entities/player.js index 7bc2a69..96071f2 100644 --- a/src/entities/player.js +++ b/src/entities/player.js @@ -56,19 +56,19 @@ class Player extends TileSprite { // Animate if (this.keys.x == -1) { // Left - (this.keys.action && !(this.stamina.current <= 0)) ? + (this.keys.ctrl && !(this.stamina.current <= 0)) ? this.anims.play("run_w") : this.anims.play("walk_w"); } else if (this.keys.x == 1) { // Right - (this.keys.action && !(this.stamina.current <= 0)) ? + (this.keys.ctrl && !(this.stamina.current <= 0)) ? this.anims.play("run_e") : this.anims.play("walk_e"); } else if (this.keys.y == -1) { // Up - (this.keys.action && !(this.stamina.current <= 0)) ? + (this.keys.ctrl && !(this.stamina.current <= 0)) ? this.anims.play("run_n") : this.anims.play("walk_n"); } else if (this.keys.y == 1) { // Down - (this.keys.action && !(this.stamina.current <= 0)) ? + (this.keys.ctrl && !(this.stamina.current <= 0)) ? this.anims.play("run_s") : this.anims.play("walk_s"); } else { // Idle @@ -76,7 +76,7 @@ class Player extends TileSprite { } // Lose stamina - if (this.keys.action) { + if (this.keys.ctrl) { if (!(this.stamina.current <= 0)) { this.stamina.current -= dt; } else { @@ -91,9 +91,9 @@ class Player extends TileSprite { } // Speed - const xo = this.keys.x * ((this.keys.action && !(this.stamina.current <= 0)) ? + const xo = this.keys.x * ((this.keys.ctrl && !(this.stamina.current <= 0)) ? this.speed.running : this.speed.walking) * dt; - const yo = this.keys.y * ((this.keys.action && !(this.stamina.current <= 0)) ? + const yo = this.keys.y * ((this.keys.ctrl && !(this.stamina.current <= 0)) ? this.speed.running : this.speed.walking) * dt; // Move diff --git a/src/game.js b/src/game.js index ca0d4d1..793e4a2 100644 --- a/src/game.js +++ b/src/game.js @@ -29,7 +29,8 @@ const keys = new KeyControls(); var player = new Player(keys, window); -var level = new Level(player); + +var level = new Level(require("./src/levels/1-1.js"), keys, player); player.pos = level.startPos; player.level = level; @@ -44,8 +45,6 @@ game.run(() => { if (mouseAim.isDown) { console.log("cliccccccccccc"); } - // Check gamestate - // TODO }); /* ********************************************************* diff --git a/src/levels/1-1.js b/src/levels/1-1.js new file mode 100644 index 0000000..69e05eb --- /dev/null +++ b/src/levels/1-1.js @@ -0,0 +1,36 @@ +const tileSize = 32; + +var 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, + 4, 17, 17, 17, 17, 7, 0, 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 7, 0, 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 8, 2, 3, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 22, 22, 22, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, + 14, 15, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 9 + ], + size: { + w: 960, + h: 480 + }, + startPos: { + x: 48, + y: 64 + }, + entities: [ + { type: "Mage", pos: { x: 8 * 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}; } } + ] +}; + +module.exports = level; \ No newline at end of file diff --git a/src/levels/level.js b/src/levels/level.js index 7a21b7f..5aaa1f8 100644 --- a/src/levels/level.js +++ b/src/levels/level.js @@ -7,64 +7,46 @@ const tiles = require("../../res/tilemap.min.js"); const tileSize = 32; const Mage = require("../entities/mage.js"); - -const levelSize = { w: 960, h: 480 }; -var levelData = [ - 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, - 4, 17, 17, 17, 17, 7, 0, 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 7, 0, 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 8, 2, 3, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 22, 22, 22, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 4, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 7, - 14, 15, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 10, 9 -]; - -var levelTiles = levelData.map(function(e) { - return { x: (tiles[e].x / tileSize), y: (tiles[e].y / tileSize), walkable: tiles[e].walkable }; -}); +const Chest = require("../entities/chest.js"); class Level extends TileMap { - constructor (player) { - super(levelTiles, levelSize.w / tileSize, levelSize.h / tileSize, tileSize, tileSize, texture); - this.pos = { x: 0, y: 0 }; - this.startPos = { x: 48, y: 64 }; - this.scale = { x: 1, y: 1 }; - this.w = levelSize.w; - this.h = levelSize.h; + constructor (level, keys, player) { + // 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.mages = [ - new Mage({ x: 8 * tileSize, y: 2 * tileSize }, this.player, this), - new Mage({ x: 1 * tileSize, y: 8 * tileSize }, this.player, this) - ]; - - for (let index = 0; index < this.mages.length; index++) { - this.children.push(this.mages[index]); + // Handle Entities + for (let index = 0; index < level.entities.length; index++) { + const e = level.entities[index]; + var entity; + switch (e.type) { + case "Mage": + 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); }); + break; + } + console.log(e); + this.children.push(entity); } - + } update(dt) { super.update(dt); - if (this.player) { - for (let index = 0; index < this.mages.length; index++) { - const mage = this.mages[index]; - if (entity.distance(this.player, mage) < 100) { - mage.state = 1; - } else { - mage.state = 0; - } - } - } } }