From b2a4d2e5475cd2646e61f0bd110824111af02e25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20K=C3=B6hring?= Date: Wed, 15 Feb 2023 16:16:17 +0100 Subject: [PATCH] dig blocks, finally --- src/App.vue | 43 +++++++++++++++++++++----------- src/level/def.ts | 22 +++++++++++------ src/level/index.ts | 59 +++++++++++++++++++++++++++++++++++++------- src/util/useInput.ts | 8 ------ 4 files changed, 92 insertions(+), 40 deletions(-) diff --git a/src/App.vue b/src/App.vue index 3b5fe3d..2dd47d8 100644 --- a/src/App.vue +++ b/src/App.vue @@ -3,7 +3,7 @@ import { ref, computed, onMounted } from 'vue' import Help from './screens/help.vue' import Inventory from './screens/inventory.vue' -import { BLOCK_SIZE, STAGE_WIDTH, STAGE_HEIGHT, type Block } from './level/def' +import { BLOCK_SIZE, STAGE_WIDTH, STAGE_HEIGHT, type Block, blockTypes } from './level/def' import createLevel from './level' import useTime from './util/useTime' @@ -12,7 +12,7 @@ import usePlayer from './util/usePlayer' const { updateTime, timeOfDay, clock } = useTime() const { player, direction, dx, dy } = usePlayer() -const { inputX, inputY, running, digging, paused, help, inventory } = useInput() +const { inputX, inputY, running, paused, help, inventory } = useInput() const level = createLevel(STAGE_WIDTH + 2, STAGE_HEIGHT + 2) player.inventory.push( @@ -67,8 +67,24 @@ const blocked = computed(() => { } }) -function dig() { - console.warn('digging not yet implemented') +function dig(blockX: number, blockY: number, oldBlockType: BlockType) { + // ยง 4 ArbZG + if (paused.value) return + + // TODO: temporary filter + if (oldBlockType === 'air' || oldBlockType === 'cave') return + + level.change({ + type: 'exchange', + x: floorX.value + blockX, + y: floorY.value + blockY, + newType: 'air' + }) + // This feels like cheating, but it makes Vue recalculate floorX + // which then recalculates the blocks, so that the changes are + // applied. Otherwise, they wouldn't be visible before moving + x.value = x.value + 0.01 + x.value = x.value - 0.01 } let lastTimeUpdate = 0 @@ -107,13 +123,9 @@ const move = (thisTick: number): void => { if (dy_ > 0 && blocked.value.down) dy_ = 0 else if (dy_ < 0 && blocked.value.up) dy_ = 0 - if (!inputY.value && digging.value) { - dx_ = 0 - dig() - } - const optimal = 16 // 16ms per tick => 60 FPS const movementMultiplier = (tickDelta / optimal) * 2 + const fallMultiplier = movementMultiplier * 2 // TODO: accelerated fall? if (arriving.value && dy_ === 0) { arriving.value = false @@ -122,7 +134,12 @@ const move = (thisTick: number): void => { walking.value = !!dx_ x.value += dx_ * movementMultiplier - y.value += dy_ * movementMultiplier + + if (dy_ < 0 || arriving.value) { + y.value += dy_ * movementMultiplier + } else { + y.value += dy_ * fallMultiplier + } lastTick = thisTick } @@ -140,10 +157,6 @@ onMounted(() => { lastTick = performance.now() move(lastTick) }) - -function log(...args: any[]) { - console.log(...args) -}