• 21 Avril 2019, 14:55:38


Auteur Sujet: Scripting Tutorial 7 - Bomp Shop  (Lu 5963 fois)

0 Membres et 1 Invité sur ce sujet

Hors ligne spykerc8

  • *
  • 'The GTAOnline.fr GodFather'
  • Messages: 3821
    • Voir le profil
    • http://www.gtaonline.fr
Scripting Tutorial 7 - Bomp Shop
« le: 14 Mars 2007, 15:04:50 »
Ce petit tutorial va vous montrer comment créer un bomb shop shop (atelier de bombe pour voiture) en scripting lua. 
Les 4 différentes bombes dans ce script s'installent via un checkpoint pour voiture dans l'atelier.

Les 4 bombes présentées dans la vidéo sont : 1. La bombe avec déclenchement à distance ; 2. La bombe à compte à rebours ; 3. La bombe au démarage ; 4. La bombe selon le poids du véhicule.

[yt=425,350]5BODD40H1-s[/yt]



Script complet



Réglages et définitions des paramètres :

Premièrement comme dans tout script, nous allons définir les variables. Nous allons ensuite créer la sphère du cp dans l'atelier.

Code: Lua
  1. cost = 10000
  2. --This is our global cost value, for how much a bomb costs
  3. bombshopCol = createColSphere ( 1850.3569335938, -1856.2515869141, 14.3828125, 2.4 )
  4. --we also make a col object to detect people entering it in the main body of the code
  5. --so it can be used in event handlers.
  6. addEventHandler ( "onResourceStart", getRootElement(), "bombshopLoad" )
  7. --when the resource is started
  8. function bombshopLoad ( name )
  9. if name ~= getThisResource () then return else
  10. --add a check to make sure that its the current resource
  11. local marker = createMarker ( 1850.3569335938, -1856.2515869141,
  12. 13.3828125, "cylinder", 3, 255, 0, 0, 200 )
  13. --create a pretty marker for our bombshop
  14. bombshopMenu = textCreateDisplay ()
  15. --create a menu text display
  16.  


Création des textes :

Nous allons ensuite créer les textes d'aide, de prix et de choix de bombes.

Code: Lua
  1. local menuText1 = textCreateTextItem ( "Press 1-5 to choose your bomb.", 0.15,
  2. 0.2, 2, 255, 255, 255, 255, 1.2 )
  3. local menuText2 = textCreateTextItem ( "Cost: $"..cost, 0.15, 0.227, 2, 255, 255, 255,
  4. 255, 1.2 ) --note the cost variable is used to display the cost
  5. local menuText3 = textCreateTextItem ( "1) Time bomb", 0.15, 0.262, 2, 255, 255, 255,
  6. 255, 1.2 )
  7. local menuText4 = textCreateTextItem ( "2) Detonator bomb", 0.15, 0.289, 2, 255, 255,
  8. 255, 255, 1.2 )
  9. local menuText5 = textCreateTextItem ( "3) Engine start bomb", 0.15, 0.316, 2, 255, 255,
  10. 255, 255, 1.2 )
  11. local menuText6 = textCreateTextItem ( "4) Weight bomb", 0.15, 0.37, 2, 255, 255, 255,
  12. 255, 1.2 )
  13. --add all this text to our bombshopMenu display
  14. textDisplayAddText ( bombshopMenu, menuText1 )
  15. textDisplayAddText ( bombshopMenu, menuText2 )
  16. textDisplayAddText ( bombshopMenu, menuText3 )
  17. textDisplayAddText ( bombshopMenu, menuText4 )
  18. textDisplayAddText ( bombshopMenu, menuText5 )
  19. textDisplayAddText ( bombshopMenu, menuText6 )
  20. end
  21. end
  22.  

Créer la bombe :

Régler la bombe quand vous rentrez dans la sphère du cp :

