I'll build a physics-based construction puzzle game inspired by World of Goo. This is an ambitious project, so let me plan carefully.
Let me start by exploring the project structure.
I have a good understanding of the project. Let me plan the architecture for this complex game.
Architecture Plan:
- Physics Engine (
src/physics/engine.ts) - Custom Verlet integration spring-mass system with gravity, strut stretching, and snapping.
- Goo balls - Beads that connect via spring struts. They need to feel alive (squash/stretch, eyes, wobble).
- Levels (
src/game/levels.ts) - 5+ levels with different terrain, seed structures, ball supplies, pipe placement.
- Rendering - HTML5 Canvas for the game world with rich atmospheric visuals (backgrounds, parallax, particles).
- Game component (
src/game/Game.tsx) - Main game loop, drag interactions, physics stepping.
- Level Select (
src/game/LevelSelect.tsx) - Beautiful level selection screen.
- UI overlay - Ball counter, win state, building.
Let me start building. First the physics engine.
Now let me create the levels definition with terrain, seed structure, balls supply, and pipe placement.
Now let me create the visual themes (color palettes for each level's atmosphere).
Now let me create the main Game component. This is the core that handles the canvas rendering, physics stepping, drag interaction, crawling mechanics, and win/lose states. This is the most complex file.
Let me think carefully about the design:
- Goo ball rendering - Each ball rendered as a squishy blob with eyes. To feel alive, I'll deform the circle based on velocity and nearby strut strain.
- Drag interaction - Player picks a loose ball, drags it. Show spring preview to nearest attachable ball when in range. On release, connect.
- Crawling / suction - When structure touches pipe reach, loose balls crawl up connected structure and get sucked in one-by-one. Each collected ball counts toward target.
Let me think about how balls are added. When player drops a ball attached to structure, it becomes connected (part of structure). When pipe reached, remaining loose balls crawl UP the structure then get sucked in.
I'll track:
supply: total loose balls (includes attached ones)
collected: balls sucked in for score
used: balls attached to structure
When you attach a ball, it leaves the "loose pile" and becomes part of structure. When pipe is active, each remaining loose ball (not attached) crawls up to structure, then gets sucked into pipe, incrementing collected. Balls spent on building are NOT collected.
The win condition: collected >= target.
Let me design the crawling: when triggered, an unattached loose ball spawns at the nearest structure ball position and animates along the connected structure toward the pipe... Actually, simpler: it crawls up along the chain of connected balls to the ball nearest the pipe, then slides into the pipe.
Let me write the Game component carefully. It's large. I'll use refs for the mutable game state (physics world, balls, etc.) and React state only for UI overlay counters and win/lose.
Let me structure the code:
world ref: PhysicsWorld
ballEntities array: holds per-ball render metadata (squash, blink, attached flag, kind: seed/attached/loose)
- Game loop via requestAnimationFrame
- Rendering all on canvas
For liveness of balls: eyes, blink, a happy/squish reaction when plucked, squash when under strain.
Let me write it.
Let me fix these type errors. Several are unused variables/dead code, plus the EntityKind type issue. Let me clean up the Game component.
First, let me add "attached-grab" to EntityKind type, and remove unused declarations. Let me fix each error systematically.
Let me fix the remaining unused variable errors. Let me look at the specific lines.
Let me fix these unused variables. I'll remove the world parameters from the helper functions, remove floatersRef and wrap, remove unused params, remove pathTerrain, and the world param in drawTerrain.
I need to fix the syncLooseQueue(world) call at line 386, usedBallCount(world) calls, and remove unused vars. Let me fix the helper functions' world params too.
I removed nearbyConnectedBall but it's used at line 139. Let me check line 139 (used in updateCrawlers), and update all the usedBallCount(world) calls and syncLooseQueue(world) calls. Let me also remove the remaining unused imports/declarations.
Let me look at where nearbyConnectedBall is called and remove that dead code (it's assigned to mouth which is voidd).
Now let me fix the usedBallCount(world) and syncLooseQueue(world) calls at lines 185, 275, 375. Let me look at those lines.
Let me fix these three calls.
Now the remaining fixes: wrap unused, m/dt params in render, pathTerrain dead code, world param in drawTerrain, kind param in drawGooBall. Let me fix them.
I need to fix the render(world, m, dt) call at line 404, and remove pathTerrain, fix drawTerrain's world param, and drawGoo's kind param. Let me find and fix the render call.
Now let me remove the pathTerrain dead function, fix drawTerrain(world) parameter, and drawGooBall kind parameter.
Now fix the kind parameter in drawGooBall. It's used to customize rendering maybe. Let me check where drawGooBall is called and if kind is used in its body.
The kind parameter isn't used in the body. Let me remove it from both the call and definition.
All type errors are resolved. Now let me also reconsider the setupRef logic for replay in the win overlay — I had a weird double-assignment. Let me fix the replay button to use resetLevel.
Let me check the win overlay's replay button.
Let me find the replay button in win overlay and fix it. Let me search for it.
Let me fix the replay button to properly call resetLevel. The issue is that the setup effect only runs once. When we reset, we should be able to reset the level state. Since resetLevel is defined in the component and reads refs, calling it directly works. Let me replace the replay button.
Now let me create the LevelSelect screen. It needs to be visually rich with theme backgrounds per level.
Let me fix the unused now param in the LevelSelect frame function.
Now let me set up App.tsx to manage the screen state and persist best results in localStorage.
Now let me update index.html title and add the Fraunces font, and update the CSS with animations and font imports.
Now let me update the CSS with the font, the pop animation used in the win overlay, and a goo cursor.
Now let me review the physics engine for correctness before building. A key concern: the strut constraint correction uses concerns the stiffness. In Verlet, position correction for a distance constraint should be diff * 0.5 split between masses, not correction = 1/w with iterational hardness. Let me reconsider.
For a distance constraint between two free particles, each moves half the required correction: off = (dist - rest) / dist, dx correction; a moves + off * 0.5, b moves - off * 0.5. With fixed-mass weighting, a heavy particle moves less. My current code applies nearly full correction which is too stiff and can cause explosion. Let me use proper softened correction.
Let me rewrite the strut constraint with proper mass weighting and a stiffness factor. Since balls are all equal mass, this resolves to 0.5 split. For a fixed ball, the other gets full correction.
Wait, there's a subtle issue with the mass weighting. With m1 = 0 (a fixed), w = 1, m2 = 1. Then a.pos += offX * m2 / w = offX * 1 / 1 = offX — but a is fixed (m1=0) so we skip the if (m1 > 0) block. Good. And b.pos -= offX * m1 / w = 0. So b doesn't move, but a (the fixed ball) would move but it's skipped. That's wrong! When a is fixed and b is free, b should take the full correction.
Let me reconsider: the forces should be split inversely to mass. For correction between two bodies, displacement is divided proportionally: body1 moves off * m2/(m1+m2), body2 moves off * m1/(m1+m2). With fixed body having infinite mass (m=0 effectively), free body gets all correction.
So a.pos += offX * m2 / w and b.pos -= offX * m1 / w, with no extra *correction=1. Let me fix: when a.fixed, m1=0, w=1, so b gets offX * 0 / 1 = 0 — still wrong, b should get it all.
The issue: "fixed" should mean infinite mass, so it receives zero displacement and the OTHER body receives full displacement. Let me treat fixed as "doesn't move at all". For displacement distribution: if a is fixed, a moves 0, b moves full. Standard Verlet distance constraint without mass:
- both free: a moves +0.5off, b moves -0.5off
- one fixed: movable moves full (+off or -off)
- both fixed: nothing
So the rule: aFraction = a.fixed ? 0 : 1/(count of movable). Let me implement it simply with two bools.
Now let me also reconsider the gravity and integration. In Verlet: pos += (pos - prev) + g*dt*dt. This is correct. But pos - prev already contains g from the previous step folded into prev-to-pos. Good.
Actually wait — in standard Verlet, after constraints you don't re-integrate. The order is: newPos = prev + (pos-prev)*damping*dt... . Let me re-examine my step:
vx = (pos.x - prev.x) * damping
vy = (pos.y - prev.y) * damping
prev.x = pos.x
prev.y = pos.y
pos.x += vx
pos.y += vy + gravity*dt*dt
(pos - prev) is the velocity × dt from the previous frame. Multiplying by prev.x = pos.x (store old), then pos.x += vx. This is a half-step Verlet but with damping applied to velocity. Actually here prev ends up storing the position BEFORE the update, so next pos = prev stored + velocity. The velocity step uses position difference which is fine. This is a symplectic-ish integration and works fine for Verlet. Good enough.
One concern: gravity*dt*dt with dt=0.033 and gravity=1150 → 1150*0.00109 = 1.25 px per step. Fine.
Now there's the step(dt) being called with variable dt. My groundY logic sets groundY = Math.min(groundY, segg.y1) per segment but that's unused for rendering (I removed its usage?). Let me check — groundY is declared but I never read it after. That could be an unused-something, but since it's a let assigned in the loop within the useEffect closure, TS may warn groundY assigned but never used only if it's never read. Actually it's read nowhere. Let me check if it causes a TS error — it may be flagged. Let me build to find out.
Let me build the project now.
The build succeeds. Now let me review the game logic more carefully for runtime correctness and gameplay bugs. Let me re-read the full Game component to verify the flow is coherent.
There are several concerns I want to address to make the game robust:
-
resetLevel/setup ordering: The main loop runs its frame using closures over world (stable ref), but resetLevel mutates worldRef.current in place (since it's the same object). Good — the loop reads the same object. But frame captures level, theme — fine.
-
The crawlers update within frame closure reads refs — good.
-
A bigger gameplay issue: When the player drags and builds balls, the loose balls near the structure will still be resting on terrain. When the pipe activates, queuedCrawlersRef gets set to all loose entities and they crawl. But at the moment of activation, syncLooseQueue() reads e.kind === "loose". However, I set queuedCrawlersRef once. If the player grabs a loose ball AFTER pipe activation during crawling, that ball was already in the queue... but if player grabs it (kind becomes attached-grab), it will still crawl since the loop doesn't re-check kind. That's a minor conflict but acceptable — actually it could double-add. Let me make the crawler loop skip entities that are being dragged.
-
Loose balls falling through terrain or resting — loose balls are placed slightly above terrain and rest. But several loose ball positions are mid-air (e.g., level 2 has loose balls at y=500 in mid-air). Those would just fall to terrain and roll. That's fine — they'll land on the platform. Actually in World of Goo, loose balls roll around. My physics treats them as physics balls that fall and rest on terrain via segment collision. Good. But they have no rotational/rolling — they'll just slide/settle. Acceptable.
-
Picking up balls already resting against structure: onDown picks any loose/attached? No — it excludes seed and crawling, but includes attached kind! That means player can grab already-attached balls and detach them. Actually the kind check: if (e.kind === "seed") continue; if (e.kind === "crawling") continue;. So attached and loose and attached-grab are grabbable. When grabbing an attached ball, on release without preview it becomes "loose" and the struts? The struts aren't removed! So it stays connected via struts but kind = loose. That's buggy. Let me restrict grab to loose only, OR handle detaching attached nodes (World of Goo also lets you grab attached balls and re-place them).
Actually in World of Goo, you CAN grab attached goo balls and move them (they disconnect and reconnect). That's a nice feature but adds complexity. To keep it robust, let me make grabbing attached balls remove their struts (so they become free) and re-placeable. On pickup of an attached ball: remove all struts to/from it (the connected chain above may collapse — that's fine and realistic). Let me implement:
- On down: if grabbing a ball that is
attached (connected), set kind to "attached-grab", remove its struts, mark connected=false.
- On release: if preview, re-add strut and set connected=true, kind=attached. Else kind=loose.
But when we remove struts of an interior ball, the balls above it also need to have their struts removed (they're on the other side of the removed nodes). Actually if ball X is grabbed, only struts incident to X are removed; balls attached to X lose connection to X but remain connected to each other via their own struts. Balls above X form a separate cluster that may fall. That's acceptable.
Hmm, but this could lead to confusion. Given time constraints and to keep things stable, let me simplify: only allow grabbing loose balls. Attached balls stay put. This keeps the build-vs-collect tradeoff clean and avoids instability. Actually, allowing re-grab is a nice quality feature but risky. Let me keep it simple and only grab loose balls.
Wait, but then once you place a ball it can't be recovered. In World of Goo you often need to re-place. But for robustness I'll allow grabbing attached goo too, with strut removal, since it adds depth. Let me implement it carefully.
Actually, let me reconsider. The attached-grab kind already exists. Let me implement pickup of attached balls: remove incident struts. And during drag, the ball is just positioned by mouse. On release with preview → reconnect. This is clean.
Let me also handle: when removing a strut that connects an attached ball to the rest, the ball's connected should become... if it's connected to nothing else after, it's effectively detached. We'll set connected=false during grab, and only reconnect on successful placement.
But there's the "structure loses goo" implication: balls spent building are counted via used = attached+seed. If you grab an attached ball back to loose, it becomes unchosen, so it could be collected later. That's actually a nice recovery mechanic!
Let me also make sure the crawler queue doesn't include a ball currently being dragged. And if a ball in the queue becomes a building ball, it should be removed from queue.
Let me implement the grab logic modifications. Let me read the interaction section.
Let me update the onDown and onUp handlers to handle grabbing attached balls (remove struts on pickup, reconnect on placement). I'll also update onUp to use the "attached-grab" transition properly and remove the ball from the crawler queue if it was grabbed.
Let me revise the interaction logic:
I need to add the removeStrutsOf method to the physics engine. Let me update it.
Now I need to also update onUp. Since a grabbed attached ball (now in "attached-grab") will reconnect on preview. The current onUp code handles this but there's a subtle issue: after picking up an attached ball and severing struts, if the player releases without a valid preview, it becomes "loose" — good. Let me also ensure usedBallCount() is recomputed, and add a check that reconnecting doesn't happen when pipe is already active. Let me view the current onUp again and update.
I referenced a puff particle array that I need to define. Let me add a particle effects system to the frame loop. First, let me add a puffs ref and the update/draw logic. Let me find where floaters/refs are declared and where drawInteractions is, to add particle rendering.
Let me add a particles system near the refs and integrate it into the frame/render.
I need to reference puffsRef.current instead of puff in onUp. Let me fix that and define the Puff type. Let me add the Puff interface near the top and fix the onUp reference. The onUp is inside the useEffect closure so it needs access to puffsRef (defined in component scope, accessible). Let me change puff.push to puffsRef.current.push.
Now I need to add the update and rendering of puffs in the frame loop. Let me add the puff update logic within frame and draw them in render. Let me look at the frame function and render to find good insertion points.
I see the groundY reassignment is there but not used. Also I want to add puff particle updates. Let me update the frame function to update puffs and remove the unused groundY. Let me view a bit further to see where the frame stops and render begins.
Let me update the frame function to update puffs and remove the unused groundY logic (and remove the groundY variable declaration). Let me first replace the groundY/pipe section and add puff updates.
I need to remove the groundY variable declaration. Let me find it.
Now I need to draw the puffs in the render function. Let me add a drawPuffs() to the render, and add the drawPuffs function. Let me find the render function's draw order and add a call + implementation.
Let me add the drawPuffs function. I'll place it right before drawInteractions definition.
Now let me update drawGoo to draw attached-grab balls too. The current drawGoo iteration order is ["attached", "seed", "crawling", "loose"] and won't draw attached-grab (the ball being dragged) at all since it's not in the list. When you pick up an attached ball, its kind becomes attached-grab and it won't render. Let me add it to the draw order.
Files built: src/physics/engine.ts, src/game/levels.ts, src/game/themes.ts, src/game/Game.tsx, src/game/LevelSelect.tsx, src/App.tsx, index.html, src/index.css