TILEMAPS
With tilemaps it's quite easy to create and render cool games levels/maps. In addition, they consume very little memory, and are fast to draw. CoolBasic tilemaps have 4 layers from which 2 can be drawn. You can also write extra data to tem and animate them.
Tilemaps are handled with the standard object commands. For instance, you position the map with the PositionObject-command. Only one map can be loaded at a time i.e. if you try to load or create another one, the previous is automatically overwritten. You can remove the map from game wold with the DeleteObject-command.
Tilemaps can be created on-the-fly or loaded from disk. You use Tilester (Editor Tools menu) to create tilemaps. Tilester has its own manual that can be found at Program Files\Tilester.
EditMap
This command can alter the tilemap on-the-fly. Meaning that you can build and/or edit tilemaps during the game without any performance hit. You can, for instance, blow off walls or make obstacles. Or you can turn water into lava and so on..
Tilemaps are basically arrays. Each array cell contain a number of tile that is to be drawn there. For example, if your map is filled with the value of one, the map will draw only the first tile in the tileset. This way you can dramatically affect the look of the map or modify the collision layer (2).
CoolBasic tilemaps have 4 layers: Back, Fore, Hit and Data. Only 0 and 1 is drawn. The hit-layer (2) contains only ones and zeros (1 means collision wall). Thus, if you were to blow off a wall, you would write a zero to the hit-layer to that location you want the invisible wall to be destroyed.
The coordinates are measured in tiles, meaning that the upper left corner would be (1,1). With EditMap everything is possible. The only limitation is your level of imagination.
Usage:
EditMap map_variable, layer, X, Y, Nb_tile
Example:
FrameLimit 40
'Create a map from Blank
map=MakeMap(32,32,32,32)
u=LoadObject("Media\guy.bmp",72)
'Apply some graphics on it
tileset=LoadImage("Media\tileset.bmp")
PaintObject map,tileset
'Randomly fill the map
For y=1 To MapHeight()
For x=1 To MapWidth()
If Rand(1,5)=5 Then
EditMap map,0,x,y,2 'drawing
EditMap map,2,x,y,1 'collision
Else
EditMap map,0,x,y,145
EndIf
Next x
Next y
'The game loop-itself
Repeat
If UpKey() Then MoveObject u,2
If DownKey() Then MoveObject u, -2
If LeftKey() Then TurnObject u,5
If RightKey() Then TurnObject u, -5
'Glue camera to object position
CloneCameraPosition u
DrawScreen
Forever
SetMap
With this command you can hide or show layers of tilemap. Tilemaps have 2 drawable layers: Back and Over (0 and 1). Objects are drawn in between of these two. If your came only uses the back-layer, there's no need to draw the over-layer (despite it's blank). You'll gain a little boost in performance. On another hand, you could set the Back-layer invisible so you could create cool Z-depth background scrolling in conjunction of MakeObjectFloor. See the example.
Usage:
SetMap map_variable, Back, Over
Example:
'Make an infinite background
grass=MakeObjectFloor()
PaintObject grass,LoadImage("Media\grass.bmp")
'Make it run as it were far away
PositionObject grass,0,0, -2
'Load the map
map=LoadMap("Media\testmap.til","Media\tileset.bmp")
AddText "Use Arrows to move..."
AddText "F1 = toggle back layer on/off"
AddText "F2 = toggle fore layer on/off"
'Defaults
back=1
fore=1
PositionCamera 0, -90
RotateCamera 0,90 'make the camera face upwards
PlayObject map,0,0,1
'The game-loop itself
Repeat
If KeyHit(cbkeyf1) Then back=Not back
If KeyHit(cbkeyf2) Then fore=Not fore
'Controls
If LeftKey() Then MoveCamera -1,0
If RightKey() Then MoveCamera 1,0
If UpKey() Then MoveCamera 0,1
If DownKey() Then MoveCamera 0, -1
SetMap map,back,fore 'update map properties
'Let's draw the game world before the text
'Otherwise the text would be covered by the map
DrawGame
Text 20,70,"BACK: "+back
Text 20,82,"FORE: "+fore
DrawScreen
Until EscapeKey()
SetTile
Use this command to build a tilemap on-the-fly. This command animates tileset i.e. the image that is used when map is drawn. Each tile has its own index number that begins from 1 and continues left to right, up to down. Like this:
01 02 03 04 05 06
07 08 09 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36
You can animate each tile differently. It's done by defining a number of animated tiles. For example, the value of one will animate two-tile sequence (the selected tile + one after). Disable animation by setting OFF or zero to sequence length. The last tile is unanimateable; you can't create a sequence that wraps to the start.
Optionally, you can define a slowness-factory of the animation sequence. The value of one is normal, while 2 means twice as slow and so on. Defaults to one (normal speed)
Usage:
SetTile tile_index, length [, slowness_factor]
Example:
'Create a tilemap
map=MakeMap(9,9,32,32)
'fill it randomly
For y=1 To 9
For x=1 To 9
c=Rand(1,3)
If c=1 Then
EditMap map,0,x,y,2
ElseIf c=2
EditMap map,0,x,y,4
Else
EditMap map,0,x,y,6
EndIf
Next x
Next y
'Load a tileset and apply it to the map
tileset=LoadImage("Media\tileset.bmp")
PaintObject map,tileset
'Set some animation sequences
SetTile 2,1
SetTile 4,1,3 'slowness factor 3
'-----------------
'animate map
PlayObject map,0,0,0.5
Repeat
DrawScreen
Until EscapeKey()
LoadMap
This function loads a tilemap from disk to memory and returns its handle. The map will be located at coordinates (0,0) by default. Tilemaps are drawn always centered and they use game world coordinate system. Notice that only one map can be loaded at a time. If you create or load a new map the previous one will automatically be overwritten.
Maps are made with Tilester – a tilemap editor and mapper that comes with CoolBasic. Tilester has its own user manual and you should read it before use. Tilester can be found in CoolBasic Tools menu or tai Program Files\Tilester – where the manual is located.
Tilemaps always come with 4 layers: Back, Over, Hit and Data. Only Back ond Over are drawn. Ove-layer is drawn on top of other objects, so you ccn create areas that objects can hide in. Also, you can set the drawing of unnecessary layers OFF (for example if you don't need Over) with the SetMap-command.
A tileset is used to draw tilemaps. It’s a big image that contains smaller images representing tiles. Tiles are ordered seamlessly together. You should take a look of the sample tilesets in the Media-folder in order to comprehend. A tileset is simply an image, and the following formats ae supported: BMP/JPG/PNG/TGA. The tileset can also be changed on-the-fly during the game.
The hit-layer contains ones and zeros that represent te unwalkable areas i.e. collision data.
You can also utilize the data-layer to store additional information to the map. For-example identify some triggering-zones, make mud, lava, store born locations, teleports and so one. Use GetMap or GetMap2 to retrueve this data. The data-layer is aimed for advanced users, beginners might find it dfficult to understand and/or use.
Tilemaps are commaded with the standard object commands and functions. For example, you can position a map by using the PositionObject –command. The map is also animated using the PlayObject-command. Please notice that the purpose of PlayObject is lightly different with tilemaps. For more information, check out the PlayObject manual.
Tilemaps should usually be created first before other objects. By doing that you ensure that the map will be drawn on the background.
Never load anything from disk within a loop. You should use this functions at the program start/media loading part.
Usage:
map_variable = LoadMap (filename$, tileset$)
Example:
FrameLimit 40 'limit game performance
'Load map and animate it (2 times a second)
map= LoadMap("Media\cdm2.til","Media\tileset.bmp")
PlayObject map,0,0,0.5
'Load the character and setup a collision for it
guy= LoadObject("Media\soldier.bmp",72)
SetupCollision guy,map,2,4,2
'The game
Repeat
'Control the guy
If LeftKey() Then TurnObject guy,5
If RightKey() Then TurnObject guy, -5
If UpKey() Then MoveObject guy,4
If DownKey() Then MoveObject guy, -4
'Stick camera to the guy
CloneCameraPosition guy
DrawScreen
Until EscapeKey()
MakeMap
If you haven't read about LoadMap yet, please do it now. This function creates a tilemap on-the-fly and prepares it to be edited. The map is initially filled by zeros, so you can't see anything. Also, you must use PaintObject to assign a tileset to the map.
Then build the map with EditMap and SetTile. Tilemaps should be created before any other objects are loaded, so they'll be drawn onto back. This function is also aimed to advanced users.
Usage:
map_variable = MakeMap (width, height, tile_width, tile_height)
Example:
<See EditMap example>
GetMap
This function allows you to read map data according to game world coordinates. See the example.
Usage:
tile = GetMap (layer, X#, Y#)
Example:
FrameLimit 40 'limit game performance
'Load map and animate it
map= LoadMap("Media\cdm2.til","Media\tileset.bmp")
PlayObject map,0,0,0.5
cursor=LoadImage("Media\cursor.bmp")
ShowMouse cursor
AddText "Arrows to scroll..."
AddText "Pinpoint tiles with the mouse"
Repeat
'Move the camera
If UpKey() Then MoveCamera 0,2
If DownKey() Then MoveCamera 0, -2
If LeftKey() Then MoveCamera-2
If RightKey() Then MoveCamera 2
'Otherwise we wouldn't be able to see the text
DrawGame
Color cbyellow
'Calculate the tile under mouse
'(200 is half the SCREENWIDTH, 150 is half the SCREENHEIGHT)
tile=GetMap(0,MouseWX(), MouseWY())
Text 60,60,"Tile under mouse: "+tile
DrawScreen
Forever
GetMap2
This is the same but the coordinates are measured in tiles, not game world coordinates. The upper left corner of the map starts from (1,1).
Usage:
tile = GetMap2 (layer, X, Y)
MapWidth
Tells the map width, in tiles. To get the map width in pixels use the ObjectSizeX-function.
Usage:
width = MapWidth ()
MapHeight
Tells the map height, in tiles. To get the map height in pixels use the ObjectSizeY-function.
Usage:
height = MapHeight ()