Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tilemaps allow for easy level construction and fast collisions. You can even use them for some games instead of entities (tetris comes to mind)

Direction constants used for autotile

const (
	top*   = 1
	right* = 2
	bot*   = 4
	left*  = 8
)

struct Tilemap

type Tilemap* = struct {
	atlas: atlas::Atlas
	pos: th::Vf2
	w: th::uu // width of tilemap
	cells: []th::uu // all cells (this will draw the tile in tiles with number in cells - 1)
	collMask: []bool // if true, the tile collides
	scale: th::fu
}

Tilemap struct

fn mk

fn mk*(w, h: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {

Make a tilemap with all cells set to 0.

fn mk2

fn mk2*(cells: []th::uu, w: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {

Make a tilemap from a list of cells.

fn Tilemap.has

fn (t: ^Tilemap) has*(x, y: int): bool {

Check if a tile exists at the given coordinates.

fn Tilemap.edit

fn (t: ^Tilemap) edit*(x, y, tile: int){

Edit a tile in the tilemap.

fn Tilemap.get

fn (t: ^Tilemap) get*(x, y: int): int {

Get a tile from the tilemap.

fn Tilemap.draw

fn (t: ^Tilemap) draw*(tr: th::Transform, tint: uint32 = 0xFFFFFFFF) {

Draws the tilemap.

fn Tilemap.getColl

fn (t: ^Tilemap) getColl*(e: ent::Ent, ic: ^th::Vf2, pos: ^th::Vf2): bool {

Checks whether e collides with any of the tiles in t, which are in the collmask.

  • ic[out] - the position where a collision occured
  • pos[out] - coordinates of a tile where a collision occured

Note: While there may be multiple collisions with a tilemap, this function will only return one.

fn Tilemap.getCollLine

fn (t: ^Tilemap) getCollLine*(b, e: th::Vf2, ic: ^th::Vf2): bool {

Check for a collision between a tilemap and a line. ic will be a point of an intersection, if there is one.

fn Tilemap.autotile

fn (t: ^Tilemap) autotile*(src, tileCfg: []th::uu, tile: th::uu) {

Autotile turns all tile tiles in src into tiles in tileCfg, so they follow up correctly. tileCfg is an array of 16 tiles. They are placed in a way where OR of all the places where the tile continues (top, right bot, right). The constants for them are defined in this file. Example: tileCfg[top | bot] = 21 top | bot would look something like this: |