Author Topic: Sword Script  (Read 2569 times)

sam

  • Fanatic
  • ***
  • Posts: 303
  • This statement is false.
    • MSN Messenger - samlancashire@hotmail.com
    • View Profile
    • samlancashire.com
    • Email
Sword Script
« on: 2006-04-10, 01:43:04 PM »
Okay, I wanna have it so the player can use a sword in my game.
Yes, I have looked at the Sword Sample project.
I looked through the script in wordpad (reading the comments).
The script is for both the "magic ball" ability and the sword.
My Player Sprite is 8-directional and so is my sword sprite.
How can I edit out the magic part.
Script is attached.

bat

  • Fanatic
  • ***
  • Posts: 234
  • Cheak out the B*t project
    • MSN Messenger - ethanbatmay@hotmail.com
    • Yahoo Instant Messenger - ethanbatmay
    • View Profile
    • The B*t project
    • Email
Re: Sword Script
« Reply #1 on: 2006-09-20, 09:46:04 PM »
'orginally generated by wizard; slightly modified by Mark
'deals with magical attack, see internal documentation
Sub DoFireButton1()

try taking out a bunch below that... i don't really know though...
~bat

sam

  • Fanatic
  • ***
  • Posts: 303
  • This statement is false.
    • MSN Messenger - samlancashire@hotmail.com
    • View Profile
    • samlancashire.com
    • Email
Re: Sword Script
« Reply #2 on: 2006-09-21, 02:19:16 PM »
this is fairly old...I gave up on all those sword games a long time ago...XD

cbass

  • Expert
  • Regular
  • *****
  • Posts: 97
  • script this
    • View Profile
    • Squest RPG
Re: Sword Script
« Reply #3 on: 2006-09-24, 11:46:28 PM »
I think i made the correct changes (i think), but I couldnt test it becuase when I went to find the original sample from the gameprojects link on main page, I kept getting rerouted to google.

Tell me if it works or not.



GRRRRR, also it wont let me upload the file.  Here is the txt from the file.

Code: [Select]
'====================================
' File: sprite stuff.vbs
' Description: Facilitates sword and magic use for player. Also switches sprites for player sprite with code instead of Map Special Functions
' Programmer: Mark Ribau (original shooting code be Ben Marty)
' Documentation: See each function for a description of its use
'====================================


Dim nExpectCount 'number of sprites to expect in
Dim nShot0Count 'number of sword sprites
Dim arBtn0Shots(0) 'sword sprites


Dim nButton0State

'generated by wizard
'watches for button/key press by player
Sub Player_OnControllerMove(OldActions, NewActions)
   If (Not OldActions) And NewActions And ACTION_BUTTON1 Then nButton0State = 1
   If (Not NewActions) And ACTION_BUTTON1 Then nButton0State = 0
End Sub

'generated by wizard
'apprears to remove references to sprites that are not on screen/in existence
Sub RecountShots()
   Dim nSIdx, nLIdx
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      nSIdx = 0
      Do While nSIdx < nShot0Count
         For nLIdx = 0 to .SpriteCount - 1
            If arBtn0Shots(nSIdx) Is .Sprite(nLIdx) Then
               Exit For
            End If
         Next
         If nLIdx >= .SpriteCount Then
            Set arBtn0Shots(nSIdx) = arBtn0Shots(nShot0Count - 1)
            nShot0Count = nShot0Count - 1
         Else
            nSIdx = nSIdx + 1
         End If
      Loop

      nExpectCount = .SpriteCount
   End With
End Sub

'generated by wizard
'removes a sprite from the layer/screen
Sub RemoveShot(Spr)
   Dim nIdx
   With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
      For nIdx = 0 to .SpriteCount - 1
         If .Sprite(nIdx) Is Spr Then .RemoveSprite(nIdx) : Exit Sub
      Next
   End With
End Sub

