Main game engine class that manages the game loop and scenes.
new Engine(config)
Parameters:
config.width (number): Canvas width (default: 800)config.height (number): Canvas height (default: 600)config.parent (HTMLElement): Parent element (default: document.body)config.backgroundColor (string): Background color (default: ‘#000000’)config.antialias (boolean): Enable antialiasing (default: true)config.targetFPS (number): Target FPS (default: 60)canvas (HTMLCanvasElement): The game canvasctx (CanvasRenderingContext2D): Canvas contextisRunning (boolean): Whether engine is runningisPaused (boolean): Whether engine is pauseddeltaTime (number): Time since last frame (seconds)fps (number): Current frames per secondstart()
Starts the game loop.
stop()
Stops the game loop.
pause()
Pauses the game.
resume()
Resumes the game.
addScene(name, scene)
Adds a scene to the engine.
name (string): Scene identifierscene (Scene): Scene instancesetScene(name)
Switches to a different scene.
name (string): Scene nameresize(width, height)
Resizes the canvas.
width (number): New widthheight (number): New heightdestroy()
Destroys the engine and cleans up resources.
Container for game objects and game state.
new Scene(name)
Parameters:
name (string): Scene namename (string): Scene identifiergameObjects (Array): List of game objectscamera (Camera): Scene cameraphysics (Physics): Physics systeminput (Input): Input systemadd(gameObject)
Adds a game object to the scene.
remove(gameObject)
Removes a game object from the scene.
findByName(name)
Finds a game object by name.
findByTag(tag)
Finds all game objects with a tag.
clear()
Removes all game objects.
onEnter()
Called when scene becomes active. Override in subclass.
onExit()
Called when scene becomes inactive. Override in subclass.
Base class for all game entities.
new GameObject(x, y)
Parameters:
x (number): X position (default: 0)y (number): Y position (default: 0)x, y (number): Positionrotation (number): Rotation in radiansscaleX, scaleY (number): Scale factorsname (string): Object nametag (string): Object tagactive (boolean): Whether object is activevisible (boolean): Whether object is visiblezIndex (number): Render orderparent (GameObject): Parent objectchildren (Array): Child objectsaddComponent(component)
Adds a component to the game object.
getComponent(ComponentClass)
Gets a component by type.
getComponents(ComponentClass)
Gets all components of a type.
removeComponent(component)
Removes a component.
addChild(child)
Adds a child object.
removeChild(child)
Removes a child object.
getWorldPosition()
Gets world position accounting for parent transforms.
destroy()
Destroys this game object.
Base class for all components.
new Component()
gameObject (GameObject): The game object this component is attached toactive (boolean): Whether component is activeonStart()
Called when component is added to a game object. Override in subclass.
fixedUpdate(dt)
Fixed update for physics. Override in subclass.
update(dt)
Update game logic. Override in subclass.
render(ctx)
Render the component. Override in subclass.
onDestroy()
Called when component is destroyed. Override in subclass.
Manages viewport and camera transformations.
new Camera(x, y, width, height)
follow(target, speed)
Makes camera follow a game object.
target (GameObject): Target to followspeed (number): Follow speed (0-1)setBounds(x, y, width, height)
Sets camera movement bounds.
shake(intensity, duration)
Creates camera shake effect.
update(dt)
Updates camera (follow, shake, etc.)
screenToWorld(screenX, screenY)
Converts screen coordinates to world coordinates.
worldToScreen(worldX, worldY)
Converts world coordinates to screen coordinates.
Renders sprites or colored rectangles.
new SpriteRenderer(config)
Parameters:
config.image (Image): Image to renderconfig.width (number): Widthconfig.height (number): Heightconfig.color (string): Fill color (if no image)config.alpha (number): Opacity (0-1)config.flipX (boolean): Flip horizontallyconfig.flipY (boolean): Flip verticallysetSprite(x, y, width, height)
Sets sprite from sprite sheet.
Renders text.
new TextRenderer(text, config)
Parameters:
text (string): Text to renderconfig.font (string): Font (e.g., ‘16px Arial’)config.color (string): Text colorconfig.align (string): Alignment (‘left’, ‘center’, ‘right’)config.stroke (boolean): Enable strokeconfig.strokeColor (string): Stroke colorconfig.strokeWidth (number): Stroke widthAdds physics properties to a game object.
new RigidBody(config)
Parameters:
config.mass (number): Mass (default: 1)config.drag (number): Drag coefficient (default: 0.01)config.gravityScale (number): Gravity multiplier (default: 1)config.useGravity (boolean): Enable gravity (default: true)config.isKinematic (boolean): Kinematic body (default: false)velocityX, velocityY (number): Current velocityisGrounded (boolean): Whether on groundisTouchingWall (boolean): Whether touching walladdForce(x, y)
Applies a continuous force.
addImpulse(x, y)
Applies an instant impulse.
setVelocity(x, y)
Sets velocity directly.
Axis-aligned box collider.
new BoxCollider(width, height, offset)
Parameters:
width (number): Widthheight (number): Heightoffset (Object): Offset {x, y}isTrigger (boolean): Is this a trigger collider?onCollisionEnter(other)
Called when collision starts. Override.
onCollisionStay(other)
Called while colliding. Override.
onCollisionExit(other)
Called when collision ends. Override.
Circle collider.
new CircleCollider(radius, offset)
Physics system managing simulation and collision detection.
new Physics(scene)
gravity (number): Gravity acceleration (default: 980)addCollider(collider)
Registers a collider.
removeCollider(collider)
Unregisters a collider.
detectCollisions()
Performs collision detection. Call each frame.
raycast(x, y, dirX, dirY, distance)
Performs a raycast.
Manages keyboard, mouse, touch, and gamepad input.
new Input(canvas)
update(camera)
Updates input state. Call once per frame.
isKeyDown(keyCode)
Checks if key is currently pressed.
isKeyPressed(keyCode)
Checks if key was just pressed this frame.
isKeyReleased(keyCode)
Checks if key was just released this frame.
isMouseButtonDown(button)
Checks if mouse button is pressed.
button: 0=left, 1=middle, 2=rightisMouseButtonPressed(button)
Checks if mouse button was just pressed.
isMouseButtonReleased(button)
Checks if mouse button was just released.
getGamepad(index)
Gets gamepad by index.
isGamepadButtonDown(button, gamepadIndex)
Checks if gamepad button is pressed.
getGamepadAxis(axis, gamepadIndex)
Gets gamepad axis value.
mapAction(actionName, mapping)
Maps an action to inputs.
isActionDown(actionName)
Checks if action is active.
isActionPressed(actionName)
Checks if action was just pressed.
getAxis(actionName)
Gets axis value for action.
Manages audio playback.
init()
Initializes audio context. Must be called after user interaction.
loadSound(name, url)
Loads a sound.
playSound(name, options)
Plays a sound effect.
options.volume (number): Volume (0-1)options.pitch (number): Pitch multiplieroptions.loop (boolean): Loop soundplayMusic(name, options)
Plays background music.
stopMusic()
Stops music.
setMasterVolume(volume)
Sets master volume (0-1).
setMusicVolume(volume)
Sets music volume (0-1).
setSfxVolume(volume)
Sets sound effects volume (0-1).
Manages sprite animations.
new Animator(spriteRenderer)
addAnimation(name, animation)
Adds an animation.
animation.frames: Array of {x, y, width, height}animation.frameRate: Frames per secondanimation.loop: Whether to loopplay(name, restart)
Plays an animation.
stop()
Stops animation.
Animates object properties over time.
new Tween(target, to, duration, options)
Parameters:
target (Object): Object to animateto (Object): Target propertiesduration (number): Duration in secondsoptions.easing (Function): Easing functionoptions.loop (boolean): Loop animationoptions.yoyo (boolean): Reverse animationoptions.onComplete (Function): Completion callbackTween.Easing
Built-in easing functions: