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

local this=nil
local owner=nil
local pew={}
local charges=3

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

function anim(t)
   local player_pos=get_object_world_pos(owner)
   local spawn_pos=get_trigger_point_world_pos(get_trigger_point(1))
   local d=vec3_sqr_dist(player_pos,spawn_pos)
   if (d<25) then
      if (charges<3) then
         say(owner,"Ah, I feel power returning\nto the Orb!",3000,{ 0.2,1.0,0.1,1 })
         charges=3
      end
   end

   for i,p in ipairs(pew) do
      p.color=p.color-0.00025*t
      set_light_color(p.light,p.color,p.color,p.color,1)

      if (p.color<=0) then
         kill_light(p.light)
         table.remove(pew,i)
      end
   end
end

function trigger()
   if (charges==0) then
      say(owner,"Oh no, no more charges!",1000,{ 0.2,1.0,0.1,1 })
      return
   end
   -- Instance particle system
   local pos=get_object_world_pos(owner)
   local psystem=instance_particle_system("light_burst",pos)
   local l=instance_light(add_vec3(pos,{0,3,0}),30)
   table.insert(pew,{ color=1, light=l })
   charges=charges-1
end

function get_energy()
   return charges/3
end
