From 97d97508e759c698c93ce9bcc188de7a12a63458 Mon Sep 17 00:00:00 2001 From: Nulo Date: Wed, 30 Jun 2021 21:18:13 +0000 Subject: [PATCH] Agregar tiempo --- src/jugando.ts | 34 ++++++++++++++++++++++++---------- src/utils.ts | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/jugando.ts b/src/jugando.ts index 5e4809b..c11e85b 100644 --- a/src/jugando.ts +++ b/src/jugando.ts @@ -1,5 +1,5 @@ import { Juego } from './main' -import { boxCollision } from './utils' +import { boxCollision, drawText } from './utils' const ENEMIES_NUM = 20 const SEED_COOLDOWN = 300 @@ -7,6 +7,7 @@ const MAP_MIN = 1000 const MAP_MAX = 5000 const MAP_SIZE = MAP_MAX - MAP_MIN const TREES_TO_WIN = 70 +const TIME = 2 * 60 * 1000 export type State = { current: "jugando" @@ -16,6 +17,7 @@ export type State = { enemies: { x: number }[] seeds: { x: number; velocity: { x: number } }[] trees: { x: number }[] + time: number, seedCooldown: number, } @@ -28,11 +30,18 @@ export function createJugandoState(): State { enemies: [], seeds: [], trees: [], + time: TIME, seedCooldown: 0, } } export function update(juego: Juego, dt: number) { + juego.state.time -= dt + if (juego.state.time < 0) { + // TODO: convertir en baldoza + return + } + const playerSpeed = juego.canvas.width * 0.15 const enemySpeed = juego.canvas.width * 0.05 if (juego.keyboard.keys.d || juego.keyboard.keys.ArrowRight) { @@ -179,15 +188,20 @@ export function draw(juego: Juego, timestamp: number) { drawJugadorx(juego) drawSeeds(juego) - juego.ctx.font = `bold 1rem sans-serif` - juego.ctx.fillText('Usá las flechitas para moverte, y espacio para "disparar" semillas.', 100, 100) + drawText(juego, 'Usá las flechitas para moverte, y espacio para "disparar" semillas.', { x: 0, y: 100 }, {}) - const fontSize = '3rem' - juego.ctx.font = `bold ${fontSize} sans-serif` - const treesText = `Arboles: ${juego.state.trees.length}/${TREES_TO_WIN}` - const treesTextMeasure = juego.ctx.measureText(treesText) + const arbolesBox = drawText( + juego, `Arboles: ${juego.state.trees.length}/${TREES_TO_WIN}`, + { x: juego.canvas.width - 10, y: 10 }, + { bold: true, align: 'right'}) - juego.ctx.fillText(treesText, - juego.canvas.width - treesTextMeasure.width - 10, - 10 + treesTextMeasure.actualBoundingBoxAscent) + const tiempoBox = drawText( + juego, `Tiempo restante`, + { x: 10, y: 10 }, + { bold: false, align: 'left'}) + + juego.ctx.fillRect( + 10, tiempoBox.y + tiempoBox.height, + (juego.canvas.width - arbolesBox.width - 20) * (juego.state.time / TIME), + 30) } diff --git a/src/utils.ts b/src/utils.ts index 9dc7ab9..930a3da 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,9 +1,12 @@ +import { Juego } from "./main"; + export type Pos = { x: number, y: number, }; export type Box = Pos & { width: number, height: number, } + export function posInBox(box: Box, pos: Pos) { return pos.x > box.x && pos.x < box.x + box.width && pos.y > box.y && pos.y < box.y + box.height @@ -18,3 +21,23 @@ export function boxCollision(box1: Box, box2: Box) { ) } +export function drawText(juego: Juego, text: string, pos: Pos, { + bold = false, + size = 2, + align = "left", + baseline = "top", +}: { + align?: CanvasTextAlign, + baseline?: CanvasTextBaseline, + bold?: boolean, + // in rem + size?: number, +}): Box { + juego.ctx.font = `${bold ? 'bold ' : ''}${size}rem sans-serif` + juego.ctx.textAlign = align + juego.ctx.textBaseline = baseline + juego.ctx.fillText(text, pos.x, pos.y) + + const measure = juego.ctx.measureText(text) + return { ...pos, width: measure.width, height: measure.actualBoundingBoxDescent } +}