-- DESCRIPTION: This object will be treated as a button that removes entities when player interacts with it.
-- DESCRIPTION: This object will also be treated as a button for activating other objects or game elements.
-- DESCRIPTION: Plays the animation in objects anim0 slot and the sound file in <Sound0> field when the object is clicked on by the player.
-- DESCRIPTION: Customize the [ButtonText$="Push Button"] field to change the button text.
-- DESCRIPTION: Type entity name to be removed in the [RemoveEntity$="Type Entity Name"] box.
-- DESCRIPTION: ALL ENTITIES WITH THAT NAME WILL BE REMOVED from game world.
-- DESCRIPTION: CHANGE the NAME of ALL ENTITIES YOU WANT REMOVED to that name.
-- DESCRIPTION: The entity to be removed MUST be set to DYNAMIC NOT STATIC physics On or Off and the NAME in the BOX MUST have an ENTITY with MATCHING NAME or the BUTTON WILL NOT WORK.
-- ScriptModBy: EyeFlyGames (ChrisWilson)

g_remove = {}

function remove_entity_button_properties(e, buttontext, removeentity)
 g_remove[e]['initialstate'] = 0
 g_remove[e]['buttontext'] = buttontext
 g_remove[e]['removeentity'] = removeentity
end 

function remove_entity_button_init(e)
 g_remove[e] = {}
end

function remove_entity_button_main(e)
 if g_remove[e]['buttontext'] == nil then g_remove[e]['buttontext'] = "Push Button" end
 if g_remove[e]['removeentity'] == nil then g_remove[e]['removeentity'] = "Type Entity Name" end
 for k,v in pairs(g_Entity) do
  if GetEntityName(k) == g_remove[e]['removeentity'] then
 if g_remove[e]['initialstate'] ~= nil then
  if g_remove[e]['initialstate'] >=0 then
   if g_remove[e]['initialstate'] == 0 then SetActivatedWithMP(e,101) end
   if g_remove[e]['initialstate'] == 1 then SetActivatedWithMP(e,201) end
  end
 end
 local PlayerDist = GetPlayerDistance(e)
 if PlayerDist < 60 and g_PlayerHealth > 0 then
  local LookingAt = GetPlrLookingAtEx(e,1)
  if LookingAt == 1 then
   if g_Entity[e]['activated'] == 0 then
    if g_remove[e]['buttontext'] ~= "" then
     if GetGamePlayerStateXBOX() == 1 then
      PromptLocalForVR(e,"Y Button " .. g_remove[e]['buttontext'],3)
     else
      if GetHeadTracker() == 1 then
       PromptLocalForVR(e,"Trigger " .. g_remove[e]['buttontext'],3)
      else
       PromptLocalForVR(e,"(E) " .. g_remove[e]['buttontext'],3)
      end
     end
    end
    if g_KeyPressE == 1 then
     SetActivatedWithMP(e,201)
    end
   else
    if g_Entity[e]['activated'] == 1 then
     if g_remove[e]['buttontext'] ~= "" then
      if GetGamePlayerStateXBOX() == 1 then
       PromptLocalForVR(e,"(Y Button) " .. g_remove[e]['buttontext'],3)
      else
       if GetHeadTracker() == 1 then
        PromptLocalForVR(e,"(Trigger) " .. g_remove[e]['buttontext'],3)
       else
        PromptLocalForVR(e,"(E) " .. g_remove[e]['buttontext'],3)
       end
      end
     end
     if g_KeyPressE == 1 then
      SetActivatedWithMP(e,101)
     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
  SetAnimationName(e,"on")
  PlayAnimation(e)
  if g_remove[e]['initialstate'] == -1 then 
   PerformLogicConnections(e)
   PlaySound(e,1) 
  end
  g_remove[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
  SetAnimationName(e,"on")
  PlayAnimation(e)
  if g_remove[e]['initialstate'] == -1 then 
   PerformLogicConnections(e)
   PlaySound(e,0)   
  end
  g_remove[e]['initialstate'] = -1
  SetActivated(e,202)
 end
 if g_Entity[e]['activated'] == 202 then
  if g_KeyPressE == 0 then SetActivated(e,0) end
  Destroy(k)
 end  
 end
 end
end
