[code]: Added stats bar.

This commit is contained in:
corner 2020-04-11 17:47:15 +02:00
parent edba1ebfc6
commit 0b07ff3f2a
2 changed files with 64 additions and 0 deletions

60
src/entities/stats.js Normal file
View File

@ -0,0 +1,60 @@
const { Container, TileSprite, Rect, Texture, Text } = require("asdf-games");
const tilemapFrames = require("../../res/tilemap.js");
const tilemap = new Texture("res/tilemap.png");
const tileSize = 32;
const fillStyle = {fill: "#ffffff", font: "24px Arial"};
class Stats extends Container {
constructor(player) {
super();
this.player = player;
const background = new Rect(640, 42, {fill: "rgba(0,0,0, 0.5)"});
background.pos = {x: 0, y: 280};
const livesText = new Text("LIVES:", fillStyle);
livesText.pos = {x: 10, y: 310};
const livesCounter = new Text(player.lives, fillStyle);
livesCounter.pos = {x: 90, y: 310};
const heart = new TileSprite(tilemap, tileSize, tileSize);
heart.frame = {x: tilemapFrames[42].x / tileSize, y: tilemapFrames[42].y / tileSize};
heart.pos = {x: 104, y: 284};
const staminaText = new Text("| STAMINA:", fillStyle);
staminaText.pos = {x: 140, y: 310};
this.children = [
background,
livesText,
livesCounter,
heart,
staminaText
// Stamina is added afterward
];
Array(5).fill().forEach((_val, index) => {
const sprite = new TileSprite(tilemap, tileSize, tileSize);
sprite.frame = {x: tilemapFrames[44].x / tileSize, y: tilemapFrames[44].y / tileSize};
sprite.pos = {x: index * 26 + 270, y: 286};
this.children.push(sprite);
});
}
update() {
this.children[2].text = this.player.lives;
Array(5).fill().forEach((_val, index) => {
//console.log(Math.round(this.player.stamina.current) - 1, index);
this.children[index + 5].frame =
(Math.round(this.player.stamina.current)) <= index
? {x: tilemapFrames[45].x / tileSize, y: tilemapFrames[45].y / tileSize}
: {x: tilemapFrames[44].x / tileSize, y: tilemapFrames[44].y / tileSize};
});
}
}
module.exports = Stats;

View File

@ -27,9 +27,13 @@ player.level = level;
const camera = new Camera(player, window, { w: level.w * 2, h: level.h * 2 });
const Stats = require("./src/entities/stats.js");
const stats = new Stats(player);
scene.add(camera);
camera.add(level);
camera.add(player);
scene.add(stats);
function switchLevel(module, pos = 0) {
camera.map(function(e) {