-- DESCRIPTION: This object will be treated as a searchable container.
-- DESCRIPTION: Use logical connections to spawn "loot" items.
-- DESCRIPTION: For "loot" items, uncheck "show at start of level" in general tab.
-- DESCRIPTION: Delay trigger state -1 = waiting to be activated - Counts up to the set Delay Length Seconds number then resets to waiting state.
-- DESCRIPTION: Use [Empty!=0] to display the Empty Text after the player searches the object.
-- DESCRIPTION: Use [ShowActiveText!=0] to display the Active State Text after the player interacts with the object.
-- DESCRIPTION: Use [ShowFoundText!=0] to display the Found Text after the player searches the object. Use if you're spawning an item that already has a prompt such as health or ammo.
-- DESCRIPTION: Use [ShowTimer!=0] to display timer on screen for debug purposes or if you just want to show it.
-- DESCRIPTION: Adjust [DelayLengthSeconds=4(1,10)] to set the number of seconds to delay until the timer triggers in your other script.
-- DESCRIPTION: Plays the animation in objects Anim0 slot and the sound file in <Sound0> field when the object is searched by the player.
-- DESCRIPTION: Customize the [PromptText$="Search"] field to change the prompt text.
-- DESCRIPTION: Customize the [ActiveText$="Searching..."] field to change the active state prompt text.
-- DESCRIPTION: Customize the [FoundText$="You Found"] to change the prompt for finding items, after the player searches the object.
-- DESCRIPTION: Customize the [EmptyText$="Nothing There..."] field to change the prompt for an empty object, after the player searches the object.
-- DESCRIPTION: Customize the progress bar images in "scriptbank\\EyeFly\\images\\timers".
-- DESCRIPTION: Add 1 image for each second of delay including 0. 4 seconds will need 5 images, 0-4, and 3 seconds will need 4 images, 0-3, and so on.
-- ScriptModBy: EyeFlyGS

 -- Search object code
 g_search_active = {}
 local search_timer = {}
 local searchtimer_image0 = {}
 local searchtimer_image1 = {}
 local searchtimer_image2 = {}
 local searchtimer_image3 = {}
 local searchtimer_image4 = {}
 local searchtimer_image5 = {}
 local searchtimer_image6 = {}
 local searchtimer_image7 = {}
 local searchtimer_image8 = {}
 local searchtimer_image9 = {}
 local searchtimer_image10 = {}

 local searched = {}
 local search_object = {}
 local search_timer_image0 = {}
 local search_timer_image1 = {}
 local search_timer_image2 = {}
 local search_timer_image3 = {}
 local search_timer_image4 = {}
 local search_timer_image5 = {}
 local search_timer_image6 = {}
 local search_timer_image7 = {}
 local search_timer_image8 = {}
 local search_timer_image9 = {}
 local search_timer_image10 = {}

 -- Delay code
 local activate_stimer = {}
 local search_delay_trigger = {}
 local showstimer = {}
 local set_search_delay_length = {}
 local search_delay_length = {}
 local dlytimer = {}

function search_object_delayed_properties(e, empty, showactivetext, showfoundtext, showtimer, delaylengthseconds, prompttext, activetext, foundtext, emptytext)
 search_object[e]['initialstate'] = 0
 search_object[e]['empty'] = empty
 search_object[e]['showactivetext'] = showactivetext
 search_object[e]['showfoundtext'] = showfoundtext
 showstimer[e]["showtimer"] = showtimer
 search_delay_length[e]["delaylengthseconds"] = delaylengthseconds
 search_object[e]['prompttext'] = prompttext
 search_object[e]['activetext'] = activetext
 search_object[e]['foundtext'] = foundtext
 search_object[e]['emptytext'] = emptytext
 search_timer[e] = g_Entity[e]
end 

