Module cocos.tiles

Tile maps are made of three components:

images
Images are from individual files for from image atlases in a single file.
tiles
Tiles may be stand-alone or part of a TileSet.
maps
MapLayers are made of either rectangular or hexagonal Cells which reference the tiles used to draw the cell.

The intent is that you may have a tile set defined in one XML file which is shared amongst many map files (or indeed within a single XML file). Images may be shared amongst multiple tiles with the tiles adding different meta-data in each case.

These may be constructed manually or loaded from XML resource files which are used to store the specification of tile sets and tile maps. A given resource XML file may define multiple tile sets and maps and may even reference other external XML resource files. This would allow a single tile set to be used by multiple tile maps.

Tile Maps

The RectMapLayer class extends the regular Cocos layer to handle a regular grid of tile images, or Cells. The map layer may be manipulated like any other Cocos layer. It also provides methods for interrogating the map contents to find cells (eg. under the mouse) or neighboring cells (up, down, left, right.)

Maps may be defined in Python code, but it is a little easier to do so in XML. To support map editing the maps support round-tripping to XML.

The XML File Specification

Assuming the following XML file called "example.xml":

<?xml version="1.0"?>
<resource>
  <require file="ground-tiles.xml" namespace="ground" />

  <rectmap id="level1">
   <column>
    <cell>
      <tile ref="ground:grass" />
    </cell>
    <cell>
      <tile ref="ground:house" />
      <property type="bool" name="secretobjective" value="True" />
    </cell>
   </column>
  </map>
</resource>

You may load that resource and examine it:

>>> r = load('example.xml')
>>> r['level1']

and then, assuming that level1 is a map:

>>> scene = cocos.scene.Scene(r['level1'])

if you wish for the level to scroll, you use the ScrollingManager:

>>> manager = tiles.ScrollingManager()
>>> manager.append(r['level1']

and later set the focus with:

>>> manager.set_focus(focus_x, focus_y)

Typically the focus is set to the center of the player's sprite. The focusing will honor any tile map boundaries and prevent area outside those boundaries from being displayed.

If you are only scrolling layers with sprites in them (ie. regular Cocos Layers, not RectMapLayers) then there are no boundaries and the map will scroll without bound in any direction.

If you wish you may force the focus to display areas outside your tile maps you may use the force_focus method of ScrollingManager.

XML file contents

XML resource files must contain a document-level tag <resource>:

<?xml version="1.0"?>
<resource>
 ...
</resource>

You may draw in other resource files by using the <require> tag:

<require file="road-tiles.xml" />

This will load "road-tiles.xml" into the resource's namespace. If you wish to avoid id clashes you may supply a namespace:

<require file="road-tiles.xml" namespace="road" />

If a namespace is given then the element ids from the "road-tiles.xml" will be prefixed by the namespace and a period, e.g. "road.bitumen".

Other tags within <resource> are:

<image file="" id="">
Loads file with pyglet.image.load and gives it the id which is used by tiles to reference the image.
<imageatlas file="" [id="" size="x,y"]>

Sets up an image atlas for child <image> tags to use. Child tags are of the form:

<image offset="" id="" [size=""]>

If the <imageatlas> tag does not provide a size attribute then all child <image> tags must provide one. Image atlas ids are optional as they are currently not reference directly.

<tileset id="">

Sets up a TileSet object. Child tags are of the form:

<tile id="">
[<image ...>]

</tile>

The <image> tag is optional; these tiles may have only properties (or be completely empty). The id is used by maps to reference the tile.

<rectmap id="" tile_size="" [origin=""]>

Sets up a RectMap object. Child tags are of the form:

<column>
<cell tile="" />

</column>

Most tags may additionally have properties specified as:

<property [type=""] name="" value="" />

Where type is one of "unicode", "int", "float" or "bool". The property will be a unicode string by default if no type is specified. The properties are loaded into a dictionary called ".properties" on the appropriate Image, Tile, Map and Cell instance.

Classes

  ResourceError
  Resource
Load some tile mapping resources from an XML file.
  Tile
Tiles hold an image and some optional properties.
  TileSet
Contains a set of Tile objects referenced by some id.
  ScrollableLayer
A Cocos Layer that is scrollable in a Scene.
  MapLayer
Base class for Maps.
  RegularTesselationMapLayer
A class of MapLayer that has a regular array of Cells.
  RectMapLayer
Rectangular map.
  Cell
Base class for cells from rect and hex maps.
  RectCell
A rectangular cell from a MapLayer.
  HexMapLayer
MapLayer with flat-top, regular hexagonal cells.
  HexCell
A flat-top, regular hexagon cell from a HexMap.
  ScrollingManager
Manages scrolling of Layers in a Cocos Scene.

Functions

  load(filename, paths=None)
Load resource(s) defined in the indicated XML file.
  image_factory(resource, tag)
  imageatlas_factory(resource, tag)
  tileset_factory(resource, tag)
  rectmap_factory(resource, tag)
  hexmap_factory(resource, tag)
  hex_width(height)
Determine a regular hexagon's width given its height.

Function Details

image_factory

image_factory(resource, tag)
Decorators:
  • @Resource.register_factory('image')

imageatlas_factory

imageatlas_factory(resource, tag)
Decorators:
  • @Resource.register_factory('imageatlas')

tileset_factory

tileset_factory(resource, tag)
Decorators:
  • @Resource.register_factory('tileset')

rectmap_factory

rectmap_factory(resource, tag)
Decorators:
  • @Resource.register_factory('rectmap')

hexmap_factory

hexmap_factory(resource, tag)
Decorators:
  • @Resource.register_factory('hexmap')