• 28 Mars 2024, 20:26:14


Auteur Sujet: Scripting Tutorial 7 - Bomp Shop  (Lu 15072 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
Vidéo HD


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) [Sélectionner]
cost = 10000
--This is our global cost value, for how much a bomb costs
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.
addEventHandler ( "onResourceStart", getRootElement(), "bombshopLoad" )
--when the resource is started
function bombshopLoad ( name )
if name ~= getThisResource () then return else
--add a check to make sure that its the current resource
local marker = createMarker ( 1850.3569335938, -1856.2515869141,
13.3828125, "cylinder", 3, 255, 0, 0, 200 )
--create a pretty marker for our bombshop
bombshopMenu = textCreateDisplay ()
--create a menu text display


Création des textes :

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

Code: (lua) [Sélectionner]
local menuText1 = textCreateTextItem ( "Press 1-5 to choose your bomb.", 0.15,
0.2, 2, 255, 255, 255, 255, 1.2 )
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
local menuText3 = textCreateTextItem ( "1) Time bomb", 0.15, 0.262, 2, 255, 255, 255,
255, 1.2 )
local menuText4 = textCreateTextItem ( "2) Detonator bomb", 0.15, 0.289, 2, 255, 255,
255, 255, 1.2 )
local menuText5 = textCreateTextItem ( "3) Engine start bomb", 0.15, 0.316, 2, 255, 255,
255, 255, 1.2 )
local menuText6 = textCreateTextItem ( "4) Weight bomb", 0.15, 0.37, 2, 255, 255, 255,
255, 1.2 )
--add all this text to our bombshopMenu display
textDisplayAddText ( bombshopMenu, menuText1 )
textDisplayAddText ( bombshopMenu, menuText2 )
textDisplayAddText ( bombshopMenu, menuText3 )
textDisplayAddText ( bombshopMenu, menuText4 )
textDisplayAddText ( bombshopMenu, menuText5 )
textDisplayAddText ( bombshopMenu, menuText6 )
end
end

Créer la bombe :

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

