feat: Enhance turtle model and visualization with block surroundings and world block management
This commit is contained in:
@@ -13,6 +13,38 @@ app.use(express.json());
|
||||
// Store connected web clients and turtle data
|
||||
const webClients = new Set();
|
||||
const turtleData = new Map(); // turtleID -> turtle state
|
||||
const worldBlocks = new Map(); // "x,y,z" -> {name, metadata, discoveredBy, timestamp}
|
||||
|
||||
// Helper to store discovered blocks
|
||||
function storeBlock(x, y, z, blockData, turtleID) {
|
||||
const key = `${x},${y},${z}`;
|
||||
worldBlocks.set(key, {
|
||||
...blockData,
|
||||
discoveredBy: turtleID,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
// Helper to calculate block position based on turtle position and facing
|
||||
function getBlockPosition(turtlePos, facing, direction) {
|
||||
if (!turtlePos) return null;
|
||||
|
||||
const pos = { ...turtlePos };
|
||||
|
||||
if (direction === 'up') {
|
||||
pos.y += 1;
|
||||
} else if (direction === 'down') {
|
||||
pos.y -= 1;
|
||||
} else if (direction === 'forward') {
|
||||
// Calculate based on facing direction
|
||||
if (facing === 0) pos.z -= 1; // North
|
||||
else if (facing === 1) pos.x += 1; // East
|
||||
else if (facing === 2) pos.z += 1; // South
|
||||
else if (facing === 3) pos.x -= 1; // West
|
||||
}
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
// Create HTTP server
|
||||
const server = createServer(app);
|
||||
@@ -29,10 +61,17 @@ wss.on('connection', (ws) => {
|
||||
console.log('🌐 New web client connected');
|
||||
webClients.add(ws);
|
||||
|
||||
// Send current turtle data to new client
|
||||
// Send current turtle data and world blocks to new client
|
||||
const blocks = [];
|
||||
for (const [key, blockData] of worldBlocks.entries()) {
|
||||
const [x, y, z] = key.split(',').map(Number);
|
||||
blocks.push({ x, y, z, ...blockData });
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
type: 'initial_state',
|
||||
turtles: Array.from(turtleData.values())
|
||||
turtles: Array.from(turtleData.values()),
|
||||
blocks: blocks
|
||||
}));
|
||||
|
||||
ws.on('message', (message) => {
|
||||
@@ -101,6 +140,16 @@ app.post('/api/turtle/update', (req, res) => {
|
||||
...turtleUpdate,
|
||||
lastUpdate: Date.now()
|
||||
});
|
||||
|
||||
// Store discovered blocks in world map
|
||||
if (turtleUpdate.surroundings && turtleUpdate.position && turtleUpdate.facing !== undefined) {
|
||||
for (const [direction, blockData] of Object.entries(turtleUpdate.surroundings)) {
|
||||
const blockPos = getBlockPosition(turtleUpdate.position, turtleUpdate.facing, direction);
|
||||
if (blockPos) {
|
||||
storeBlock(blockPos.x, blockPos.y, blockPos.z, blockData, turtleID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Broadcast to web clients
|
||||
broadcastToClients({
|
||||
@@ -149,6 +198,19 @@ app.get('/api/turtles', (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Get world blocks for map visualization
|
||||
app.get('/api/world/blocks', (req, res) => {
|
||||
const blocks = [];
|
||||
for (const [key, blockData] of worldBlocks.entries()) {
|
||||
const [x, y, z] = key.split(',').map(Number);
|
||||
blocks.push({
|
||||
x, y, z,
|
||||
...blockData
|
||||
});
|
||||
}
|
||||
res.json({ blocks });
|
||||
});
|
||||
|
||||
// Send command to turtle
|
||||
app.post('/api/turtle/:id/command', (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user