Code: Lua
  1. addEventHandler ( "onColShapeHit", bombshopCol, "bombshopEnter" )
  2. --when someone hits the bombshopCol collision shape
  3. function bombshopEnter ( element, dim )
  4. if ( getElementType ( element ) == "vehicle" ) then
  5. --check if the element is a vehicle
  6. local player = getVehicleOccupant ( element, 0 )
  7. --if it is, get the player inside this vehicle
  8. if getElementData ( element, "bombType" ) == "0" then
  9. --if the bombtype is blank
  10. textDisplayAddObserver ( bombshopMenu, player )
  11. --bind all the keys according to the menu. we dont provide any special arguments as we
  12. --can differentiate using keys
  13. bindKey ( player, "1", "down", "fitBomb", element )
  14. bindKey ( player, "2", "down", "fitBomb", element )
  15. bindKey ( player, "3", "down", "fitBomb", element )
  16. bindKey ( player, "4", "down", "fitBomb", element )
  17. else
  18. outputChatBox ( "This car is already rigged!", player )
  19. --if the bombtype is not blank, display a message that a bomb is already fitted.
  20. end
  21. end
  22.  


Achat de bombe :

Il faut maintenant créer la partie achat de bombe et gestion de l'argent. Nous allons aussi voir comment afficher les instructions.

Code: Lua
  1. function fitBomb ( player, key, keyState, vehicle )
  2. --this is the custom fitBomb function
  3. if getPlayerMoney ( player ) >= cost then
  4. --first we check the player has enough money.
  5. setElementData ( vehicle, "bombType", key )
  6. --flag the vehicle with the bombtype that is desired - according to the key pressed
  7. if key ~= "2" then
  8. --if the key does NOT == 2, in other words is not the detonator
  9. bomb then it must be a bomb that is activated like normal. Tell them to press fire to
  10. --arm the bomb, and bind the fire key to the "armBomb" function
  11. outputChatBox ( "Press fire to arm the bomb", player )
  12. bindKey ( player, "vehicle_fire", "down", "armBomb", vehicle )
  13. else
  14. --however, if it is the detonator bomb
  15. outputChatBox ( "Use the detonator to trigger the bomb", player )
  16. --tell them to use the detonator
  17. giveWeapon ( player, 40 ) --give them a detonator
  18. setElementData ( player, "detonaterVehicle", vehicle )
  19. --flag the player with the vehicle which the detonator blows up
  20. end
  21. --get rid of all our keybinds, and remove the text for the bombshop
  22. unbindKey ( player, "1", "down", "fitBomb" )
  23. unbindKey ( player, "2", "down", "fitBomb" )
  24. unbindKey ( player, "3", "down", "fitBomb" )
  25. unbindKey ( player, "4", "down", "fitBomb" )
  26. textDisplayRemoveObserver ( bombshopMenu, player )
  27. --play an activation sound
  28. playSoundFrontEnd ( player, 46 )
  29. --and remove money according to the cost
  30. takePlayerMoney ( player, math.abs(cost) )
  31. else
  32. --if he doesnt have enough money, tell him he cant afford to fit one
  33. outputChatBox ( "You cannot afford to fit a bomb!", player )
  34. end
  35. end
  36. addEventHandler ( "onColShapeLeave", bombshopCol, "bombshopLeave" )
  37. --when someone leaves the bombshopCol
  38. function bombshopLeave ( element, dim )
  39. if getElementType ( element ) == "player" then
  40. --if the element is a player
  41. --unbind all the keys to buy a bomb, and remove the menu.
  42. unbindKey ( element, "1", "down", "fitBomb" )
  43. unbindKey ( element, "2", "down", "fitBomb" )
  44. unbindKey ( element, "3", "down", "fitBomb" )
  45. unbindKey ( element, "4", "down", "fitBomb" )
  46. textDisplayRemoveObserver ( bombshopMenu, element )
  47. end
  48. end
  49.  


Déclencher ou régler la mise à feu

Nous allons vérifier la présence de bombe, ou armer la bombe à la sortie du véhicule.

