VBfx / Tile tutorial / Step 1

Extending the units

As for the tiles we need to extend the tUnit Type and the AddUnit function to provide animations. Furthermore we need a Animate function and we have to update DrawUnits. Let's start with the Type, there are some new properties we need:

Public Type tUnit
    'Position (in pixel)
    X as Long
    Y as Long
    
    'Current map position
    MapIndex as Long
    Moved as Boolean
    
    'Unit size
    W as Long
    H as Long
    
    'Animation setup
    FrameCount as Integer
    FrameDelay as Long
    
    'Animation data
    Animated as Boolean
    ActFrame as Integer
    NextTick as Long
    
    'Display
    Bitmap as Long
    Direction as Long
End Type

Code discussion

I only picked out the new properties here.

    Moved as Boolean

This property will indicate whether the unit moved in the current frame or not. We need to know this so we can correctly stop the animation as the unit stops movement.

    'Animation setup
    FrameCount as Integer
    FrameDelay as Long
    
    'Animation data
    Animated as Boolean
    ActFrame as Integer
    NextTick as Long

This is again the animation info you already know from the tiles.

The only new property is Animated which tells the Animate function whether to animate the unit or not. You'll understand how this works when reading the MoveUnit and Animate function later.

AddUnit

Again we need to extend the parameters of the add function so we can setup our unit's animations:

Public Sub AddUnit( iPictureFile as String, iW as Long, iH as Long, iFrameCount as Long, iFrameDelay 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
        
        'Setup animation info
        .FrameCount = iFrameCount
        .FrameDelay = iFrameDelay
        
        'Reset animation
        .ActFrame = 0
        .NextTick = 0
        
        'Reset map index
        .MapIndex = -1
    End With
End Sub

This is again the same extension we did for the AddTile function so nothing to explain.

Navigation