home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "Hero"
- '---------------------------------------------------------------
- ' Visual Basic Game Programming for Teens
- ' Hero.bas File
- '---------------------------------------------------------------
-
- Option Explicit
- Option Base 0
-
- 'added in chapter 18
- Public Enum HEROSTATES
- HERO_STOPPED = 0
- HERO_WALKING = 1
- HERO_ATTACKING = 2
- End Enum
-
- 'keep track of player info
- Public Type TPLAYER
- state As HEROSTATES
- health As Integer
- 'add more data for the hero here
- 'such as an inventory array
- End Type
-
- 'main player character
- Public Player As TCHARACTER
- Public PlayerData As TPLAYER
-
- Public Function PlayerPos() As point
- 'get tile pos at center of screen
- PlayerPos.x = ScrollX + SCREENWIDTH / 2
- PlayerPos.y = ScrollY + SCREENHEIGHT / 2
- End Function
-
-
- 'added in chapter 18
- Public Sub InitHero()
-
- Player.name = "Gandalf"
- PlayerData.state = HERO_STOPPED
-
- 'load the hero walking sprite
- Set heroImgWalk = LoadTexture(d3ddev, App.Path & "\hero_axe_walking.bmp")
-
- 'initialize the walking sprite
- InitSprite d3ddev, heroSprWalk
- With heroSprWalk
- .FramesPerRow = 9
- .FrameCount = 9
- .CurrentFrame = 0
- .AnimDelay = 1
- .width = 96
- .height = 96
- .ScaleFactor = 1
- .x = (SCREENWIDTH - .width) / 2
- .y = (SCREENHEIGHT - .height) / 2
- End With
-
- 'load the hero attack animations
- Set heroImgAttack = LoadTexture(d3ddev, App.Path & "\hero_axe_attacking.bmp")
-
- 'initialize the attacking sprite
- InitSprite d3ddev, heroSprAttack
- With heroSprAttack
- .FramesPerRow = 13
- .FrameCount = 13
- .CurrentFrame = 0
- .AnimDelay = 0
- .width = 96
- .height = 96
- .ScaleFactor = 1
- .x = (SCREENWIDTH - .width) / 2
- .y = (SCREENHEIGHT - .height) / 2
- End With
-
- End Sub
-
- Public Function IsFacing(ByRef spr1 As TSPRITE, ByRef spr2 As TSPRITE) As Boolean
- Dim n As Long
- Dim a As point
- Dim b As point
-
- 'are both sprites in range of each other?
- If Not Collision(spr1, spr2) Then
- IsFacing = False
- Exit Function
- End If
-
- a.x = spr1.x + spr1.width / 2
- a.y = spr1.y + spr1.height / 2
- b.x = spr2.x + spr2.width / 2
- b.y = spr2.y + spr2.height / 2
-
- Select Case spr1.AnimSeq
- 'looking up
- Case 7, 0, 1
- If b.y < a.y Then IsFacing = True
-
- 'looking down
- Case 5, 4, 3
- If b.y > a.y Then IsFacing = True
-
- 'looking left
- Case 6
- If b.x < a.x Then IsFacing = True
-
- 'looking right
- Case 2
- If b.x > a.x Then IsFacing = True
- End Select
-
- End Function
-
- 'added in chapter 18
- Public Sub UpdateHero()
- Dim state As String
-
- Select Case PlayerData.state
-
- Case HERO_STOPPED
- state = "STOPPED"
- DrawSprite heroImgWalk, heroSprWalk, C_WHITE
-
- Case HERO_WALKING
- state = "WALKING"
-
- 'animate the walking hero
- If heroSprWalk.Animating Then
- AnimateSprite heroSprWalk
- End If
-
- 'draw the walking hero
- DrawSprite heroImgWalk, heroSprWalk, C_WHITE
-
- Case HERO_ATTACKING
- state = "ATTACKING"
-
- 'animate the attacking hero
- If heroSprAttack.Animating Then
- AnimateSprite heroSprAttack
-
- 'done attacking? go back to walking
- If heroSprAttack.Animating = False Then
- PlayerData.state = HERO_STOPPED
- End If
- End If
-
- 'draw the walking hero
- DrawSprite heroImgAttack, heroSprAttack, C_WHITE
-
- 'check for a hit
- CheckForHits
-
- Case Else
- Debug.Print "Hero state error!"
-
- End Select
-
- 'display hero state
- PrintText fontImg, fontSpr, 400, 452, C_WHITE, state
-
- End Sub
-
- Public Sub StopHero()
- Scroll 0, 0
- Select Case heroSprWalk.AnimSeq
- Case 0
- ScrollY = ScrollY + WALKSPEED
-
- Case 1
- ScrollY = ScrollY + WALKSPEED
- ScrollX = ScrollX - WALKSPEED
- Case 2
- ScrollX = ScrollX - WALKSPEED
- Case 3
- ScrollX = ScrollX - WALKSPEED
- ScrollY = ScrollY - WALKSPEED
- Case 4
- ScrollY = ScrollY - WALKSPEED
- Case 5
- ScrollX = ScrollX + WALKSPEED
- ScrollY = ScrollY - WALKSPEED
- Case 6
- ScrollX = ScrollX + WALKSPEED
- Case 7
- ScrollX = ScrollX + WALKSPEED
- ScrollY = ScrollY + WALKSPEED
- End Select
- End Sub
-
-
- Public Sub CheckForHits()
- 'this is temporary--replace with weapon attack value
- Const ATTACKVALUE As Long = 1
-
- Dim n As Long
-
- For n = 0 To NUMNPCS - 1
- If IsFacing(heroSprAttack, charWalkSpr(n)) Then
- AttackNPC charStates(n), ATTACKVALUE
- Exit For
- End If
- Next n
-
- End Sub
-
- Public Sub CheckTileCollisions()
- Dim tilenum As Long
-
- tilenum = CurrentTile()
- If IsBadTile(tilenum) Then
- StopHero
- End If
-
- End Sub
-
-
-
-