TomatitoToho commited on
Commit
caa5fe0
·
verified ·
1 Parent(s): 51d2e26

Upload src/engine/gameLoop.ts with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/engine/gameLoop.ts +171 -0
src/engine/gameLoop.ts ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useGameStore } from '@/stores/gameStore'
2
+ import { useWorldStore } from '@/stores/worldStore'
3
+ import { usePlayerStore } from '@/stores/playerStore'
4
+ import { TICK_DURATION_MS, MAX_TICKS } from '@/engine/constants'
5
+
6
+ let lastTime = 0
7
+ let accumulatedTime = 0
8
+ let frameTime = 0
9
+ let tickTime = 0
10
+ let tickCount = 0
11
+ let frameCount = 0
12
+ let lastSecondTime = 0
13
+
14
+ /**
15
+ * Main game loop that handles fixed timestep updates and rendering
16
+ * @param timestamp Current timestamp from requestAnimationFrame
17
+ */
18
+ function gameLoop(timestamp: number) {
19
+ // Initialize time on first frame
20
+ if (!lastTime) {
21
+ lastTime = timestamp
22
+ }
23
+
24
+ // Calculate time differences
25
+ const delta = timestamp - lastTime
26
+ lastTime = timestamp
27
+ accumulatedTime += delta
28
+
29
+ // Update frame counter for FPS calculation
30
+ frameCount++
31
+ frameTime += delta
32
+
33
+ // Process game ticks at fixed intervals
34
+ while (accumulatedTime >= TICK_DURATION_MS) {
35
+ // Update game state
36
+ updateGame()
37
+
38
+ // Update tick counter for TPS calculation
39
+ tickCount++
40
+ tickTime += TICK_DURATION_MS
41
+ accumulatedTime -= TICK_DURATION_MS
42
+ }
43
+
44
+ // Calculate and update performance metrics
45
+ updatePerformanceMetrics(timestamp)
46
+
47
+ // Render the game
48
+ renderGame()
49
+
50
+ // Schedule next frame
51
+ requestAnimationFrame(gameLoop)
52
+ }
53
+
54
+ /**
55
+ * Update game state (fixed timestep)
56
+ */
57
+ function updateGame() {
58
+ const gameStore = useGameStore.getState()
59
+ const worldStore = useWorldStore.getState()
60
+ const playerStore = usePlayerStore.getState()
61
+
62
+ // Skip updates if game is paused
63
+ if (gameStore.paused) return
64
+
65
+ // Update game tick counter
66
+ gameStore.actions.incrementTick()
67
+
68
+ // Update world time
69
+ worldStore.actions.updateWorldTime()
70
+
71
+ // Update player position and physics
72
+ updatePlayerPhysics()
73
+
74
+ // Update entities
75
+ updateEntities()
76
+
77
+ // Update scheduled block ticks
78
+ updateScheduledTicks()
79
+
80
+ // Update random block ticks
81
+ updateRandomTicks()
82
+ }
83
+
84
+ /**
85
+ * Update player physics and movement
86
+ */
87
+ function updatePlayerPhysics() {
88
+ const playerStore = usePlayerStore.getState()
89
+ const player = playerStore.player
90
+
91
+ if (!player) return
92
+
93
+ // Apply gravity
94
+ player.velocity[1] += -32 * (TICK_DURATION_MS / 1000)
95
+
96
+ // Apply movement
97
+ // This is a simplified version - actual implementation would handle input and collision detection
98
+ player.position[0] += player.velocity[0] * (TICK_DURATION_MS / 1000)
99
+ player.position[1] += player.velocity[1] * (TICK_DURATION_MS / 1000)
100
+ player.position[2] += player.velocity[2] * (TICK_DURATION_MS / 1000)
101
+
102
+ // Update player state
103
+ playerStore.actions.updatePosition(player.position)
104
+ }
105
+
106
+ /**
107
+ * Update all entities in the world
108
+ */
109
+ function updateEntities() {
110
+ // Implementation would iterate through all entities and update their state
111
+ // including AI, physics, and animations
112
+ }
113
+
114
+ /**
115
+ * Process scheduled block ticks
116
+ */
117
+ function updateScheduledTicks() {
118
+ // Implementation would handle scheduled block updates like redstone ticks
119
+ }
120
+
121
+ /**
122
+ * Process random block ticks (like plant growth)
123
+ */
124
+ function updateRandomTicks() {
125
+ // Implementation would handle random block updates based on world's randomTickSpeed
126
+ }
127
+
128
+ /**
129
+ * Render the game scene
130
+ */
131
+ function renderGame() {
132
+ // Implementation would handle rendering with Three.js
133
+ // including camera updates, chunk rendering, and UI
134
+ }
135
+
136
+ /**
137
+ * Update performance metrics (FPS, TPS)
138
+ * @param timestamp Current timestamp
139
+ */
140
+ function updatePerformanceMetrics(timestamp: number) {
141
+ // Calculate FPS and TPS
142
+ if (timestamp - lastSecondTime >= 1000) {
143
+ const fps = frameCount / (frameTime / 1000)
144
+ const tps = tickCount / (tickTime / 1000)
145
+
146
+ useGameStore.getState().actions.updatePerformance({
147
+ averageFPS: fps,
148
+ averageTPS: tps,
149
+ })
150
+
151
+ // Update debug UI
152
+ useUIStore.getState().actions.updateDebugInfo({
153
+ fps: Math.round(fps),
154
+ tps: Math.round(tps),
155
+ })
156
+
157
+ // Reset counters
158
+ frameCount = 0
159
+ frameTime = 0
160
+ tickCount = 0
161
+ tickTime = 0
162
+ lastSecondTime = timestamp
163
+ }
164
+ }
165
+
166
+ /**
167
+ * Start the game loop
168
+ */
169
+ export function startGameLoop() {
170
+ requestAnimationFrame(gameLoop)
171
+ }