Code: Lua
  1. addEventHandler ( "onVehicleEnter", getRootElement(), "bombshopVehicleEnter" )
  2. --when a vehicle is entered
  3. function bombshopVehicleEnter ( player, seat, jacked )
  4. local type = getElementData ( source, "bombType" )
  5. --get the bomb type
  6. if type == false then --if type returns false
  7. setElementData ( source, "bombType", "0" )
  8. --set it to a blank type
  9. elseif type ~= "2" and getElementData ( source, "armed" ) == false then
  10. bindKey ( player, "vehicle_fire", "down", "armBomb", source )
  11. --if its not type 2, and the vehicle is nor armed bind the vehicle fire button to
  12. --armBomb
  13. elseif type ~= "2" and getElementData ( source, "armed" ) ~= false then
  14. --if it isnt type 2, but IS armed, then we initiate any bomb functions that might occur
  15. unbindKey ( player, "vehicle_fire", "down", "armBomb" )
  16. --start off by getting rid of the keybind
  17. --ENGINE START BOMB.
  18. if type == "3" and getElementData ( source, "armed" ) ~= false then
  19. --if the type is a engine start bomb, i.e. 3, then we need to blow the vehicle when a
  20. --driver enters a vehicle.
  21. if seat == 0 then --check if the seat the player got into was id 0 - i.e. driver seat
  22. blowVehicle ( source ) --if it was, toast him
  23. end
  24. end
  25. --WEIGHT BOMB
  26. if type == "4" then
  27. --if it is a weight bomb, i.e. type 4, we need to blow the vehicle accoring to weight
  28. local i = 0
  29. --define a loop variable
  30. local totalPassengers = 0
  31. --define a loop total passengers
  32. while i ~= 8 do --loop until i == 8
  33. if ( getVehicleOccupant ( source, i ) ) then
  34. --check if the "i" id has a passenger inside
  35. totalPassengers = totalPassengers + 1
  36. --if it does, add totalPassengers by one
  37. end
  38. i = i + 1
  39. --add the loop variable by one
  40. end
  41. if totalPassengers > 1 then
  42. --if the total passengers in the vehicle, is indeed greater than one
  43. blowVehicle ( source )
  44. --fry the vehicle.
  45. end
  46. end
  47. end
  48. end
  49. addEventHandler ( "onVehicleExit", getRootElement(), "bombshopVehicleExit" )
  50. --when a vehicle is exited
  51. function bombshopVehicleExit ( player, seat, jacker )
  52. unbindKey ( player, "vehicle_fire", "down", "armBomb" )
  53. --get rid of any binds to activate the bomb.
  54. end
  55. --the arm bomb function - to activate it before the bomb can detonate
  56. function armBomb ( source, key, keyState, vehicle )
  57. setElementData ( vehicle, "armed", true )
  58. --flag the vehicle as armed
  59. outputChatBox ( "The bomb has been activated.", source )
  60. --announce it
  61. playSoundFrontEnd ( source, 42 )
  62. --play an activation sound
  63. unbindKey ( source, "vehicle_fire", "down", "armBomb" )
  64. --unbind the ability to activate it
  65. if getElementData ( vehicle, "bombType" ) == "1" then
  66. --if the bomb is a timebomb
  67. outputChatBox ( "10", source )
  68. --start a chatbox countdown from 10
  69. setTimer ( "timeBombCountDown", 1000, 1, 9, source, vehicle )
  70. --set a timer to call the timbBombCountDown after 1 second, with arguements of 9
  71. --seconds remainging, the player and the vehicle
  72. end
  73. end
  74. function timeBombCountDown ( currentTime, source, vehicle )
  75. if currentTime ~= 0 then
  76. --if the time is not 0
  77. playSoundFrontEnd ( source, 5 )
  78. --play a timer sound
  79. outputChatBox ( currentTime, source )
  80. --announce the remaining time
  81. currentTime = currentTime - 1
  82. --change to a new time
  83. setTimer ( "timeBombCountDown", 1000, 1, currentTime, source, vehicle )
  84. --call the function again in one second with the new time
  85. else --otherwise, if the time remaining is 0
  86. outputChatBox ( "BOOOOOM!" )
  87. --announce that it has been exploded
  88. blowVehicle ( vehicle )
  89. --blow it sky high.
  90. end
  91. end
  92. ------------------BOMB TWO - DETONATED ---------------
  93. --Possibly the most complex bomb. Requires a player to detonate it on foot
  94. addEventHandler ( "onPlayerWeaponSwitch", getRootElement(), "bombshopWeaponSwitch" )
  95. --use the onPlayerWeaponSwitch function to detect when a player switches his weapon
  96. function bombshopWeaponSwitch ( previousWeaponID, currentWeaponID )
  97. if currentWeaponID == 40 then
  98. --if he switches it to ID 40, i.e. the detonator
  99. bindKey ( source, "fire", "down", "detonateVehicle" )
  100. --then bind his fire key to the detonateVehicle function
  101. else
  102. unbindKey ( source, "fire", "down", "detonateVehicle" )
  103. --if it isn't id 40, unbind it
  104. end
  105. end
  106. function detonateVehicle ( source, key, keyState )
  107. --the detonateVehicle function the bind is bound do
  108. blowVehicle ( getElementData ( source, "detonaterVehicle" ) )
  109. --find the vehicle that the detonator is attached to by looking up a flag on the
  110. --player - then blow the crap out of it
  111. end
  112.  


