Intermediate • Lesson 4

Camera2D, TileMaps & 2.5D Isometric

Most games are bigger than a single screen. You need a camera that follows your hero, a fast way to build large maps out of reusable floor tiles, an angled "2.5D" perspective (like classic strategy RPGs), and smart navigation so characters walk around walls instead of getting stuck.

Without Camera, TileMaps & Pathfinding

Without a camera, you'd have to subtract the player's position from every tree, rock, and enemy by hand. Drawing 1,000 floor tiles one-by-one every frame makes browsers lag, doing diamond-grid 2.5D math from scratch is notoriously confusing, and characters walking in straight lines get trapped on every wall corner.

Why Maverick Built These Tools

Camera2D moves the whole world like a real movie camera (with smooth follow and zoom). TileMap pre-paints chunks of tiles for 60 FPS speed. worldToIso/isoToWorld handle the 2.5D diamond math, and GridPathfinder acts like a GPS around obstacles.

1. World Camera (Camera2D)

Think of Viewport.beginCamera(cam) and endCamera() like filming with a drone: everything drawn between them moves, zooms, and shakes with the camera (terrain, heroes, trees). Everything drawn after endCamera() (like your health bar or pause button) stays glued to the glass of your screen.

import { Camera2D } from 'maverick';

const cam = new Camera2D({
  viewportWidth: 480,
  viewportHeight: 320,
  zoom: 1.5,
  bounds: { x: 0, y: 0, width: 1600, height: 1200 },
  followLerp: 0.15,
});
cam.followTarget = player;

// In onUpdate(dt):
cam.update(dt);

// In onDraw(dt):
this.Viewport.clear();
this.Viewport.beginCamera(cam);
map.draw(this.Viewport, this.Sprites, cam);
this.Elements.redrawSorted(this, dt);
this.Viewport.endCamera();
this.UI.draw(this.Viewport);

2. Orthogonal & Isometric TileMap

Instead of painting one giant 4000×4000 background image, a TileMap builds worlds out of small Lego-like grid tiles (grass, stone, water). Orthogonal is a flat top-down grid (like classic Zelda), while Isometric (2.5D) tilts the grid into diamonds to create an illusion of 3D depth. Maverick caches offscreen chunks so huge maps still run smoothly.

import { TileMap } from 'maverick';

const map = new TileMap({
  columns: 32,
  rows: 32,
  tileWidth: 64,
  tileHeight: 32,
  orientation: 'isometric',
  tilePalette: { '1': 'grassTile', '2': 'stoneTile' },
});
map.addLayer({ name: 'ground', data: new Array(32 * 32).fill(1) });
map.setTile('ground', 4, 5, 2);

3. 2.5D Isometric Helpers & A* Pathfinding

When a player clicks a tilted diamond tile on screen, isoToWorld() translates that mouse click back into a simple grid row & column. Then GridPathfinder.findPath() uses the A* ("A-star") algorithm—just like Google Maps finding the shortest route around closed roads—to steer your character around stone obstacles.

import { GridPathfinder, isoToWorld, worldToIso } from 'maverick';

const screenPos = worldToIso(3, 2, 16, { tileWidth: 64, tileHeight: 32 });
const gridPos = isoToWorld(screenPos.x, screenPos.y, { tileWidth: 64, tileHeight: 32 });

const path = GridPathfinder.findPath(
  { x: 0, y: 0 },
  { x: 10, y: 8 },
  {
    columns: 32,
    rows: 32,
    allowDiagonal: true,
    isWalkable: (x, y) => map.getTile('ground', x, y) !== 2,
  }
);
Live Proof of Concept — 2.5D Isometric TileMap, Camera2D & A* Pathfinding
Click any green isometric tile to compute an A* path around stone obstacles.