'originally generated by wizard; modified/debugged by Mark
'returns the appropriate "velocity" for a magic attack based on PlayerSprite's state
Function GetStateDeltas(DX, DY)
   With ProjectObj.GamePlayer.PlayerSprite
      Select Case .rDef.Template.StateType
      Case STATE_SINGLE
         If Abs(DX) + Abs(DY) > 1 Then
            DX = .DX / Abs(.DX) + Abs(.DY)
            DY = .DY / Abs(.DX) + Abs(.DY)
         Else
            If Abs(DX) + Abs(DY) < 1 Then DX = 0 : DY = -1
         End If
      Case STATE_LEFT_RIGHT
         DY = 0
         If (.CurState mod 2) = 0 Then DX = -1 Else DX = 1
      Case STATE_8_DIRECTION
         Select Case (.CurState mod 8)
         Case 0 : DX = 0 : DY = -1
         Case 1 : DX = 1 : DY = -1
         Case 2 : DX = 1 : DY = 0
         Case 3 : DX = 1 : DY = 1
         Case 4 : DX = 0 : DY = 1
         Case 5 : DX = -1 : DY = 1
         Case 6 : DX = -1 : DY = 0
         Case 7 : DX = -1 : DY = -1
         End Select
      Case Else
         DX = Cos((.CurState mod 36) * 3.14159 / 18)
         DY = -Sin((.CurState mod 36) * 3.14159 / 18)
      End Select
   End With
End Function

'orginally generated by wizard; modified by Mark
'deals with particularities of each sprite before each frame is drawn, see internal documentation for more information
Sub Player_OnAfterMoveSprites
   'setup some values for screen
   Dim nIdx, VLeft, VTop, VRight, VBottom, VDat
   With ProjectObj.GamePlayer
      VLeft = .MapScrollX
      VTop = .MapScrollY
      VRight = VLeft + .rMap.ViewWidth
      VBottom = VTop + .rMap.ViewHeight
   End With

   If nButton0State > 0 Then DoFireButton0

     
   If nExpectCount <> ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount Then RecountShots
'if player is attacking with sword
    if nShot0Count = 1 then
'check if the last frame of the sword is being displayed, if true, remove sword sprite, change player sprite to non-attacking sprite
If arBtn0Shots(0).CurFrame = arBtn0Shots(0).rDef.StateFrameCount(arBtn0Shots(0).CurState) - 1 then
RemoveShot arBtn0Shots(0)
nShot0Count = 0
nExpectCount = nExpectCount - 1
SwitchPlayerSprite "Player1Copy"
'Set ProjectObj.GamePlayer.PlayerSprite.rDef.Template = ProjectObj.GamePlayer.rMap.SpriteTemplates("Player")
'if false, fix direction (state) of sword sprite with respect to player sprite
'and move the sword sprite to appropriate location based on player sprite location
else
GetStateDeltas DX, DY
With ProjectObj.GamePlayer.PlayerSprite
arBtn0Shots(0).CurState = (.CurState mod 8)
     
If DX = 0 Then
arBtn0Shots(0).X = .X + (.Width - arBtn0Shots(0).Width) / 2
ElseIf DX = -1 Then
arBtn0Shots(0).X = .X - .Width
ElseIf DX = 1 Then
arBtn0Shots(0).X = .X + .Width
End If

If DY = 1 AND DX = 0 Then
arBtn0Shots(0).Y = .Y + .Height
ElseIf DY = 0 Then
arBtn0Shots(0).Y = .Y + (.Height - arBtn0Shots(0).Height) / 2
ElseIf DY = -1 AND DX = 0 Then
arBtn0Shots(0).Y = .Y - .Height
else
arBtn0Shots(0).Y = .Y + (.Height - arBtn0Shots(0).Height) / 2
End If
end with

end if
end if

   'manually advance animation frame of sword (due to an error in 1.2.3 -- motion/animate speed integrated)
   'NOTE: unnecessary looping involved: to be modified
   Dim nAnimIdx
   For nAnimIdx = 0 to nShot0Count - 1
      arBtn0Shots(nAnimIdx).AdvanceFrame 1
   Next
   
   
   ******MIGHT HAVE TO DELETE THESE 2 LINES TO MAKE IT WORK***********
   If nExpectCount <> ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer.SpriteCount Then RecountShots
   nIdx = 0
   ******MIGHT HAVE TO DELETE THESE 2 LINES TO MAKE IT WORK***********

End Sub

'originally generated by wizard; heavily modified by Mark
'deals with attacking with sword, see internal documentation
Sub DoFireButton0()
   Dim NewSpr, DX, DY

   nButton0State = nButton0State + 1
   If nButton0State < 20 And nButton0State > 2 Then Exit Sub Else nButton0State = 2


   'check for how many sword attacks there are on screen, if 1 or more, exit sub (so another sword attack will not be created)
   If nShot0Count >= 1 Then Exit Sub

   'create a sword sprite
   Set NewSpr = ProjectObj.GamePlayer.rMap.SpriteDefs("Sword").MakeInstance
   Set arBtn0Shots(nShot0Count) = NewSpr
   nShot0Count = 1
   With ProjectObj.GamePlayer.PlayerSprite
      .rDef.rLayer.AddSprite HostObj.AsObject(NewSpr)
      nExpectCount = nExpectCount + 1

      'set swords velocity to 0 (flying sword kind of odd in this case--though, this could be useful in a Zelda-esque charged sword situation)
      GetStateDeltas DX, DY
      NewSpr.DX = 0
      NewSpr.DY = 0

