
maxPlayerNum = 2;    -- Just change this to add more players.
maxEnemyNum = 4;
playerControls = {}; -- Global thing with control numbers.

screenBottom = -384;

respawnWaveTimer = 30;

include("actor.lua");
include("player.lua");
include("spawnpoint.lua");
include("npc.lua");
include("bonusitem.lua");
include("bonusegg.lua");
include("autogen_spritedefs.lua");
include("spritefuncs.lua");

maxControlNum = 0;
function addControl(name)
   
   local ret = maxControlNum;
   setControlName(maxControlNum, name);
   maxControlNum = maxControlNum + 1;
   return ret;

end

function setupPlayerControls()

   local allPlayerControls = {};

   local function addControlForPlayer(currentPlayer, playerName, controlName)
      currentPlayer[controlName] = addControl(playerName.." "..controlName);
   end

   for i = 1, maxPlayerNum do
      
      local currentPlayer = {};

      local playerName = "Player "..i;

      addControlForPlayer(currentPlayer, playerName, "Left");
      addControlForPlayer(currentPlayer, playerName, "Right");
      addControlForPlayer(currentPlayer, playerName, "Jump");

      allPlayerControls[i] = currentPlayer;

   end

   return allPlayerControls;

end

function spawnPlayers()

   for i = 1, maxPlayerNum do

      if entitiesByName["Player_"..i] then

         -- Player is still alive.

      else

         -- No player. Spawn them.

         local newSpawn = getRandomSpawnPoint();

         if newSpawn then
            
            local newPlayer = Player:create();
            --newPlayer:setPos((i - maxPlayerNum/2) * 64, 0);
            
            newPlayer:setPos(newSpawn:getPos());

         end
      end
   end

end


function spawnEnemies()

   for i = totalNPCs, (maxEnemyNum-1) do
      
      local newSpawn = getRandomSpawnPoint();
      
      if newSpawn then
         
         local newEnemy = NPC:create();            
         newEnemy:setPos(newSpawn:getPos());
         
      end

   end

end


respawnWaveCountdown = 0;
function worldUpdate()

   if respawnWaveCountdown <= 0 then

      spawnPlayers();
      spawnEnemies();

      respawnWaveCountdown = respawnWaveTimer;

   else

      respawnWaveCountdown = respawnWaveCountdown - 1;

   end

end


playerControls = setupPlayerControls();

loadTiles("default.xml");
loadEntities("default.xml");

spawnPlayers();





