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:
@@ -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}`);
|
||||
|
||||
Reference in New Issue
Block a user