'use DX and DY to determine the placement of sword sprite with respect to player sprite     
If DX = 0 Then
NewSpr.X = .X + (.Width - NewSpr.Width) / 2  'center sword horozontally
ElseIf DX = -1 Then
NewSpr.X = .X - .Width 'put sword on left
ElseIf DX = 1 Then
NewSpr.X = .X + .Width 'put sword on right
End If

If DY = 1 AND DX = 0 Then
NewSpr.Y = .Y + .Height 'put sword on bottom
ElseIf DY = 0 Then
NewSpr.Y = .Y + (.Height - NewSpr.Height) / 2  'center sword vertically
ElseIf DY = -1 AND DX = 0 Then
NewSpr.Y = .Y - .Height 'put sword on top
else
NewSpr.Y = .Y + (.Height - NewSpr.Height) / 2  'center sword vertically
End If

if ProjectObj.GamePlayer.PlayerSprite.CurState > 7 then
      arBtn0Shots(0).CurState = ProjectObj.GamePlayer.PlayerSprite.CurState - 8
else
arBtn0Shots(0).CurState = ProjectObj.GamePlayer.PlayerSprite.CurState
end if

'change the player sprite to an attacking sprite
SwitchPlayerSprite "Player1Jab"
'Set ProjectObj.GamePlayer.PlayerSprite.rDef.Template = ProjectObj.GamePlayer.rMap.SpriteTemplates("PlayerJab")

   End With
End Sub

'generated by Wizard
'appears to change from X & Y velocities to "polar" state (36 state)
Function RectToPolarState(X, Y)
   Dim Angle, Pi
   Pi = 3.14159
   If X <> 0 Then
       Angle = Atn(-Y / X)
   Else
       Angle = -(Pi / 2) * Sgn(Y)
   End If
   If X < 0 Then
       Angle = Pi + Angle
   ElseIf Y > 0 Then
       Angle = Pi * 2 + Angle
   End If
   RectToPolarState = ((Angle * 18) / Pi) Mod 36
End Function

'catches special functions from map, both are called by collision definitions
'the "Kill" special function is used to add 1 to kill inventory item
'the "Die" special frunction is used to remove 5 HP from player, and then at 0 HP display a "game over" screen
Sub Player_OnSpecialFunction(SpecialFunction)
If SpecialFunction.Name = "Kill" then
ProjectObj.GamePlayer.InvQuantityOwned(1) = ProjectObj.GamePlayer.InvQuantityOwned(1) + 1
end if
if SpecialFunction.Name = "Die" then
ProjectObj.GamePlayer.InvQuantityOwned(0) = ProjectObj.GamePlayer.InvQuantityOwned(0) - 5
If ProjectObj.GamePlayer.InvQuantityOwned(0) = 0 then
ProjectObj.GamePlayer.ActivateFunction ProjectObj.Maps("Map").Specials("Game Over")
ProjectObj.GamePlayer.bQuit = True
End If
End If

End Sub

'switches player sprite from one sprite to another based on the name of the new sprite
Sub SwitchPlayerSprite(NewSpriteName)
       Dim NewSpr
       Dim Idx
       Dim MyState
       MyState = ProjectObj.GamePlayer.PlayerSprite.CurState
       With ProjectObj.GamePlayer.PlayerSprite.rDef.rLayer
             Set NewSpr = .pMap.SpriteDefs(NewSpriteName).MakeInstance
             .AddSprite(NewSpr)
             NewSpr.X = ProjectObj.GamePlayer.PlayerSprite.X
             NewSpr.Y = ProjectObj.GamePlayer.PlayerSprite.Y
             For Idx = 0 to .SpriteCount - 1
                   If .Sprite(Idx) Is ProjectObj.GamePlayer.PlayerSprite Then
                         .RemoveSprite Idx
                         Exit For
                   End If
             Next
             Set ProjectObj.GamePlayer.PlayerSprite = NewSpr
       End With
       ProjectObj.GamePlayer.PlayerSprite.CurState = MyState       
End Sub

'required code to work (attaches script to project at runtime)
HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16