A tile-based game engine for prototyping abstract games.
Download the game template Spielmatrix.zip and unzip it somewhere on your hard drive.
The template contains a game.js file like this one that you can modify to create your game.
Creating a Spielmatrix instance:
var sm = new Spielmatrix(options);
'options' is an object which specifies how the Spielmatrix instance will look and behave.
This is done for you in the template.
You can add attributes to the node that the game runs in to override certain basic options.
Sets the maximum width of the drawing area, in pixels. Overrides the "renderWidthMax" option. Defaults to 480.
data-width-max="480"
Sets the minimum width of the drawing area, in pixels. Overrides the "renderWidthMin" option. Defaults to 240.
data-width-min="240"
Changes the behavior of how the rendering area is allowed to resize. Overrides the "pixelPerfect" option. If true, the tiles are forced to render at an integer multiple of their base size (8px). If false, the tiles can be any integer size. Defaults to true.
data-pixel-perfect="true"
If set to true, the canvas is allowed to dynamically resize. Otherwise, it is set to the renderWidthMax initially, and does not change. Overrides the "resizeCanvas" option. Defaults to true.
data-resize-canvas="true"
<div id="test" data-width-max="1024" data-width-min="320" data-pixel-perfect="true" data-resize-canvas="true"></div>
You can register event methods which are called whenever something of interest happens in the engine.
Called every rendering frame. The amount of time that passed is sent in the variable data.time. Measured in seconds.
update : function(data) {
SM.log("Delta time: " + data.time);
}
A key was pressed.
keydown : function(key) {
SM.log("Key down: " + key);
}
A key was released.
keyup : function(key) {
SM.log("Key up: " + key);
}
The mouse button was pressed.
mousedown : function(x, y) {
SM.log("Mouse down: " + x + ", " + y);
}
The mouse button was released.
mouseup : function(x, y) {
SM.log("Mouse up: " + x + ", " + y);
}
The mouse moved from one tile to another.
mousemove : function(oldX, oldY, x, y) {
SM.log("Mouse moved: " + oldX + ", " + oldY + " " + x + ", " + y);
}
The mouse entered a tile.
mouseenter : function(x, y) {
SM.log("Mouse entered: " + x + ", " + y);
}
The mouse left a tile.
mouseleave : function(x, y) {
SM.log("Mouse left: " + x + ", " + y);
}
The mouse entered the tile area.
mouseover : function(x, y) {
SM.log("Mouse over: " + x + ", " + y);
}
The mouse left the tile area.
mouseout : function(x, y) {
SM.log("Mouse out: " + x + ", " + y);
}
Sets the color of the tile at x, y
SM.color(x, y, 0x229922);
Changes the glyph of the tile at x, y.
SM.glyph(x, y, 0x40);
Changes the color of the glyph of the tile at x, y.
SM.glyphColor(x, y, color);
Changes the stored data of the tile at x, y.
SM.data(x, y, {temperature:72});
Sets multiple properties of the tile at x, y.
SM.set(x, y, {color:color, glyph:glyph, glyphColor:glyphColor, data:data});
Returns a reference to the tile at x, y.
var tile = SM.tile(2, 4);
Returns an integer between lo and hi, inclusive.
var result = SM.randBetween(1, 20);
Logs a message to the debug console.
SM.log("Hello");
Plays the specified sound file. The following sounds are built into the engine:
'Appear', 'Blip1', 'Blip2', 'Blip3', 'Blip4', 'Coin1', 'Coin2', 'Coin3', 'Explosion1', 'Explosion2', 'Explosion3', 'Export', 'Hit1', 'Hit2', 'Jump1', 'Jump2', 'Laser1', 'Laser2', 'Laser3', 'Modem', 'Powerup1', 'Powerup2', 'Powerup3', 'UhOh1', 'UhOh2', 'UhOh3', 'Yarp'
It's recommended to use the 'preloadAudio' option for any sounds that you plan on using in your game.
Changes the color of the tile.
tile.setColor(0x336699);
Changes the color of the glyph on the tile.
tile.setColorGlyph(0x336699);
Changes the glyph on the tile.
tile.setGlyph(0x40);
Changes the stored data in the tile.
tile.setData({type:"Tree"});
Sets multiple properties of the tile.
tile.set({color:0x000000, glyph:0x26, glyphColor:0xFF0000, data:"A red &"});
The color of a tile.
var color = tile.color;
The glyph of a tile.
var glyph = tile.glyph;
The color of the glyph of a tile.
var glyphColor = tile.glyphColor;
The data property of a tile.
var data = tile.data;
You can get a reference to the powerful Tile Selector by invoking the selector() method.
The selector, when invoked, will return a list of tiles that match your query, or every tile in your game if no query is passed.
// Get a reference to the tile selector.
var S = SM.selector();
// Return a list of every tile in your game.
var tiles = S();
The selector can take as an argument either a predicate function, an object describing which properties you are looking for, or a pair of coordinates.
By default, if you don't pass any arguments, it will return a list of every tile in your game.
// Returns every tile in column x=2
var tilesCol = S({x:2});
// Returns every tile in row y=3
var tilesRow = S(function(tile) {
return tile.y === 3;
});
// Returns every tile
var allTiles = S();
All of the examples below assume that you have a variable S that is set to the tile selector.
Lists of tiles can have operations performed on them to modify the tiles themselves.
Sets the color of every tile in the list.
// Set every tile to red.
S().color(0xFF0000);
Sets the glyph of every tile in the list.
// Set every tile to show '@'.
S().glyph(0x40);
Sets the glyphColor of every tile in the list.
// Set every tile's glyph color to dark green.
S().glyphColor(0x005500);
Sets the data of every tile in the list.
// Set every tile have the data "Hello".
S().data("Hello");
Sets everything at once on every tile in the list.
// Set all of the properties of every tile in the list.
S().set({color:0x000000, glyph:0x86, glyphColor:0x333333, data:"Dark"});
Invokes the function once per tile in the list, passing in tile as the only parameter.
// Increments the data of every tile that has numerical data
S().exec(function(tile) {
if (typeof tile.data === 'number') {
tile.data += 1;
}
});
Selects some tile from the list, randomly. If count is omitted, select one tile.
// Chooses 5 tiles and makes them landmines
S().rand(5).set({data:"Landmine"});
Filter the list to only tiles that satisfy the properties or predicate that you pass in.
Similar to the main selector query, except that passing in nothing at all will result in an empty list.
var tilesX3 = S().where({x:3});
var tilesYGreater2 = S().where(function(tile){ return tile.y > 2; });
Filter the list to exclude tiles that satisfy the properties or predicate that you pass in.
var tilesThatAreNotBlack = S().not({color:0x000000});
var tilesThatAreNotOnTheDiagonal = S().not(function(tile) { return tile.x === tile.y; });