-- smashbros.levelgen
tau = 2 * math.pi
knockbackPerPercent = 1000

shipsAlive = {}

function textNearShip(ship, textMsg, team)
	local pos = ship:getPos()
	textitem = TextItem.new()
	textitem:setText(textMsg)
	textitem:setTeam(team)
	
	local textGeom = {
	point.new(pos.x - 150, pos.y + 25),
	point.new(pos.x + 150, pos.y + 25)
	}
	textitem:setGeom(textGeom)

	levelgen:addItem(textitem)
	removeObjectFromGameWithDelay(textitem, 500)
end
function removeObjectFromGameWithDelay(object, delay)
	if object ~= nil then
		Timer:scheduleOnce(function() object:removeFromGame() end, delay)
	end
end
function searchAndRemoveFromTable(someTable, valueObject)
	local localIndexToRemove = -1
	for i, v in ipairs(someTable)
	do
		if(v == valueObject) then
			localIndexToRemove = i
			break
		end
	end

	if localIndexToRemove ~= -1 then
		table.remove(someTable, localIndexToRemove)
		
	end
end
function insertIntoTableIfUnique(someTable, valueObject)
	local foundObject = false
	for i, v in ipairs(someTable)
	do
		if(v == valueObject) then
			foundObject = true
			break
		end
	end

	if foundObject == false then
		table.insert(someTable, valueObject)
	end
end

function launchShip(ship)
	shipAsString = tostring(ship)
	if(shipsAlive[shipAsString]["knockback"] ~= 0) then
		randomX = math.random(shipsAlive[shipAsString]["knockback"] * knockbackPerPercent) - (shipsAlive[shipAsString]["knockback"]/2)
		randomY = math.random(shipsAlive[shipAsString]["knockback"] * knockbackPerPercent) - (shipsAlive[shipAsString]["knockback"]/2)
		ship:setVel(point.new(randomX, randomY))
	end	
end


function onShipDamaged(ship, damagedAmount)
	shipAsString = tostring(ship)
	if(ship ~= nil) then
		ship:setHealth(1)
		if(shipsAlive[shipAsString] ~= nil) then
			shipsAlive[shipAsString]["knockback"] = shipsAlive[shipAsString]["knockback"] + damagedAmount
			textNearShip(ship, math.floor(shipsAlive[shipAsString]["knockback"]*50) .. "%!", ship:getTeamIndex())
			shipsAlive[shipAsString]["lastHealth"] = ship:getHealth()
			launchShip(ship)
		end
	end
end
function onShipEnteredZone(ship, zone, zoneType, zoneId)
	local info = ship:getPlayerInfo()
	local playerTeam = info:getTeamIndex()
	if(zoneId == 1) then --Touched boundaries
		ship:setHealth(0)
	end
end
function onTick()
	for index, value in pairs(shipsAlive) do
		if(value["ship"] ~= nil) then
			health = value["ship"]:getHealth()
			if(health ~= nil) then
				if(health < value["lastHealth"]) then
					logprint(health .. " < " .. value["lastHealth"])
					onShipDamaged(value["ship"], (value["lastHealth"] - health))
				end
			end
		end
	end
end

function onShipSpawned(ship)
	shipAsString = tostring(ship)
	shipsAlive[shipAsString] = {}
	shipsAlive[shipAsString]["knockback"] = 0
	shipsAlive[shipAsString]["lastHealth"] = ship:getHealth()
	shipsAlive[shipAsString]["ship"] = ship
end
function onShipKilled(ship)
	shipAsString = tostring(ship)
	searchAndRemoveFromTable(shipsAlive, shipAsString)
end

function main()
	subscribe(Event.ShipSpawned)
    subscribe(Event.ShipEnteredZone)
	subscribe(Event.Tick)
	subscribe(Event.ShipKilled)

	for a = 1, 50 do  --I like a clean console
		logprint(" ")
	end
end

function tprint (tbl, indent)
	if not indent then indent = 0 end
	for k, v in pairs(tbl) do
	  formatting = string.rep("  ", indent) .. k .. ": "
	  if type(v) == "table" then
		logprint(formatting)
		tprint(v, indent+1)
	  elseif type(v) == 'boolean' then
		logprint(formatting .. tostring(v))      
	  else
		logprint(formatting .. tostring(v))
	  end
	end
  end