From 7098bffd2550cfbabdd0e8c18b465c517d4e4736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20K=C3=B6hring?= Date: Sun, 16 Mar 2025 18:01:08 +0100 Subject: [PATCH] unique block ids, better running, remove deprecated tree parts --- src/App.vue | 14 ++++++---- src/assets/field.css | 4 +++ src/level/blockExt.ts | 6 ++--- src/level/def.ts | 25 ++++++++--------- src/level/items.ts | 62 +++++++++++++++++++------------------------ src/types.d.ts | 18 +++++-------- 6 files changed, 61 insertions(+), 68 deletions(-) diff --git a/src/App.vue b/src/App.vue index ca02ea5..788cd73 100644 --- a/src/App.vue +++ b/src/App.vue @@ -21,9 +21,9 @@ const level = createLevel(STAGE_WIDTH + 2, STAGE_HEIGHT + 2) const lightMapEl = ref(undefined) let updateLightMap: ReturnType -pocket({ name: 'Wooden Shovel', type: 'tool', icon: 'shovel', quality: 'wood' }) -pocket({ name: 'Wooden Sword', type: 'tool', icon: 'sword', quality: 'wood' }) -pocket({ name: 'Wooden Pick Axe', type: 'tool', icon: 'pick', quality: 'wood' }) +pocket(getItem('tool_shovel_wood')) +pocket(getItem('tool_sword_wood')) +pocket(getItem('tool_pickaxe_wood')) let animationFrame = 0 let lastTick = 0 @@ -126,7 +126,7 @@ function build(blockX: number, blockY: number, block: InventoryItem) { } function interactWith(blockX: number, blockY: number, block: Block) { - console.log('interact with', blockX, blockY, block.type) + if (debug) console.debug('interact with', blockX, blockY, block.type) // ยง 4 ArbZG if (paused.value) return @@ -235,7 +235,11 @@ onMounted(() => {
diff --git a/src/assets/field.css b/src/assets/field.css index 8d113c0..ea85618 100644 --- a/src/assets/field.css +++ b/src/assets/field.css @@ -99,6 +99,10 @@ background-image: url(/Tiles/brick_grey.png); } +#field { + user-select: none; +} + #field .block:hover, #field .block.block.highlight { filter: brightness(1.2) saturate(1.2); diff --git a/src/level/blockExt.ts b/src/level/blockExt.ts index 7117e30..268cc22 100644 --- a/src/level/blockExt.ts +++ b/src/level/blockExt.ts @@ -2,8 +2,6 @@ import type { NoiseFunction2D } from 'simplex-noise' import { blockTypes as T, level as L, probability as P } from './def' import type { Block } from '../types.d' -const TREE_ROOT = [T.treeRootLeft, T.treeRootMiddle, T.treeRootRight] - function trees(r: number, i: number, row: Block[], previousRow: Block[]) { const max = row.length - 1 const h = i - 1 @@ -45,7 +43,8 @@ function ground(r: number, i: number, current: Block, above: Block) { return current } -function ground_(r: number, i: number, row: Block[], previousRow: Block[]) { +/* +function ground(r: number, i: number, row: Block[], previousRow: Block[]) { const rootParts = [T.treeRootLeft, T.treeRootMiddle, T.treeRootRight] const prevBlock = previousRow[i] @@ -56,6 +55,7 @@ function ground_(r: number, i: number, row: Block[], previousRow: Block[]) { if (row[i] === T.soil) row[i] = T.grass } } +*/ function rock(r: number, i: number, current: Block, above: Block) { if (above === T.soil && r < P.fray) return T.soil diff --git a/src/level/def.ts b/src/level/def.ts index d09b3c3..b420a7c 100644 --- a/src/level/def.ts +++ b/src/level/def.ts @@ -14,22 +14,19 @@ export const blockTypes: Record = { air: { type: 'air', hp: Infinity, walkable: true, transparent: true }, cave: { type: 'cave', hp: Infinity, walkable: true, transparent: true }, // Tree Parts - treeCrown: { type: 'treeCrown', hp: 1, walkable: true, transparent: true, drops: 'leaves' }, - treeLeaves: { type: 'treeLeaves', hp: 1, walkable: true, transparent: true, drops: 'leaves' }, - treeTrunk: { type: 'treeTrunk', hp: 10, walkable: true, climbable: true, transparent: true, drops: 'wood' }, - treeRoot: { type: 'treeRoot', hp: 10, walkable: true, climbable: true, drops: 'wood' }, - treeRootLeft: { type: 'treeRootLeft', hp: 10, walkable: true, climbable: true, drops: 'wood' }, - treeRootMiddle: { type: 'treeRootMiddle', hp: 10, walkable: true, climbable: true, drops: 'wood' }, - treeRootRight: { type: 'treeRootRight', hp: 10, walkable: true, climbable: true, drops: 'wood' }, + treeCrown: { type: 'treeCrown', hp: 1, walkable: true, transparent: true, drops: 'block_leaves' }, + treeLeaves: { type: 'treeLeaves', hp: 1, walkable: true, transparent: true, drops: 'block_leaves' }, + treeTrunk: { type: 'treeTrunk', hp: 10, walkable: true, climbable: true, transparent: true, drops: 'block_wood' }, + treeRoot: { type: 'treeRoot', hp: 10, walkable: true, climbable: true, drops: 'block_wood' }, // Opaque Natural Blocks - grass: { type: 'grass', hp: 5, walkable: false, drops: 'dirt' }, - soil: { type: 'soil', hp: 5, walkable: false, drops: 'dirt' }, - soilGravel: { type: 'soilGravel', hp: 5, walkable: false, drops: 'gravel' }, - stoneGravel: { type: 'stoneGravel', hp: 10, walkable: false, drops: 'gravel' }, - stone: { type: 'stone', hp: 10, walkable: false, drops: 'stone' }, - bedrock: { type: 'bedrock', hp: 25, walkable: false, drops: 'stone' }, + grass: { type: 'grass', hp: 5, walkable: false, drops: 'block_dirt' }, + soil: { type: 'soil', hp: 5, walkable: false, drops: 'block_dirt' }, + soilGravel: { type: 'soilGravel', hp: 5, walkable: false, drops: 'block_gravel' }, + stoneGravel: { type: 'stoneGravel', hp: 10, walkable: false, drops: 'block_gravel' }, + stone: { type: 'stone', hp: 10, walkable: false, drops: 'block_stone' }, + bedrock: { type: 'bedrock', hp: 25, walkable: false, drops: 'block_stone' }, // Built Blocks - brickWall: { type: 'brickWall', hp: 25, walkable: false, drops: 'stone' }, + brickWall: { type: 'brickWall', hp: 25, walkable: false, drops: 'block_gravel' }, } export const level = { diff --git a/src/level/items.ts b/src/level/items.ts index aa7645d..12cafdd 100644 --- a/src/level/items.ts +++ b/src/level/items.ts @@ -1,51 +1,43 @@ import type { ItemQuality, Item, InventoryItem } from '../types.d' -export const items: Item[] = [ - { name: 'Wooden Shovel', type: 'tool', icon: 'shovel', quality: 'wood' }, - { name: 'Wooden Pick Axe', type: 'tool', icon: 'pick', quality: 'wood' }, - { name: 'Wooden Sword', type: 'tool', icon: 'sword', quality: 'wood' }, +export const items = { + tool_shovel_wood: { id: 'tool_shovel_wood', name: 'Wooden Shovel', type: 'tool', icon: 'shovel', quality: 'wood' } as Item, + tool_pickaxe_wood: { id: 'tool_pickaxe_wood', name: 'Wooden Pick Axe', type: 'tool', icon: 'pick', quality: 'wood' } as Item, + tool_sword_wood: { id: 'tool_sword_wood', name: 'Wooden Sword', type: 'tool', icon: 'sword', quality: 'wood' } as Item, - { name: 'Iron Shovel', type: 'tool', icon: 'shovel', quality: 'iron' }, - { name: 'Iron Pick Axe', type: 'tool', icon: 'pick', quality: 'iron' }, - { name: 'Iron Sword', type: 'tool', icon: 'sword', quality: 'iron' }, + tool_shovel_iron: { id: 'tool_shovel_iron', name: 'Iron Shovel', type: 'tool', icon: 'shovel', quality: 'iron' } as Item, + tool_pickaxe_iron: { id: 'tool_pickaxe_iron', name: 'Iron Pick Axe', type: 'tool', icon: 'pick', quality: 'iron' } as Item, + tool_sword_iron: { id: 'tool_sword_iron', name: 'Iron Sword', type: 'tool', icon: 'sword', quality: 'iron' } as Item, - { name: 'Diamond Shovel', type: 'tool', icon: 'shovel', quality: 'diamond' }, - { name: 'Diamond Pick Axe', type: 'tool', icon: 'pick', quality: 'diamond' }, - { name: 'Diamond Sword', type: 'tool', icon: 'sword', quality: 'diamond' }, + tool_shovel_diamond: { id: 'tool_shovel_diamond', name: 'Diamond Shovel', type: 'tool', icon: 'shovel', quality: 'diamond' } as Item, + tool_pickaxe_diamond: { id: 'tool_pickaxe_diamond', name: 'Diamond Pick Axe', type: 'tool', icon: 'pick', quality: 'diamond' } as Item, + tool_sword_diamond: { id: 'tool_sword_diamond', name: 'Diamond Sword', type: 'tool', icon: 'sword', quality: 'diamond' } as Item, - { name: 'Wooden Shovel', type: 'tool', icon: 'shovel', quality: 'wood' }, - { name: 'Wooden Pick Axe', type: 'tool', icon: 'pick', quality: 'wood' }, - { name: 'Wooden Sword', type: 'tool', icon: 'sword', quality: 'wood' }, + block_leaves: { id: 'block_leaves', name: 'leaves', type: 'block', icon: 'leaves', builds: 'treeLeaves' } as Item, + block_dirt: { id: 'block_dirt', name: 'dirt', type: 'block', icon: 'dirt', builds: 'soil' } as Item, + block_wood: { id: 'block_wood', name: 'wood', type: 'block', icon: 'wood', builds: 'treeTrunk' } as Item, + block_stone: { id: 'block_stone', name: 'stone', type: 'block', icon: 'stone', builds: 'brickWall' } as Item, + block_gravel: { id: 'block_gravel', name: 'gravel', type: 'block', icon: 'stone' /*, builds??? TODO */ } as Item, - { name: 'leaves', type: 'block', icon: 'leaves', builds: 'treeLeaves' }, - { name: 'dirt', type: 'block', icon: 'dirt', builds: 'soil' }, - { name: 'wood', type: 'block', icon: 'wood', builds: 'treeTrunk' }, - { name: 'stone', type: 'block', icon: 'stone', builds: 'brickWall' }, - { name: 'gravel', type: 'block', icon: 'stone' }, // TODO + ore_coal: { id: 'ore_coal', name: 'coal', type: 'ore', icon: 'ore_coal' } as Item, + ore_iron: { id: 'ore_iron', name: 'iron', type: 'ore', icon: 'ore_iron' } as Item, + ore_silver: { id: 'ore_silver', name: 'silver', type: 'ore', icon: 'ore_silver' } as Item, + ore_gold: { id: 'ore_gold', name: 'gold', type: 'ore', icon: 'ore_gold' } as Item, + ore_ruby: { id: 'ore_ruby', name: 'ruby', type: 'ore', icon: 'ore_ruby' } as Item, + ore_diamond: { id: 'ore_diamond', name: 'diamond', type: 'ore', icon: 'ore_diamond' } as Item, + ore_emerald: { id: 'ore_emerald', name: 'emerald', type: 'ore', icon: 'ore_emerald' } as Item, +} as const - { name: 'coal', type: 'ore', icon: 'ore_coal' }, - { name: 'iron', type: 'ore', icon: 'ore_iron' }, - { name: 'silver', type: 'ore', icon: 'ore_silver' }, - { name: 'gold', type: 'ore', icon: 'ore_gold' }, - { name: 'ruby', type: 'ore', icon: 'ore_ruby' }, - { name: 'diamond', type: 'ore', icon: 'ore_diamond' }, - { name: 'emerald', type: 'ore', icon: 'ore_emerald' }, -] +export type ItemId = keyof typeof items export const damage: Record = { wood: 1, iron: 3, diamond: 5, -} +} as const -export function getItem(name: string, quality?: ItemQuality) { - const item = items.find(i => i.name === name) - if (item) { - return { - ...item, - quality, - } - } +export function getItem(id: ItemId) { + return items[id] } export function getItemClass(item: InventoryItem) { diff --git a/src/types.d.ts b/src/types.d.ts index e77acd2..9bb3738 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,15 +1,10 @@ +import type { ItemId } from './level/items' export type ItemQuality = 'wood' | 'iron' | 'diamond' export type ItemType = 'tool' | 'block' | 'ore' -export type DropItem = - | 'Wooden Shovel' | 'Iron Shovel' | 'Diamond Shovel' - | 'Wooden Pick Axe' | 'Iron Pick Axe' | 'Diamond Pick Axe' - | 'Wooden Sword' | 'Iron Sword' | 'Diamond Sword' - | 'leaves' | 'dirt' | 'wood' | 'stone' | 'gravel' - | 'coal' | 'iron' | 'silver' | 'gold' | 'ruby' | 'diamond' | 'emerald' - export interface Item { - name: DropItem + id: string + name: string type: ItemType icon: string quality?: ItemQuality @@ -28,8 +23,9 @@ export interface BlockItem extends Item { export type BlockType = | 'air' | 'grass' - | 'treeCrown' | 'treeLeaves' | 'treeTrunk' | 'treeRoot' | 'treeRootRight' | 'treeRootMiddle' | 'treeRootLeft' - | 'soil' | 'soilGravel' | 'stone' | 'stoneGravel' + | 'treeCrown' | 'treeLeaves' | 'treeTrunk' | 'treeRoot' + | 'soil' | 'soilGravel' + | 'stone' | 'stoneGravel' | 'bedrock' | 'cave' | 'brickWall' @@ -40,7 +36,7 @@ export type Block = { climbable?: boolean, // can I climb it? transparent?: boolean, // can I see through it? illuminated?: boolean, // is it glowing? - drops?: DropItem, // what do I get, when loot it? + drops?: ItemId, // what do I get, when loot it? } // describes a changed block, eg digged or placed by the player