Code: (lua) [Sélectionner]
addEventHandler ( "onColShapeHit", bombshopCol, "bombshopEnter" )
--when someone hits the bombshopCol collision shape
function bombshopEnter ( element, dim )
if ( getElementType ( element ) == "vehicle" ) then
--check if the element is a vehicle
local player = getVehicleOccupant ( element, 0 )
--if it is, get the player inside this vehicle
if getElementData ( element, "bombType" ) == "0" then
--if the bombtype is blank
textDisplayAddObserver ( bombshopMenu, player )
--bind all the keys according to the menu. we dont provide any special arguments as we
--can differentiate using keys
bindKey ( player, "1", "down", "fitBomb", element )
bindKey ( player, "2", "down", "fitBomb", element )
bindKey ( player, "3", "down", "fitBomb", element )
bindKey ( player, "4", "down", "fitBomb", element )
else
outputChatBox ( "This car is already rigged!", player )
--if the bombtype is not blank, display a message that a bomb is already fitted.
end
end


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) [Sélectionner]
function fitBomb ( player, key, keyState, vehicle )
--this is the custom fitBomb function
if getPlayerMoney ( player ) >= cost then
--first we check the player has enough money.
setElementData ( vehicle, "bombType", key )
--flag the vehicle with the bombtype that is desired - according to the key pressed
if key ~= "2" then
--if the key does NOT == 2, in other words is not the detonator
bomb 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
outputChatBox ( "Press fire to arm the bomb", player )
bindKey ( player, "vehicle_fire", "down", "armBomb", vehicle )
else
--however, if it is the detonator bomb
outputChatBox ( "Use the detonator to trigger the bomb", player )
--tell them to use the detonator
giveWeapon ( player, 40 ) --give them a detonator
setElementData ( player, "detonaterVehicle", vehicle )
--flag the player with the vehicle which the detonator blows up
end
--get rid of all our keybinds, and remove the text for the bombshop
unbindKey ( player, "1", "down", "fitBomb" )
unbindKey ( player, "2", "down", "fitBomb" )
unbindKey ( player, "3", "down", "fitBomb" )
unbindKey ( player, "4", "down", "fitBomb" )
textDisplayRemoveObserver ( bombshopMenu, player )
--play an activation sound
playSoundFrontEnd ( player, 46 )
--and remove money according to the cost
takePlayerMoney ( player, math.abs(cost) )
else
--if he doesnt have enough money, tell him he cant afford to fit one
outputChatBox ( "You cannot afford to fit a bomb!", player )
end
end
addEventHandler ( "onColShapeLeave", bombshopCol, "bombshopLeave" )
--when someone leaves the bombshopCol
function bombshopLeave ( element, dim )
if getElementType ( element ) == "player" then
--if the element is a player
--unbind all the keys to buy a bomb, and remove the menu.
unbindKey ( element, "1", "down", "fitBomb" )
unbindKey ( element, "2", "down", "fitBomb" )
unbindKey ( element, "3", "down", "fitBomb" )
unbindKey ( element, "4", "down", "fitBomb" )
textDisplayRemoveObserver ( bombshopMenu, element )
end
end


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) [Sélectionner]
addEventHandler ( "onVehicleEnter", getRootElement(), "bombshopVehicleEnter" )
--when a vehicle is entered
function bombshopVehicleEnter ( player, seat, jacked )
local type = getElementData ( source, "bombType" )
--get the bomb type
if type == false then --if type returns false
setElementData ( source, "bombType", "0" )
--set it to a blank type
elseif type ~= "2" and getElementData ( source, "armed" ) == false then
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
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
unbindKey ( player, "vehicle_fire", "down", "armBomb" )
--start off by getting rid of the keybind
--ENGINE START BOMB.
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.
if seat == 0 then --check if the seat the player got into was id 0 - i.e. driver seat
blowVehicle ( source ) --if it was, toast him
end
end
--WEIGHT BOMB
if type == "4" then
--if it is a weight bomb, i.e. type 4, we need to blow the vehicle accoring to weight
local i = 0
--define a loop variable
local totalPassengers = 0
--define a loop total passengers
while i ~= 8 do --loop until i == 8
if ( getVehicleOccupant ( source, i ) ) then
--check if the "i" id has a passenger inside
totalPassengers = totalPassengers + 1
--if it does, add totalPassengers by one
end
i = i + 1
--add the loop variable by one
end
if totalPassengers > 1 then
--if the total passengers in the vehicle, is indeed greater than one
blowVehicle ( source )
--fry the vehicle.
end
end
end
end
addEventHandler ( "onVehicleExit", getRootElement(), "bombshopVehicleExit" )
--when a vehicle is exited
function bombshopVehicleExit ( player, seat, jacker )
unbindKey ( player, "vehicle_fire", "down", "armBomb" )
--get rid of any binds to activate the bomb.
end
--the arm bomb function - to activate it before the bomb can detonate
function armBomb ( source, key, keyState, vehicle )
setElementData ( vehicle, "armed", true )
--flag the vehicle as armed
outputChatBox ( "The bomb has been activated.", source )
--announce it
playSoundFrontEnd ( source, 42 )
--play an activation sound
unbindKey ( source, "vehicle_fire", "down", "armBomb" )
--unbind the ability to activate it
if getElementData ( vehicle, "bombType" ) == "1" then
--if the bomb is a timebomb
outputChatBox ( "10", source )
--start a chatbox countdown from 10
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
end
end
function timeBombCountDown ( currentTime, source, vehicle )
if currentTime ~= 0 then
--if the time is not 0
playSoundFrontEnd ( source, 5 )
--play a timer sound
outputChatBox ( currentTime, source )
--announce the remaining time
currentTime = currentTime - 1
--change to a new time
setTimer ( "timeBombCountDown", 1000, 1, currentTime, source, vehicle )
--call the function again in one second with the new time
else --otherwise, if the time remaining is 0
outputChatBox ( "BOOOOOM!" )
--announce that it has been exploded
blowVehicle ( vehicle )
--blow it sky high.
end
end
------------------BOMB TWO - DETONATED ---------------
--Possibly the most complex bomb. Requires a player to detonate it on foot
addEventHandler ( "onPlayerWeaponSwitch", getRootElement(), "bombshopWeaponSwitch" )
--use the onPlayerWeaponSwitch function to detect when a player switches his weapon
function bombshopWeaponSwitch ( previousWeaponID, currentWeaponID )
if currentWeaponID == 40 then
--if he switches it to ID 40, i.e. the detonator
bindKey ( source, "fire", "down", "detonateVehicle" )
--then bind his fire key to the detonateVehicle function
else
unbindKey ( source, "fire", "down", "detonateVehicle" )
--if it isn't id 40, unbind it
end
end
function detonateVehicle ( source, key, keyState )
--the detonateVehicle function the bind is bound do
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
end


