Tile map management and rendering.
This module provides an API for loading, saving and rendering a map constructed of image tiles.
Bases: exceptions.Exception
Bases: exceptions.Exception
Bases: exceptions.Exception
Bases: object
Base class for cells from rect and hex maps.
Properties are available through the dictionary interface, ie. if the cell has a property ‘cost’ then you may access it as:
cell[‘cost’]
You may also set properties in this way and use the .get() method to supply a default value.
If the named property does not exist on the cell it will be looked up on the cell’s tile.
Bases: cocos.tiles.Cell
A flat-top, regular hexagon cell from a HexMap.
Note that all pixel attributes are not adjusted for screen, view or layer transformations.
Bases: cocos.tiles.RegularTesselationMap
MapLayer with flat-top, regular hexagonal cells.
Calculated attributes:
edge_length – length of an edge in pixels = int(th / sqrt(3)) tw – with of a “tile” in pixels = edge_length * 2
Hexmaps store their cells in an offset array, column-major with y increasing up, such that a map:
/d\ /h /b\_/f\_/
\_/c\_/g /a\_/e\_/
\_/ \_/
has cells = [[‘a’, ‘b’], [‘c’, ‘d’], [‘e’, ‘f’], [‘g’, ‘h’]]
(and this the cell at (0, 0) is ‘a’ and (1, 1) is ‘d’)
Get the Cell at pixel (x,y).
Return None if out of bounds.
Return cells (in [column][row]) that are within the pixel bounds specified by the bottom-left (x1, y1) and top-right (x2, y2) corners.
returns the grid coordinates for the hex that covers the point (x, y)
Get the neighbor HexCell in the given direction which is one of self.UP, self.DOWN, self.UP_LEFT, self.UP_RIGHT, self.DOWN_LEFT or self.DOWN_RIGHT.
Return None if out of bounds.
Get all neighbor cells for the nominated cell.
Return a dict with the directions (self.UP, self.DOWN, etc) as keys and neighbor cells as values.
Bases: cocos.tiles.HexMap, cocos.tiles.MapLayer
A renderable, scrollable tile map covered by hexagonal tiles
While visually the tiles look hexagonal, the texture that draws each tile is rectangular and should comply:
- depicts an hexagon with upper and lower sides paralel to the x-axis
- area out of the hexagon should be transparent
- tile size must comply width == int(height / sqrt(3) * 2)
Be warned that some hexagonal tilesets found in the net use other proportions or the pointy orientation ( left and right sides paralel to the y-axis) ; neither will work with HexMapLayer
The Layer has a calculated attribute:
edge_length – length of an edge in pixels = int(th / sqrt(3)) tw – with of a “tile” in pixels = edge_length * 2
Hexmaps store their cells in an offset array, column-major with y increasing up, such that a map:
/d\ /h /b\_/f\_/
\_/c\_/g /a\_/e\_/
\_/ \_/
has cells = [[‘a’, ‘b’], [‘c’, ‘d’], [‘e’, ‘f’], [‘g’, ‘h’]]
Bases: cocos.layer.scrolling.ScrollableLayer
Base class for Maps.
Maps are comprised of tiles and can figure out which tiles are required to be rendered on screen.
Both rect and hex maps have the following attributes:
id – identifies the map in XML and Resources (width, height) – size of map in cells (px_width, px_height) – size of map in pixels (tw, th) – size of each cell in pixels (origin_x, origin_y, origin_z) – offset of map top left from origin in pixels cells – array [i][j] of Cell instances debug – display debugging information on cells properties – arbitrary properties
The debug flag turns on textual display of data about each visible cell including its cell index, origin pixel and any properties set on the cell.
Find all cells that match the properties specified.
For example:
map.find_cells(player_start=True)
Return a list of Cell instances.
Given the current view in map-space pixels, transform it based on the current screen-space transform and figure the region of map-space pixels currently visible.
Pass to get_in_region to return a list of Cell instances.
Determine whether the indicated rect (with .x, .y, .width and .height attributes) located in this Layer is visible.
Bases: cocos.rect.Rect, cocos.tiles.Cell
A rectangular cell from a MapLayer.
The cell may have the standard properties “top”, “left”, “bottom” and “right” which are booleans indicating that those sides are impassable. These are used by RectCellCollider.
Note that all pixel attributes are not adjusted for screen, view or layer transformations.
Bases: cocos.tiles.RegularTesselationMap
Rectangular map.
Cells are stored in column-major order with y increasing up, allowing [i][j] addressing:
+---+---+---+
| d | e | f |
+---+---+---+
| a | b | c |
+---+---+---+
Thus cells = [[‘a’, ‘d’], [‘b’, ‘e’], [‘c’, ‘f’]]
(and thus the cell at (0, 0) is ‘a’ and (0, 1) is ‘d’)
Return Cell at pixel px=(x,y) on the map.
The pixel coordinate passed in is in the map’s coordinate space, unmodified by screen, layer or view transformations.
Return None if out of bounds.
(x1, y1) and (x2, y2) are the lower left and upper right corners respectively, in map’s coordinate space, unmodified by screen, layer or view transformations
Return a list of Cell instances.
returns the grid coordinates for the hex that covers the point (x, y)
Get the neighbor Cell in the given direction (dx, dy) which is one of self.UP, self.DOWN, self.LEFT or self.RIGHT.
Returns None if out of bounds.
Get all cells touching the sides of the nominated cell.
If “diagonals” is True then return the cells touching the corners of this cell too.
Return a dict with the directions (self.UP, self.DOWN, etc) as keys and neighbor cells as values.
Bases: object
This class implements collisions between a moving rect object and a tilemap.
Collide a rect with the given RectMap map.
Apart from “map” the arguments are as per do_collision.
Mutates the new rect to conform with the map.
Returns the (possibly modified) (dx, dy)
Collide a Rect moving from “last” to “new” with the given map RectCell “cell”. The “dx” and “dy” values may indicate the velocity of the moving rect.
The RectCell must have the boolean properties “top”, “left”, “bottom” and “right” for those sides which the rect should collide.
If there is no collision then nothing is done.
If there is a collision:
Mutates the new rect to conform with the map.
Returns the (possibly modified) (dx, dy)
Bases: cocos.tiles.RectMap, cocos.tiles.MapLayer
A renderable, scrollable rect map.
Bases: object
A regularly tesselated map that allows access to its cells by index (i, j).
Return Cell at cell pos=(i, j).
Return None if out of bounds.
Bases: object
Load some tile mapping resources from an XML file.
Find all elements of the given class in this resource.
Find all elements of the given class in this resource and all <requires>’ed resources.
Save this resource’s XML to the indicated file.
Bases: object
Tiles hold an image and some optional properties.
Bases: dict
Contains a set of Tile objects referenced by some id.
Add a new Tile to this TileSet, generating a unique id if necessary.
Returns the Tile instance.
returns a bytes object
decompress the input array of bytes to an array of bytes using gzip
decompress the input array of bytes to an array of bytes using zlib
Determine a regular hexagon’s width given its height.
Load resource(s) defined in the indicated XML file.
Load some tile mapping resources from an XML file.
Load some tile mapping resources from a TMX file.