
std.include("scripts/common.lua")

local this=nil
local owner=nil
local pew={}
local charges=2000
local trigger_pressed=false
local time_of_last_use=0

function init(t,o)
   this=t
   owner=o
end

function anim(t)
   if ((get_time()-time_of_last_use)>4000) then
      if (charges<2000) then
         charges=charges+t
         if (charges>2000) then charges=2000 end
      end
   end
   if (trigger_pressed) then
      if (is_trigger_pressed()) then
         charges=charges-t
         if (charges>0) then
            set_screen_color(1,0.25,0.25,0.5)
            set_global_speed(0.15)
            set_sprite_speed(owner,6.6)
            time_of_last_use=get_time()
         else
            charges=0
            set_screen_color(1,1,1,0)
            set_global_speed(1.0)
            set_sprite_speed(owner,1)
            trigger_pressed=false
         end
      else
         set_screen_color(1,1,1,0)
         set_global_speed(1.0)
         set_sprite_speed(owner,1)
         trigger_pressed=false
      end
   end
end

function trigger()
   if (charges<=0) then
      say(owner,"It doesn't have any charge left!\nGot to wait for it to recharge!",1000,{ 0.2,1.0,0.1,1 })
      return
   end
   if (not trigger_pressed) then
      play_sound("speed_up",1)
   end
   trigger_pressed=true
   time_of_last_use=get_time()
end

function get_energy()
   return charges/2000
end
