feat: handle spawn-npc socket event with cap and position logic

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
root
2026-03-07 13:37:36 +00:00
parent 1576e6b717
commit 7e5f430204
+12 -1
View File
@@ -1,9 +1,10 @@
import { Server } from 'socket.io';
import type http from 'http';
import type { ClientEvents, ServerEvents, PlayerInput, Position, Movement, PlayerControlled, Appearance } from '@dflike/shared';
import { Direction } from '@dflike/shared';
import { Direction, MAX_NPC_COUNT } from '@dflike/shared';
import { generateRandomAppearance } from '../spawner/appearanceGenerator.js';
import type { GameLoop } from '../game/GameLoop.js';
import { spawnNPC } from '../game/spawner.js';
import { serializeWorldState, serializeStateUpdate } from './stateSerializer.js';
export class SocketServer {
@@ -58,6 +59,16 @@ export class SocketServer {
this.handleInput(playerId, entity, input);
});
// Handle NPC spawn requests
socket.on('spawn-npc', (data: { x: number; y: number }) => {
const npcCount = world.query('npcBrain').length;
if (npcCount >= MAX_NPC_COUNT) return;
const target = map.findNearestWalkable(data.x, data.y, 3)
?? map.getRandomWalkable();
spawnNPC(world, map, target);
});
// Handle disconnect
socket.on('disconnect', () => {
console.log(`Player disconnected: ${playerId}`);