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*(cells: []th::uu, w: th::uu, at: atlas::Atlas, scale: th::fu = 1): Tilemap {

fn Tilemap.edit

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

Sets tile at [x, y] to tile.

fn Tilemap.draw

fn (t: ^Tilemap) draw*() {

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: |