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": {
"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",

View File

@ -10,7 +10,7 @@
"author": "McArn",
"license": "ISC",
"dependencies": {
"asdf-games": "^1.0.8"
"asdf-games": "^1.0.11"
},
"devDependencies": {
"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) {
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

View File

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

View File

@ -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
});
/* *********************************************************

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