Skip to content

Latest commit

 

History

History
330 lines (251 loc) · 10.5 KB

TileMap.md

File metadata and controls

330 lines (251 loc) · 10.5 KB

Tile Maps

Tilemaps are a 2-dimensional grid of bytes, where each entry points to an Entry of a TileSet

TileMap Graphic

Build in Map Layers

In the Display Layers, You get access to two TileMaps out of the Box.

The Maps default Size is 32x32 tiles. You probably want to adjust that, depending on your chosen resolution and the size of the TileSet you plan to use. (See Engine-Setup for more info on how to change the Maps size)

Before you can enable any of the two Build in Maps, you must first define a TileSet, that your chosen layer will use

The following would need to happen during your Scenen Load-Lifecycle-Hook

func (me *ExampleScene) Load(state *core.EngineState, canvas *core.Canvas) {
    var tileSet := core.TileSet{}
    tileSet.InitFromMapSheet( 
        /* your core.Bitmap */, 
        /* PixelWidth of each Tile */,
        /* PixelHeight of each Tile */,
    );
    // ...

with That, you can then enable your chosen Map-Layer through the Engine State

    // ... 
    state.EnableMap1Layer(&tileSet)
    // ...

And Start filling it with Data

    //...
    state.Map1.SetMap([]byte{ /* your Maps Tile definitions goes here */ })
}

Constructor / Init Methods

Init

func (tm *TileMap) Init(tileSet *core.TileSet, width, height uint32) *TileMap 

Warning

using this method on the Build In Maps, will cause GO to panic.

If you don't use the Build In Maps, you MUST initialize your Maps first. see Creating your own TileMaps for more details.

Parameters

Parameter Type Description
ts *core.TileSet The catalogue from which the map will define, how to draw each tile.
Must not have more than 255 Tiles
width uint32 Numer of tiles on the x axis
height uint32 Numer of tiles on the y axis

Returns

*core.TileMap The Tilemap, on which that Method was executed.
(That way, you can chain multiple Setter Functions together)

Setters

SetMap

func (*TileMap) SetMap (tiles []byte) *TileMap

Redefines all Tiles in the Map

Parameters

Parameter Type Description
tiles []byte A slice of bytes containing the new Map definition

Tiles are updated from left to right first and top to bottom second. So in a 6x3 Tilemap, the Bytes would be assigned as follows

------------------------------------------------------------------------------------
|  tiles[0]   |  tiles[1]   |  tiles[2]   | tiles[3]   |  tiles[4]   |  tiles[5]   |
------------------------------------------------------------------------------------
|  tiles[6]   |  tiles[7]   |  tiles[8]   | tiles[9]   |  tiles[10]  |  tiles[11]  |
------------------------------------------------------------------------------------
|  tiles[12]  |  tiles[13]  |  tiles[14]  | tiles[15]  |  tiles[16]  |  tiles[17]  |
------------------------------------------------------------------------------------

Warning

How many Bytes the given Slice must contain depends either on width * height given during the Maps Init call or, if you use the Build In Maps the TileMapWith * TileMapHeight given during the Engine Setup

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

SetTileSet

func (me *TileMap) SetTileSet(ts *TileSet) *TileMap

Allows for changing the Maps TileSet after the Map was Initialized.

Note

If a Tiles Tile-Pixel-Size changes the overall displayed pixel-Size of the map will change with it

Parameters

Parameter Type Description
ts *core.TileSet The catalogue from which the map will define, how to draw each tile.
Must not have more than 255 Tiles

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

Clear

func (me *TileMap) Clear(tileIndex byte) *TileMap 

Resets all Tiles in the Map back to 0

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

SetTile

func (me *TileMap) SetTile(x, y uint32, tile byte) *TileMap

Changes what Tile is displayed at coordinates x, y

Parameters

Parameter Type Description
x uint32 The Tiles X coordinate in the Map
y uint32 The Tiles Y coordinate in the Map
tile byte The new Tiles index inside the TileSet

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

AlphaSet

func (me *TileMap) AlphaSet(a CanvasAlpha) *TileMap {

Parameters

(see: Alpha-Blending => AlphaSet)

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

AlphaReset

func (me *TileMap) AlphaReset() *TileMap {

Parameters

(see: Alpha-Blending => AlphaReset)

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

ScrollTo

func (me *TileMap) ScrollTo(x, y int32) *TileMap

Offsets the visible area of the map by x and y pixels.

ScrollTo-Graphics

Parameters

Parameter Type Description
x int32 Absolute horizontal offset starting from the left of the Map
y int32 Absolute vertical offset starting from the top of the map

[!info]
Should the Viewport area reach the Map Borders, the Map will wrap around, to fill the Screen.
ScrollTo-Wrap-Example

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

ScrollBy

func (me *TileMap) ScrollBy(x, y int32) *TileMap

It works as ScrollTo does, but the x and y values are relative to the current scroll-offset of the Map

Parameters

Parameter Type Description
x int32 relative horizontal offset from the current scroll-x coordinate
y int32 relative vertical offset from the current scroll-y coordinate

Returns

*core.TileMap The Tilemap, on which that Method was executed (That way, you can chain multiple Setter Functions together)

Getters

Alpha

func (me *TileMap) Alpha() CanvasAlpha

Returns

CanvasAlpha the current Alpha-Level of the Map

HasTileSet

func (me *TileMap) HasTileSet() bool 

Returns bool

true == the Maps has a Tileset, it can render from. (use SetTileSet(nil) to deactivate the current aktive tileset)

X

func (me *TileMap) X() int32 

Returns int32

The current horizontal Scroll of the Map

Y

func (me *TileMap) Y() int32           { return me.opts.Y }

Returns int32

The current vertical Scroll of the Map

XY

func (me *TileMap) XY() (int32, int32) { return me.opts.X, me.opts.Y }

Returns scrollx int32, scrolly int32

The current horizontal and vertical Scroll of the Map

ToCanvas

func (me *TileMap) ToCanvas(ca *core.Canvas) 

Renders the Map onto the given core.Canvas

Parameters

Parameter Type Description
ca core.Canvas the target canvase onto which to render the Map

Creating your own TileMaps

If you don't want to remain bound to the 2 Layers, that GoWas comes with, you can also define your own maps, that can be rendered on the Scene.Draw Layer during your Scenes Draw-Lifecycle-hook

To do so, you first must define where your Map will live inside your applications.

    myMap := core.TileMap{}
    //...

Then you must initialize it. For this, you can use The Maps Init-Method

    //...
    myMap.Init(*someTileset, 10, 10)
    //...