5. TYPES
This lesson is literally the POWERHOUSE of ALL the lessons.
Seriously! Object based TYPES will keep your hordes of characters organized and easily managed. Combined with DATA tables to store your level object information in an easy to read and highly organized fashion is any game designers dream. Ironically, this happens to be the shortest of all the lessons :)

If you haven't already downloaded the tutorial files do so now, unzip to your desktop and OPEN up lesson5.bb, functions5.bb, and loadworld5.bb (below) and RUN / LANUCH the lesson5.bb program by pressing the ROCKET ICON

and you should see this...

NEW CODE is BOLD.

Here's the CODE!
lesson5.bb

Include "functions5.bb"

Global escape_key = 1
Global screen_width = 640, screen_height = 480

Graphics3D screen_width, screen_height, 16, 2
SetBuffer BackBuffer()

AmbientLight 200, 200, 200
light1 = CreateLight(2)

cam1 = CreateCamera()
CameraViewport cam1, 0, 0, screen_width, screen_height

Include "loadworld5.bb"

LoadMusic()
LoadSFX()

MoveMouse screen_width/2, screen_height/2

While Not KeyHit( escape_key )


    ; Keep our new characters animated and moving
    MoveCharacters( player )

    If run_vol# > 0 Then AnimSprite( player, anim_tex, 200, 4 )
    object_key_control( player )
    ChannelVolume runchannel, run_vol#
    PositionEntity sky, EntityX( player ), EntityY( player ), EntityZ( player )

    UpdateWorld
    RenderWorld

    ; 2D here


Flip
Wend

End


functions5.bb

. . .
Function MoveCharacters( target )
    For Character.Sprite = Each Sprite
        
TurnEntity Character\ID, 0, Rand( -5, 5 ), 0
        
MoveEntity Character\ID, 0, 0, Rnd( .01, .05 )
        
AnimSprite(Character\ID, Character\char_tex, 200, 4)
    Next
End Function

. . .


loadworld5.bb

Type Sprite
    Field ID, char_tex
End Type

For C = 1 To 3
    Character.Sprite = New Sprite
    Character\ID
= CreateSprite()
    Read img$, X, Y, Z
    Character\char_tex
= LoadAnimTexture( img$, 7, 32, 48, 0, 4 )
   
 EntityTexture Character\ID, Character\char_tex, 1
    HandleSprite Character\ID, 0, -1
    ScaleSprite Character\ID, 2, 2
    
EntityAutoFade Character\ID, 100, 120
    
PositionEntity Character\ID, X, Y, Z
Next

.Characters
Data "char2.bmp", 10, 0, 10
Data "char3.bmp", -10, 0, 10
Data "char4.bmp", 0, 0, 10

. . .


NEW COMMANDS
lesson5.bb

. . .
    ; Keep our new characters animated and moving
    MoveCharacters( player )
. . .

Check out what this function does below!

function5.bb

Function MoveCharacters( target )
    For Character.Sprite = Each Sprite
        
TurnEntity Character\ID, 0, Rand( -5, 5 ), 0
        
MoveEntity Character\ID, 0, 0, Rnd( .01, .05 )
        
AnimSprite(Character\ID, Character\char_tex, 200, 4)
    Next
End Function

This MoveCharacters( target ) function is specifically designed for using TYPES.
The For Character.Sprite = Each Sprite is a special TYPE ONLY loop that quickly runs through the list of all the objects you are tracking and applies whatever changes you wish.

loadworld5.bb

Type Sprite
    Field ID, char_tex
End Type

For C = 1 To 3         ; 3 Characters
    Character.Sprite = New Sprite
    Character\ID
= CreateSprite()
    Read img$, X, Y, Z
    Character\char_tex
= LoadAnimTexture( img$, 7, 32, 48, 0, 4 )
   
 EntityTexture Character\ID, Character\char_tex, 1
    HandleSprite Character\ID, 0, -1
    ScaleSprite Character\ID, 2, 2
    
EntityAutoFade Character\ID, 100, 120
    
PositionEntity Character\ID, X, Y, Z
Next

.Characters
Data "char2.bmp", 10, 0, 10
Data "char3.bmp", -10, 0, 10
Data "char4.bmp", 0, 0, 10

; More Characters added here?

Type Sprite
    Field ID, char_tex
End Type

Behold! The POWER of TYPES! Using the TYPE construct you will be able to easily keep track of all your important level objects, quickly make changes to any of their properties, and interact with them efficiently. Before you create a TYPE you need to carefully plan what characteristics of your game objects you care about or need to keep track of. We will be using TYPES for our 3 new game characters. We could even create a TYPE to manage all our trees if we cared to. Our 3 new characters will be loaded as animated sprites and they will need to be IDentified for movement and have their sprite strip ( char_tex ) updated. We can easily add more values in the future just by extending our Field list.

Our FOR . . . NEXT will loop 3 times creating 3 NEW Sprite TYPES named Character with Character.Sprite = New Sprite
We create a new sprite and save it's system ID in Character\ID

Read img$, X, Y, Z amazingly loads the values from the Data "char2.bmp", 10, 0, 10 record under the .Characters label just below the FOR . . . NEXT loop. The remainder of the loop loads the texture, applies it to the sprite, positions, etc. This READ ... DATA structure allows you to easily design, edit and save your levels!

EXPERIMENT !
Try changing the record values in
Data "char2.bmp", 10, 0, 10
Data "char3.bmp", -10, 0, 10
Data "char4.bmp", 0, 0, 10

THE CHALLENGE!!!
Try adding more characters!

NEXT : Lesson 6 - A . I .