Intermediate • Lesson 5

Input Actions & Collision Systems

Games feel alive when players can control a character and objects actually bump into, block, or shoot each other. Here you'll learn how Maverick turns raw key presses into clean gameplay actions and detects hits without slowing down.

Without Action Mapping & Spatial Grids

Checking if (key === 'ArrowRight' || key === 'd' || key === 'D') everywhere makes key-rebinding a nightmare. Worse, checking if every bullet hits every enemy one-by-one (N × M checks) explodes into tens of thousands of math checks per frame as your game grows.

Why Maverick Built Input & CollisionSystem

InputManager lets you bind multiple keys to one logical action like 'move_right'. CollisionSystem splits the map into a grid (so objects only check nearby neighbors), fires enter/stay/exit events, and supports instant laser-like raycast() checks.

1. Action-Mapped Input (game.Input)

Think of actions like a car's steering wheel and pedals: your gameplay code only asks "is the player pressing 'attack'?", not which physical button they used. isActionPressed is true while held (walking), while isActionJustPressed is true only on the first tick pressed (swinging a sword once per tap).

import { GameMouseButtons } from 'maverick';

// In onReady():
this.Input.bindAction('move_right', ['ArrowRight', 'd', 'D']);
this.Input.bindAction('attack', [' ', { mouseButton: GameMouseButtons.left }]);

// In onUpdate(dt):
if (this.Input.isActionPressed('move_right')) {
  player.Config.pos!.x += 180 * dt;
}
if (this.Input.isActionJustPressed('attack')) {
  player.triggerAttack();
}

2. Collision Layers, Events & Spatial Queries

A CollisionBox is an invisible hitbox around your character. Layers & masks act like team jerseys—your player can pass through friendly coins while bumping into enemies. A raycast is like shining a laser pointer in a straight line to see what it hits first, and queryRect() acts like a radar sweep to find all enemies inside a box.

import { CollisionBox, CollisionSets, CollisionSystem } from 'maverick';

const LAYER_PLAYER = 0b001;
const LAYER_ENEMY  = 0b010;

const playerCol = new CollisionSets(LAYER_PLAYER, LAYER_ENEMY);
playerCol.Active = true;
playerCol.onCollisionEnter = (other) => console.log('Hit:', other.Config.name);
playerCol.onCollisionExit = (other) => console.log('Left:', other.Config.name);
playerCol.addBox(new CollisionBox(player, { xOffset: 0, yOffset: 0, width: 24, height: 32, color: '#0f0' }));
player.Config.collisions = playerCol;

const collisions = new CollisionSystem(64);
// Each fixed step:
collisions.step(this.Elements.All);

// Raycast & area queries:
const hit = collisions.raycast(this.Elements.All, 100, 100, 1, 0, 300, LAYER_ENEMY);
const nearby = collisions.queryRect(this.Elements.All, { x: 80, y: 80, width: 64, height: 64 }, LAYER_ENEMY);
Live Proof of Concept — InputManager, CollisionSystem & raycast()
Use WASD / Arrow Keys (or buttons above) to move the blue player box. Space fires a raycast right.