unicodetiles.js

This file contains the main tile engine namespace.  All coordinates are assumed to be integers - behaviour is undefined if you feed in floats (or anything other) as x and y (or similar) parameters.

Summary
unicodetiles.jsThis file contains the main tile engine namespace.
utContainer namespace.
Constants
Semi-internal constants for ut namespace
TileRepresents a unicode character tile with various attributes.
Functions
TileConstructs a new Tile object.
getCharReturns the character of this tile.
setCharSets the character of this tile.
setColorSets the foreground color of this tile.
setGreySets the foreground color to the given shade (0-255) of grey.
setBackgroundSets the background color of this tile.
resetColorClears the color of this tile / assigns default color.
resetBackgroundClears the background color of this tile.
getColorHexReturns the hexadecimal representation of the color
getBackgroundHexReturns the hexadecimal representation of the background color
getColorRGBReturns the CSS rgb(r,g,b) representation of the color
getBackgroundRGBReturns the CSS rgb(r,g,b) representation of the background color
getColorJSONReturns the JSON representation of the color, i.e.
getBackgroundJSONReturns the JSON representation of the background color, i.e.
copyMakes this tile identical to the one supplied.
cloneReturns a new copy of this tile.
ViewportThe tile engine viewport / window.
Functions
ViewportConstructs a new Viewport object.
updateStyleIf the style of the parent element is modified, this needs to be called.
setRendererSwitch renderer at runtime.
getRendererStringGets the currently used renderer.
putPuts a tile to the given coordinates.
unsafePutPuts a tile to the given coordinates.
putStringCreates a row of tiles with the chars of the given string.
getReturns the tile in the given coordinates.
clearClears the viewport buffer by assigning empty tiles.
renderRenders the buffer as html to the element specified at construction.
EngineThe tile engine itself.
Functions
EngineConstructs a new Engine object.
setTileFuncSets the function to be called with coordinates to fetch each tile.
setMaskFuncSets the function to be called to fetch mask information according to coordinates.
setShaderFuncSets the function to be called to post-process / shade each visible tile.
setWorldSizeTiles outside of the range x = [0,width[; y = [0,height[ are not fetched.
setCacheEnabledEnables or disables the usage of tile cache.
updateUpdates the viewport according to the given player coordinates.

Constants

Semi-internal constants for ut namespace

VERSIONVersion of the library as string.
NULLCHARCharacter used when none is specified otherwise.
CSSCLASSThe CSS class name used for the tile engine element.
NULLTILEThe tile used as placeholder for empty tile.

Tile

Represents a unicode character tile with various attributes.

Summary
Functions
TileConstructs a new Tile object.
getCharReturns the character of this tile.
setCharSets the character of this tile.
setColorSets the foreground color of this tile.
setGreySets the foreground color to the given shade (0-255) of grey.
setBackgroundSets the background color of this tile.
resetColorClears the color of this tile / assigns default color.
resetBackgroundClears the background color of this tile.
getColorHexReturns the hexadecimal representation of the color
getBackgroundHexReturns the hexadecimal representation of the background color
getColorRGBReturns the CSS rgb(r,g,b) representation of the color
getBackgroundRGBReturns the CSS rgb(r,g,b) representation of the background color
getColorJSONReturns the JSON representation of the color, i.e.
getBackgroundJSONReturns the JSON representation of the background color, i.e.
copyMakes this tile identical to the one supplied.
cloneReturns a new copy of this tile.

Functions

Tile

ut.Tile = function(ch,
r,
g,
b,
br,
bg,
bb)

Constructs a new Tile object.

Parameters

cha character to display for this tile
r(optional) red foregorund color component 0-255
g(optional) green foreground color component 0-255
b(optional) blue foreground color component 0-255
br(optional) red background color component 0-255
bg(optional) green background color component 0-255
bb(optional) blue background color component 0-255

getChar

ut.Tile.prototype.getChar = function()

Returns the character of this tile.

setChar

ut.Tile.prototype.setChar = function(ch)

Sets the character of this tile.

setColor

ut.Tile.prototype.setColor = function(r,
g,
b)

Sets the foreground color of this tile.

setGrey

ut.Tile.prototype.setGrey = function(grey)

Sets the foreground color to the given shade (0-255) of grey.

setBackground

ut.Tile.prototype.setBackground = function(r,
g,
b)

Sets the background color of this tile.

resetColor

ut.Tile.prototype.resetColor = function()

Clears the color of this tile / assigns default color.

resetBackground

ut.Tile.prototype.resetBackground = function()

Clears the background color of this tile.

getColorHex

ut.Tile.prototype.getColorHex = function()

Returns the hexadecimal representation of the color

getBackgroundHex

ut.Tile.prototype.getBackgroundHex = function()

Returns the hexadecimal representation of the background color

getColorRGB

ut.Tile.prototype.getColorRGB = function()

Returns the CSS rgb(r,g,b) representation of the color

getBackgroundRGB

ut.Tile.prototype.getBackgroundRGB = function()

Returns the CSS rgb(r,g,b) representation of the background color

getColorJSON

ut.Tile.prototype.getColorJSON = function()

Returns the JSON representation of the color, i.e. object { r, g, b }

getBackgroundJSON

ut.Tile.prototype.getBackgroundJSON = function()

Returns the JSON representation of the background color, i.e. object { r, g, b }

copy

ut.Tile.prototype.copy = function(other)

Makes this tile identical to the one supplied.  Custom properties are not copied.

clone

ut.Tile.prototype.clone = function()

Returns a new copy of this tile.  Custom properties are not cloned.

Viewport

The tile engine viewport / window.  Takes care of initializing a proper renderer.

Summary
Functions
ViewportConstructs a new Viewport object.
updateStyleIf the style of the parent element is modified, this needs to be called.
setRendererSwitch renderer at runtime.
getRendererStringGets the currently used renderer.
putPuts a tile to the given coordinates.
unsafePutPuts a tile to the given coordinates.
putStringCreates a row of tiles with the chars of the given string.
getReturns the tile in the given coordinates.
clearClears the viewport buffer by assigning empty tiles.
renderRenders the buffer as html to the element specified at construction.

Functions

Viewport

ut.Viewport = function(elem,
w,
h,
renderer,
squarify)

Constructs a new Viewport object.  If you wish to display a player character at the center, you should use odd sizes.

Parameters

elemthe DOM element which shall be transformed into the tile engine
w(integer) width in tiles
h(integer) height in tiles
renderer(optional) choose rendering engine, see Viewport.setRenderer, defaults to “auto”.
squarify(optional) set to true to force the tiles square; may break some box drawing

updateStyle

this.updateStyle = function(updateRenderer)

If the style of the parent element is modified, this needs to be called.

setRenderer

this.setRenderer = function(newrenderer)

Switch renderer at runtime.  All methods fallback to “dom” if unsuccesful.  Possible values:

  • ”webgl” - Use WebGL with an HTML5 <canvas> element
  • ”canvas” - Use HTML5 <canvas> element
  • ”dom” - Use regular HTML element manipulation through DOM
  • ”auto” - Use best available, i.e. try the above in order, picking the first that works

getRendererString

ut.Viewport.prototype.getRendererString = function()

Gets the currently used renderer.

Returns

One of “webgl”, “canvas”, “dom”, “”.

put

ut.Viewport.prototype.put = function(tile,
x,
y)

Puts a tile to the given coordinates.  Checks bounds and does nothing if invalid coordinates are given.

Parameters

tilethe tile to put
x(integer) x coordinate
y(integer) y coordinate

unsafePut

ut.Viewport.prototype.unsafePut = function(tile,
x,
y)

Puts a tile to the given coordinates.  Does not check bounds; throws exception if invalid coordinates are given.

Parameters

tilethe tile to put
x(integer) x coordinate
y(integer) y coordinate

putString

ut.Viewport.prototype.putString = function(str,
x,
y,
r,
g,
b,
br,
bg,
bb)

Creates a row of tiles with the chars of the given string.  Wraps to next line if it can’t fit the chars on one line.

Parameters

str(string) the string to put
x(integer) x coordinate (column)
y(integer) y coordinate (row)
r(optional) red foregorund color component 0-255
g(optional) green foreground color component 0-255
b(optional) blue foreground color component 0-255
br(optional) red background color component 0-255
bg(optional) green background color component 0-255
bb(optional) blue background color component 0-255

get

ut.Viewport.prototype.get = function(x,
y)

Returns the tile in the given coordinates.  Checks bounds and returns empty tile if invalid coordinates are given.

Parameters

x(integer) x coordinate
y(integer) y coordinate

Returns

The tile.

clear

ut.Viewport.prototype.clear = function()

Clears the viewport buffer by assigning empty tiles.

render

ut.Viewport.prototype.render = function()

Renders the buffer as html to the element specified at construction.

Engine

The tile engine itself.

Summary
Functions
EngineConstructs a new Engine object.
setTileFuncSets the function to be called with coordinates to fetch each tile.
setMaskFuncSets the function to be called to fetch mask information according to coordinates.
setShaderFuncSets the function to be called to post-process / shade each visible tile.
setWorldSizeTiles outside of the range x = [0,width[; y = [0,height[ are not fetched.
setCacheEnabledEnables or disables the usage of tile cache.
updateUpdates the viewport according to the given player coordinates.

Functions

Engine

ut.Engine = function(vp,
func,
w,
h)

Constructs a new Engine object.  If width or height is given, it will not attempt to fetch tiles outside the boundaries.  In that case 0,0 is assumed as the upper-left corner of the world, but if no width/height is given also negative coords are valid.

Parameters

vpthe Viewport instance to use as the viewport
functhe function used for fetching tiles
w(integer) (optional) world width in tiles
h(integer) (optional) world height in tiles

setTileFunc

ut.Engine.prototype.setTileFunc = function(func,
effect,
duration)

Sets the function to be called with coordinates to fetch each tile.  Optionally can apply a transition effect.  Effects are: “boxin”, “boxout”, “circlein”, “circleout”, “random”

Parameters

funcfunction taking parameters (x, y) and returning an ut.Tile
effect(string) (optional) name of effect to use (see above for legal values)
duration(integer) (optional) how many milliseconds the transition effect should last

setMaskFunc

ut.Engine.prototype.setMaskFunc = function(func)

Sets the function to be called to fetch mask information according to coordinates.  If mask function returns false to some coordinates, then that tile is not rendered.

Parameters

funcfunction taking parameters (x, y) and returning a true if the tile is visible

setShaderFunc

ut.Engine.prototype.setShaderFunc = function(func)

Sets the function to be called to post-process / shade each visible tile.  Shader function is called even if caching is enabled, see Engine.setCacheEnabled.

Parameters

funcfunction taking parameters (tile, x, y) and returning an ut.Tile

setWorldSize

ut.Engine.prototype.setWorldSize = function(width,
height)

Tiles outside of the range x = [0,width[; y = [0,height[ are not fetched.  Set to undefined in order to make the world infinite.

Parameters

width(integer) new world width
height(integer) new world height

setCacheEnabled

ut.Engine.prototype.setCacheEnabled = function(mode)

Enables or disables the usage of tile cache.  This means that extra measures are taken to not call the tile function unnecessarily.  This means that all animating must be done in a shader function, see Engine.setShaderFunc.  Cache is off by default, but should be enabled if the tile function does more computation than a simple array look-up.

Parameters

modetrue to enable, false to disable

update

ut.Engine.prototype.update = function(x,
y)

Updates the viewport according to the given player coordinates.  The algorithm goes as follows:

  • Record the current time
  • For each viewport tile:
  • Check if the tile is visible by testing the mask
  • If not visible, continue to the next tile in the viewport
  • Otherwise, if cache is enabled try to fetch the tile from there
  • Otherwise, call the tile function and check for shader function presence
  • If there is shader function, apply it to the tile, passing the recorded time
  • Put the tile to viewport

Parameters

x(integer) viewport center x coordinate in the tile world
y(integer) viewport center y coordinate in the tile world
ut.Tile = function(ch,
r,
g,
b,
br,
bg,
bb)
Constructs a new Tile object.
ut.Tile.prototype.getChar = function()
Returns the character of this tile.
ut.Tile.prototype.setChar = function(ch)
Sets the character of this tile.
ut.Tile.prototype.setColor = function(r,
g,
b)
Sets the foreground color of this tile.
ut.Tile.prototype.setGrey = function(grey)
Sets the foreground color to the given shade (0-255) of grey.
ut.Tile.prototype.setBackground = function(r,
g,
b)
Sets the background color of this tile.
ut.Tile.prototype.resetColor = function()
Clears the color of this tile / assigns default color.
ut.Tile.prototype.resetBackground = function()
Clears the background color of this tile.
ut.Tile.prototype.getColorHex = function()
Returns the hexadecimal representation of the color
ut.Tile.prototype.getBackgroundHex = function()
Returns the hexadecimal representation of the background color
ut.Tile.prototype.getColorRGB = function()
Returns the CSS rgb(r,g,b) representation of the color
ut.Tile.prototype.getBackgroundRGB = function()
Returns the CSS rgb(r,g,b) representation of the background color
ut.Tile.prototype.getColorJSON = function()
Returns the JSON representation of the color, i.e.
ut.Tile.prototype.getBackgroundJSON = function()
Returns the JSON representation of the background color, i.e.
ut.Tile.prototype.copy = function(other)
Makes this tile identical to the one supplied.
ut.Tile.prototype.clone = function()
Returns a new copy of this tile.
ut.Viewport = function(elem,
w,
h,
renderer,
squarify)
Constructs a new Viewport object.
this.updateStyle = function(updateRenderer)
If the style of the parent element is modified, this needs to be called.
this.setRenderer = function(newrenderer)
Switch renderer at runtime.
ut.Viewport.prototype.getRendererString = function()
Gets the currently used renderer.
ut.Viewport.prototype.put = function(tile,
x,
y)
Puts a tile to the given coordinates.
ut.Viewport.prototype.unsafePut = function(tile,
x,
y)
Puts a tile to the given coordinates.
ut.Viewport.prototype.putString = function(str,
x,
y,
r,
g,
b,
br,
bg,
bb)
Creates a row of tiles with the chars of the given string.
ut.Viewport.prototype.get = function(x,
y)
Returns the tile in the given coordinates.
ut.Viewport.prototype.clear = function()
Clears the viewport buffer by assigning empty tiles.
ut.Viewport.prototype.render = function()
Renders the buffer as html to the element specified at construction.
ut.Engine = function(vp,
func,
w,
h)
Constructs a new Engine object.
ut.Engine.prototype.setTileFunc = function(func,
effect,
duration)
Sets the function to be called with coordinates to fetch each tile.
ut.Engine.prototype.setMaskFunc = function(func)
Sets the function to be called to fetch mask information according to coordinates.
ut.Engine.prototype.setShaderFunc = function(func)
Sets the function to be called to post-process / shade each visible tile.
ut.Engine.prototype.setWorldSize = function(width,
height)
Tiles outside of the range x = [0,width[; y = [0,height[ are not fetched.
ut.Engine.prototype.setCacheEnabled = function(mode)
Enables or disables the usage of tile cache.
ut.Engine.prototype.update = function(x,
y)
Updates the viewport according to the given player coordinates.
The tile engine viewport / window.
Close