Skip to content

LameMap

A tile engine for LameGFX. It provides functions to display and interact with tile-based game maps.

map : "LameMap"

Demos for this library can be found in the /demos/maps/ folder of the SDK.

About

Game maps consist of three parts: a tile map, a level map, and an optional collision map.

  • A tile map is a 2D array of sprites intended for creating maps, organized so that they can be indexed by a level map. On LameStation, the tile map format is just an ordinary sprite with frames.

  • A collision map mirrors the tile map and determines which tiles can be walked on and which ones can't. On LameStation, a collision map is an image of black and white squares, the same size as the tile map. A black square cannot be walked on, and a white one can.

  • A level map combines the data in the tile map and collision map to build a space that can be navigated as part of the gameplay. The level map format is described in the next section.

Level Map Format

Level Map Data

Map data is raw data describing what an area looks like and how the player can interact with it.

  • width, height (0-65535) - The width and height of the image in tiles.

  • tile - The array of tiles that make up this level, placed left-to-right, top-to-bottom.

DAT
map_data

word    <width>, <height>

byte    <tile>, [<tile>]...

Each tile in a map is one byte, with 1 bit used for collision, and the other 7 used for the tile number. Collision data is stored in the level to save space and so collision can be customized for the map.

Tile 0 is used as a null tile, so the maximum usable tilemap size is 127 tiles.

The collision bit stores whether a tile will register a collision when touched. All collision functions return 0 unless this bit is set.

Level Map Interface

The address of the map data can be passed directly to map.Load, unless stored in another object. If so, the following function is used as an interface.

PUB Addr
    return @map_data

Functions

map.Draw

Draw the current map to the screen.

map.Draw(offset_x, offset_y)
  • offset_x - The horizontal offset from the map origin.

  • offset_y - The vertical offset from the map origin.

The red rectangle is the offset, while the blue rectangle is the size of the visible map portion.

map.DrawRectangle

Draw the current map to a portion of the screen.

map.DrawRectangle(offset_x, offset_y, x1, y1, x2, y2)
  • offset_x - The horizontal offset from the map origin.

  • offset_y - The vertical offset from the map origin.

  • x1 - The left edge of the viewing window.

  • y1 - The top edge of the viewing window.

  • x2 - The right edge of the viewing window (exclusive).

  • y2 - The bottom edge of the viewing window (exclusive).

The bottom and right coordinates are exclusive, which means they specify a rectangle up to, but not including their points.

The red rectangle is the offset, while the blue rectangle is the size of the visible map portion.

map.Height

Return the height of the current map in tiles.

map.Height

You can get the number of horizontal and vertical tiles in your map with map.Width and map.Height.

mapsize := map.Width * map.Height

map.Load

Load a map into LameMap.

map.Load(source_tilemap, source_levelmap)

LameGFX must be started before LameMap can be used. Call map.Load to load a map. Only one map can be loaded at a time.

map.TestCollision

Test if the region has collided with a map tile.

map.TestCollision(x, y, w, h)
  • x - Horizontal position of the rectangle.

  • y - Vertical position of the rectangle.

  • w - Width of the rectangle.

  • h - Height of the rectangle.

LameMap has collision support built in. You can test whether or not collision has occurred with map.TestCollision, passing the rectangle to test.

Info

Only valid map regions are tested for collisions; positions that are off the map area will always return 0.

map.TestMoveX

Apply horizontal movement to an object's position and test if it will collide. Syntax

map.TestMoveX(x, y, w, h, newx)
  • x - Horizontal position of the rectangle.

  • y - Vertical position of the rectangle.

  • w - Width of the rectangle.

  • h - Height of the rectangle.

  • newx - New horizontal position of the rectangle.

Returns a ± integer offset that when added to newx will remove the object under test from collision. A zero value indicates no collision has occurred. About

Knowing whether a collision has happened is often not enough, as you might need to keep the player inside a known region. map.TestMoveX and map.TestMoveY allow you to test if a potential horizontal or vertical movement will cause a collision.

If so, it will return a correction factor needed to remove the object from the collision. In this way, you can build walls that players can slide along and won’t escape from.

Info

Only valid map regions are tested for collisions; positions that are off the map area will always return 0.

Info

The TestMove- functions only check for collisions once at the new location. If the distance is large enough, they may miss potential collisions (e.g. halfway between the old and new locations).

map.TestMoveY

Apply vertical movement to an object's position and test if it will collide.

map.TestMoveY(x, y, w, h, newy)
  • x - Horizontal position of the rectangle.

  • y - Vertical position of the rectangle.

  • w - Width of the rectangle.

  • h - Height of the rectangle.

  • newy - New vertical position of the rectangle.

Returns a ± integer offset that when added to newy will remove the object under test from collision. A zero value indicates no collision has occurred. About

Knowing whether a collision has happened is often not enough, as you might need to keep the player inside a known region. map.TestMoveX and map.TestMoveY allow you to test if a potential horizontal or vertical movement will cause a collision.

If so, it will return a correction factor needed to remove the object from the collision. In this way, you can build walls that players can slide along and won’t escape from.

Info

Only valid map regions are tested for collisions; positions that are off the map area will always return 0.

Info

The TestMove- functions only check for collisions once at the new location. If the distance is large enough, they may miss potential collisions (e.g. halfway between the old and new locations).

map.TestPoint

Test whether a tile in the map is collidable.

map.TestPoint(x, y)
  • x - The horizontal index of the tile.

  • y - The vertical index of the tile.

Returns true if collidable, otherwise false.

LameMap has collision support built in. map.TestPoint returns whether a tile has collision enabled. You can test whether or not collision has occurred with map.TestCollision, passing the rectangle of the object.

Info

Only valid map regions are tested for collisions; positions that are off the map area will always return 0.

map.Width

Return the width of the current map in tiles.

map.Width

You can get the number of horizontal and vertical tiles in your map with map.Width and map.Height.

mapsize := map.Width * map.Height