mirror of
https://github.com/trafficlunar/blockmatic.git
synced 2026-06-28 14:44:12 +00:00
fix: add air block and fix transparency on images
This commit is contained in:
parent
5c856d55c4
commit
5804853395
6 changed files with 44 additions and 20 deletions
|
|
@ -85,6 +85,7 @@ function Blocks({ blocks, setBlocks, missingTexture, textures, solidTextures, im
|
|||
|
||||
for (let i = 0; i < imageData.data.length; i += 4) {
|
||||
const block = findBlockFromRgb(blockData, imageData.data[i], imageData.data[i + 1], imageData.data[i + 2], imageData.data[i + 3]);
|
||||
if (block == "air") continue;
|
||||
|
||||
const x = Math.floor((i / 4) % imageData.width);
|
||||
const y = Math.floor(i / 4 / imageData.width);
|
||||
|
|
|
|||
|
|
@ -109,8 +109,29 @@ function Canvas() {
|
|||
return radiusBlocks;
|
||||
};
|
||||
|
||||
const eraseTool = () => {
|
||||
// Fixes Infinity and NaN errors
|
||||
if (blocks.length == 1) return;
|
||||
|
||||
const halfSize = Math.floor(radius / 2);
|
||||
const startX = mouseCoords.x - (radius % 2 === 0 ? 0 : halfSize);
|
||||
const startY = mouseCoords.y - (radius % 2 === 0 ? 0 : halfSize);
|
||||
|
||||
const updated = blocks.filter((block) => {
|
||||
const withinSquare = block.x >= startX && block.x < startX + radius && block.y >= startY && block.y < startY + radius;
|
||||
return !withinSquare;
|
||||
});
|
||||
|
||||
setBlocks(updated);
|
||||
};
|
||||
|
||||
switch (tool) {
|
||||
case "pencil": {
|
||||
if (selectedBlock == "air") {
|
||||
eraseTool();
|
||||
break;
|
||||
}
|
||||
|
||||
const newBlocks = getBlocksWithinRadius(mouseCoords.x, mouseCoords.y, radius, selectedBlock);
|
||||
const mergedBlocks = blocks.filter((block) => {
|
||||
return !newBlocks.some((newBlock) => block.x === newBlock.x && block.y === newBlock.y);
|
||||
|
|
@ -120,19 +141,7 @@ function Canvas() {
|
|||
break;
|
||||
}
|
||||
case "eraser": {
|
||||
// Fixes Infinity and NaN errors
|
||||
if (blocks.length == 1) break;
|
||||
|
||||
const halfSize = Math.floor(radius / 2);
|
||||
const startX = mouseCoords.x - (radius % 2 === 0 ? 0 : halfSize);
|
||||
const startY = mouseCoords.y - (radius % 2 === 0 ? 0 : halfSize);
|
||||
|
||||
const updated = blocks.filter((block) => {
|
||||
const withinSquare = block.x >= startX && block.x < startX + radius && block.y >= startY && block.y < startY + radius;
|
||||
return !withinSquare;
|
||||
});
|
||||
|
||||
setBlocks(updated);
|
||||
eraseTool();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,12 @@ function Replace() {
|
|||
};
|
||||
|
||||
const onClickReplace = () => {
|
||||
setBlocks((prevBlocks) => prevBlocks.map((block) => (block.name === block1 ? { ...block, name: block2 } : block)));
|
||||
// If block2 name is air, delete the block instead.
|
||||
setBlocks((prevBlocks) =>
|
||||
prevBlocks
|
||||
.map((block) => (block.name === block1 ? (block2 === "air" ? null : { ...block, name: block2 }) : block))
|
||||
.filter((block) => block !== null)
|
||||
);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue