-------------------------------------------------------------------------------
-- ****************************************************************************
--  Entity: Final Boss
--  State: Stalk Around (3)
--  Phase: 3 of 3
-- ****************************************************************************
-------------------------------------------------------------------------------

--[[
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////                              Variables                              /////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
--]]

-- Define the table that will contain the state
MegaWhiteShark_Phase3_StalkAround = {}

-- Name of state
local STATE_NAME = "MegaWhiteShark_Phase3_StalkAround" -- a string
registerStateFSM( STATE_NAME )  -- do NOT touch this!!

-- Name of the state to change to when the necesary conditions are met
local OUT_STATE = "MegaWhiteShark_Phase3_DiveAndDisappear"

-- Timer
local TIME_IN_STATE = 5.5 -- a float number
local timer = CTimer( TIME_IN_STATE ) -- do NOT touch this!!

-- Animation
local ANIMATION_NAME = "swim" -- a string

-- In order to stalk around the lakeplants we need to know the index of the current
-- position to go towards it!
local CURRENT_STALK_INDEX = 0

local DISTANCE_OFFSET = 2.0


--[[
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////                               Functions                             /////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
--]]

local function getCurrentStalkPosition()

  -- calculate the positions where the megaWhiteShark will stalk around
  -- this is done by using the following formula:
  --   · offset_vector = lake_platform_i_position - central_platform_position
  --   · new_stalk_pos = central_platform_position + offset_vector * distance_offset
  
  local central_platform_position = getEntityPosition ( MWS_FINAL_LAKE_PLANTS[ 0 ] )
  
  local lake_platform_i_name = MWS_FINAL_STALK_AROUND_NAMES[ CURRENT_STALK_INDEX ]
  local lake_platform_i_position = getEntityPosition( lake_platform_i_name )
  
  local offset_vector = lake_platform_i_position - central_platform_position

  local new_stalk_pos = central_platform_position + offset_vector * DISTANCE_OFFSET
  
  return new_stalk_pos

end

local function selectNextStalkPosition()

  -- increase current platform counter
  CURRENT_STALK_INDEX = CURRENT_STALK_INDEX + 1
  
  -- calc the mod of the current platform
  CURRENT_STALK_INDEX = math.mod( CURRENT_STALK_INDEX, MWS_TOTAL_STALK_POSITIONS )
  
end

--[[
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
/////                               States                                /////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
--]]

-------------------------------------------------------------------------------
-- Enter
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_StalkAround["Enter"] = function(entity)

  -- play animation and restart the timer
  playAnimationCycle( entity:ID(), ANIMATION_NAME )
  timer:restart()
  
  -- set the direction to move forward
  entity:setDirection( Vector3(0.0, 0.0, 1.0) )
  
  -- TODO: canviar aixo per un tracker definit
  --local center_platform = Vector3(-82.5969,4.13399,-120.506)
  local reappear_pos = getEntityPosition( MWS_FINAL_LAKE_PLANTS[0] ) + Vector3(1,0,1) * 8
  setEntityPosition( entity:ID(), reappear_pos )
  
  -- recoloquem la camera
  setFinalBossPhase3Camera()
  
  -- per si acas, desactivem el modo dive per a que no floti en el aire
  entity:setDiveMode( false )
  
end


-------------------------------------------------------------------------------
-- Execute
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_StalkAround["Execute"] = function(entity)
  
  
  if ( onlyFinalLakePlant() ) then
    changeEntityState( entity:ID(), OUT_STATE )
  end
 
  local stalk_position = getCurrentStalkPosition()
  turnEnemyTo( entity:ID(), stalk_position, 150 )
  --entity:turnTo( stalk_position )
  
  local RUN_SPEED = entity:getRunSpeed()
  
  entity:moveFront( RUN_SPEED )
  
  local entity_pos = getEntityPositionByID( entity:ID() )
  
  stalk_position.y = 0
  entity_pos.y = 0
  local dist_to_stalk_pos_sq = distanceSquared( stalk_position, entity_pos )
  local area_detection_change_sq = 2*2 -- 2 meters squared
  
  -- if entity is in range of attacking the player attack him
  if( dist_to_stalk_pos_sq <= area_detection_change_sq ) then
    selectNextStalkPosition()
  end
  
  -- if NOT in range of attacking: keep running towards player
  if( timer:isOutOfTime() ) then
    changeEntityState( entity:ID(), OUT_STATE )
  end
  
end


-------------------------------------------------------------------------------
-- Exit
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_StalkAround["Exit"] = function(entity)

  -- remove animation cycle
  removeAnimationCycle( entity:ID(), ANIMATION_NAME )
  
end


-------------------------------------------------------------------------------
-- OnMessage
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_StalkAround["OnMessage"] = function(entity, message)
  -- if message NOT handled, return false
  return false
end


-------------------------------------------------------------------------------
-- GetNameOfCurrentState
-------------------------------------------------------------------------------
MegaWhiteShark_Phase3_StalkAround["GetNameOfCurrentState"] = function()
  return STATE_NAME
end
