------------------------------------------------------------------------
-- Spawner class. This isn't just an example/demo class. This is part
-- of the engine. Messing with it isn't recommended unless you know
-- what you're doing.
------------------------------------------------------------------------

Spawner = Entity:createSubClass();
Spawner.baseClass = nil;
Spawner.spawnDone = false;
Spawner.baseClassName = "";
Spawner.isSpawner = true;
Spawner.iconArea = {};
Spawner.iconTextureName = "";

function Spawner:init()
	Entity.init(self);

	if not self._loadedXML then
		-- These will all be saved by the Entity serializer.
		self:setBlockable(false);
		self:setBlocker(false);
		self:setSize(16, 16);
		self:setTexture(self.iconTextureName);
		self:setSpriteFrame(self.iconArea.x1, self.iconArea.y1, self.iconArea.x2, self.iconArea.y2);
		self:setSize(math.abs(self.iconArea.x1 - self.iconArea.x2), math.abs(self.iconArea.y1 - self.iconArea.y2));
		self:setGravityInfluence(0);
		self:setAlpha(0.5);
		self:setEditorVisibleOnly(true);
		self:setForceUpdate(true);
	end

	-- We're going to make a copy of the editorProperties table for
	-- this instance of the spawner.
	if self.editorProperties ~= nil then
		local newProperties = {};
		local k, v;
		for k,v in pairs(self.editorProperties) do
			newProperties[k] = v;
		end
		self.editorProperties = newProperties;
	end
	
	self:setForceUpdate(true);
end

function Spawner:triggerSpawn()
	-- If we have a named spawn, check to see if it's already around.
	if self.customName and self.customName ~= "" then
		if entitiesByName[self.customName] then
			-- Yep. Already here.
			return;
		end
	end

	local newItem = nil;
	-- First, check the baseClass and spawn that.
	if self.baseClass then
		newItem = self.baseClass:create(self.customData);
	else
		-- If there's no baseClass, we'll fall back on looking it
		-- up by string.
		if self.baseClassName then
			newItem = _G[self.baseClassName]:create(self.customData);
		end
	end
	
	-- Now we have to setup the new object.
	if newItem then
		if self.customName and self.customName ~= "" then
				newItem:setName(self.customName);
		end
		if self.customData then
			newItem.customData = self.customData;
		end
		newItem.spawnerName = self:getName();
		newItem:setLayer(self:getLayer());
		newItem:setPos(self:getPos());

		-- Add in editor properties.
		if self.editorProperties ~= nil then
			local newProperties = {};
			local k, v;
			for k,v in pairs(self.editorProperties) do
				newItem[k] = v;
			end
		end

	end
end

function Spawner:update()
	if not self.spawnDone then
		self:triggerSpawn();
		self.spawnDone = true;
	end
end

function createSpawnType(spawnName, baseClass, iconTextureName, iconArea, editorProperties)
	local newSpawner = Spawner:createSubClass();
	newSpawner.baseClass = baseClass;
	newSpawner.baseClassName = spawnName;
	newSpawner.iconArea = iconArea;
	newSpawner.iconTextureName = iconTextureName;
	newSpawner.editorProperties = editorProperties;
	_G[spawnName] = newSpawner;
	
	-- Find the global that this thing is so we know the base class
	-- name. This operation scales in a really bad way, but it's
	-- more convenient to the designers, I imagine.
	for k,v in pairs(_G) do
		if v == baseClass then
			newSpawner.baseClassName = k;
		end
	end
	
	registerSpawn(spawnName);
	return newSpawner;
end
