juceGameTools

December 5, 2025 ยท View on GitHub

A collection of C++ classes for building 2D games using the JUCE framework. This toolkit provides fundamental building blocks for managing game objects, creating game worlds with physics and levels, and implementing a flexible camera system.

Core Classes

The project is built around three main classes: GameObject, GameWorld, and CameraDisplay.

GameObject

Header: GameObject.h

The GameObject class is the base element for everything that exists in the game world. It's a versatile class that can represent players, enemies, projectiles, platforms, or trigger zones.

Key Features:

  • Physics & Position: Manages properties like position (x, y), velocity (vx, vy), mass, and acceleration. It supports static objects that are immovable (isStatic) and solid objects that block others (isSolid).
  • Collision: Provides a bounding box (getPhysicalBounds()) for collision detection. You can define its shape as a Rectangle or Ellipse.
  • Visuals: Can be rendered as a solid colour, a text displayLabel, or a graphical sprite. The sprite rendering can be configured to stretch or be centered.
  • Identification: Uses a tag string for easy categorization (e.g., "player", "enemy") and a canonicalName for a persistent identifier.

GameWorld

Header: GameWorld.h

GameWorld is an abstract class that manages the entire game state, logic, and object interactions. You are expected to create a subclass of GameWorld to define the specific rules and content for your game.

Key Features:

  • Object Management: Holds and manages the lifecycle of all GameObject instances in a juce::OwnedArray.
  • Game Loop: Features a central update() method that drives physics calculations and collision detection for all objects.
  • Level Management: Provides a system for defining and switching between levels via the abstract setupLevel(int levelIndex) method. It includes helpers for loading, restarting, and advancing levels.
  • Collision Events: Offers virtual callback methods (onCollisionEnter, onCollision, onCollisionExit) that you can override in your subclass to implement game logic when objects interact.
  • Event System: A Listener interface allows other parts of your application (like the UI) to be notified when objects are added or removed from the world.
  • JUCE Integration: Can be linked to a juce::AudioProcessorValueTreeState to synchronize game parameters.

CameraDisplay

Header: CameraDisplay.h

This juce::Component acts as the player's window into the GameWorld. It is responsible for rendering the state of the world to the screen and handles all coordinate transformations.

Key Features:

  • World Rendering: Iterates through the GameObjects in a given GameWorld and draws them at their correct on-screen positions.
  • Camera Control: The camera's view can be controlled by setting its position (cameraX, cameraY) and zoom level (cameraWidth).
  • Target Following: Can be set to automatically follow a GameObject or any dynamic point.
  • Smooth Motion: Implements camera smoothing (cameraSmoothSpeed) for a more fluid and cinematic feel.
  • Dead Zone: A configurable "dead zone" prevents minor, jittery camera movements when the target is near the center of the screen.
  • Parallax Background: Supports multi-layered scrolling by applying a parallaxFactor to the background image, creating a sense of depth.

How They Work Together

  1. You create a concrete class that inherits from GameWorld and implement its pure virtual methods like setupLevel() to populate it with GameObject instances.
  2. You create an instance of your custom GameWorld.
  3. You create an instance of CameraDisplay, passing it a reference to your GameWorld.
  4. The CameraDisplay component is added to your UI.
  5. In your main application loop (e.g., a juce::Timer callback), you call gameWorld.update() to process game logic and cameraDisplay.update() to move the camera and trigger a repaint.
  6. The CameraDisplay::paint() method then renders all the objects from the GameWorld from the camera's perspective.