As pointed out by bpd some code snippets are taken from Visual Basic Game Programming For Teens book.
Hello guys im going to show you all a script that most begginer game VB programmers use for a combat system this is one of my scripts i ever used in the line of combat systems.
There are two basic things you need to do to allow the player to fight with NPCS:
1.Make sure the player is close enough to an enemy to hit him.
2.Make sure the player is facing an enemy while attacking.
If you can take care of these two problems, then you can create a combat system for the game. Tackle these two key problems in order.
The first thing you need to check on before you can handle a combat strike is whether the player is close enough to an enemy character to actually hit him. That is accomplished with a simple call to the Collision function. If there is no collision between the two sprites, then there definitely can't be an attack, and the swing misses! After determining if the two sprites are close enough for an attack, see if the attacker is at least facing the enemy.
I wrote a function called IsFacing that returns True or False depending on whether one sprite is facing another sprite. This is also useful for pitting NPCs against each other, not just for the player.
===================================== CODE =====================================
After you have the combat code ready to go, There's just one little problem: The source code writing in the game so far just draws the walking version of the player. With combat, the player has to swing his weapon too. The main game loop has to be modified so that you can check the player's state and then draw either the walking or the attacking animations based on what the player is doing
I modified the game loop so that all the player update code is replaced with a single call to UpdateHero, Which is listed here:
===================================== CODE =====================================
As you can see for yourself, there is a lot more to drawing the hero's sprite now that combat is involved.Now though, two different states have to be monitored and the correct sprite has to be animated. State engines are very helpful when you need to keep track of a uncomplicated series of conditions in a game, Because each condition is isolated from the rest, allowing you to write code for each condition separately.
Here is the new List of states being use for each NPC:
===================================== CODE =====================================
The array's that keep track of character classes, images and sprites have also been changed to accommodate the new combat system:
===================================== CODE =====================================
The section of code under the HERO_ATTACKING state includes a call to a subroutine called CheckForHits:
===================================== CODE =====================================
CheckForHits looks at all of the NPCs in the game and then calls IsFacing on each one to see if the player is close to and facing the NPC. If these two conditions are met, then the player hits the NPC with the weapon swing. If no NPC is in range ( in front of the player) then the swing doesn't hit anything! See how easy it is when a state engine is being used?
Now take a look at the AttackNPC subroutine that is called from the preceding routine you just looked at. This new routine is actually only called when the player has definitely hit an NPC. When this happens, the NPCS health needs to be cut down by an appropiate amount, and he dies if health is 0! aAttack NPC has some test code that prints a message above the player, and message above the target NPC during an attack, To tell you that hte game registered the hit. When the NPCS's Health reaches 0 the state of the character is set to NPC_DYING.
===================================== CODE =====================================
There is a death sequence where the NPC is frozen and fades into nothingness (a simple way to show that the NPC has died). If this happens, then h the NPCs state takes over the death allowing your players code to continue without worrying about dealing with the NPC's resting place. The state engine in characters.bas manages the state of the NPCs.
When the dying state has played out (using a simple counter to keep the faded boyd visible for a short time), then the state of the bad guy is set to NPC_KILLED. This state triggers the calling of KILLNPC, Which Respawns the character.
===================================== CODE =====================================
With all theses different states to handle walking attacking and dying the code that moves and draws the NPCs has to modified to take them into account. Here is the current MoveNPCs subroutine, which is called by the main game loop"
===================================== CODE =====================================
In addition to moving the NPCs differently based on state, the drawing code also has to take into account that characters state. Different sequences for the walking and attacking animations have to be accounted for in the draw routine. This is where the dying sequence takes place as well. When the state is NPC_DYING the sprite is drawn using a gray color that renders the sprite with about 50% translucently. (the color is &H99FFFFFF, which has an RGB for white, but a 50% alpha or thereabouts.)
===================================== CODE =====================================
The DrawNPcs routine calls on the more specific drawnpc subroutine to do the actual work. This routine also checks the state to draw the attack animation. When you attack an NPC, that character goes into the NPC_ATTACKING state to fight back. The NPCs are still pretty dumb because they go about their business as if nothing happened if you stop fighting with them.
As long as your attacking them, the NPCS fight back.
The alpha channel support is utilized by drawing the NPC in ired when the NPCS are engaging the player in combat. I wanted to clearly show when an NPC is attacking your player because the attack animations are so similar to the walking animations; its hard to tell exactly which NPC is fighting back. The red coloration of the sprite is a fantastic effect in fact i like it so much that i think it should be a part of the game and left in place it would be cool to use this coloring effect for other states and you can use it with some great results for things like spells and so on.
As you know the NPCs need to be drawn even when they arent just walking or attacking, Because the other states ( such as NPC_TALKING) must habve the sprite being updated on the screen. DRAWNPC checks for new states and then assumes NPC_WALKING for any state that is not explicitly programmed to handle everything else that the NPC might be doing. If you add animations to the NPCs you need to add the state condition here to account for it
===================================== CODE =====================================
Well that is it i hope you all injoyed this tutorial if you need any help please dont hesitate to ask me
NOTE:NO ONE IS ALLOWED TO COPY ANY OF THIS MATERIAL WITHOUT MY CONSENT PLEASE BE NICE AND LISTEN TO WHAT I HAD TO SAY I WROTE THIS TUTORIAL TODAY AND IT TOOK ME A LONG TIME PLEASE JUST DO NOT COPY IT IF YOU DO WITHOUT MY CONSENT PLEASE JUST GIVE ME CREDIDATION FOR MY WORK THANKYOU VERY MUCH
Hello guys im going to show you all a script that most begginer game VB programmers use for a combat system this is one of my scripts i ever used in the line of combat systems.
Step 1. Engaging in Combat
There are two basic things you need to do to allow the player to fight with NPCS:
1.Make sure the player is close enough to an enemy to hit him.
2.Make sure the player is facing an enemy while attacking.
If you can take care of these two problems, then you can create a combat system for the game. Tackle these two key problems in order.
Step 2. The first thing
The first thing you need to check on before you can handle a combat strike is whether the player is close enough to an enemy character to actually hit him. That is accomplished with a simple call to the Collision function. If there is no collision between the two sprites, then there definitely can't be an attack, and the swing misses! After determining if the two sprites are close enough for an attack, see if the attacker is at least facing the enemy.
I wrote a function called IsFacing that returns True or False depending on whether one sprite is facing another sprite. This is also useful for pitting NPCs against each other, not just for the player.
===================================== CODE =====================================
Code:
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 +spr2.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
Step 3. Managing the Player's State
After you have the combat code ready to go, There's just one little problem: The source code writing in the game so far just draws the walking version of the player. With combat, the player has to swing his weapon too. The main game loop has to be modified so that you can check the player's state and then draw either the walking or the attacking animations based on what the player is doing
I modified the game loop so that all the player update code is replaced with a single call to UpdateHero, Which is listed here:
===================================== CODE =====================================
Code:
Public Sub UpdateHero() Dim state As String Select Case PlayerData.state Case HERO_STOPPED State = "STOPPED" DrawSprite heroImgWalk, herSprWalk, 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 heroSprAttacking 'done attacking? go back to walking If herSprAttack.Animating = False Then PlayerData.State = HERO_STOPPED End If End If 'Draw the walking hero DrawSprite heroImgAttack, erSprAttack, 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
Step 4. Managing the Npc States
As you can see for yourself, there is a lot more to drawing the hero's sprite now that combat is involved.Now though, two different states have to be monitored and the correct sprite has to be animated. State engines are very helpful when you need to keep track of a uncomplicated series of conditions in a game, Because each condition is isolated from the rest, allowing you to write code for each condition separately.
Here is the new List of states being use for each NPC:
===================================== CODE =====================================
Code:
Public Enum NPCSTATES NPC__STOPPED = 0 NPC__WALKING = 1 NPC__PAUSED = 2 NPC__TALKING = 3 NPC__DYING = 4 NPC__KILLED = 5 NPC_ATTACKING = 6 End Num
===================================== CODE =====================================
Code:
Public Sonst NUMCHARS As Long =2 Public CharWalk(NUMCHARS) As Direct3Texture8 Public CharAttack(NUMCHARS) As Direct3DTexture8 Public Char Classes(NUMCHARS) As TCHARACTER 'unique data for each individual NPC Public Const NUMNPCS As Long = 10 Public CharStates(NUMNPCS) As TNPC Public CharWalkSpr(NUMNPCS) As TSPRITE Public CharAttackSpr(NUMNPCS) As TSPRITE
Step 5. Checking for Attack Hits on NPCs
The section of code under the HERO_ATTACKING state includes a call to a subroutine called CheckForHits:
===================================== CODE =====================================
Code:
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)) Tehn
AttackNPC CharStates (N), ATTACKVALUE
Exit For
End If
Next N
End Sub
Step 6. Doing Damage to an NPC
Now take a look at the AttackNPC subroutine that is called from the preceding routine you just looked at. This new routine is actually only called when the player has definitely hit an NPC. When this happens, the NPCS health needs to be cut down by an appropiate amount, and he dies if health is 0! aAttack NPC has some test code that prints a message above the player, and message above the target NPC during an attack, To tell you that hte game registered the hit. When the NPCS's Health reaches 0 the state of the character is set to NPC_DYING.
===================================== CODE =====================================
Code:
Public Sub AttackNPC9ByRef target As TNPC, ByVal attack As long)
'fight back
target.state = NPC_ATTACKING
'decrease health
target.health - attack
If target.health < 1 Then
target.state = NPC_DYING
End If
' Display a message to indicate the NPC was hit!
printText frontimg, fontSrp, __
Herosprattack.x, herosprattack.y, C_WHITE,_
"Take that1! (" & attack & " prts)"
'make the target respond to the hit
Dim p As point
p.x = target.curpose.x - SCrollX
p.y = Target.curpose.y - ScrollY
printText fontImg, - fontspr ,_
p.x, p.y, C_WHITE, _
"Argh, Ive beenhit! (" & target.health & ")"
End sub
Step 7. Death Sequence
There is a death sequence where the NPC is frozen and fades into nothingness (a simple way to show that the NPC has died). If this happens, then h the NPCs state takes over the death allowing your players code to continue without worrying about dealing with the NPC's resting place. The state engine in characters.bas manages the state of the NPCs.
When the dying state has played out (using a simple counter to keep the faded boyd visible for a short time), then the state of the bad guy is set to NPC_KILLED. This state triggers the calling of KILLNPC, Which Respawns the character.
===================================== CODE =====================================
Code:
public sub KILLNPC(byRef dude as TNPC) dim p As point p.x = PLAYERSTARTx * TILEWIDTH + Random(1000) p.y = PLAYERSTARTY * TILEHEIGHT + Random(1000) with dude .startpos = p .curpos = p .speedDelay = 1 .speedCount = 0 .health = 20 .state = NPC_WALKING End With SetRandomDestination Dude End Sub
Step 8. Moving the state-based NPC
With all theses different states to handle walking attacking and dying the code that moves and draws the NPCs has to modified to take them into account. Here is the current MoveNPCs subroutine, which is called by the main game loop"
===================================== CODE =====================================
Code:
Public Sub MoveNPCs() Dim n As Long 'loop through all of the NPCs and move them For n = 0 To NUMNPCS - 1 Select Case charstates(n).State Case NPC_ATTACKING 'stop attacking if the player leaves or if I'm Dead... If charstates(n).health <0 Then Charstates(n).state = NPC_STOPPED End If If Not collision(charWalkSrp(n), herosprwalk) then charstates(n).state = NPC_STOPPED end if Case NPC_TALKING FacePlayer n Case NPC_PAuSED SetrandomDestination charstates(n) Case NPC_WALKING Move NPC n Case NPC_STOPPED SetRandomDestination charstates(N) case NPC_DYING Charstates(n).destpos = Charstates(n).curpos charStates(n).health = charStates(n).health - 1 If charStates(n).Health < -100 Then charstates(n).state = NPC_KILLED End If Case NPC_KILLED KILLNPC charstates(N) End Select Next n END SUB
Step 9. Drawing the state-based NPC
In addition to moving the NPCs differently based on state, the drawing code also has to take into account that characters state. Different sequences for the walking and attacking animations have to be accounted for in the draw routine. This is where the dying sequence takes place as well. When the state is NPC_DYING the sprite is drawn using a gray color that renders the sprite with about 50% translucently. (the color is &H99FFFFFF, which has an RGB for white, but a 50% alpha or thereabouts.)
===================================== CODE =====================================
Code:
Public Sub DrawnNPCs() dim n As Long 'loop through all of the NPCs and draw them For n = 0 To NUMNPCS - 1 Select Case charstates(n).state Case NPC_ATTACKING DrawNPC n, C_RED Charstates(n).state = NPC_WALKING Case NPC_TALKING DrawNPC n, C_WHITE CharStates(n) . state = NPC_WALKING If diStates.Key(KEY_SPACE) > 0 Then talktoplayer n end if case NPC_PAUSED DrawNPC n, C_WHITE Charstates(n) .state = NPC_WALKING Case NPC_WALKING drawNPC n, C_WHITE Case NPC_STOPPED DrawNPC n, C_WHITE charstates(n) .state = NPC-WALKING''Case NPC_DYING DrawNPC n, &H99FFFFFF End Select Next N End SUB
As long as your attacking them, the NPCS fight back.
The alpha channel support is utilized by drawing the NPC in ired when the NPCS are engaging the player in combat. I wanted to clearly show when an NPC is attacking your player because the attack animations are so similar to the walking animations; its hard to tell exactly which NPC is fighting back. The red coloration of the sprite is a fantastic effect in fact i like it so much that i think it should be a part of the game and left in place it would be cool to use this coloring effect for other states and you can use it with some great results for things like spells and so on.
As you know the NPCs need to be drawn even when they arent just walking or attacking, Because the other states ( such as NPC_TALKING) must habve the sprite being updated on the screen. DRAWNPC checks for new states and then assumes NPC_WALKING for any state that is not explicitly programmed to handle everything else that the NPC might be doing. If you add animations to the NPCs you need to add the state condition here to account for it
===================================== CODE =====================================
Code:
Public Sub drawNPC(byVal num As Long, ByVal color As Long) Dim r As RECT Dim classindex As long ' grab a shortcut to theses long bariable names r.left = charstates(num) .curpose .x r.top = charstates(num) .curpos .y r.right = r.left + charwalkspr (num) .Width r.Bottom = r.top + charWalkspr(num) .height 'remember,image are referred to using the NPCs classinex! 'the sprite and state arrays are for every single unique NPC, 'but the bitmap image and class data are s hared by all NPCs classindex = charstates(num) .classindex 'now check to see if the sprite is within the scrolling viewport 'sprites position is actually global, so determine if its visible If r.left > ScrollX -1 And r.right <ScrollX + SCREENWIDTH + ! And _ r.top > SCrollY -1 And r.Bottom <ScrollY + SCREENHEIGHT =1 Then Select case charstates(num) .state case NPC_ATTACKING animatesprite charattackspr (num) charattackspr(num) .x = charstates(num) .curpos.x - ScrollX charattackspr(num) .animseq = charstates(num) .facing drawSprite charattack(classindex) , charattackspr(num), color Case Else 'update animation frame if walking animatesprite charWalkSpr(num) 'draw the sprite-remember, its using shared image charWalkSpr(num).x = charstates(num).curpos.x - scrollX charWalkSpr(num).y = charstates(num).curpos.x - scrolly charWalkSpr(num).Animseq = charstates(num).facing DrawSprite charwalk(classindex), charwalkspr(num) , color End Select End If End sub
NOTE:NO ONE IS ALLOWED TO COPY ANY OF THIS MATERIAL WITHOUT MY CONSENT PLEASE BE NICE AND LISTEN TO WHAT I HAD TO SAY I WROTE THIS TUTORIAL TODAY AND IT TOOK ME A LONG TIME PLEASE JUST DO NOT COPY IT IF YOU DO WITHOUT MY CONSENT PLEASE JUST GIVE ME CREDIDATION FOR MY WORK THANKYOU VERY MUCH