function search_object_delayed_init(e)
-- Search object code
 g_search_active = 0
 search_object[e] = {}
 searched[e] = 0
 search_object[e]['empty'] = 0
 search_timer[e] = g_Entity[e]
 
 ------------------------------------------------ load images ----------------------------------
 
 search_timer[e]['search_timer_image0'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer0.png" -- image to be loaded.
 searchtimer_image0 = CreateSprite(LoadImage(search_timer[e]['search_timer_image0']))
 SetSpriteSize(searchtimer_image0,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image0,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image1'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer1.png" -- image to be loaded. 
 searchtimer_image1 = CreateSprite(LoadImage(search_timer[e]['search_timer_image1']))
 SetSpriteSize(searchtimer_image1,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image1,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image2'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer2.png" -- image to be loaded.
 searchtimer_image2 = CreateSprite(LoadImage(search_timer[e]['search_timer_image2']))
 SetSpriteSize(searchtimer_image2,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image2,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image3'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer3.png" -- image to be loaded. 
 searchtimer_image3 = CreateSprite(LoadImage(search_timer[e]['search_timer_image3']))
 SetSpriteSize(searchtimer_image3,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image3,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image4'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer4.png" -- image to be loaded. 
 searchtimer_image4 = CreateSprite(LoadImage(search_timer[e]['search_timer_image4']))
 SetSpriteSize(searchtimer_image4,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image4,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image5'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer5.png" -- image to be loaded. 
 searchtimer_image5 = CreateSprite(LoadImage(search_timer[e]['search_timer_image5']))
 SetSpriteSize(searchtimer_image5,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image5,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image6'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer6.png" -- image to be loaded. 
 searchtimer_image6 = CreateSprite(LoadImage(search_timer[e]['search_timer_image6']))
 SetSpriteSize(searchtimer_image6,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image6,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image7'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer7.png" -- image to be loaded. 
 searchtimer_image7 = CreateSprite(LoadImage(search_timer[e]['search_timer_image7']))
 SetSpriteSize(searchtimer_image7,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image7,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image8'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer8.png" -- image to be loaded. 
 searchtimer_image8 = CreateSprite(LoadImage(search_timer[e]['search_timer_image8']))
 SetSpriteSize(searchtimer_image8,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image8,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image9'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer9.png" -- image to be loaded. 
 searchtimer_image9 = CreateSprite(LoadImage(search_timer[e]['search_timer_image9']))
 SetSpriteSize(searchtimer_image9,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image9,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 search_timer[e]['search_timer_image10'] = "scriptbank\\EyeFly\\images\\timers\\searchtimer10.png" -- image to be loaded. 
 searchtimer_image10 = CreateSprite(LoadImage(search_timer[e]['search_timer_image10']))
 SetSpriteSize(searchtimer_image10,5,-1) -------------------------------------------------------- image size. -1 keeps the aspect ratio original.
 SetSpritePosition(searchtimer_image10,200,200) ------------------------------------------------- don't need to change position here. change position lower down script.
 
 -- Delay timer code
 search_delay_trigger[e] = -1
 activate_stimer[e] = 0
 showstimer[e] = {}
 search_delay_length[e] = {}
 set_search_delay_length[e] = {}
 dlytimer[e] = 0
end


function search_object_delayed_main(e)
 ------------------------------------ Search object code ----------------------------------
if searched[e] <= 1 and search_delay_trigger[e] == -1 and g_search_active == 0 then
 if search_object[e]['prompttext'] == nil then search_object[e]['prompttext'] = "Search" end
 if search_object[e]['initialstate'] ~= nil then
  if search_object[e]['initialstate'] >=0 then
   if search_object[e]['initialstate'] == 0 then SetActivatedWithMP(e,101) end
   if search_object[e]['initialstate'] == 1 then SetActivatedWithMP(e,201) end
  end
 end
 local PlayerDist = GetPlayerDistance(e)
 if PlayerDist < 80 and g_PlayerHealth > 0 then
  local LookingAt = GetPlrLookingAtEx(e,1)
  if LookingAt == 1 then
   if g_Entity[e]['activated'] == 0 then
    if search_object[e]['prompttext'] ~= "" then
     if GetGamePlayerStateXBOX() == 1 then
      PromptLocalForVR(e,"Y Button " .. search_object[e]['prompttext'],3)
     else
      if GetHeadTracker() == 1 then
       PromptLocalForVR(e,"Trigger " .. search_object[e]['prompttext'],3)
      else
       PromptLocalForVR(e,"(E) " .. search_object[e]['prompttext'],3)
      end
     end
    end
    if g_KeyPressE == 1 then
     SetActivatedWithMP(e,201)
	 PlayAnimation(e,0)
	 g_search_active = 1
	 activate_stimer[e] = 1
	 searched[e] = 1
	 if search_object[e]['showactivetext'] == 1 then
	 PromptDuration( search_object[e]['activetext'], 2000 )
    end
	end
   else
    if g_Entity[e]['activated'] == 1 then
     if search_object[e]['prompttext'] ~= "" then
      if GetGamePlayerStateXBOX() == 1 then
       PromptLocalForVR(e,"(Y Button) " .. search_object[e]['prompttext'],3)
      else
       if GetHeadTracker() == 1 then
        PromptLocalForVR(e,"(Trigger) " .. search_object[e]['prompttext'],3)
       else
        PromptLocalForVR(e,"(E) " .. search_object[e]['prompttext'],3)
       end
      end
     end
     if g_KeyPressE == 1 then
      SetActivatedWithMP(e,101)
     end
    end
   end
 end
 end
 end
 ------- proximity independence --------
 if g_Entity[e]['activated'] == 100 then
  SetActivated(e,0)
 end
 if g_Entity[e]['activated'] == 101 then
  if search_object[e]['initialstate'] == -1 then 
  end
  search_object[e]['initialstate'] = -1
  SetActivated(e,102)
 end
 if g_Entity[e]['activated'] == 102 then
  if g_KeyPressE == 0 then SetActivated(e,0) end
 end
 if g_Entity[e]['activated'] == 201 then
  if search_object[e]['initialstate'] == -1 then 
   PlaySound(e,0)   
  end
  search_object[e]['initialstate'] = -1
  SetActivated(e,202)
 end
 if g_Entity[e]['activated'] == 202 then
  if g_KeyPressE == 0 then SetActivated(e,0) end
 end 
 if search_delay_trigger[e] == set_search_delay_length[e] and searched[e] == 1 then
 if search_object[e]['empty'] == 1 then
    searched[e] = 2
    PromptDuration( search_object[e]['emptytext'], 2000 )
	g_search_active = 0
	StopSound(e,0)
 else
 PerformLogicConnections(e)
 g_search_active = 0
 StopSound(e,0) 
 searched[e] = 2
 if search_object[e]['showfoundtext'] == 1 then
 PromptDuration( search_object[e]['foundtext'], 2000 )
 end
end
end

 ----------------------------- put images on screen ----------------------------------
 if search_delay_trigger[e] == 0 then
    PasteSpritePosition(searchtimer_image0,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 1 then
    PasteSpritePosition(searchtimer_image1,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 2 then
    PasteSpritePosition(searchtimer_image2,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 3 then
    PasteSpritePosition(searchtimer_image3,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 4 then
    PasteSpritePosition(searchtimer_image4,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 5 then
    PasteSpritePosition(searchtimer_image5,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 6 then
    PasteSpritePosition(searchtimer_image6,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 7 then
    PasteSpritePosition(searchtimer_image7,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 8 then
    PasteSpritePosition(searchtimer_image8,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 9 then
    PasteSpritePosition(searchtimer_image9,47.5, 45.6) -- change image position X,Y.
	else
 if search_delay_trigger[e] == 10 then
    PasteSpritePosition(searchtimer_image10,47.5, 45.6) -- change image position X,Y.
end
end
end
end
end
end
end
end
end
end
end

 -------------------------- Delay timer code ----------------------------
    set_search_delay_length[e] = search_delay_length[e]["delaylengthseconds"]
    if activate_stimer[e] == 1 then
    if dlytimer[e] == nil then
        dlytimer[e] = g_Time + 1000
    elseif g_Time > dlytimer[e] then
        dlytimer[e] = g_Time + 1000
		search_delay_trigger[e] = search_delay_trigger[e] +1
		if search_delay_trigger[e] > search_delay_length[e]["delaylengthseconds"] then
		search_delay_trigger[e] = -1
		activate_stimer[e] = 0
	end
	end
	end

 -------------------------- Delay Timer Display --------------------------
    if showstimer[e]["showtimer"] == 1 then
        TextCenterOnX( 50, 93, 3, search_delay_trigger[e] ) -- Change the numbers to adjust the timer on screen - (50 = X position, 93 = Y position, 3 = font size) For X, 0 = left side of the screen, 50 = center of screen, 100 = right side of the screen. For Y, 0 = top of screen, 50 = center of screen, 100 = bottom of screen.
	end
end -- main
