This is the code for the Breakout game at https://ivasilev.net/playground/breakout.
The README in the root directory contains references to important bits of documentation that may aid in understanding the code.
An overview of how the game works is given on the web page. The code is structured so that it is easy to understand. The entry point is ./index.ts and the user interaction code is concentrated in the breakout component (the component system is explained in the README file in ../../common/rendering).
The game logic is based on discrete-time state evolutions. The current state is stored a StateStore instance, the working of which is explained here. All non-dynamic state is extracted as SCREAMING_SNAKE_CASE constants in ./constants.ts.
-
The ball evolves in accordance to the
evolveBallfunction, which gets invoked on intervals maintained by aanimationFrameObservableobservable. TheevolveBallfunction contains the majority of game logic, modulo the ray intersection logic in./geom/intersection.tsbased on the geometric code from../../common/math/geom2d.The movement speed is fixed and adjusted based on the FPS, so that the speed does not double on 120Hz vs 60Hz, and does not halve during JavaScript's garbage collection.
-
The paddle evolution, governed by the
evolvePaddlefunction, happens before ball evolution on every frame. The updated state is passed to toevolveBall.The paddle evolution takes care of paddle movement, which is toggled by holding the corresponding keyboard key or on-screen button.
-
Finally, brick evolution is governed by
evolveBrickson regular intervals signaled by atimeIntervalobservable.