-- 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: Use [Empty!=0] to disable item spawning and display the Empty Text after the player searches 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: 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 [FoundText$="You Found"] to change the prompt for finding items, after the player searches the object.
-- DESCRIPTION: Customize the [EmptyText$="Nothing There..."] to change the prompt for an empty object, after the player searches the object.
-- ScriptModBy: EyeFlyGames (ChrisWilson)

local search = {}
local searched = {}
local search_object = {}


function search_object_properties(e, empty, showfoundtext, prompttext, foundtext, emptytext)
 search_object[e]['initialstate'] = 0
 search_object[e]['empty'] = empty
 search_object[e]['showfoundtext'] = showfoundtext
 search_object[e]['prompttext'] = prompttext
 search_object[e]['foundtext'] = foundtext
 search_object[e]['emptytext'] = emptytext
 search[e] = g_Entity[e]
end 

function search_object_init(e)
 search_object[e] = {}
 searched[e] = 0
 search_object[e]['empty'] = 0
 search[e] = g_Entity[e]
end

function search_object_main(e)
if searched[e] <= 1 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)
	 searched[e] = 1
    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)
   PlayAnimation(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 searched[e] == 1 then
 if search_object[e]['empty'] == 1 then
    searched[e] = 2
    PromptDuration( search_object[e]['emptytext'], 2000 )
 else
 PerformLogicConnections(e) 
 searched[e] = 2
 if search_object[e]['showfoundtext'] == 1 then
 PromptDuration( search_object[e]['foundtext'], 2000 )
end
end
end
end
