Skip to content

Commit

Permalink
add tempo so we see the fall
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheelax committed Nov 19, 2024
1 parent b47cbd7 commit 2127b98
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions client/src/ui/components/Tutorial/TutorialGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,6 @@ const TutorialGrid: React.FC<GridProps> = forwardRef(
setBlocks(initialData);
}, [initialData]);

useEffect(() => {
const interval = setInterval(() => {
if (
gameState === GameState.GRAVITY ||
gameState === GameState.GRAVITY2
) {
applyGravity();
}
}, 100);

return () => clearInterval(interval);
}, [gameState]);

const calculateFallDistance = (block: Block, blocks: Block[]) => {
let maxFall = gridHeight - block.y - 1;
for (let y = block.y + 1; y < gridHeight; y++) {
Expand Down Expand Up @@ -152,31 +139,39 @@ const TutorialGrid: React.FC<GridProps> = forwardRef(
};

const applyGravity = useCallback(() => {
let hasBlocksMoved = false;

setBlocks((prevBlocks) => {
const newBlocks = prevBlocks.map((block) => {
const fallDistance = calculateFallDistance(block, prevBlocks);
console.log("Calculating fall distance", {
blockId: block.id,
fallDistance,
let newBlocks = [...prevBlocks];
let blocksMoved;

do {
blocksMoved = false;
newBlocks = newBlocks.map((block) => {
const fallDistance = calculateFallDistance(block, newBlocks);
if (fallDistance > 0) {
blocksMoved = true;
hasBlocksMoved = true;
return { ...block, y: block.y + 1 };
}
return block;
});
} while (blocksMoved);

if (fallDistance > 0) {
return { ...block, y: block.y + 1 };
}
return block;
});

const hasChanged = newBlocks.some(
(block, i) => block.y !== prevBlocks[i].y,
);

if (!hasChanged) {
setIsMoving(false);
}

return hasChanged ? newBlocks : prevBlocks;
setIsMoving(hasBlocksMoved);
return newBlocks;
});
}, [gridHeight]);
}, [calculateFallDistance]);

useEffect(() => {
if (
gameState === GameState.GRAVITY ||
gameState === GameState.GRAVITY2 ||
gameState === GameState.GRAVITY_BONUS
) {
applyGravity();
}
}, [gameState, applyGravity]);

const handleDragStart = (x: number, block: Block) => {
console.log("Drag start:", block);
Expand Down Expand Up @@ -267,6 +262,10 @@ const TutorialGrid: React.FC<GridProps> = forwardRef(
gridPosition.top + b.y * gridSize,
);
});
if (tutorialStep === 3) {
setTimeout(() => onUpdate(true), 1000);
return;
}
} else if (bonus === BonusType.Totem) {
setBlocks(removeBlocksSameWidth(block, blocks));
getBlocksSameWidth(block, blocks).forEach((b) => {
Expand All @@ -276,23 +275,26 @@ const TutorialGrid: React.FC<GridProps> = forwardRef(
gridPosition.top + b.y * gridSize,
);
});
if (tutorialStep === 4) {
setTimeout(() => onUpdate(true), 1000);
return;
}
} else if (bonus === BonusType.Hammer) {
setBlocks(removeBlockId(block, blocks));
if (gridPosition === null) return;
handleTriggerLocalExplosion(
gridPosition.left + block.x * gridSize + (block.width * gridSize) / 2,
gridPosition.top + block.y * gridSize,
);
if (tutorialStep === 2) onUpdate(true);
if (tutorialStep === 2) {
setTimeout(() => onUpdate(true), 1000);
return;
}
}

// Apply gravity immediately after bonus
applyGravity();
setIsMoving(true);
setGameState(GameState.GRAVITY_BONUS);
if (bonus === BonusType.Hammer && tutorialStep === 2) onUpdate(true);
if (bonus === BonusType.Wave && tutorialStep === 3) onUpdate(true);
if (bonus === BonusType.Totem && tutorialStep === 4) onUpdate(true);
};

const handleMouseDown = (e: React.MouseEvent, block: Block) => {
Expand Down Expand Up @@ -425,10 +427,6 @@ const TutorialGrid: React.FC<GridProps> = forwardRef(
const explosionY =
gridRect.top + block.y * gridSize + gridSize / 2;

console.log("Triggering explosion at:", {
explosionX,
explosionY,
});
handleTriggerLocalExplosion(explosionX, explosionY);
});
}
Expand All @@ -437,7 +435,9 @@ const TutorialGrid: React.FC<GridProps> = forwardRef(
setBlocks(updatedBlocks);
setIsMoving(true);
setGameState(newGravityState);
onUpdate(true);
if (tutorialStep === 1) {
setTimeout(() => onUpdate(true), 1000);
}
} else {
setGameState(newStateOnComplete);
}
Expand Down

0 comments on commit 2127b98

Please sign in to comment.