home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter18 / CelticCrusader3 / Hero.bas < prev    next >
Encoding:
BASIC Source File  |  2004-11-03  |  5.5 KB  |  215 lines

  1. Attribute VB_Name = "Hero"
  2. '---------------------------------------------------------------
  3. ' Visual Basic Game Programming for Teens
  4. ' Hero.bas File
  5. '---------------------------------------------------------------
  6.  
  7. Option Explicit
  8. Option Base 0
  9.  
  10. 'added in chapter 18
  11. Public Enum HEROSTATES
  12.     HERO_STOPPED = 0
  13.     HERO_WALKING = 1
  14.     HERO_ATTACKING = 2
  15. End Enum
  16.  
  17. 'keep track of player info
  18. Public Type TPLAYER
  19.     state As HEROSTATES
  20.     health As Integer
  21.     'add more data for the hero here
  22.     'such as an inventory array
  23. End Type
  24.  
  25. 'main player character
  26. Public Player As TCHARACTER
  27. Public PlayerData As TPLAYER
  28.  
  29. Public Function PlayerPos() As point
  30.     'get tile pos at center of screen
  31.     PlayerPos.x = ScrollX + SCREENWIDTH / 2
  32.     PlayerPos.y = ScrollY + SCREENHEIGHT / 2
  33. End Function
  34.  
  35.  
  36. 'added in chapter 18
  37. Public Sub InitHero()
  38.  
  39.     PlayerData.state = HERO_STOPPED
  40.  
  41.     'load the hero walking sprite
  42.     Set heroImgWalk = LoadTexture(d3ddev, App.Path & "\hero_staff_walking.bmp")
  43.     
  44.     'initialize the walking sprite
  45.     InitSprite d3ddev, heroSprWalk
  46.     With heroSprWalk
  47.         .FramesPerRow = 9
  48.         .FrameCount = 9
  49.         .CurrentFrame = 0
  50.         .AnimDelay = 1
  51.         .width = 96
  52.         .height = 96
  53.         .ScaleFactor = 1
  54.         .x = (SCREENWIDTH - .width) / 2
  55.         .y = (SCREENHEIGHT - .height) / 2
  56.     End With
  57.     
  58.     'load the hero attack animations
  59.     Set heroImgAttack = LoadTexture(d3ddev, App.Path & "\hero_staff_attacking.bmp")
  60.     
  61.     'initialize the attacking sprite
  62.     InitSprite d3ddev, heroSprAttack
  63.     With heroSprAttack
  64.         .FramesPerRow = 13
  65.         .FrameCount = 13
  66.         .CurrentFrame = 0
  67.         .AnimDelay = 0
  68.         .width = 96
  69.         .height = 96
  70.         .ScaleFactor = 1
  71.         .x = (SCREENWIDTH - .width) / 2
  72.         .y = (SCREENHEIGHT - .height) / 2
  73.     End With
  74.  
  75. End Sub
  76.  
  77. Public Function IsFacing(ByRef spr1 As TSPRITE, ByRef spr2 As TSPRITE) As Boolean
  78.     Dim n As Long
  79.     Dim a As point
  80.     Dim b As point
  81.     
  82.     'are both sprites in range of each other?
  83.     If Not Collision(spr1, spr2) Then
  84.         IsFacing = False
  85.         Exit Function
  86.     End If
  87.     
  88.     a.x = spr1.x + spr1.width / 2
  89.     a.y = spr1.y + spr1.height / 2
  90.     b.x = spr2.x + spr2.width / 2
  91.     b.y = spr2.y + spr2.height / 2
  92.     
  93.     Select Case spr1.AnimSeq
  94.         'looking up
  95.         Case 7, 0, 1
  96.             If b.y < a.y Then IsFacing = True
  97.             
  98.         'looking down
  99.         Case 5, 4, 3
  100.             If b.y > a.y Then IsFacing = True
  101.         
  102.         'looking left
  103.         Case 6
  104.             If b.x < a.x Then IsFacing = True
  105.             
  106.         'looking right
  107.         Case 2
  108.             If b.x > a.x Then IsFacing = True
  109.     End Select
  110.     
  111. End Function
  112.  
  113. 'added in chapter 18
  114. Public Sub UpdateHero()
  115.     Dim state As String
  116.             
  117.     Select Case PlayerData.state
  118.         
  119.         Case HERO_STOPPED
  120.             state = "STOPPED"
  121.             DrawSprite heroImgWalk, heroSprWalk, C_WHITE
  122.         
  123.         Case HERO_WALKING
  124.             state = "WALKING"
  125.             
  126.             'animate the walking hero
  127.             If heroSprWalk.Animating Then
  128.                 AnimateSprite heroSprWalk
  129.             End If
  130.             
  131.             'draw the walking hero
  132.             DrawSprite heroImgWalk, heroSprWalk, C_WHITE
  133.             
  134.         Case HERO_ATTACKING
  135.             state = "ATTACKING"
  136.             
  137.             'animate the attacking hero
  138.             If heroSprAttack.Animating Then
  139.                 AnimateSprite heroSprAttack
  140.                 
  141.                 'done attacking? go back to walking
  142.                 If heroSprAttack.Animating = False Then
  143.                     PlayerData.state = HERO_STOPPED
  144.                 End If
  145.             End If
  146.             
  147.             'draw the walking hero
  148.             DrawSprite heroImgAttack, heroSprAttack, C_WHITE
  149.             
  150.             'check for a hit
  151.             CheckForHits
  152.         
  153.         Case Else
  154.             Debug.Print "Hero state error!"
  155.  
  156.     End Select
  157.     
  158.     'display hero state
  159.     PrintText fontImg, fontSpr, 400, 452, C_WHITE, state
  160.     
  161. End Sub
  162.  
  163. Public Sub CheckForHits()
  164.     'this is temporary--replace with weapon attack value
  165.     Const ATTACKVALUE As Long = 1
  166.     
  167.     Dim n As Long
  168.     
  169.     For n = 0 To NUMNPCS - 1
  170.         If IsFacing(heroSprAttack, charWalkSpr(n)) Then
  171.             AttackNPC charStates(n), ATTACKVALUE
  172.             Exit For
  173.         End If
  174.     Next n
  175.     
  176. End Sub
  177.  
  178. Public Sub CheckTileCollisions()
  179.     Dim tilenum As Long
  180.     
  181.     tilenum = CurrentTile()
  182.     If IsBadTile(tilenum) Then
  183.         Scroll 0, 0
  184.         
  185.         Select Case heroSprWalk.AnimSeq
  186.             Case 0
  187.                 ScrollY = ScrollY + WALKSPEED
  188.                 
  189.             Case 1
  190.                 ScrollY = ScrollY + WALKSPEED
  191.                 ScrollX = ScrollX - WALKSPEED
  192.             Case 2
  193.                 ScrollX = ScrollX - WALKSPEED
  194.             Case 3
  195.                 ScrollX = ScrollX - WALKSPEED
  196.                 ScrollY = ScrollY - WALKSPEED
  197.             Case 4
  198.                 ScrollY = ScrollY - WALKSPEED
  199.             Case 5
  200.                 ScrollX = ScrollX + WALKSPEED
  201.                 ScrollY = ScrollY - WALKSPEED
  202.             Case 6
  203.                 ScrollX = ScrollX + WALKSPEED
  204.             Case 7
  205.                 ScrollX = ScrollX + WALKSPEED
  206.                 ScrollY = ScrollY + WALKSPEED
  207.         End Select
  208.         
  209.     End If
  210.     
  211. End Sub
  212.  
  213.  
  214.  
  215.