TomatitoToho commited on
Commit
9d75fd2
·
verified ·
1 Parent(s): 72553b6

Upload src/stores/worldStore.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/stores/worldStore.ts +87 -0
src/stores/worldStore.ts ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client'
2
+
3
+ import { create } from 'zustand'
4
+ import type { ChunkData, World } from '@/types'
5
+ import { CHUNK_WIDTH, CHUNK_HEIGHT, CHUNK_DEPTH, BLOCK_AIR } from '@/engine/constants'
6
+
7
+ interface WorldStore {
8
+ world: World | null
9
+ chunks: Map<string, ChunkData>
10
+ initWorld: (seed: number) => void
11
+ setChunk: (key: string, chunk: ChunkData) => void
12
+ removeChunk: (key: string) => void
13
+ getBlock: (x: number, y: number, z: number) => number
14
+ setBlock: (x: number, y: number, z: number, blockId: number) => void
15
+ updateWorldTime: () => void
16
+ }
17
+
18
+ function chunkKey(cx: number, cz: number): string {
19
+ return `${cx},${cz}`
20
+ }
21
+
22
+ export const useWorldStore = create<WorldStore>((set, get) => ({
23
+ world: null,
24
+ chunks: new Map(),
25
+
26
+ initWorld: (seed: number) => {
27
+ const world: World = {
28
+ seed,
29
+ time: 6000, // noon
30
+ gameTime: 0,
31
+ spawnPoint: [8, 80, 8],
32
+ weather: { raining: false, thundering: false, rainTime: 0, thunderTime: 0 },
33
+ dimension: 'overworld',
34
+ }
35
+ set({ world, chunks: new Map() })
36
+ },
37
+
38
+ setChunk: (key, chunk) => {
39
+ const chunks = new Map(get().chunks)
40
+ chunks.set(key, chunk)
41
+ set({ chunks })
42
+ },
43
+
44
+ removeChunk: (key) => {
45
+ const chunks = new Map(get().chunks)
46
+ chunks.delete(key)
47
+ set({ chunks })
48
+ },
49
+
50
+ getBlock: (x, y, z) => {
51
+ if (y < -64 || y > 319) return BLOCK_AIR
52
+ const cx = Math.floor(x / CHUNK_WIDTH)
53
+ const cz = Math.floor(z / CHUNK_DEPTH)
54
+ const chunk = get().chunks.get(chunkKey(cx, cz))
55
+ if (!chunk) return BLOCK_AIR
56
+ const lx = ((x % CHUNK_WIDTH) + CHUNK_WIDTH) % CHUNK_WIDTH
57
+ const lz = ((z % CHUNK_DEPTH) + CHUNK_DEPTH) % CHUNK_DEPTH
58
+ const ly = y + 64 // offset for negative Y
59
+ const idx = lx + lz * CHUNK_WIDTH + ly * CHUNK_WIDTH * CHUNK_DEPTH
60
+ return chunk.blocks[idx] || BLOCK_AIR
61
+ },
62
+
63
+ setBlock: (x, y, z, blockId) => {
64
+ if (y < -64 || y > 319) return
65
+ const cx = Math.floor(x / CHUNK_WIDTH)
66
+ const cz = Math.floor(z / CHUNK_DEPTH)
67
+ const key = chunkKey(cx, cz)
68
+ const chunk = get().chunks.get(key)
69
+ if (!chunk) return
70
+ const lx = ((x % CHUNK_WIDTH) + CHUNK_WIDTH) % CHUNK_WIDTH
71
+ const lz = ((z % CHUNK_DEPTH) + CHUNK_DEPTH) % CHUNK_DEPTH
72
+ const ly = y + 64
73
+ const idx = lx + lz * CHUNK_WIDTH + ly * CHUNK_WIDTH * CHUNK_DEPTH
74
+ chunk.blocks[idx] = blockId
75
+ chunk.dirty = true
76
+ chunk.meshVersion++
77
+ set({ chunks: new Map(get().chunks) })
78
+ },
79
+
80
+ updateWorldTime: () => {
81
+ const { world } = get()
82
+ if (!world) return
83
+ world.time = (world.time + 1) % 24000
84
+ world.gameTime++
85
+ set({ world })
86
+ },
87
+ }))