Added chests, variable levels and extra controls.

This commit is contained in:
Arne van Iterson 2020-03-31 10:33:44 +02:00
parent c86f2ed370
commit 02c8e5587c
8 changed files with 137 additions and 62 deletions

6
package-lock.json generated
View File

@ -151,9 +151,9 @@
} }
}, },
"asdf-games": { "asdf-games": {
"version": "1.0.8", "version": "1.0.11",
"resolved": "https://registry.npmjs.org/asdf-games/-/asdf-games-1.0.8.tgz", "resolved": "https://registry.npmjs.org/asdf-games/-/asdf-games-1.0.11.tgz",
"integrity": "sha512-l6wE9BpbjzoJ9EetUWuglfw60UcbgA1JEC968+SnRCZlNGG92LzV+kPSbiAyLuz6+4pkuBgWJnSI/gbWZ3bzLA==" "integrity": "sha512-PXk+PKlm932HHMEsiaKsWwiC/+ZnbBSISqy80Pin8p8TM51P4Sec6iBjyIJ38SbCiDOqZqCc/fGDf0dSQoBHVg=="
}, },
"astral-regex": { "astral-regex": {
"version": "1.0.0", "version": "1.0.0",

View File

@ -10,7 +10,7 @@
"author": "McArn", "author": "McArn",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"asdf-games": "^1.0.8" "asdf-games": "^1.0.11"
}, },
"devDependencies": { "devDependencies": {
"electron": "^8.0.2", "electron": "^8.0.2",

52
src/entities/chest.js Normal file
View File

@ -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;

View File

@ -46,6 +46,12 @@ class Mage extends TileSprite {
update(dt) { update(dt) {
super.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) { if (this.state != states.idle) {
// Move // Move

View File

@ -56,19 +56,19 @@ class Player extends TileSprite {
// Animate // Animate
if (this.keys.x == -1) { if (this.keys.x == -1) {
// Left // 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"); this.anims.play("run_w") : this.anims.play("walk_w");
} else if (this.keys.x == 1) { } else if (this.keys.x == 1) {
// Right // 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"); this.anims.play("run_e") : this.anims.play("walk_e");
} else if (this.keys.y == -1) { } else if (this.keys.y == -1) {
// Up // 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"); this.anims.play("run_n") : this.anims.play("walk_n");
} else if (this.keys.y == 1) { } else if (this.keys.y == 1) {
// Down // 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"); this.anims.play("run_s") : this.anims.play("walk_s");
} else { } else {
// Idle // Idle
@ -76,7 +76,7 @@ class Player extends TileSprite {
} }
// Lose stamina // Lose stamina
if (this.keys.action) { if (this.keys.ctrl) {
if (!(this.stamina.current <= 0)) { if (!(this.stamina.current <= 0)) {
this.stamina.current -= dt; this.stamina.current -= dt;
} else { } else {
@ -91,9 +91,9 @@ class Player extends TileSprite {
} }
// Speed // 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; 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; this.speed.running : this.speed.walking) * dt;
// Move // Move

View File

@ -29,7 +29,8 @@ const keys = new KeyControls();
var player = new Player(keys, window); 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.pos = level.startPos;
player.level = level; player.level = level;
@ -44,8 +45,6 @@ game.run(() => {
if (mouseAim.isDown) { if (mouseAim.isDown) {
console.log("cliccccccccccc"); console.log("cliccccccccccc");
} }
// Check gamestate
// TODO
}); });
/* ********************************************************* /* *********************************************************

36
src/levels/1-1.js Normal file
View File

@ -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;

View File

@ -7,64 +7,46 @@ const tiles = require("../../res/tilemap.min.js");
const tileSize = 32; const tileSize = 32;
const Mage = require("../entities/mage.js"); const Mage = require("../entities/mage.js");
const Chest = require("../entities/chest.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 };
});
class Level extends TileMap { class Level extends TileMap {
constructor (player) { constructor (level, keys, 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;
// 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.player = player;
this.mages = [ // Handle Entities
new Mage({ x: 8 * tileSize, y: 2 * tileSize }, this.player, this), for (let index = 0; index < level.entities.length; index++) {
new Mage({ x: 1 * tileSize, y: 8 * tileSize }, this.player, this) const e = level.entities[index];
]; var entity;
switch (e.type) {
for (let index = 0; index < this.mages.length; index++) { case "Mage":
this.children.push(this.mages[index]); 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) { update(dt) {
super.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;
}
}
}
} }
} }