From 0b07ff3f2a3efbaf0cf59fcec728a9db2253c920 Mon Sep 17 00:00:00 2001 From: stickyPiston Date: Sat, 11 Apr 2020 17:47:15 +0200 Subject: [PATCH] [code]: Added stats bar. --- src/entities/stats.js | 60 +++++++++++++++++++++++++++++++++++++++++++ src/game.js | 4 +++ 2 files changed, 64 insertions(+) create mode 100644 src/entities/stats.js diff --git a/src/entities/stats.js b/src/entities/stats.js new file mode 100644 index 0000000..25f1429 --- /dev/null +++ b/src/entities/stats.js @@ -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; \ No newline at end of file diff --git a/src/game.js b/src/game.js index eaa4636..8d92792 100644 --- a/src/game.js +++ b/src/game.js @@ -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) {