feat: server tracks and broadcasts game time

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-03-08 03:04:08 +00:00
parent b295ef1af7
commit f5857f72d8
3 changed files with 13 additions and 4 deletions
+9 -1
View File
@@ -1,4 +1,4 @@
import { TICK_RATE, BROADCAST_EVERY_N_TICKS } from '@dflike/shared';
import { TICK_RATE, BROADCAST_EVERY_N_TICKS, ENERGY_DECAY_PER_TICK, DAY_NIGHT_RATIO } from '@dflike/shared';
import { World } from '../ecs/World.js';
import { GameMap } from '../map/GameMap.js';
import { generateMap } from '../map/mapGenerator.js';
@@ -32,6 +32,7 @@ export class GameLoop {
// Load terrain and decorations
this.map.terrain = generated.terrain;
this.map.decorations = generated.decorations;
this.map.trunkDecorations = generated.trunkDecorations;
this.map.loadObstacles(generated.obstacles);
// Points of interest from generator
@@ -86,4 +87,11 @@ export class GameLoop {
getTick(): number {
return this.tick;
}
getGameTime(): number {
const dayTicks = 100 / ENERGY_DECAY_PER_TICK;
const nightTicks = dayTicks / DAY_NIGHT_RATIO;
const cycleTicks = dayTicks + nightTicks;
return (this.tick % cycleTicks) / cycleTicks;
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ export class SocketServer {
private setupBroadcast(): void {
this.gameLoop.setBroadcastHandler(() => {
const update = serializeStateUpdate(this.gameLoop.world, this.gameLoop.getTick());
const update = serializeStateUpdate(this.gameLoop.world, this.gameLoop.getTick(), this.gameLoop.getGameTime());
this.io.emit('state-update', update);
});
}
+3 -2
View File
@@ -80,10 +80,11 @@ export function serializeWorldState(world: World, map: GameMap): WorldState {
pointsOfInterest: map.getPointsOfInterest(),
terrain: map.terrain,
decorations: map.decorations,
trunkDecorations: map.trunkDecorations,
};
}
export function serializeStateUpdate(world: World, tick: number): StateUpdate {
export function serializeStateUpdate(world: World, tick: number, gameTime: number): StateUpdate {
const entities = world.query('position').map(id => serializeEntity(world, id));
return { entities, tick };
return { entities, tick, gameTime };
}