--A switch that toggles state when a custom key is pressed by the player within it's bounding box.
--by aarbro [aarbro@hotmail.com]

Switch = {
	
	Properties = {
		bEnabled = 1, --whether the switch is working
		bState = 0,
		Key = "f",
		vectorBBox = {x=1,y=1,z=1},
		TextInstruction= "",
	},

	Editor={
		Model="Objects/Editor/t.cgf",
	},

}

-------------------------------------------------------------------------------
function Switch:OnPropertyChange()
	self:OnReset();
end

-------------------------------------------------------------------------------
function Switch:OnInit()
	self:OnReset();
end

-------------------------------------------------------------------------------
function Switch:OnReset()

	local Min = { x=-self.Properties.vectorBBox.x/2, y=-self.Properties.vectorBBox.y/2, z=-self.Properties.vectorBBox.z/2 };
	local Max = { x=self.Properties.vectorBBox.x/2, y=self.Properties.vectorBBox.y/2, z=self.Properties.vectorBBox.z/2 };
	self:SetBBox( Min, Max );
	
	if ( self.Properties.bState == 1 ) then
		self:Event_SwitchOn();
	else
		self:Event_SwitchOff();
	end
	
	if ( self.Properties.bEnabled == 1 ) then
		self:TrackColliders(1);
	else
		self:TrackColliders(0);
	end
	
end
-------------------------------------------------------------------------------
function Switch:OnEnterArea()
	if Hud then
--	Hud.label( self.Properties.TextInstruction );
		Hud:AddMessage( self.Properties.TextInstruction );
 	end
 	self:EnableUpdate(1);
end

-------------------------------------------------------------------------------

function Switch:OnLeaveArea()
	self:EnableUpdate(0);
end

-------------------------------------------------------------------------------

function Switch:OnUpdate()
	local PressedKey = Input:GetXKeyPressedName();
	if ( tostring(PressedKey) == self.Properties.Key ) then
		if ( self.Properties.bState == 1 ) then
			self:Event_SwitchOff();
		else
			self:Event_SwitchOn();
		end
	end
end

-------------------------------------------------------------------------------
function Switch:OnSave(stm)	
	stm:WriteInt(self.switch);
end

-------------------------------------------------------------------------------
function Switch:OnLoad(stm)	 
	self.switch = stm:ReadInt();
end

-------------------------------------------------------------------------------
function Switch:Event_SwitchOn()
	if ( self.Properties.bEnabled == 1 ) then
		self.Properties.bState = 1;
		BroadcastEvent(self, "SwitchOn");
	end
end
-------------------------------------------------------------------------------
function Switch:Event_SwitchOff()
	if ( self.Properties.bEnabled == 1 ) then
		self.Properties.bState = 0;
		BroadcastEvent(self, "SwitchOff");
	end
end
-------------------------------------------------------------------------------
function Switch:Event_Enable()
	self.Properties.bEnabled = 1;
	self:TrackColliders(1);
	BroadcastEvent(self, "Enable");
end
-------------------------------------------------------------------------------
function Switch:Event_Disable()
	self.Properties.bEnabled = 0;
	self:TrackColliders(0);
	BroadcastEvent(self, "Disable");
end
-------------------------------------------------------------------------------



-- Default keys used in the game

--1,2,3,4
--a,b,d,e,f,g,h,j,l,q,r,s,t,u,w,x,y,z
--f1,f12
--add,divide,subtract
--right,left,up
--numpad0,numpad2,numpad4,numpad5,numpad6,numpad7,numpad9
--numlock,numpad enter
--lctrl,lshift
--pagedown,pageup
--spacebar,tab
--maxisx,maxisy
--mouse1,mouse2
--mwheeldown,mwheelup
