Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
FogNetwork
GitHub Repository: FogNetwork/Tsunami
Path: blob/main/public/games/files/algaes-escapade/js/lib/io.js
1036 views
1
/**
2
* Basic input/output class. Listens for state changes on the inputs and
3
* notifies outputs if a change happens
4
*
5
* @author David North
6
*/
7
function io()
8
{
9
/**
10
* @var boolean
11
*/
12
this._state = false;
13
14
/**
15
* @var array An array of inputs, that determine the state of this object
16
*/
17
this._inputs = [];
18
19
/**
20
* @var array An array of outputs, to send the state of this object to
21
*/
22
this._outputs = [];
23
24
//Load the variables required by gamejs.sprite.Sprite
25
io.superConstructor.apply(this, [0, 0]);
26
this.image = gamejs.image.load('img/blank.png');
27
this.rect = new gamejs.Rect([0,0]);
28
29
/**
30
* Sets the position of the object
31
*
32
* @param float x The X co-ordinate
33
* @param float y The Y co-ordinate
34
*
35
* @return io
36
*/
37
this.setPosition = function(x, y){
38
this.rect.x = x;
39
this.rect.y = y;
40
return this;
41
}
42
43
/**
44
* Private method fired when the state of the object has changed
45
* (such as when a switch is pressed). Once the state has changed,
46
* all other outputs need to be made aware of the change
47
*
48
* @return io
49
*/
50
var _stateChange = function(obj){
51
//Change the state of all outputs to the new state of this object,
52
//effectively causing a knock-on affect down the chain
53
for( var i = 0; i < obj.getOutputs().length; i++ )
54
{
55
obj.getOutputs()[i].setState( obj.getState() );
56
}
57
58
return this;
59
};
60
61
/**
62
* Sets the state of this object
63
*
64
* @param boolean state The state to change to
65
*
66
* @return io
67
*/
68
this.setState = function(state){
69
if ( typeof(state) != 'boolean' )
70
{
71
throw 'State must be a boolean';
72
}
73
74
//Set the new state and set the state change event only if the state
75
//has actually changed, otherwise we could waste time notifying objects
76
///that don't require notification
77
if ( state != this.getState() )
78
{
79
this._state = state;
80
_stateChange(this);
81
}
82
83
return this;
84
};
85
86
/**
87
* Gets the state of the object
88
*
89
* @return boolean
90
*/
91
this.getState = function(){
92
return this._state;
93
};
94
95
/**
96
* Gets the inputs assigned to this object
97
*
98
* @return array
99
*/
100
this.getInputs = function(){
101
return this._inputs;
102
};
103
104
/**
105
* Gets the outputs assigned to this object
106
*
107
* @return array
108
*/
109
this.getOutputs = function(){
110
return this._outputs;
111
};
112
113
/**
114
* Adds a new input to the object. Essentially this adds subscribes
115
* this object to the stateChange event of the input
116
*
117
* @param io input
118
*
119
* @return io
120
*/
121
this.addInput = function( input ){
122
if ( !(input instanceof io) )
123
{
124
throw 'Input must be of type \'io\'';
125
}
126
127
//Ensure that the input has not already been added. We don't want to
128
//attempt to add it again, that's just damned inefficient
129
if ( $.inArray(input, this._inputs) == -1 )
130
{
131
this._inputs.push(input);
132
input.addOutput(this);
133
}
134
135
return this;
136
};
137
138
/**
139
* Adds an output to this object. Effectively this subscribes 'output'
140
* to the stateChange event of this object
141
*
142
* @param io output
143
*
144
* @return io
145
*/
146
this.addOutput = function( output ){
147
if ( !(output instanceof io) )
148
{
149
throw 'Output must be of type \'io\'';
150
}
151
152
//Ensure that the output has not already been added. We don't want to
153
//attempt to add it again, that would be silly
154
if ( $.inArray(output, this._outputs) == -1 )
155
{
156
this._outputs.push(output);
157
output.addInput(this);
158
_stateChange(this);
159
}
160
161
return this;
162
};
163
}
164
165
//Some IOs need to be drawn. To deal with this, extend the sprite
166
gamejs.utils.objects.extend(io, gamejs.sprite.Sprite);
167
168