home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter16 / CelticCrusader1 / Characters.bas < prev    next >
Encoding:
BASIC Source File  |  2004-10-24  |  7.9 KB  |  275 lines

  1. Attribute VB_Name = "Characters"
  2. '---------------------------------------------------------------
  3. ' Visual Basic Game Programming for Teens
  4. ' Characters.bas File
  5. '---------------------------------------------------------------
  6.  
  7. Option Explicit
  8. Option Base 0
  9.  
  10. 'main character class data type
  11. Public Type TCHARACTER
  12.     name As String * 20
  13.     classtype As String * 20
  14.     experience As Integer
  15.     level As Integer
  16.     strength As Integer
  17.     dexterity As Integer
  18.     intellect  As Integer
  19.     charisma As Integer
  20.     stamina As Integer
  21.     fillerstr As String * 80
  22.     fillerint(10) As Integer
  23. End Type
  24.  
  25. 'keeps track of NPC state
  26. Public Enum NPCSTATES
  27.     NPC_STOPPED = 0
  28.     NPC_WALKING = 1
  29. End Enum
  30.  
  31. 'keeps track of each character
  32. Public Type TNPC
  33.     name As String
  34.     state As NPCSTATES
  35.     startpos As point
  36.     curpos As point
  37.     destpos As point
  38.     classindex As Integer
  39.     SpeedDelay As Integer
  40.     SpeedCount As Integer
  41.     Facing As Integer
  42. End Type
  43.  
  44. 'generic data for the character classes
  45. 'images and data are shared by the NPCs
  46. Const NUMCHARS As Long = 1
  47. Dim charImages(NUMCHARS) As Direct3DTexture8
  48. Dim charClasses(NUMCHARS) As TCHARACTER
  49.  
  50. 'unique data for each individual NPC
  51. Const NUMNPCS As Long = 10
  52. Dim charStates(NUMNPCS) As TNPC
  53. Dim charSprites(NUMNPCS) As TSPRITE
  54.  
  55. 'main player character
  56. Public Player As TCHARACTER
  57.  
  58.  
  59. Public Sub InitCharacters()
  60.     Dim p As point
  61.     Dim n As Long
  62.     
  63.     'set up all the base character classes, sprites, and images
  64.     'so far we're only using a single type of character--Viking Warrior
  65.     charClasses(0) = LoadCharacterBinaryFile(App.Path & "\warrior.dat")
  66.     Set charImages(0) = LoadTexture(d3ddev, App.Path & "\viking_walking.bmp")
  67.     
  68.     'now create the individual characters used in the game
  69.     'all of these will share the base data above
  70.     For n = 0 To NUMNPCS - 1
  71.         
  72.         'initialize sprite data
  73.         InitSprite d3ddev, charSprites(n)
  74.         With charSprites(n)
  75.             .FramesPerRow = 8
  76.             .FrameCount = 8
  77.             .AnimDelay = 2
  78.             .width = 96
  79.             .height = 96
  80.         End With
  81.             
  82.         'start NPCs at the player's location
  83.         '(to test NPC movement at this stage)
  84.         p.x = PLAYERSTARTX * TILEWIDTH
  85.         p.y = PLAYERSTARTY * TILEHEIGHT
  86.         
  87.         'customize the Viking character
  88.         With charStates(n)
  89.             
  90.             'this is the key! points to the base image/sprite/data
  91.             .classindex = 0
  92.             
  93.             .name = "Viking"
  94.             .startpos = p
  95.             .curpos = p
  96.             .SpeedDelay = 1
  97.             .SpeedCount = 0
  98.             .state = NPC_WALKING
  99.             SetRandomDestination n
  100.  
  101.         End With
  102.     Next n
  103.     
  104. End Sub
  105.  
  106. Public Sub SetRandomDestination(ByVal num As Long)
  107.     With charStates(num)
  108.     
  109.         'set random X near the starting position
  110.         '(the NPC will never wander away from his "home")
  111.         .destpos.x = .startpos.x + Random(600)
  112.         If .destpos.x > GAMEWORLDWIDTH Then
  113.             .destpos.x = GAMEWORLDWIDTH - 1
  114.         End If
  115.         
  116.         'set random Y near the starting position
  117.         .destpos.y = .startpos.y + Random(600)
  118.         If .destpos.y > GAMEWORLDHEIGHT Then
  119.             .destpos.y = GAMEWORLDHEIGHT - 1
  120.         End If
  121.     End With
  122. End Sub
  123.  
  124. Public Sub MoveNPC(ByVal num As Long)
  125.     'moves a single NPC
  126.     With charStates(num)
  127.     
  128.         'update movement rate--exit if not there yet
  129.         .SpeedCount = .SpeedCount + 1
  130.         If .SpeedCount < .SpeedDelay Then Exit Sub
  131.             
  132.         'okay, time to move, reset move counter
  133.         .SpeedCount = 0
  134.         
  135.         'check to see if destination reached
  136.         If .curpos.x = .destpos.x And .curpos.y = .destpos.y Then
  137.             'yes! set a new destination then exit
  138.             .state = NPC_STOPPED
  139.             SetRandomDestination num
  140.             Exit Sub
  141.         Else
  142.             .state = NPC_WALKING
  143.         End If
  144.         
  145.         'time to set the NPC's "facing" direction
  146.         'and update the X,Y position
  147.         If .curpos.x < .destpos.x Then
  148.             
  149.             'needs to walk westward
  150.             .curpos.x = .curpos.x + 1
  151.             
  152.             If .curpos.y < .destpos.y Then
  153.                 'facing SE
  154.                 .curpos.y = .curpos.y + 1
  155.                 .Facing = 3
  156.             ElseIf .curpos.y > .destpos.y Then
  157.                 'facing NE
  158.                 .curpos.y = .curpos.y - 1
  159.                 .Facing = 1
  160.             Else
  161.                 'facing EAST
  162.                 .Facing = 2
  163.             End If
  164.         
  165.         ElseIf .curpos.x > .destpos.x Then
  166.             
  167.             'needs to walk eastward
  168.             .curpos.x = .curpos.x - 1
  169.             
  170.             If .curpos.y < .destpos.y Then
  171.                 'facing SW
  172.                 .curpos.y = .curpos.y + 1
  173.                 .Facing = 5
  174.             ElseIf .curpos.y > .destpos.y Then
  175.                 'facing NW
  176.                 .curpos.y = .curpos.y - 1
  177.                 .Facing = 7
  178.             Else
  179.                 'facing WEST
  180.                 .Facing = 6
  181.             End If
  182.             
  183.         Else 'must be facing due NORTH or SOUTH
  184.             
  185.             If .curpos.y < .destpos.y Then
  186.                 'facing SOUTH
  187.                 .curpos.y = .curpos.y + 1
  188.                 .Facing = 4
  189.             ElseIf .curpos.y > .destpos.y Then
  190.                 'facint NORTH
  191.                 .curpos.y = .curpos.y - 1
  192.                 .Facing = 0
  193.             End If
  194.         
  195.         End If
  196.     
  197.     End With
  198. End Sub
  199.  
  200. Public Sub DrawNPC(ByVal num As Long)
  201.     Dim r As RECT
  202.     Dim classindex As Long
  203.     
  204.     'grab a shortcut to these long variable names
  205.     r.Left = charStates(num).curpos.x
  206.     r.Top = charStates(num).curpos.y
  207.     r.Right = r.Left + charSprites(num).width
  208.     r.bottom = r.Top + charSprites(num).height
  209.     
  210.     'remember, images/data are referred to using the NPC's classindex!
  211.     'the sprite and state arrays are for every single unique NPC,
  212.     'but the bitmap image and class data are shared by all NPCs
  213.     classindex = charStates(num).classindex
  214.     
  215.     'now check to see if the sprite is within the scrolling viewport
  216.     'sprite's position is actually global, so this determines if it's visible
  217.     If r.Left > ScrollX - 1 And r.Right < ScrollX + SCREENWIDTH + 1 And _
  218.        r.Top > ScrollY - 1 And r.bottom < ScrollY + SCREENHEIGHT + 1 Then
  219.     
  220.         'update animation frame if walking
  221.         If charStates(num).state = NPC_WALKING Then
  222.             AnimateSprite charSprites(num)
  223.         End If
  224.         
  225.         'draw the sprite--remember, it's using the shared image (texture)
  226.         charSprites(num).x = charStates(num).curpos.x - ScrollX
  227.         charSprites(num).y = charStates(num).curpos.y - ScrollY
  228.         charSprites(num).AnimSeq = charStates(num).Facing
  229.         DrawSprite charImages(classindex), charSprites(num), C_WHITE
  230.     
  231.     End If
  232.  
  233. End Sub
  234.  
  235. Public Sub MoveNPCs()
  236.     Dim n As Long
  237.     
  238.     'loop through all of the NPCs and move them
  239.     For n = 0 To NUMNPCS - 1
  240.         MoveNPC n
  241.     Next n
  242. End Sub
  243.  
  244. Public Sub DrawNPCs()
  245.     Dim n As Long
  246.     
  247.     'loop through all of the NPCs and draw them
  248.     For n = 0 To NUMNPCS - 1
  249.         DrawNPC n
  250.     Next n
  251. End Sub
  252.  
  253. Public Function LoadCharacterBinaryFile(ByVal filename As String) As TCHARACTER
  254.     Dim filenum As Integer
  255.     Dim dude As TCHARACTER
  256.     
  257.     filenum = FreeFile()
  258.     Open filename For Binary As filenum Len = Len(dude)
  259.     Get filenum, , dude
  260.     Close filenum
  261.     
  262.     LoadCharacterBinaryFile = dude
  263. End Function
  264.  
  265. Public Sub SaveCharacterBinaryFile(ByVal filename As String, ByRef dude As TCHARACTER)
  266.     Dim filenum As Integer
  267.     
  268.     filenum = FreeFile()
  269.     Open filename For Binary As filenum Len = Len(dude)
  270.     Put filenum, , dude
  271.     Close filenum
  272. End Sub
  273.  
  274.  
  275.