-- entity script

-- this method is called when the script is first connected to its object, so that you can initialize it
function OnCreate(self)

    -- set the model, and make it twice as big
    self:SetMesh("Models\\Warrior\\Warrior.model")
    self:SetScaling(2)
    self:SetTraceAccuracy(Vision.TRACE_AABOX)
    self:IncOrientation(90, 0, 0)
    
    --[[
    local propType = self:GetPropertyType("myProp")
    local propVal = self:GetProperty("myProp")
    Debug:PrintLine("")
    Debug:PrintLine("The entity's property 'myProp' is of type '" .. propType .. "' and contains the value: " .. tostring(propVal))
    ]]--
	
    local numProps = self:GetNumProperties();
    Debug:PrintLine("The entity has " .. numProps .. " prop(s)...")
    Debug:PrintLine("")

    -- test dynamic properties on components
    Debug:PrintLine("Creating component with dynamic property...")
    local myComponent = Game:CreateComponent("VFollowPathComponent")
    local compPropType = myComponent:GetPropertyType("Time")
    local compPropVal = myComponent:GetProperty("Time")
  
    -- add component
    self:AddComponent(myComponent) 
    
    Debug:PrintLine("The components's property 'Time' is of type '" .. compPropType .. "' and contains the value: " .. compPropVal)
    Debug:PrintLine("")
    
    Debug:PrintLine("Setting a new property value...")
    self:SetProperty("myProp", Vision.hkvVec3(1.2, 2.3, 3.4));
    propVal = self:GetProperty("myProp");
    Debug:PrintLine("The entity's new property value is: " .. tostring(propVal))

    -- accessing global, gamescript and scenescript variables
    Debug:PrintLine("")
    Debug:PrintLine(G.message)
    Debug:PrintLine(G.GameScript.message)
    Debug:PrintLine(G.SceneScript.message)
    
end


-- this method is called within regular intervals (use Timer:GetTimeDiff() to get the time since the last call)
function OnThink(self)
  
  -- call some methods
  -- self:SetCastShadows(not self:GetCastShadows())

  -- rotate entity over time
  t = Timer:GetTimeDiff()
  self:IncOrientation(t*10, 0, 0)
  
end


-- called before a scene update
function OnUpdateSceneBegin(self)
end


-- called after a scene update
function OnUpdateSceneFinished(self)
end


coldetected = false

-- called when this object collides with something
function OnCollision(self, info)
  if (coldetected ~= true) then
  
    coldetected = true
    
    Debug:PrintLine("Warrior: ouch!");
    Debug:PrintLine("A collision happened at the following touch point: " .. info.HitPoint)
    
    
    if (info.ColliderObject ~= nil) then    
      if (info.ColliderType == "Entity") then

        if(info.ColliderObject:HasKey()) then
          Debug:PrintLine("Entity collided with another entity: " .. info.ColliderObject:GetKey())
        else
          Debug:PrintLine("Entity collided with another entity...")
        end

      elseif (info.ColliderType == "Mesh") then
        Debug:PrintLine("Entity collided with another mesh: " .. info.ColliderObject:GetName())
      elseif (info.ColliderType == "Terrain") then
        Debug:PrintLine("Entity collided with the terrain.")
      elseif (info.ColliderType == "Decoration") then
        Debug:PrintLine("Entity collided with a decoration group.")
      end
    end

  end
end



-- called when this object is serialized and de-serialized (no OnDestroy/OnCreate call will be called when serializing and de-serializing)
function OnSerialize(self, ar)

  if (ar:IsLoading()) then
    Debug:PrintLine("");
    local s = ar:Read()
    Debug:PrintLine("Read back the following object: " .. s)
    local v = ar:Read()
    Debug:PrintLine("Read back the following object: " .. tostring(v))
    local tab = ar:Read()
    Debug:PrintLine("Read back the following table: " .. table.concat(tab, ","))
  else
    Debug:PrintLine("");
    Debug:PrintLine("Serializing entity: " .. self:GetKey())
    ar:Write("Hello, Lua")
    ar:Write(Vision.hkvVec3(-1, 0, 1))
    local tab = { "a", "b", "c", 1 }
    ar:Write(tab)
  end

end


-- called after the object has been fully restored after de-serialization
function OnDeserializationCallback(self)
  Debug:PrintLine("Deserialization call on object: " .. self:GetKey())
end
