1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Visual Baisc RPG attack combat system script Tutorial

Discussion in 'Game Programming' started by XXxxImmortalxxXX, Jul 4, 2008.

  1. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
    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.

    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
    
    
    The array's that keep track of character classes, images and sprites have also been changed to accommodate the new combat system:

    ===================================== 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
    
    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?


    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
    
    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 =====================================

    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
    
    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
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Did you use VB IDE to write codes because I see you End Sub casing does not match in many of the sub routines.
     
  3. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
    No i just actually just typed all the craped in went over it once for grammer issues and just posted it if there is any errors in it feel free to edit it i as well will be looking over it now and be fixing more issues within the scripts
     
  4. Dra-cu

    Dra-cu New Member

    Joined:
    Jun 28, 2008
    Messages:
    24
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    designer
    Thanks to you :)
    VB seems to be more complicated but the structure is clear so i can grasp it and adapt it in actionscript easily ...many thanks again
     
  5. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
    lol np mate i just go so bored today lol so i felt like making one of theses you dont see lots of articles out there that give you a article on a acttack script combat system lol also i sent you a message saying get on windows live messenger and add me as a contact scyther777@live.com i also have yahoo messenger so you pick lol
     
    Last edited by a moderator: Jul 5, 2008
  6. Dra-cu

    Dra-cu New Member

    Joined:
    Jun 28, 2008
    Messages:
    24
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    designer
    I've added you as contact on messenger, and i try to send you the software i use for my game, but i have some problem to send it lol
     
  7. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
  8. Dra-cu

    Dra-cu New Member

    Joined:
    Jun 28, 2008
    Messages:
    24
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    designer
    lol ok send you the software, and added your new adress in my messenger :)
     
  9. bpd

    bpd New Member

    Joined:
    Aug 31, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    You have quite the nerve, XXxxImmortalxxXX. You're adamant that no one copies your "work", but fail to give any credit at all to Jonathan Harbour - the author of every bit of the code you've provided. As best as I can tell, every line of code comes verbatim from Jonathan's book, "Visual Basic Game Programming For Teens" :nonod:

    For example:

    • The code from Step #2 (Function IsFacing) comes from pages 313-314;
    • Step #3 (Sub UpdateHero) is from pages 315-316;
    • Step #4 (enum NPCSTATES and other declarations) is from page 316;
    • Step #5 (Sub CheckForHits) is from page 317;
    • Step #6 (AttackNPC) is from pages 317-318;
    • Step #7 (KillNPC) is from pages 318-319;
    • Step #8 (MoveNPCs) is from pages 319-320;
    • Step #9 (sub DrawNPCs and sub DrawNPC) is from pages 321-324.

    You, sir, are, to put it kindly, a fraud. :disappoin
     
    Last edited by a moderator: Aug 31, 2008
  10. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
    Hello im sry it took me to long to reply im on a vaction.

    First i would like to say im very very VERY sry for all of this.

    I had no idea this book came from a book i had read it off a forum on another vb site.And i had ask the user if i could post it on go4expert.com.

    He replyed back and gave all information to me (as in i can state thats its mine) as well as i did add some text of my own in there.


    I would like to apologize for all of this. I know you might hate me now and probably wont forgive me but i feel as if i need to apologize for this im very very VERY sry as well i taking the initiative to tell the user of his copyright infringmant.

    Thankyou so much for telling me this im very very very sry
     
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    As in Rooting Tutorial article you mentioned the author details I would assume you have the same here and till its clarified we have added the Additional details at the bigening of the article.

    Also bpd I have edited the link which was your amazon affiliate link and we do not allow affiliate links in posts.
     
  12. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
    okay well there is nothing to be clarified in this situation as stated this user gave me such rights to post this as well as give rights to state His sopposed to be article rights are entitiled to be mine.

    And sence we all just found out this script was made in a book i take no responsibility of this being mine anymore and rights are now given back to the orgianl user that stated he made it.

    Delete Topic
     
  13. XXxxImmortalxxXX

    XXxxImmortalxxXX New Member

    Joined:
    Jun 27, 2007
    Messages:
    569
    Likes Received:
    18
    Trophy Points:
    0
    i said delete topic please
     
  14. RockyMtnHi

    RockyMtnHi New Member

    Joined:
    Mar 22, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hard lessons learned in the world wide web. If you didn't write it then be careful where you put it, and who gets credit.
     
  15. AltF4

    AltF4 New Member

    Joined:
    Mar 31, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Cashier in the land of the bored...
    Location:
    Mars
    I know with some high graphic RPGs there is a common glitch where the character isfacing the opposite direction while attacking an object. And also, I can't find out how to make it so that when you have a character harpoon fish, the harpoon doesn't go into the water it stays on top... help?
     
  16. AltF4

    AltF4 New Member

    Joined:
    Mar 31, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Cashier in the land of the bored...
    Location:
    Mars
    I have a problem when the character is fishing making the harpoon look like it is going under the water. It stays on top and I would like to make it more realistic. Please help.
     
  17. AltF4

    AltF4 New Member

    Joined:
    Mar 31, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Cashier in the land of the bored...
    Location:
    Mars
    And I get no help -_-
     
  18. lyzcysco

    lyzcysco New Member

    Joined:
    Oct 2, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have read your topic. What's useful innformation for my job.
    I do agree with you. Those are the most effective way
    have a blessed day
     
  19. gamal zayed

    gamal zayed New Member

    Joined:
    Sep 28, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    please,i 'm new at this site
    so i need help to have more experience and send you some programmes to contradict it
     
  20. shipra123

    shipra123 New Member

    Joined:
    Oct 13, 2009
    Messages:
    62
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I am finding it quite difficult to use it.
     

Share This Page