Code complet :

Code: (lua) [Sélectionner]
cost = 10000 --This is our global cost value, for how much a bomb costs
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.

addEventHandler ( "onResourceStart", getRootElement(), "bombshopLoad" ) --when the resource is started
function bombshopLoad ( name )
if name ~= getThisResource () then return else --add a check to make sure that its the current resource
local marker = createMarker ( 1850.3569335938, -1856.2515869141, 13.3828125, "cylinder", 3, 255, 0, 0, 200 ) --create a pretty marker for our bombshop
bombshopMenu = textCreateDisplay () --create a menu text display
--Create our text items.  Displays instructions, the cost, and all possible bomb times.
local menuText1 = textCreateTextItem ( "Press 1-5 to choose your bomb.", 0.15, 0.2, 2, 255, 255, 255, 255, 1.2 )
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
local menuText3 = textCreateTextItem ( "1) Time bomb", 0.15, 0.262, 2, 255, 255, 255, 255, 1.2 )
local menuText4 = textCreateTextItem ( "2) Detonator bomb", 0.15, 0.289, 2, 255, 255, 255, 255, 1.2 )
local menuText5 = textCreateTextItem ( "3) Engine start bomb", 0.15, 0.316, 2, 255, 255, 255, 255, 1.2 )
local menuText6 = textCreateTextItem ( "4) Weight bomb", 0.15, 0.37, 2, 255, 255, 255, 255, 1.2 )
--add all this text to our bombshopMenu display
textDisplayAddText ( bombshopMenu, menuText1 )
textDisplayAddText ( bombshopMenu, menuText2 )
textDisplayAddText ( bombshopMenu, menuText3 )
textDisplayAddText ( bombshopMenu, menuText4 )
textDisplayAddText ( bombshopMenu, menuText5 )
textDisplayAddText ( bombshopMenu, menuText6 )
end
end

addEventHandler ( "onColShapeHit", bombshopCol, "bombshopEnter" ) --when someone hits the bombshopCol collision shape
function bombshopEnter ( element, dim )
if ( getElementType ( element ) == "vehicle" ) then --check if the element is a vehicle
local player = getVehicleOccupant ( element, 0 ) --if it is, get the player inside this vehicle
if getElementData ( element, "bombType" ) == "0" then --if the bombtype is blank
textDisplayAddObserver ( bombshopMenu, player )
--bind all the keys according to the menu.  we dont provide any special arguments as we can differentiate using keys
bindKey ( player, "1", "down", "fitBomb", element )
bindKey ( player, "2", "down", "fitBomb", element )
bindKey ( player, "3", "down", "fitBomb", element )
bindKey ( player, "4", "down", "fitBomb", element )
else
outputChatBox ( "This car is already rigged!", player ) --if the bombtype is not blank, display a message that a bomb is already fitted.
end
end

function fitBomb ( player, key, keyState, vehicle ) --this is the custom fitBomb function
if getPlayerMoney ( player ) >= cost then --first we check the player has enough money.
setElementData ( vehicle, "bombType", key ) --flag the vehicle with the bombtype that is desired - according to the key pressed
if key ~= "2" then --if the key does NOT == 2, in other words is not the detonator bomb
--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
outputChatBox ( "Press fire to arm the bomb", player )
bindKey ( player, "vehicle_fire", "down", "armBomb", vehicle )
else --however, if it is the detonator bomb
outputChatBox ( "Use the detonator to trigger the bomb", player ) --tell them to use the detonator
giveWeapon ( player, 40 ) --give them a detonator
setElementData ( player, "detonaterVehicle", vehicle ) --flag the player with the vehicle which the detonator blows up
end
--get rid of all our keybinds, and remove the text for the bombshop
unbindKey ( player, "1", "down", "fitBomb" )
unbindKey ( player, "2", "down", "fitBomb" )
unbindKey ( player, "3", "down", "fitBomb" )
unbindKey ( player, "4", "down", "fitBomb" )
textDisplayRemoveObserver ( bombshopMenu, player )
--play an activation sound
playSoundFrontEnd ( player, 46 )
--and remove money according to the cost
takePlayerMoney ( player, math.abs(cost) )
else --if he doesnt have enough money, tell him he cant afford to fit one
outputChatBox ( "You cannot afford to fit a bomb!", player )
end
end

