feat: Enhance turtle model and visualization with block surroundings and world block management
This commit is contained in:
@@ -10,6 +10,7 @@ console.log('📡 API URL:', API_URL);
|
||||
export const useTurtleStore = create((set, get) => ({
|
||||
// State
|
||||
turtles: {},
|
||||
worldBlocks: [],
|
||||
selectedTurtleId: null,
|
||||
connected: false,
|
||||
ws: null,
|
||||
@@ -32,7 +33,10 @@ export const useTurtleStore = create((set, get) => ({
|
||||
data.turtles.forEach(turtle => {
|
||||
turtlesMap[turtle.turtleID] = turtle;
|
||||
});
|
||||
set({ turtles: turtlesMap });
|
||||
set({
|
||||
turtles: turtlesMap,
|
||||
worldBlocks: data.blocks || []
|
||||
});
|
||||
} else if (data.type === 'turtle_update') {
|
||||
set(state => ({
|
||||
turtles: {
|
||||
@@ -40,6 +44,11 @@ export const useTurtleStore = create((set, get) => ({
|
||||
[data.turtle.turtleID]: data.turtle
|
||||
}
|
||||
}));
|
||||
|
||||
// Update world blocks from turtle surroundings
|
||||
if (data.turtle.surroundings && data.turtle.position && data.turtle.facing !== undefined) {
|
||||
get().updateBlocksFromSurroundings(data.turtle);
|
||||
}
|
||||
} else if (data.type === 'turtle_disconnected') {
|
||||
set(state => {
|
||||
const newTurtles = { ...state.turtles };
|
||||
@@ -71,6 +80,52 @@ export const useTurtleStore = create((set, get) => ({
|
||||
selectTurtle: (turtleId) => {
|
||||
set({ selectedTurtleId: turtleId });
|
||||
},
|
||||
|
||||
updateBlocksFromSurroundings: (turtle) => {
|
||||
const { surroundings, position, facing } = turtle;
|
||||
if (!surroundings || !position || facing === undefined) return;
|
||||
|
||||
const newBlocks = [];
|
||||
const blockMap = new Map(get().worldBlocks.map(b => [`${b.x},${b.y},${b.z}`, b]));
|
||||
|
||||
// Calculate block positions based on turtle position and facing
|
||||
const directions = {
|
||||
forward: { x: 0, y: 0, z: 0 },
|
||||
up: { x: 0, y: 1, z: 0 },
|
||||
down: { x: 0, y: -1, z: 0 }
|
||||
};
|
||||
|
||||
// Facing: 0=North(-Z), 1=East(+X), 2=South(+Z), 3=West(-X)
|
||||
if (surroundings.forward) {
|
||||
if (facing === 0) directions.forward = { x: 0, y: 0, z: -1 };
|
||||
else if (facing === 1) directions.forward = { x: 1, y: 0, z: 0 };
|
||||
else if (facing === 2) directions.forward = { x: 0, y: 0, z: 1 };
|
||||
else if (facing === 3) directions.forward = { x: -1, y: 0, z: 0 };
|
||||
}
|
||||
|
||||
Object.entries(surroundings).forEach(([direction, blockData]) => {
|
||||
const offset = directions[direction];
|
||||
if (!offset) return;
|
||||
|
||||
const blockPos = {
|
||||
x: position.x + offset.x,
|
||||
y: position.y + offset.y,
|
||||
z: position.z + offset.z
|
||||
};
|
||||
|
||||
const key = `${blockPos.x},${blockPos.y},${blockPos.z}`;
|
||||
if (!blockMap.has(key)) {
|
||||
blockMap.set(key, {
|
||||
...blockPos,
|
||||
...blockData,
|
||||
discoveredBy: turtle.turtleID,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
set({ worldBlocks: Array.from(blockMap.values()) });
|
||||
},
|
||||
|
||||
sendCommand: async (turtleId, command, param = null) => {
|
||||
const { ws } = get();
|
||||
|
||||
Reference in New Issue
Block a user