MCPcopy Create free account
hub / github.com/esengine/esengine / floodFill

Method floodFill

packages/tilemap-editor/src/tools/FillTool.ts:77–129  ·  view source on GitHub ↗
(startX: number, startY: number, ctx: ToolContext)

Source from the content-addressed store, hash-verified

75 }
76
77 private floodFill(startX: number, startY: number, ctx: ToolContext): void {
78 const { tilemap, selectedTiles, editingCollision, currentLayer } = ctx;
79
80 if (startX < 0 || startX >= tilemap.width || startY < 0 || startY >= tilemap.height) {
81 return;
82 }
83
84 if (editingCollision) {
85 // Flood fill collision
86 const targetCollision = tilemap.hasCollision(startX, startY);
87 const newCollision = targetCollision ? 0 : 1;
88
89 const stack: [number, number][] = [[startX, startY]];
90 const visited = new Set<string>();
91
92 while (stack.length > 0) {
93 const [x, y] = stack.pop()!;
94 const key = `${x},${y}`;
95
96 if (visited.has(key)) continue;
97 if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
98 if (tilemap.hasCollision(x, y) !== targetCollision) continue;
99
100 visited.add(key);
101 tilemap.setCollision(x, y, newCollision);
102
103 stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
104 }
105 } else {
106 // Flood fill tiles
107 const targetTile = tilemap.getTile(currentLayer, startX, startY);
108 const newTile = selectedTiles ? (selectedTiles.tiles[0] ?? 1) : 1;
109
110 if (targetTile === newTile) return;
111
112 const stack: [number, number][] = [[startX, startY]];
113 const visited = new Set<string>();
114
115 while (stack.length > 0) {
116 const [x, y] = stack.pop()!;
117 const key = `${x},${y}`;
118
119 if (visited.has(key)) continue;
120 if (x < 0 || x >= tilemap.width || y < 0 || y >= tilemap.height) continue;
121 if (tilemap.getTile(currentLayer, x, y) !== targetTile) continue;
122
123 visited.add(key);
124 tilemap.setTile(currentLayer, x, y, newTile);
125
126 stack.push([x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]);
127 }
128 }
129 }
130}

Callers 1

onMouseDownMethod · 0.95

Calls 8

hasCollisionMethod · 0.80
popMethod · 0.80
setCollisionMethod · 0.80
getTileMethod · 0.80
setTileMethod · 0.80
hasMethod · 0.65
pushMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected