I talked about moving objects but from now on I'll call them units. This is because in most tile-based games you have people, robots, cars or space ships that move around so the name unit fits in most cases. Also objects are more general things like swords, guns, books, pots, trees or even doors so we'll leave this name out for later. Now units have some other properties than tiles like direction and size. Unlie tiles units can be of different sizes. However note that the collision area is limited to one tile, even if the graphic fills out the whole screen. You can get around this problem by registering large objects to more than 1 map position.
Also we need a map index property so the unit knows the map position where it's currently registered. I'll show you how this works right afterwards.
Public Type tUnit 'Position (in pixel) X as Long Y as Long 'Current map position MapIndex as Long 'Unit size W as Long H as Long 'Display data Bitmap as Long Direction as Long End Type
Direction is necessary so we know where the unit looks. In the sample code I used a 4-direction system where 0 means up, 1 right, 2 down and 3 left. The direction is only important when drawing the picture, it doesn't affect the map or index at all. You can however use the direction to achieve random movement. The idea is to move each unit in the current direction and change the direction from time to time.
As already mentioned the MapIndex property represents the current map index where the unit is registered to. When moving the unit around we can use this index to clear that position. If set to -1 there's no current position, meaning that it's a newly created unit.
And as always we also need a variable declaration for the array:
Public UnitCount as Long Public Unit() as tUnit
As for the tiles we need a add-function for this manager. It's called AddUnit and the code looks like this:
Public Sub AddUnit( iPictureFile as String, iW as Long, iH as Long ) 'Allocate memory UnitCount = UnitCount + 1 ReDim Preserve Unit( UnitCount ) With Unit( UnitCount ) 'Load bitmap .Bitmap = LoadBitmap( SourcePath & iPictureFile, True ) 'Setup size .W = iW .H = iH 'Reset map index .MapIndex = -1 End With End Sub
Here the bitmap is loaded directly when calling the add function, if you're going to save and load units to and from files you'll have to store the file name instead and load resources afterwards. This is exactly the same as we did with tiles in a earlier tutorial. I left it out because in most games the units randomly appear as the player enters the map. We'll get back to this when adding object and unit sources (templates) later.
Note that we have to set the MapIndex to -1 since 0 would mean position 0, 0 on the map.
Adding the new layer is done in the map type where we already defined two layers: Floor and Roof. The new Type is shown below, the new line is selected:
Public Type tMap W as Long 'W stands for width H as Long 'H stands for height 'Maps Floor() as Long Roof() as LongUnits() as LongEnd Type
Also we need to extend the CreateMap function so that it creates the units map:
Public Sub CreateMap( iW as Long, iH as Long ) Dim A as Long With Map 'Get size .W = iW .H = iH 'Create maps ReDim .Floor( .W * .H ) ReDim .Roof( .W * .H )ReDim .Units( .W * .H )End With End Sub