Advertisement
Guest User

flashlight gameguru

a guest
Oct 21st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 KB | None | 0 0
  1. flashlight_battery_life = {}
  2. flashlight_battery_drain_delay = {}
  3. local new_flashlight_battery_drain_delay = {}
  4. local new_flicker_delay_off = nil
  5. local new_flicker_delay_on = nil
  6. local new_regen_delay = 0
  7.  
  8. local replace_battery_delay = 3 --how many seconds til battery reloading is complete
  9. flashlight_batteries = 0 --number of batteries player starts with (can be 0, forcing player to have no flashlight until a battery is found *note, the flashlight will still have a battery inside it so remember to set regen_delay to a very high number)
  10. max_flashlight_batteries = 5 --maximum batteries player can hold at one time
  11. local flickering_on = 1 --set to 1 for flickering effect, 0 for constant on (when flstate is on)
  12. local flickering_perc_start = 40 --what % of battery life the flickering will begin
  13. local flicker_delay_off = math.random(1,3) --how many seconds before flashlight can go off again while flickering
  14. local flicker_delay_on = 0.2 --how long til light comes back on while flickering
  15.  
  16. local regen_while_off = 0 --set to 0 to turn battery automatically restore power while off
  17. local regen_delay = 10 --this is actually a modifier and multiplies the drain speed by this to get the value (so a drain of 0.5s becomes 2s to restore 1% at 4)
  18.  
  19. local current_battery = 0
  20. flstate = "off"
  21. fpressed = 0
  22. local timer = {}
  23.  
  24. local show_battery_icon = 1 --set to 0 if you aren't using spare batteries and don't want the sprite icon to appear on screen
  25. local fl_i = LoadImage("scriptbank\\images\\flashlight\\fl_icon.png") --path to the flash light icon images\\flashlight\\fl_icon
  26. local fl_sprite_posx = 13 --x position of the flashlight image on screen (% of screen)
  27. local fl_sprite_posy = 86 --y position of the flashlight image on screen (% of screen)
  28. local fl_sprite_sizex = 10 --x size of the flashlight image on screen (% of screen)
  29. local fl_sprite_sizey = 13 --y size of the flashlight image on screen (% of screen)
  30.  
  31. local flb_i = LoadImage("scriptbank\\images\\flashlight\\flb_icon.png") --path the the battery image
  32. local flb_sprite_posx = 23
  33. local flb_sprite_posy = 92
  34. local flb_sprite_sizex = 5
  35. local flb_sprite_sizey = 7
  36.  
  37. flashlight_batteries_txt = flashlight_batteries
  38. is_flashight_active = 0
  39.  
  40.  
  41. --sound at slot 0 is for replacing batteries
  42. --sound at slot 1 is for clicking on/off
  43. --set entity always active & use with flashlight_battery.lua for extra batteries
  44. function flashlight_test_init(e)
  45.     flashlight_battery_life[0] = 0
  46.     flashlight_battery_drain_delay[0] = 0
  47.     SetFlashLightKeyEnabled(0)
  48.     is_flashlight_active = 0
  49.      
  50.     if flashlight_batteries > 0 then
  51.         for a = 1,flashlight_batteries do
  52.             flashlight_battery_life[a] = 100 --starting amount of life for the batteries
  53.             flashlight_battery_drain_delay[a] = 0.2 --how fast the batteries drain (in seconds) original 0.2
  54.         end
  55.     end
  56.     timer[e] = 0
  57.  
  58. end
  59.  
  60.  
  61. function flashlight_test_main(e)
  62.     if is_flashlight_active == 0 then
  63.         flstate = "off"
  64.         return
  65.     end
  66.    
  67.     if fl_sprite == nil and fl_i ~= nil then
  68.         fl_sprite = CreateSprite(fl_i)
  69.         SetSpriteSize(fl_sprite,fl_sprite_sizex,fl_sprite_sizey)
  70.         SetSpritePosition(fl_sprite,200,200)
  71.         if show_battery_icon == 1 then
  72.             if flb_sprite == nil and flb_i ~= nil then
  73.                 flb_sprite = CreateSprite(flb_i)
  74.                 SetSpriteSize(flb_sprite,flb_sprite_sizex,flb_sprite_sizey)
  75.             end
  76.         end
  77.     else           
  78.         if flashlight_batteries == 0 then
  79.             current_battery = 0
  80.         end
  81.         SetFlashLightKeyEnabled(0)
  82.         if g_KeyPressF == 0 then reloading = 0 end
  83. if g_KeyPressF == 1 and fpressed < 3 then
  84.     fpressed = 2
  85.     if os.time() > timer[e] + (replace_battery_delay * 1.00) then
  86.             flstate = "off"--JCLEON 19/10/2019
  87.             SetFlashLight(0)--JCLEON 19/10/2019
  88.         if current_battery < flashlight_batteries then
  89.                if reloading == 0 then
  90.                 SetGamePlayerStateIsMelee(1)
  91.                reloading = 1
  92.                 end
  93.             TextCenterOnX(50,85,3,"Replacing flash light batteries...")                            
  94.             if os.time() > timer[e] + replace_battery_delay then
  95.                 PlaySoundIfSilent(e,1)
  96.                 PlaySoundIfSilent(e,0)
  97.                 current_battery = current_battery + 1
  98.                 flashlight_batteries_txt = flashlight_batteries_txt - 1
  99.                 timer[e] = os.time()
  100.                 fpressed = 3
  101.             end
  102.         else
  103.             TextCenterOnX(50,85,3,"No spare batteries remaining...")
  104.         end
  105.     end
  106.         end
  107.        
  108.        
  109.         if flstate == "on" then
  110.             if flashlight_battery_life[current_battery] > 0 then
  111.                 if new_flashlight_battery_drain_delay[current_battery] == nil then
  112.                     new_flashlight_battery_drain_delay[current_battery] = g_Time + (flashlight_battery_drain_delay[current_battery]*1000)
  113.                 end
  114.                 if flickering_on == 1 then
  115.                     if flashlight_battery_life[current_battery] <= flickering_perc_start then
  116.                         if new_flicker_delay_off == nil then
  117.                             new_flicker_delay_off = g_Time + (flicker_delay_off*1000)
  118.                             new_flicker_delay_on = g_Time + (flicker_delay_off*1000)
  119.                         end
  120.                         if g_Time > new_flicker_delay_off then
  121.                             SetFlashLight(0)
  122.                             new_flicker_delay_on = g_Time + (flicker_delay_on*1000)
  123.                             new_flicker_delay_off = g_Time + (math.random(flicker_delay_off*0.8,flicker_delay_off*1.2)*1000)
  124.                         end
  125.                         if g_Time > new_flicker_delay_on then
  126.                             SetFlashLight(1)
  127.                         end
  128.                     end
  129.                 end
  130.                 if g_Time > new_flashlight_battery_drain_delay[current_battery] then
  131.                     flashlight_battery_life[current_battery] = flashlight_battery_life[current_battery] - 1
  132.                     new_flashlight_battery_drain_delay[current_battery] = g_Time +  (flashlight_battery_drain_delay[current_battery]*1000)
  133.                 end
  134.             else
  135.                 SetFlashLight(0)
  136.                 new_regen_delay = g_Time + regen_delay
  137.                 flstate = "off"
  138.                 PlaySound(e,0)
  139.             end
  140.         elseif flstate == "off" then
  141.             if regen_while_off == 1 then
  142.                 if g_Time > new_regen_delay then
  143.                     new_regen_delay = g_Time + ((flashlight_battery_drain_delay[current_battery]*1000)*regen_delay)
  144.                     if flashlight_battery_life[current_battery] < 100 then
  145.                         flashlight_battery_life[current_battery] = flashlight_battery_life[current_battery] + 1
  146.                     end
  147.                 end
  148.             end
  149.         end
  150.  
  151.         if g_KeyPressF == 0 then
  152.             timer[e] = os.time()
  153.             if fpressed == 2 then
  154.                 PlaySound(e,0)
  155.                 if flstate == "off" then
  156.                     flstate = "on"
  157.                     SetFlashLight(1)
  158.                 else
  159.                     new_regen_delay = g_Time + ((flashlight_battery_drain_delay[current_battery]*1000)*regen_delay)
  160.                     flstate = "off"
  161.                     SetFlashLight(0)
  162.                 end
  163.                 fpressed = 0
  164.             elseif fpressed == 3 then
  165.                 new_regen_delay = g_Time + ((flashlight_battery_drain_delay[current_battery]*1000)*regen_delay)
  166.                 SetFlashLight(0)
  167.                 flstate = "off"
  168.                 fpressed = 0
  169.             end
  170.         end
  171.      
  172.         PasteSpritePosition(fl_sprite,fl_sprite_posx,fl_sprite_posy)
  173.         TextCenterOnX(fl_sprite_posx+(fl_sprite_sizex/1.5),fl_sprite_posy+(fl_sprite_sizey/2),3,"   "..flashlight_battery_life[current_battery].."%")      
  174.         if show_battery_icon == 1 then
  175.             SetSpritePosition(flb_sprite,flb_sprite_posx,flb_sprite_posy)
  176.             PasteSprite(flb_sprite)
  177.             TextCenterOnX(flb_sprite_posx+(flb_sprite_sizex/1.5),flb_sprite_posy+(flb_sprite_sizey/2),3,"  x "..flashlight_batteries_txt)
  178.             SetSpritePosition(flb_sprite,200,200)
  179.         end
  180.     --TextCenterOnX(50,90,3,"spare batteries = "..remaining_batteries.."  ,  current battery life = "..flashlight_battery_life[current_battery].."%  ,  current drain per sec = 1% per "..flashlight_battery_drain_delay[current_battery].."s")
  181.     end
  182. end --main
  183.  
  184. function flashlight_exit(e)
  185.  
  186. end
  187.  
  188. function PlayerLooking(e,dis,v)
  189. if g_Entity[e] ~= nil then
  190. if dis == nil then
  191. dis = 3000
  192. end
  193. if v == nil then
  194. v = 0.5
  195. end
  196. if GetPlayerDistance(e) <= dis then
  197. local destx = g_Entity[e]['x'] - g_PlayerPosX
  198. local destz = g_Entity[e]['z'] - g_PlayerPosZ
  199. local angle = math.atan2(destx,destz)
  200. angle = angle * (180.0 / math.pi)
  201.  
  202. if angle <= 0 then
  203. angle = 360 + angle
  204. elseif angle > 360 then
  205. angle = angle - 360
  206. end
  207. while g_PlayerAngY < 0 or g_PlayerAngY > 360 do
  208. if g_PlayerAngY <= 0 then
  209. g_PlayerAngY = 360 + g_PlayerAngY
  210. elseif g_PlayerAngY > 360 then
  211. g_PlayerAngY = g_PlayerAngY - 360
  212. end
  213. end
  214. local L = angle - v
  215. local R = angle + v
  216. if L <= 0 then
  217. L = 360 + L
  218. elseif L > 360 then
  219. L = L - 360
  220. end
  221. if R <= 0 then
  222. R = 360 + R
  223. elseif R > 360 then
  224. R = R - 360
  225. end
  226.  
  227. if (L < R and math.abs(g_PlayerAngY) > L and math.abs(g_PlayerAngY) < R) then
  228. return 1
  229. elseif (L > R and (math.abs(g_PlayerAngY) > L or math.abs(g_PlayerAngY) < R)) then
  230. return 1
  231. else
  232. return 0
  233. end
  234. else
  235. return 0
  236. end
  237. end
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement