home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter19 / CelticCrusader4 / Hero.bas < prev    next >
Encoding:
BASIC Source File  |  2004-10-28  |  5.4 KB  |  219 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.     Player.name = "Gandalf"
  40.     PlayerData.state = HERO_STOPPED
  41.  
  42.     'load the hero walking sprite
  43.     Set heroImgWalk = LoadTexture(d3ddev, App.Path & "\hero_axe_walking.bmp")
  44.     
  45.     'initialize the walking sprite
  46.     InitSprite d3ddev, heroSprWalk
  47.     With heroSprWalk
  48.         .FramesPerRow = 9
  49.         .FrameCount = 9
  50.         .CurrentFrame = 0
  51.         .AnimDelay = 1
  52.         .width = 96
  53.         .height = 96
  54.         .ScaleFactor = 1
  55.         .x = (SCREENWIDTH - .width) / 2
  56.         .y = (SCREENHEIGHT - .height) / 2
  57.     End With
  58.     
  59.     'load the hero attack animations
  60.     Set heroImgAttack = LoadTexture(d3ddev, App.Path & "\hero_axe_attacking.bmp")
  61.     
  62.     'initialize the attacking sprite
  63.     InitSprite d3ddev, heroSprAttack
  64.     With heroSprAttack
  65.         .FramesPerRow = 13
  66.         .FrameCount = 13
  67.         .CurrentFrame = 0
  68.         .AnimDelay = 0
  69.         .width = 96
  70.         .height = 96
  71.         .ScaleFactor = 1
  72.         .x = (SCREENWIDTH - .width) / 2
  73.         .y = (SCREENHEIGHT - .height) / 2
  74.     End With
  75.  
  76. End Sub
  77.  
  78. Public Function IsFacing(ByRef spr1 As TSPRITE, ByRef spr2 As TSPRITE) As Boolean
  79.     Dim n As Long
  80.     Dim a As point
  81.     Dim b As point
  82.     
  83.     'are both sprites in range of each other?
  84.     If Not Collision(spr1, spr2) Then
  85.         IsFacing = False
  86.         Exit Function
  87.     End If
  88.     
  89.     a.x = spr1.x + spr1.width / 2
  90.     a.y = spr1.y + spr1.height / 2
  91.     b.x = spr2.x + spr2.width / 2
  92.     b.y = spr2.y + spr2.height / 2
  93.     
  94.     Select Case spr1.AnimSeq
  95.         'looking up
  96.         Case 7, 0, 1
  97.             If b.y < a.y Then IsFacing = True
  98.             
  99.         'looking down
  100.         Case 5, 4, 3
  101.             If b.y > a.y Then IsFacing = True
  102.         
  103.         'looking left
  104.         Case 6
  105.             If b.x < a.x Then IsFacing = True
  106.             
  107.         'looking right
  108.         Case 2
  109.             If b.x > a.x Then IsFacing = True
  110.     End Select
  111.     
  112. End Function
  113.  
  114. 'added in chapter 18
  115. Public Sub UpdateHero()
  116.     Dim state As String
  117.             
  118.     Select Case PlayerData.state
  119.         
  120.         Case HERO_STOPPED
  121.             state = "STOPPED"
  122.             DrawSprite heroImgWalk, heroSprWalk, C_WHITE
  123.         
  124.         Case HERO_WALKING
  125.             state = "WALKING"
  126.             
  127.             'animate the walking hero
  128.             If heroSprWalk.Animating Then
  129.                 AnimateSprite heroSprWalk
  130.             End If
  131.             
  132.             'draw the walking hero
  133.             DrawSprite heroImgWalk, heroSprWalk, C_WHITE
  134.             
  135.         Case HERO_ATTACKING
  136.             state = "ATTACKING"
  137.             
  138.             'animate the attacking hero
  139.             If heroSprAttack.Animating Then
  140.                 AnimateSprite heroSprAttack
  141.                 
  142.                 'done attacking? go back to walking
  143.                 If heroSprAttack.Animating = False Then
  144.                     PlayerData.state = HERO_STOPPED
  145.                 End If
  146.             End If
  147.             
  148.             'draw the walking hero
  149.             DrawSprite heroImgAttack, heroSprAttack, C_WHITE
  150.             
  151.             'check for a hit
  152.             CheckForHits
  153.         
  154.         Case Else
  155.             Debug.Print "Hero state error!"
  156.  
  157.     End Select
  158.     
  159.     'display hero state
  160.     PrintText fontImg, fontSpr, 400, 452, C_WHITE, state
  161.     
  162. End Sub
  163.  
  164. Public Sub StopHero()
  165.     Scroll 0, 0
  166.     Select Case heroSprWalk.AnimSeq
  167.         Case 0
  168.             ScrollY = ScrollY + WALKSPEED
  169.             
  170.         Case 1
  171.             ScrollY = ScrollY + WALKSPEED
  172.             ScrollX = ScrollX - WALKSPEED
  173.         Case 2
  174.             ScrollX = ScrollX - WALKSPEED
  175.         Case 3
  176.             ScrollX = ScrollX - WALKSPEED
  177.             ScrollY = ScrollY - WALKSPEED
  178.         Case 4
  179.             ScrollY = ScrollY - WALKSPEED
  180.         Case 5
  181.             ScrollX = ScrollX + WALKSPEED
  182.             ScrollY = ScrollY - WALKSPEED
  183.         Case 6
  184.             ScrollX = ScrollX + WALKSPEED
  185.         Case 7
  186.             ScrollX = ScrollX + WALKSPEED
  187.             ScrollY = ScrollY + WALKSPEED
  188.     End Select
  189. End Sub
  190.  
  191.  
  192. Public Sub CheckForHits()
  193.     'this is temporary--replace with weapon attack value
  194.     Const ATTACKVALUE As Long = 1
  195.     
  196.     Dim n As Long
  197.     
  198.     For n = 0 To NUMNPCS - 1
  199.         If IsFacing(heroSprAttack, charWalkSpr(n)) Then
  200.             AttackNPC charStates(n), ATTACKVALUE
  201.             Exit For
  202.         End If
  203.     Next n
  204.     
  205. End Sub
  206.  
  207. Public Sub CheckTileCollisions()
  208.     Dim tilenum As Long
  209.     
  210.     tilenum = CurrentTile()
  211.     If IsBadTile(tilenum) Then
  212.         StopHero
  213.     End If
  214.     
  215. End Sub
  216.  
  217.  
  218.  
  219.