require "UserData/Maps/LuaExamples/jutility"

local tagVertical		= 1 -- Platform moving vertically.
local tagHorizontal		= 2 -- Platform moving horizontally.
local tagVerticalSwitch	= 3 -- Switch for platform moving vertically.
local tagMoving			= 4 -- Tile moving to random locations within an area.
local tagThwomp			= 5 -- Player crusher.
local tagPrevMap		= 1001
local tagNextMap		= 1002

local verticalTick		= 0
local verticalStep		= 0
local verticalState		= 0 -- 0 = at bottom, 1 = moving up, 2 = at top, 3 = moving down
local verticalMax		= 6
local horizontalTick	= 0
local horizontalStep	= 0
local horizontalMax		= 12
local movingTick		= 0
local thwompTick		= 0
local thwompStep		= 0
local thwompState		= 0 -- 0 = at top, 1 = moving down, 2 = at bottom, 3 = moving up

-- Functions called by KFY:

function mapOnRoundBegin()
	displayMapName()
	verticalTick = 0
	verticalStep = 0
	verticalState = 0
	horizontalTick = 0
	horizontalStep = 0
	movingTick = 0
	thwompTick = 0
	thwompStep = 0
	thwompState = 0
end

function mapOnUpdate()
	-- Platform moving vertically:
	if (verticalState == 1 or verticalState == 3) and verticalTick == 2 then -- If platform is activated and ready to move.
		kfyMap:tileMove(tagVertical, 0, (verticalState == 3 and 1) or -1)
		verticalStep = verticalStep + 1
		if verticalStep > verticalMax - 1 then -- Platform has arrived at top/bottom.
			verticalState = (verticalState == 1 and 2) or 0
			verticalStep = 0
		end
		verticalTick = 0
	else verticalTick = verticalTick + 1 end

	-- Platform moving horizontally:
	if horizontalTick == 10 then
		kfyMap:tileMove(tagHorizontal, (horizontalStep < horizontalMax / 2 and 1) or -1, 0)
		horizontalStep = horizontalStep + 1
		if horizontalStep > horizontalMax - 1 then horizontalStep = 0 end
		horizontalTick = 0
	else horizontalTick = horizontalTick + 1 end

	-- Tile moving to random locations within an area:
	if movingTick == 20 then
		kfyMap:tileMoveTo(tagMoving, math.random(54,60), math.random(51,57))
		movingTick = 0
	else movingTick = movingTick + 1 end

	-- Thwomp:
	-- Start moving down if at top and player is below:
	if thwompState == 0 and thwompTick > 60 and kfyPlayerManager:isPlayerAtRect(69*16, 59*16, 4*16, 4*16) then
		kfyEffect:playSoundAt("AppData/Sounds/Powerups/pickup_trashcan.ogg", 1136.0, 930.0, 1.0)
		kfyMap:tileMapTilesetCoords(tagThwomp, 14, 14) -- Set angry face.
		thwompState = 1
		thwompTick = 4
	end

	if thwompState == 1 and thwompTick == 4 then -- Moving down.
		kfyMap:tileMove(tagThwomp, 0, 1)
		thwompStep = thwompStep + 1
		if thwompStep == 4 then  -- Arrived at floor.
			thwompState = 2
			thwompStep = 0
		end
		thwompTick = 0
	elseif thwompState == 2 and thwompTick == 100 then -- At bottom and ready to move up.
		kfyMap:tileMapTilesetCoords(tagThwomp, 12, 14) -- Set normal face.
		thwompState = 3
		thwompTick = 20 -- Will begin to move up at next update iteration.
	elseif thwompState == 3 and thwompTick == 20 then -- Moving up.
		kfyMap:tileMove(tagThwomp, 0, -1)
		thwompStep = thwompStep + 1
		if thwompStep == 4 then  -- Arrived at top.
			thwompState = 0
			thwompStep = 0
		end
		thwompTick = 0
	else thwompTick = thwompTick + 1 end
end

function mapOnTileLmbClick(tag, x, y, player)
	-- Next / Previous map buttons:
	if tag == tagPrevMap then
		kfyRound:prevMap(true)
	elseif tag == tagNextMap then
		kfyRound:nextMap(true)

	-- Button activating vertical platform:
	elseif tag == tagVerticalSwitch then
		if verticalState == 0 then -- Platform at bottom.
			verticalState = 1 -- Moving up.
			verticalTick = 2 -- To force movement at next update.
		elseif verticalState == 2 then -- Platform at top.
			verticalState = 3 -- Moving down.
			verticalTick = 2
		end
	end
end