Code complet :

Code: Lua
  1. cost = 10000 --This is our global cost value, for how much a bomb costs
  2. bombshopCol = createColSphere ( 1850.3569335938, -1856.2515869141, 14.3828125, 2.4 ) --we also make a col object to detect people entering it in the main body of the code so it can be used in event handlers.
  3.  
  4. addEventHandler ( "onResourceStart", getRootElement(), "bombshopLoad" ) --when the resource is started
  5. function bombshopLoad ( name )
  6.         if name ~= getThisResource () then return else --add a check to make sure that its the current resource
  7.         local marker = createMarker ( 1850.3569335938, -1856.2515869141, 13.3828125, "cylinder", 3, 255, 0, 0, 200 ) --create a pretty marker for our bombshop
  8.         bombshopMenu = textCreateDisplay () --create a menu text display
  9.         --Create our text items.  Displays instructions, the cost, and all possible bomb times.
  10.         local menuText1 = textCreateTextItem ( "Press 1-5 to choose your bomb.", 0.15, 0.2, 2, 255, 255, 255, 255, 1.2 )
  11.         local menuText2 = textCreateTextItem ( "Cost: $"..cost, 0.15, 0.227, 2, 255, 255, 255, 255, 1.2 ) --note the cost variable is used to display the cost
  12.         local menuText3 = textCreateTextItem ( "1) Time bomb", 0.15, 0.262, 2, 255, 255, 255, 255, 1.2 )
  13.         local menuText4 = textCreateTextItem ( "2) Detonator bomb", 0.15, 0.289, 2, 255, 255, 255, 255, 1.2 )
  14.         local menuText5 = textCreateTextItem ( "3) Engine start bomb", 0.15, 0.316, 2, 255, 255, 255, 255, 1.2 )
  15.         local menuText6 = textCreateTextItem ( "4) Weight bomb", 0.15, 0.37, 2, 255, 255, 255, 255, 1.2 )
  16.         --add all this text to our bombshopMenu display
  17.         textDisplayAddText ( bombshopMenu, menuText1 )
  18.         textDisplayAddText ( bombshopMenu, menuText2 )
  19.         textDisplayAddText ( bombshopMenu, menuText3 )
  20.         textDisplayAddText ( bombshopMenu, menuText4 )
  21.         textDisplayAddText ( bombshopMenu, menuText5 )
  22.         textDisplayAddText ( bombshopMenu, menuText6 )
  23.         end
  24. end
  25.  
  26. addEventHandler ( "onColShapeHit", bombshopCol, "bombshopEnter" ) --when someone hits the bombshopCol collision shape
  27. function bombshopEnter ( element, dim )
  28.         if ( getElementType ( element ) == "vehicle" ) then --check if the element is a vehicle
  29.                 local player = getVehicleOccupant ( element, 0 ) --if it is, get the player inside this vehicle
  30.                 if getElementData ( element, "bombType" ) == "0" then --if the bombtype is blank
  31.                         textDisplayAddObserver ( bombshopMenu, player )
  32.                         --bind all the keys according to the menu.  we dont provide any special arguments as we can differentiate using keys
  33.                         bindKey ( player, "1", "down", "fitBomb", element )
  34.                         bindKey ( player, "2", "down", "fitBomb", element )
  35.                         bindKey ( player, "3", "down", "fitBomb", element )
  36.                         bindKey ( player, "4", "down", "fitBomb", element )
  37.                 else
  38.                         outputChatBox ( "This car is already rigged!", player ) --if the bombtype is not blank, display a message that a bomb is already fitted.
  39.         end
  40. end
  41.  
  42. function fitBomb ( player, key, keyState, vehicle ) --this is the custom fitBomb function
  43.         if getPlayerMoney ( player ) >= cost then --first we check the player has enough money.
  44.                 setElementData ( vehicle, "bombType", key ) --flag the vehicle with the bombtype that is desired - according to the key pressed
  45.                 if key ~= "2" then --if the key does NOT == 2, in other words is not the detonator bomb
  46.                  --then it must be a bomb that is activated like normal.  Tell them to press fire to arm the bomb, and bind the fire key to the "armBomb" function
  47.                         outputChatBox ( "Press fire to arm the bomb", player )
  48.                         bindKey ( player, "vehicle_fire", "down", "armBomb", vehicle )
  49.                 else --however, if it is the detonator bomb
  50.                         outputChatBox ( "Use the detonator to trigger the bomb", player ) --tell them to use the detonator
  51.                         giveWeapon ( player, 40 ) --give them a detonator
  52.                         setElementData ( player, "detonaterVehicle", vehicle ) --flag the player with the vehicle which the detonator blows up
  53.                 end
  54.                 --get rid of all our keybinds, and remove the text for the bombshop
  55.                 unbindKey ( player, "1", "down", "fitBomb" )
  56.                 unbindKey ( player, "2", "down", "fitBomb" )
  57.                 unbindKey ( player, "3", "down", "fitBomb" )
  58.                 unbindKey ( player, "4", "down", "fitBomb" )
  59.                 textDisplayRemoveObserver ( bombshopMenu, player )
  60.                 --play an activation sound
  61.                 playSoundFrontEnd ( player, 46 )
  62.                 --and remove money according to the cost
  63.                 takePlayerMoney ( player, math.abs(cost) )
  64.         else --if he doesnt have enough money, tell him he cant afford to fit one
  65.                 outputChatBox ( "You cannot afford to fit a bomb!", player )
  66.         end
  67. end
  68.  
  69. addEventHandler ( "onColShapeLeave", bombshopCol, "bombshopLeave" )--when someone leaves the bombshopCol
  70. function bombshopLeave ( element, dim )
  71.         if getElementType ( element ) == "player" then --if the element is a player
  72.                 --unbind all the keys to buy a bomb, and remove the menu.
  73.                 unbindKey ( element, "1", "down", "fitBomb" )
  74.                 unbindKey ( element, "2", "down", "fitBomb" )
  75.                 unbindKey ( element, "3", "down", "fitBomb" )
  76.                 unbindKey ( element, "4", "down", "fitBomb" )
  77.                 textDisplayRemoveObserver ( bombshopMenu, element )    
  78.         end
  79. end
  80.  
  81. addEventHandler ( "onVehicleEnter", getRootElement(), "bombshopVehicleEnter" ) --when a vehicle is entered
  82. function bombshopVehicleEnter ( player, seat, jacked )
  83.         local type = getElementData ( source, "bombType" ) --get the bomb type
  84.         if type == false then --if type returns false
  85.                 setElementData ( source, "bombType", "0" ) --set it to a blank type
  86.         elseif type ~= "2" and getElementData ( source, "armed" ) == false then
  87.                 bindKey ( player, "vehicle_fire", "down", "armBomb", source ) --if its not type 2, and the vehicle is nor armed bind the vehicle fire button to armBomb
  88.         elseif type ~= "2" and getElementData ( source, "armed" ) ~= false then --if it isnt type 2, but IS armed, then we initiate any bomb functions that might occur
  89.                 unbindKey ( player, "vehicle_fire", "down", "armBomb" ) --start off by getting rid of the keybind
  90.                 --ENGINE START BOMB.
  91.                 if type == "3" and getElementData ( source, "armed" ) ~= false then --if the type is a engine start bomb, i.e. 3, then we need to blow the vehicle when a driver enters a vehicle.
  92.                         if seat == 0 then --check if the seat the player got into was id 0 - i.e. driver seat
  93.                                 blowVehicle ( source ) --if it was, toast him
  94.                         end
  95.                 end
  96.                 --WEIGHT BOMB
  97.                 if type == "4" then --if it is a weight bomb, i.e. type 4, we need to blow the vehicle accoring to weight
  98.                         local i = 0 --define a loop variable
  99.                         local totalPassengers = 0 --define a loop total passengers
  100.                         while i ~= 8 do --loop until i == 8
  101.                                 if ( getVehicleOccupant ( source, i ) ) then --check if the "i" id has a passenger inside
  102.                                         totalPassengers = totalPassengers + 1  --if it does, add totalPassengers by one
  103.                                 end
  104.                                 i = i + 1 --add the loop variable by one
  105.                         end
  106.                         if totalPassengers > 1 then --if the total passengers in the vehicle, is indeed greater than one
  107.                                 blowVehicle ( source ) --fry the vehicle.
  108.                         end
  109.                 end
  110.         end
  111. end
  112.  
  113. addEventHandler ( "onVehicleExit", getRootElement(), "bombshopVehicleExit" ) --when a vehicle is exited
  114. function bombshopVehicleExit ( player, seat, jacker )
  115.         unbindKey ( player, "vehicle_fire", "down", "armBomb" ) --get rid of any binds to activate the bomb.
  116. end
  117.  
  118. --the arm bomb function - to activate it before the bomb can detonate
  119. function armBomb ( source, key, keyState, vehicle )
  120.         setElementData ( vehicle, "armed", true ) --flag the vehicle as armed
  121.         outputChatBox ( "The bomb has been activated.", source ) --announce it
  122.         playSoundFrontEnd ( source, 42 ) --play an activation sound
  123.         unbindKey ( source, "vehicle_fire", "down", "armBomb" )--unbind the ability to activate it
  124.         if getElementData ( vehicle, "bombType" ) == "1" then --if the bomb is a timebomb
  125.                 outputChatBox ( "10", source ) --start a chatbox countdown from 10
  126.                 setTimer ( "timeBombCountDown", 1000, 1, 9, source, vehicle ) --set a timer to call the timbBombCountDown after 1 second, with arguements of 9 seconds remainging, the player and the vehicle
  127.         end
  128. end
  129.  
  130. function timeBombCountDown ( currentTime, source, vehicle )
  131.         if currentTime ~= 0 then --if the time is not 0
  132.                 playSoundFrontEnd ( source, 5 ) --play a timer sound
  133.                 outputChatBox ( currentTime, source ) --announce the remaining time
  134.                 currentTime = currentTime - 1 --change to a new time
  135.                 setTimer ( "timeBombCountDown", 1000, 1, currentTime, source, vehicle ) --call the function again in one second with the new time
  136.         else --otherwise, if the time remaining is 0
  137.                 outputChatBox ( "BOOOOOM!" ) --announce that it has been exploded
  138.                 blowVehicle ( vehicle ) --blow it sky high.
  139.         end
  140. end
  141.  
  142. ------------------BOMB TWO - DETONATED ---------------
  143. --Possibly the most complex bomb.  Requires a player to detonate it on foot
  144. addEventHandler ( "onPlayerWeaponSwitch", getRootElement(), "bombshopWeaponSwitch" ) --use the onPlayerWeaponSwitch function to detect when a player switches his weapon
  145. function bombshopWeaponSwitch ( previousWeaponID, currentWeaponID )
  146.         if currentWeaponID == 40 then --if he switches it to ID 40, i.e. the detonator
  147.                 bindKey ( source, "fire", "down", "detonateVehicle" ) --then bind his fire key to the detonateVehicle function
  148.         else
  149.                 unbindKey ( source, "fire", "down", "detonateVehicle" ) --if it aint id 40, unbind it
  150.         end
  151. end
  152.  
  153. function detonateVehicle ( source, key, keyState ) --the detonateVehicle function the bind is bound do
  154.         blowVehicle ( getElementData ( source, "detonaterVehicle" ) ) --find the vehicle that the detonator is attached to by looking up a flag on the player - then blow the crap out of it
  155. end
  156.  
  157. -------TEMP REMOVE FOR FINAL----
  158. addCommandHandler ( "money", "money" )
  159. function money ( source, cmd )
  160.         givePlayerMoney ( source, 3000000 )
  161. end
  162.  


~ Leader honoraire de la FsK Team ~ -
~ Membre honoraire de la PCP Team ~

Co-fondateur de GTAO - Retraité de la communauté

"Nous ne sommes rien, soyons tout !"


Hors ligne Labiloute

  • XSA
  • *
  • Tueur en série
  • Messages: 207
    • Voir le profil
    • GTA-Stunt.com
Re : Scripting Tutorial 7 - Bomp Shop
« Réponse #1 le: 14 Mars 2007, 17:43:45 »
RAS

:boum: