require "UserData/Maps/LuaExamples/jutility"

local tagDoorArea       = 1 -- Door opened when a player is within a certain distance.
local tagDoorTouch      = 2 -- Door opened upon collision with player.
local tagDoorClick      = 3 -- Door opened when clicked on.
local tagAnimated       = 4 -- A group of animated tiles.
local tagDestroyButton  = 5 -- Button for destroying a group of tiles.
local tagDestroyBlocks  = 6 -- Tiles to be destroyed.
local tagPrevMap		= 1001
local tagNextMap		= 1002

local aniFrameTagged    = 0 -- Current frame for group of tiles.
local aniFrameAtPos     = 0 -- Current frame for lone tile.
local aniIsReversing    = false -- One animated tile will alternate the direction for its animation.
local aniNumFrames      = 7
local aniTick           = 0
local aniX              = 53 -- Position (in tiles) on map.
local aniY              = 60
local aniTsX            = 4 -- Tileset coords.
local aniTsY            = 8

-- Functions called by KFY:

function mapOnRoundBegin()
	displayMapName()
	aniFrameTagged = 0
	aniFrameAtPos = 0
	aniIsReversing = false
	aniTick = 0
end

function mapOnUpdate()
	-- Door that is open when a player is nearby:
	local doorAreaPos = kfyMap:tileGetPosition(tagDoorArea) -- Position of top left tile with tag 1.
	local doorAreaX = doorAreaPos.x * 16 + 8 -- Tile to pixel coords, center of door.
	local doorAreaY = doorAreaPos.y * 16 + 16
	local isPlayerNearDoor = kfyPlayerManager:isPlayerAtCircle(doorAreaX, doorAreaY, 80.0)
	local doorAreaIsHidden = kfyMap:tileIsHidden(tagDoorArea)
	if not doorAreaIsHidden and isPlayerNearDoor then
		kfyMap:tileSetHidden(tagDoorArea, true) -- Open door.
		kfyMap:tileSetSolid(tagDoorArea, false)
	elseif doorAreaIsHidden and not isPlayerNearDoor then
		kfyMap:tileSetHidden(tagDoorArea, false) -- Close door.
		kfyMap:tileSetSolid(tagDoorArea, true)
	end

	-- Animated tiles:
	if aniTick == 4 then -- Only update animations once every 4 iterations.
		aniTick = 0
		-- Animate 7 tagged tiles:
		aniFrameTagged = (aniFrameTagged < aniNumFrames - 1 and aniFrameTagged + 1) or 0 -- Note: Lua lacks a ternary operator.
		kfyMap:tileSetTilesetCoords(tagAnimated, aniTsX + aniFrameTagged, aniTsY)
		-- Animate single tile at position, in middle layer (1):
		if aniIsReversing then
			aniFrameAtPos = aniFrameAtPos - 1
			if aniFrameAtPos < 0 then
				aniIsReversing = false
				aniFrameAtPos = 1
			end
		else
			aniFrameAtPos = aniFrameAtPos + 1
			if aniFrameAtPos > aniNumFrames - 1 then
				aniIsReversing = true
				aniFrameAtPos = aniNumFrames - 2
			end
		end
		kfyMap:tileSetTilesetCoordsAt(aniX, aniY, 1, aniTsX + aniFrameAtPos, aniTsY)
	else aniTick = aniTick + 1 end
end

function mapOnTileActivate(tag)
	-- Touched door "waking up" after having been open for 2 seconds.
	if(tag == tagDoorTouch) then
		kfyMap:tileSetHidden(tagDoorTouch, false)
		kfyMap:tileSetSolid(tagDoorTouch, true)
	end
end

function mapOnTilePlayerCollision(tag, x, y, player)
	-- Door that opens when colliding with a player:
	if tag == tagDoorTouch then
		kfyMap:tileSetHidden(tagDoorTouch, true)
		kfyMap:tileSetSolid(tagDoorTouch, false)
		kfyMap:tileSleep(tagDoorTouch, 2.0) -- Prevents mapOnTilePlayerCollision to be called for this tag for 2 seconds.
	end
end

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

	-- Door that opens/closes when clicked on:
	elseif tag == tagDoorClick then
		kfyMap:tileSetHidden(tagDoorClick, not kfyMap:tileIsHidden(tagDoorClick))
		kfyMap:tileSetSolid(tagDoorClick, not kfyMap:tileIsSolid(tagDoorClick))

	-- Button that destroys all (placed) tiles with tag tagDestroyBlocks.
	elseif tag == tagDestroyButton then
		kfyMap:tileDamage(tagDestroyBlocks, 1020, true) -- Max HP for a tile is 255 * 4 = 1020
	end
end
