Path: blob/main/public/games/files/algaes-escapade/js/lib/io.js
1036 views
/**1* Basic input/output class. Listens for state changes on the inputs and2* notifies outputs if a change happens3*4* @author David North5*/6function io()7{8/**9* @var boolean10*/11this._state = false;1213/**14* @var array An array of inputs, that determine the state of this object15*/16this._inputs = [];1718/**19* @var array An array of outputs, to send the state of this object to20*/21this._outputs = [];2223//Load the variables required by gamejs.sprite.Sprite24io.superConstructor.apply(this, [0, 0]);25this.image = gamejs.image.load('img/blank.png');26this.rect = new gamejs.Rect([0,0]);2728/**29* Sets the position of the object30*31* @param float x The X co-ordinate32* @param float y The Y co-ordinate33*34* @return io35*/36this.setPosition = function(x, y){37this.rect.x = x;38this.rect.y = y;39return this;40}4142/**43* Private method fired when the state of the object has changed44* (such as when a switch is pressed). Once the state has changed,45* all other outputs need to be made aware of the change46*47* @return io48*/49var _stateChange = function(obj){50//Change the state of all outputs to the new state of this object,51//effectively causing a knock-on affect down the chain52for( var i = 0; i < obj.getOutputs().length; i++ )53{54obj.getOutputs()[i].setState( obj.getState() );55}5657return this;58};5960/**61* Sets the state of this object62*63* @param boolean state The state to change to64*65* @return io66*/67this.setState = function(state){68if ( typeof(state) != 'boolean' )69{70throw 'State must be a boolean';71}7273//Set the new state and set the state change event only if the state74//has actually changed, otherwise we could waste time notifying objects75///that don't require notification76if ( state != this.getState() )77{78this._state = state;79_stateChange(this);80}8182return this;83};8485/**86* Gets the state of the object87*88* @return boolean89*/90this.getState = function(){91return this._state;92};9394/**95* Gets the inputs assigned to this object96*97* @return array98*/99this.getInputs = function(){100return this._inputs;101};102103/**104* Gets the outputs assigned to this object105*106* @return array107*/108this.getOutputs = function(){109return this._outputs;110};111112/**113* Adds a new input to the object. Essentially this adds subscribes114* this object to the stateChange event of the input115*116* @param io input117*118* @return io119*/120this.addInput = function( input ){121if ( !(input instanceof io) )122{123throw 'Input must be of type \'io\'';124}125126//Ensure that the input has not already been added. We don't want to127//attempt to add it again, that's just damned inefficient128if ( $.inArray(input, this._inputs) == -1 )129{130this._inputs.push(input);131input.addOutput(this);132}133134return this;135};136137/**138* Adds an output to this object. Effectively this subscribes 'output'139* to the stateChange event of this object140*141* @param io output142*143* @return io144*/145this.addOutput = function( output ){146if ( !(output instanceof io) )147{148throw 'Output must be of type \'io\'';149}150151//Ensure that the output has not already been added. We don't want to152//attempt to add it again, that would be silly153if ( $.inArray(output, this._outputs) == -1 )154{155this._outputs.push(output);156output.addInput(this);157_stateChange(this);158}159160return this;161};162}163164//Some IOs need to be drawn. To deal with this, extend the sprite165gamejs.utils.objects.extend(io, gamejs.sprite.Sprite);166167168