This is the manual for one of the add-ons that come with Game Palette. The core library is described in the core manual, and the other add-on in the LinkedSprite manual.
A checkerboard map is a gameboard made of tiles: a grid of rows and columns of squares (or rectangles), side by side like the squares of a checkerboard, each showing one kind of tile. It's the usual way to build a maze, a board game, or the ground, walls, and water of a level seen from above, from a handful of small pictures used over and over.
The add-on is in the file checkerboardmap.js. Load
it after gamepalette.js (see
Using an add-on):
<script type="text/javascript"
src="gamepalette.js"></script>
<script type="text/javascript"
src="checkerboardmap.js"></script>
It adds two things to GAMEPALETTE, which are used
together:
GAMEPALETTE.CheckerboardMap:
you make one of these for each map your game needs. It holds
the picture of the tiles, a description of each kind of tile, and
which kind of tile goes in each square of the map.GAMEPALETTE.checkerboardTileRenderer:
a sprite renderer
that draws a CheckerboardMap. Give it to a sprite, with the map
as the sprite's
rendererParameter,
and the sprite shows the map. The map then moves, turns, scales,
and fades with the sprite, like any other.Your game can check that the add-on is loaded with
GAMEPALETTE.hasAddOn("CheckerboardMap") (see
hasAddOn).
The Checkerboard map demo shows a map of grass, rocks, and water, some of it animated, with a walker to steer about it. The walker can go only on the grass, which the demo finds out from the tiles' descriptions. The Camera demo uses a map for its ground, too. The rest of this manual follows the steps of making a map, as the Checkerboard map demo does.
Begin by designing the tiles. There's no practical limit to how many kinds of tile (tile types) a map can have, and each can be animated with as many frames as you like. All the tiles must be the same size, though, and they are usually square.
All the pictures of all the tiles go in one picture file, the tileset image. Each tile type has a row of its own, and the tile types are numbered from 0, from the top: tile type 0 is the top row, tile type 1 the next row down, and so on. Along its row, a tile type has its animation frames, from left to right, starting at the left edge. A tile type that isn't animated has just the one picture, at the left. There are no gaps between the tiles.
+----+----+----+
| t0 | t0 | t0 |
| f0 | f1 | f2 |
+----+----+----+
| t1 |
| f0 |
+----+----+----+----+----+----+
| t2 | t2 | t2 | t2 | t2 | t2 |
| f0 | f1 | f2 | f3 | f4 | f5 |
+----+----+----+----+----+----+
| t3 | t3 |
| f0 | f1 |
+----+----+
Here, t is the tile type and f the frame. Tile type 0 has
3 frames, tile type 1 has just 1, tile type 2 has 6, and tile
type 3 has 2. The picture is as wide as the longest row, and the
space left over at the ends of the shorter rows isn't used, so it
can be left blank. The demo's
tileset.png
is laid out this way, with tiles of 100 by 100 pixels.
Load the tileset image as a
GAMEPALETTE.Image,
as you would any other. The map cuts it into tiles itself, from
the tile size you give the map, so the image's frame size and
frame count aren't used and can be 0 and 1. What the map does use
is the image's
frameRate:
it is the speed of the tiles' animations, unless you give the map
a frameRate of its
own. The demo animates its tiles at 15 frames a second:
s.tileImage = new GAMEPALETTE.Image(s.engine, "tileset.png",
0, 0, 1, 15);
The image loads like any other, counting toward the engine's
loadProgress,
and the map draws nothing until it has loaded.
Next, describe each tile type with an object. These go in an array, the tileset, in the order of the rows of the tileset image: the first object is tile type 0, the second tile type 1, and so on. The add-on looks for two properties on each object, both of which can be left out:
frames: how many animation frames the tile type
has, an integer greater than 0. The default is 1, a tile that
isn't animated.randomizeStartFrame: true to start
each tile of this type at a random point in its animation, so a
field of tiles of the same type (rippling water, say) doesn't all
move in step and look so obviously repeated; false,
the default, to keep them all in step, as you would for a
blinking light that should flash all together.Anything else on the objects is the game's own: whatever your game needs to know about each tile type, such as whether it can be walked on, whether it is deadly, or how fast the player can cross it. The add-on keeps the objects as they are, and leaves anything else on them alone. For the tileset image above, the tileset might be:
let tilesetData = [
{frames: 3, passable: true, speed: 6}, // tile type 0
{passable: false, deadly: true}, // tile type 1
{frames: 6, randomizeStartFrame: true,
passable: true, speed: 10}, // tile type 2
{frames: 2, randomizeStartFrame: true,
passable: true, speed: 2} // tile type 3
];
The demo keeps its tileset in a file of its own,
tileset.js,
and gives each tile type a passable property, to say
whether the walker can go on it.
The map itself is an array of tile type numbers, one for each square, row by row: first the squares of the top row, from left to right, then the squares of the second row, from left to right, and so on. A map width squares wide and height squares tall is an array of width × height numbers, and the square in column c and row r (both counted from 0) is element r × width + c. Each number must be a tile type in the tileset. Laying the array out in rows in your code makes the map easy to see. A map 6 squares wide and 4 tall might be:
let mapData = [0, 0, 3, 0, 2, 1,
1, 1, 0, 0, 3, 2,
0, 0, 3, 3, 2, 2,
1, 0, 2, 2, 0, 0];
The demo's map, 15 squares by 15, is in
map.js.
A large map is easier to make with a drawing program or a tile map
editor, and then turned into an array, than written out by
hand.
Once the tileset image has loaded, make the
GAMEPALETTE.CheckerboardMap,
giving it the tileset image, the size of a tile in pixels, the
tileset, and the map with its width and height in squares:
s.gameMap = new GAMEPALETTE.CheckerboardMap(s.engine,
s.tileImage, 100, 100, tilesetData, 6, 4, mapData);
Then make a sprite to show it, with
GAMEPALETTE.checkerboardTileRenderer
as its renderer and the map as its renderer parameter, and make the
sprite active:
s.mapSprite = new GAMEPALETTE.Sprite(s.engine, null, 0, 0, 0,
GAMEPALETTE.checkerboardTileRenderer, s.gameMap);
s.mapSprite.isActive = true;
With the sprite at (0, 0), as here, the top-left corner of the
map is at the game world's origin, and the square in column
c and row r covers x from c × 100 to
c × 100 + 100, and y likewise. That makes the squares
easy to find (see the next chapter), so it's the usual choice. The
sprite's position moves the whole map, and its
z decides what
is drawn in front of it: give the map a larger z than the sprites
that walk on it.
The point of the map that sits at the sprite's position is set
by the map's handleX and
handleY, in pixels from
the map's top-left corner, much as an image's handle is. They are 0
unless you give them, which puts the map's corner at the sprite's
position. Set them to half the map's size to put the middle of the
map there instead, so that it turns and scales about its
middle.
The sprite doesn't need an image of its own, so the example
gives it null. If it does have one (the demo gives it
the tileset image), the renderer doesn't draw it, but the image's
handle still
moves the map, as it would any sprite's picture (see
Writing a
renderer). So give such an image a handle of (0, 0), and
place the map with handleX and handleY
instead.
Because the map is drawn by a sprite, it does all that a sprite
does. The camera scrolls over it, and can follow a player about it
while keeping within its edges (see
autoCameraSprite
and the limits after it; the demo does this). It can be turned
with the sprite's
rotation,
scaled with its
scaleX and
scaleY, faded with its
alpha, and
with the engine's
perspectiveView
on, a map at a large z is a distant backdrop that scrolls slowly.
Several sprites can show the same map: a second sprite, small and
in a corner of the screen, makes a mini-map.
Most games need to know what is under a sprite: whether the
player can step there, or has fallen in the water. The add-on
doesn't yet have functions for this, but it's simple to work out
from the map's
tileWidth and
tileHeight. For a
map whose sprite is at (0, 0), not turned or scaled, with its
handle at (0, 0), the square under the game point (x, y) is in
column Math.floor(x / tileWidth) and row
Math.floor(y / tileHeight). Then look up the tile
type in the map, and its description in the tileset:
// The description of the tile type under the game point (x, y),
// or null if the point is off the map
function tileAt(map, x, y)
{
let column = Math.floor(x / map.tileWidth);
let row = Math.floor(y / map.tileHeight);
if(column < 0 || column >= map.mapWidth ||
row < 0 || row >= map.mapHeight)
return null;
let tileType = mapData[(row * map.mapWidth) + column];
return map.tileset[tileType];
}
let tile = tileAt(s.gameMap, s.walker.x, s.walker.y);
if(tile === null || tile.passable == false)
... // the walker can't be here
This looks the tile type up in the game's own copy of the map,
mapData, as the demo does, rather than in the map's
map property: that gives a
fresh copy of the whole map each time it's read, which is fine now
and then, but slow for every step of every sprite. If the game
changes the map, it keeps its own copy up to date (see
Changing the map).
If the map's sprite is somewhere else, subtract the sprite's position from (x, y) and add the map's handle first. If the map is turned or scaled, turn and scale the point the opposite way about the sprite's position as well. The demo checks the four corners of a square round the walker, so that no part of him overlaps a rock or the water.
To find the tile under the mouse pointer, first turn the
pointer's position on the canvas into a game point with the
engine's
canvasToWorld,
giving it the map sprite's z.
Everything about a map can be changed while the game runs, and the next frame drawn shows the change.
setMap, with its width
and height, which may be different from before. The map keeps a
copy of the array, so changing your array afterwards doesn't
change the map until you call setMap again. For
example, to turn the square in column 3 and row 2 into tile
type 1, a door opening, say:
mapData[(2 * s.gameMap.mapWidth) + 3] = 1;
s.gameMap.setMap(s.gameMap.mapWidth, s.gameMap.mapHeight, mapData);
Each setMap also starts every tile's animation
afresh, choosing new random starting points for the tile types
with randomizeStartFrame.frames or
randomizeStartFrame, give the map a new tileset
array with its
tileset property,
rather than changing the objects in the one it has: the map
checks a new tileset and starts the animations afresh, but
doesn't notice changes made inside the old one. The game's own
properties on the tileset objects, though, can be changed in
place at any time, since only the game uses them.tilesetImage
property; and to change the tiles' size, set
tileWidth and
tileHeight.frameRate.A whole new level can also be a whole new CheckerboardMap,
given to the sprite as its
rendererParameter.
A map can be as large as you like, since only the tiles that are on the canvas are drawn, so a large map costs no more to draw each frame than a small one.
Drawing a map well is harder than it looks. If each tile were drawn straight onto the canvas, thin seams would show between the tiles whenever the map sat part-way between pixels, as it does while it scrolls smoothly, or was turned or scaled: where two tiles each half cover a pixel, together they don't quite cover it, and whatever is behind shows through. Smoothing a tile as it is scaled would also blend in pixels from beyond its edge in the tileset image, which belong to its neighbor there.
So the renderer first puts the tiles on the canvas together into one picture of its own, where they meet exactly, and then draws that picture on the canvas in one piece, at the map's exact place. Only the outside edges of the map are blended into what is behind it. The renderer keeps the picture, and makes it again only when what it shows changes: when the view moves far enough to bring more tiles in (it covers a few tiles more than are on the canvas, so not every time), when an animated tile on the canvas moves on to its next frame, or when the map changes. When the map is drawn smaller than its full size, the picture is made smaller to match, so shrinking the map smoothly (zooming out) makes it again each frame.
The engine's
smoothing
setting is followed, so with it off, pixel-art tiles stay crisp
when the map is zoomed in.
Tile animations follow the same clock as sprite animations, the
engine's
animationTime.
So they run at their frame rate in real time, skipping frames if the
game falls behind, and stop while the engine is paused. Showing the
same map with several sprites doesn't speed them up.
A CheckerboardMap can be kept in the state object and saved with
the engine's
saveState,
like anything else. It comes back from
loadState
with its tiles, its tileset (with the game's own properties on it),
its settings, and even each tile's place in its animation. Its
tileset image is matched to the game's own image of the same
picture file, as for any image. The sprite showing it saves its
renderer by name, as usual. The pictures the renderer keeps
for drawing aren't saved; they are simply made again.
This is the full description of the add-on's class and renderer. For the classes they work with, see the core manual's reference.
A CheckerboardMap is a grid of tiles: the tileset image that
holds their pictures, the tileset that describes each tile type,
and the map of which tile type is in each square. Show it with a
sprite whose renderer is
GAMEPALETTE.checkerboardTileRenderer.
new GAMEPALETTE.CheckerboardMap(engine, tilesetImage,
tileWidth, tileHeight, tileset,
mapWidth, mapHeight, map,
handleX = 0, handleY = 0,
frameRate = null)
Creates a map for engine (your
GAMEPALETTE.Engine).
tilesetImage is the
GAMEPALETTE.Image holding the
tiles' pictures, each tileWidth by
tileHeight pixels (see
The tileset image), and
tileset is the array describing each tile type (see
Describing the tiles). map is
the array of the tile types in the map's squares, row by row, and
mapWidth and mapHeight are its size in
squares (see Laying out the map). The other
arguments set the properties of the same names.
The arguments are checked as the properties and
setMap check them, and
an unacceptable one throws a TypeError or
RangeError, as does an engine that isn't
a GAMEPALETTE.Engine. The tileset image needn't have loaded yet,
but the map can't be seen until it has.
s.gameMap = new GAMEPALETTE.CheckerboardMap(s.engine,
s.tileImage, TILE_SIZE, TILE_SIZE, tilesetData,
MAP_SIZE, MAP_SIZE, mapData);
Setting a property to a value of the wrong type throws
a TypeError, and to a value out of range
a RangeError; in either case the property keeps its
old value. Changes show from the next frame drawn.
tilesetImage passed to
the constructor
The image holding the pictures of all the tiles: one row for
each tile type, from tile type 0 at the top, with its animation
frames from left to right (see
The tileset image). Its frame size,
frame count, and handle aren't used; its
frameRate
is the tiles' animation speed, unless the map's own
frameRate is set. A
tile or frame that would be beyond the edge of the picture is
drawn as nothing, leaving that square see-through. Setting
anything other than a GAMEPALETTE.Image throws a
TypeError.
tileWidth and
tileHeight passed to the constructor
The size of one tile, both in the tileset image and in the game
world, before the sprite's scale. The map is
mapWidth ×
tileWidth pixels wide and
mapHeight ×
tileHeight pixels tall. A value that isn't an integer
throws a TypeError, and one that isn't greater than 0
a RangeError.
tileset passed to the
constructor
The descriptions of the tile types, one object each, in order
from tile type 0 (see Describing the
tiles). On each, frames, if given, must be an
integer greater than 0 (the default is 1), and
randomizeStartFrame, if given, a boolean (the default
is false). Any other properties are the game's own,
and are left alone.
Reading it gives the map's own array, not a copy, so the game can
read and change its own properties on the objects, as in
s.gameMap.tileset[2].passable = false. To change
frames or randomizeStartFrame, set a new
array instead, since changes made inside the array aren't checked
or noticed. Setting a new tileset starts every tile's animation
afresh.
A value that isn't a non-empty array, or that has a member that
isn't an object, or a frames or
randomizeStartFrame of the wrong type, throws a
TypeError, and a frames that isn't
greater than 0 a RangeError. So does a tileset with
fewer tile types than the map uses: the new tileset must have a
tile type for every number in the
map.
mapWidth and
mapHeight passed to the constructor
The size of the map, in squares: how many columns and how many
rows it has. They change only with the map, through
setMap.
map passed to
the constructor
The tile type in each square of the map, row by row from the
top, each row from left to right: the square in column c and
row r is element r ×
mapWidth +
c. Reading it gives a new copy of the whole array each
time, so changing that copy doesn't change the map, and in a
function that runs often, such as checking what's under each
sprite each step, it's quicker to keep a copy of your own (see
Finding the tile at a point). Change the
map with setMap.
handleX and
handleY passed to the constructor, or 0
The point of the map, in pixels from its top-left corner, that
is placed at the position of the sprite showing it, and that the
map turns and scales about. At (0, 0), the map's top-left corner
is at the sprite's position. Values beyond the map, or negative,
are fine. A value that isn't a finite number throws a
TypeError.
// Center the map on its sprite, so that it spins about its middle
s.gameMap.handleX = s.gameMap.mapWidth * s.gameMap.tileWidth / 2;
s.gameMap.handleY = s.gameMap.mapHeight * s.gameMap.tileHeight / 2;
frameRate passed to the
constructor, or null
How fast the tiles' animations play, in frames per second, all
at the same rate. Set it to null to use the
frameRate
of the tileset image. Reading
it always gives the rate in use, so it gives the tileset image's
rate when it was set to null. A value that is neither
null nor a finite number throws a
TypeError, and one that isn't greater than 0 a
RangeError.
Replaces the whole map with map, an array of
mapWidth × mapHeight tile types, row
by row (see Laying out the map). The new map
may be a different size from the old one. The map keeps a copy of
the array, so later changes to the array you pass don't affect the
map. Every tile's animation starts afresh, with new random
starting points for the tile types with
randomizeStartFrame. See
Changing the map.
It throws a RangeError, and leaves the map as it
was, if mapWidth or mapHeight isn't an
integer greater than 0, if map isn't an array of
exactly mapWidth × mapHeight
elements, or if any element isn't the number of a tile type in the
tileset.
// Open the door in column 3, row 2
mapData[(2 * s.gameMap.mapWidth) + 3] = DOOR_OPEN;
s.gameMap.setMap(s.gameMap.mapWidth, s.gameMap.mapHeight, mapData);
The sprite
renderer that draws a CheckerboardMap. Make it a sprite's
renderer,
and the map to draw the sprite's
rendererParameter:
s.mapSprite = new GAMEPALETTE.Sprite(s.engine, null, 0, 0, 0,
GAMEPALETTE.checkerboardTileRenderer, s.gameMap);
The map's point (handleX,
handleY) is drawn at the
sprite's position (moved, as for any renderer, by the handle of the
sprite's own image, if it has one; see
Showing the map), and the map is turned,
scaled, and faded with the sprite. The sprite's image itself isn't
drawn. Only the tiles on the canvas are drawn, together, so that
they meet without seams (see How the map is
drawn), following the engine's
smoothing.
Animated tiles play at the map's
frameRate, by the
engine's
animationTime.
It draws nothing if the sprite's renderer parameter isn't a GAMEPALETTE.CheckerboardMap, or until the map's tileset image has loaded.