VBfx / Tile tutorial / Step 2

DrawUnits again

I already mentioned the 4-directions system that fills out the rows of the unit's picture. Now we're going to fill the lines with animation frames so the final picture looks like this:

Animated unit

This is again exactly the same as we did when animating the tiles so I don't have to explain the new DrawUnit function below. The 2 changed lines are marked as usual:

Public Sub Draw()
    Dim A as Long
    
    Dim OffsetX as Long
    Dim OffsetY as Long
    
    Dim DrawX as Long
    Dim DrawY as Long
    
    For A = 1 To UnitCount
        With Unit( A )
            'Get offset position (bottom-center)
            OffsetX = .W / 2 - TileSize / 2
            OffsetY = .H - TileSize / 2
            
            'Get final draw position
            DrawX = ( .X - OffsetX ) - Camera.X
            DrawY = ( .Y - OffsetY ) - Camera.Y
            
            'Draw mask
            BitBlt Camera.FrontDC, _
                DrawX, DrawY, _
                .W, .H, _
                Bitmap( .Bitmap ).Mask.DC, _
                ( .W * .ActFrame ), ( .H * .Direction ), _
vbSrcPaint 'Draw picture BitBlt Camera.FrontDC, _ DrawX, DrawY, _ .W, .H, _ Bitmap( .Bitmap ).Picture.DC, _
                ( .W * .ActFrame ), ( .H * .Direction ), _
vbSrcAnd End With Next End Sub

Instead of just drawing from 0, 0 we now calculate the current frame's offset by multiplicating the width by the current frame number. Note again that the frame number is 0-based so frame 0 is the first frame and a FrameCount of 1 means 2 frames to animate.

Animate

Before anything happens we need to call the Animate function. The only different thing is that we check if Animated is true now. If not the animation is reset to the first frame. See code below:

Public Sub Animate()
    Dim A as Long
    Dim Temp as Long
    
    'Get current tick
    Temp = GetTickCount
    
    'Check all units
    For A = 0 To UnitCount
        With Unit( A )
            'Check timing
            If .NextTick < Temp Then
                'Check if animated
                If .Animated Then
                    'Increase frame number
                    .ActFrame = .ActFrame + 1
                    
                    'Reset animation if last frame reached
                    If .ActFrame > .FrameCount Then: .ActFrame = 0
                    
                    'Set next tick
                    .NextTick = Temp + .FrameDelay
                
                Else
                    'Reset animation
                    .ActFrame = 0
                End If
            End If
        End With
    Next
End Sub

Conclusion

Now that we have updated the units Type and the DrawUnits function correctly shows up the animation we can go on starting and stopping the animation as we need. This is all done within the MoveUnit function and a short new function called UpdateAnimations. We'll get right to this.

Navigation