
Door = Entity:createSubClass();

function Door:init() 
	self:setGravityInfluence(0);
	self:setBlocker(true);
	self:setBlockable(false);
	self:setSize(64, 16);
	self:setSpriteFrame(0, 0, 64, 16);
	self.editorProperties = {activationFlag=""};
    self.activationOverride = false;
end

function Door:initFromSave(saveData)

   -- Just so this doesn't end up as nil.
   if saveData.activationOverride then
      self.activationOverride = true;
   else
      self.activationOverride = false;
   end
end

function Door:save()
	return {
       activationOverride = self.activationOverride
    };
end

function Door:update()

	local x, y = self:getPos();

	if self.editorProperties.activationFlag or self.activationOverride then
		local alpha = self:getAlpha();
		
		if globalData[self.editorProperties.activationFlag] or self.activationOverride then
		
			alpha = alpha - 0.1;
			if alpha < 0 then
				alpha = 0;
			end
			self:setBlocker(false);
			
		else
		
			alpha = alpha + 0.1;
			if alpha > 1 then
				alpha = 1;
			end
			self:setBlocker(true);
			
		end
		
		self:setAlpha(alpha);
		
	end
	
end

registerSpawn("Door");
-- createSpawnType("DoorSpawn", Door, "data/black.png", {x1=0, y1=0, x2=64, y2=16}, {activationFlag=""});








--Switch = Entity:createSubClass();
Switch = Activatable:createSubClass();

function Switch:init()

   Activatable.init(self);

   self:setGravityInfluence(0);
   --self:setBlocker(true);
   --self:setBlockable(false);
   self:setSize(16, 16);
   self:setSpriteFrame(0, 0, 16, 16);
   self:setColor(1, 0, 0);
   self:setLabelFont("data/font1.png");
   self.editorProperties = {activationFlag="", inverted=false};
end

function Switch:initFromSave(saveData)

   Activatable.init(self);

   -- FIXME: Hack to fix old data.
   self:setBlocker(false);

end

function Switch:collided(otherEnt)
	
   Activatable.collided(self, otherEnt);

   -- if otherEnt == "player" then
   --    -- Double-negatives are awesome!
   --    globalData[self.editorProperties.activationFlag] = not self.editorProperties.inverted;
   -- end
   
end

function Switch:activated()

   --globalData[self.editorProperties.activationFlag] = not self.editorProperties.inverted;

   -- Toggle the flag! Yay.
   globalData[self.editorProperties.activationFlag] = not globalData[self.editorProperties.activationFlag];

end

function Switch:update()

   Activatable.update(self);

   if (not globalData[self.editorProperties.activationFlag]) ~= (not self.editorProperties.inverted) then
      self:setColor(0, 1, 0);
   else
      self:setColor(1, 0, 0);
   end

   local invertedStr = "";
   if self.editorProperties.inverted then
      invertedStr = "!";
   end
   self:setLabel(invertedStr..self.editorProperties.activationFlag);
   
end

function Switch:save()
	return {};
end

registerSpawn("Switch");
--createSpawnType("SwitchSpawn", Switch, "data/black.png", {x1=0, y1=0, x2=16, y2=16}, {activationFlag=""});