addEventHandler ( "onColShapeLeave", bombshopCol, "bombshopLeave" )--when someone leaves the bombshopCol
function bombshopLeave ( element, dim )
if getElementType ( element ) == "player" then --if the element is a player
--unbind all the keys to buy a bomb, and remove the menu.
unbindKey ( element, "1", "down", "fitBomb" )
unbindKey ( element, "2", "down", "fitBomb" )
unbindKey ( element, "3", "down", "fitBomb" )
unbindKey ( element, "4", "down", "fitBomb" )
textDisplayRemoveObserver ( bombshopMenu, element )
end
end

addEventHandler ( "onVehicleEnter", getRootElement(), "bombshopVehicleEnter" ) --when a vehicle is entered
function bombshopVehicleEnter ( player, seat, jacked )
local type = getElementData ( source, "bombType" ) --get the bomb type
if type == false then --if type returns false
setElementData ( source, "bombType", "0" ) --set it to a blank type
elseif type ~= "2" and getElementData ( source, "armed" ) == false then
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
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
unbindKey ( player, "vehicle_fire", "down", "armBomb" ) --start off by getting rid of the keybind
--ENGINE START BOMB.
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.
if seat == 0 then --check if the seat the player got into was id 0 - i.e. driver seat
blowVehicle ( source ) --if it was, toast him
end
end
--WEIGHT BOMB
if type == "4" then --if it is a weight bomb, i.e. type 4, we need to blow the vehicle accoring to weight
local i = 0 --define a loop variable
local totalPassengers = 0 --define a loop total passengers
while i ~= 8 do --loop until i == 8
if ( getVehicleOccupant ( source, i ) ) then --check if the "i" id has a passenger inside
totalPassengers = totalPassengers + 1  --if it does, add totalPassengers by one
end
i = i + 1 --add the loop variable by one
end
if totalPassengers > 1 then --if the total passengers in the vehicle, is indeed greater than one
blowVehicle ( source ) --fry the vehicle.
end
end
end
end

addEventHandler ( "onVehicleExit", getRootElement(), "bombshopVehicleExit" ) --when a vehicle is exited
function bombshopVehicleExit ( player, seat, jacker )
unbindKey ( player, "vehicle_fire", "down", "armBomb" ) --get rid of any binds to activate the bomb.
end

--the arm bomb function - to activate it before the bomb can detonate
function armBomb ( source, key, keyState, vehicle )
setElementData ( vehicle, "armed", true ) --flag the vehicle as armed
outputChatBox ( "The bomb has been activated.", source ) --announce it
playSoundFrontEnd ( source, 42 ) --play an activation sound
unbindKey ( source, "vehicle_fire", "down", "armBomb" )--unbind the ability to activate it
if getElementData ( vehicle, "bombType" ) == "1" then --if the bomb is a timebomb
outputChatBox ( "10", source ) --start a chatbox countdown from 10
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
end
end

function timeBombCountDown ( currentTime, source, vehicle )
if currentTime ~= 0 then --if the time is not 0
playSoundFrontEnd ( source, 5 ) --play a timer sound
outputChatBox ( currentTime, source ) --announce the remaining time
currentTime = currentTime - 1 --change to a new time
setTimer ( "timeBombCountDown", 1000, 1, currentTime, source, vehicle ) --call the function again in one second with the new time
else --otherwise, if the time remaining is 0
outputChatBox ( "BOOOOOM!" ) --announce that it has been exploded
blowVehicle ( vehicle ) --blow it sky high.
end
end

------------------BOMB TWO - DETONATED ---------------
--Possibly the most complex bomb.  Requires a player to detonate it on foot
addEventHandler ( "onPlayerWeaponSwitch", getRootElement(), "bombshopWeaponSwitch" ) --use the onPlayerWeaponSwitch function to detect when a player switches his weapon
function bombshopWeaponSwitch ( previousWeaponID, currentWeaponID )
if currentWeaponID == 40 then --if he switches it to ID 40, i.e. the detonator
bindKey ( source, "fire", "down", "detonateVehicle" ) --then bind his fire key to the detonateVehicle function
else
unbindKey ( source, "fire", "down", "detonateVehicle" ) --if it aint id 40, unbind it
end
end

function detonateVehicle ( source, key, keyState ) --the detonateVehicle function the bind is bound do
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
end

-------TEMP REMOVE FOR FINAL----
addCommandHandler ( "money", "money" )
function money ( source, cmd )
givePlayerMoney ( source, 3000000 )
end


~ 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: