1
1
class Neuron {
2
2
constructor ( x , y , neuronSize , actlvl ) {
3
- this . xCoord = x ;
4
- this . yCoord = y ;
3
+ this . x = x ;
4
+ this . y = y ;
5
5
this . size = neuronSize ;
6
6
this . activationLevel = actlvl ;
7
7
this . curCharge = 0 ;
@@ -15,7 +15,7 @@ class Neuron {
15
15
display ( ) {
16
16
this . selected ? fill ( 200 , 200 , 60 ) : fill ( 100 , 200 , 0 ) ;
17
17
stroke ( sketchOptions . gridColor ) ;
18
- rect ( this . xCoord , this . yCoord , this . size , this . size ) ;
18
+ rect ( this . x , this . y , this . size , this . size ) ;
19
19
}
20
20
21
21
displayConnections ( ) {
@@ -24,14 +24,14 @@ class Neuron {
24
24
// skew centers the connection to the middle of the neuron
25
25
var skew = this . size / 2.0 ;
26
26
_ . each ( this . outBoundConnections , outBoundNeuron => {
27
- line ( this . xCoord + skew , this . yCoord + skew , outBoundNeuron . xCoord + skew , outBoundNeuron . yCoord + skew ) ;
27
+ line ( this . x + skew , this . y + skew , outBoundNeuron . x + skew , outBoundNeuron . y + skew ) ;
28
28
} ) ;
29
29
}
30
30
31
31
erase ( ) {
32
32
stroke ( sketchOptions . gridColor ) ;
33
33
fill ( sketchOptions . bgColor ) ;
34
- rect ( this . xCoord , this . yCoord , this . size , this . size ) ;
34
+ rect ( this . x , this . y , this . size , this . size ) ;
35
35
}
36
36
37
37
reset ( ) {
@@ -60,7 +60,7 @@ class Neuron {
60
60
61
61
// Toggles an inbound connection between two neurons
62
62
inBoundConnection ( otherNeuron ) {
63
- var index = `${ otherNeuron . xCoord } ${ otherNeuron . yCoord } ` ;
63
+ var index = `${ otherNeuron . x } ${ otherNeuron . y } ` ;
64
64
65
65
if ( ! this . inBoundConnections [ index ] ) {
66
66
this . inBoundConnections [ index ] = otherNeuron ;
@@ -72,7 +72,7 @@ class Neuron {
72
72
73
73
// Toggles an outbound connection between two neurons
74
74
outBoundConnection ( otherNeuron ) {
75
- var index = `${ otherNeuron . xCoord } ${ otherNeuron . yCoord } ` ;
75
+ var index = `${ otherNeuron . x } ${ otherNeuron . y } ` ;
76
76
77
77
if ( ! this . outBoundConnections [ index ] ) {
78
78
this . outBoundConnections [ index ] = otherNeuron ;
@@ -84,7 +84,7 @@ class Neuron {
84
84
85
85
// Removes any connections to a neuron that no longer exists
86
86
removeConnection ( otherNeuron ) {
87
- var index = `${ otherNeuron . xCoord } ${ otherNeuron . yCoord } ` ;
87
+ var index = `${ otherNeuron . x } ${ otherNeuron . y } ` ;
88
88
if ( this . inBoundConnections [ index ] ) {
89
89
delete this . inBoundConnections [ index ] ;
90
90
clearScreen ( ) ;
@@ -169,78 +169,39 @@ class Neuron {
169
169
}
170
170
171
171
static createNeuron ( x , y ) {
172
- var activationLevel = parseInt ( $ ( '#activationLevel' ) . val ( ) ) ;
173
- var timeInSeconds = parseFloat ( $ ( '#timerActivationInput' ) . val ( ) ) ;
174
- var maxSteps = parseFloat ( parseInt ( $ ( '#timesInput' ) . val ( ) ) ) ;
175
- var noteMIDI = parseInt ( $ ( '#noteInput' ) . val ( ) ) ;
176
-
177
- var tempNeuron ;
178
- switch ( neuronType ) {
179
- case 'normal' :
180
- tempNeuron = new Neuron ( x , y , sketchOptions . cellSize , activationLevel ) ;
181
- break ;
182
- case 'character' :
183
- tempNeuron = new CharacterNeuron ( x , y , sketchOptions . cellSize , activationLevel ) ;
184
- break ;
185
- case 'negative' :
186
- tempNeuron = new NegativeNeuron ( x , y , sketchOptions . cellSize , activationLevel ) ;
187
- break ;
188
- case 'timer' :
189
- tempNeuron = new TimerNeuron ( x , y , sketchOptions . cellSize , activationLevel , timeInSeconds , maxSteps ) ;
190
- break ;
191
- case 'note' :
192
- tempNeuron = new NoteNeuron ( x , y , sketchOptions . cellSize , activationLevel , noteMIDI ) ;
193
- break ;
194
- }
172
+ var neuronType = $ ( '#neuronType' ) . val ( ) ;
173
+ var neuronObj = { x :x , y :y } ;
174
+
175
+ _ . each ( neuronInfo . fields , ( field , key ) => {
176
+ neuronObj [ key ] = field ( ) ;
177
+ } ) ;
195
178
196
- neuronList [ `${ x } ${ y } ` ] = tempNeuron ;
179
+ neuronList [ `${ x } ${ y } ` ] = neuronInfo [ neuronType ] . Create ( neuronObj ) ;
197
180
}
198
181
199
182
static loadExample ( exampleName ) {
200
183
var exampleNeuronNet = JSON . parse ( exampleNeuronNets [ exampleName ] ) ;
201
184
neuronList = { } ;
202
185
actionPotentialList = { } ;
203
186
204
- // Adds the neurons to the neuronLists without their connections
205
- _ . each ( exampleNeuronNet , neuron => {
206
- let neuronType = neuron . type ;
207
- let tempNeuron ;
208
-
209
- switch ( neuronType ) {
210
- case 'Neuron' :
211
- tempNeuron = new Neuron ( neuron . x , neuron . y , neuron . size , neuron . activationLevel ) ;
212
- break ;
213
- case 'CharacterNeuron' :
214
- tempNeuron = new CharacterNeuron ( neuron . x , neuron . y , neuron . size , neuron . activationLevel ) ;
215
- break ;
216
- case 'NegativeNeuron' :
217
- tempNeuron = new NegativeNeuron ( neuron . x , neuron . y , neuron . size , neuron . activationLevel ) ;
218
- break ;
219
- case 'TimerNeuron' :
220
- neuron . maxSteps = neuron . maxSteps ? neuron . maxSteps : - 1 ;
221
- tempNeuron = new TimerNeuron ( neuron . x , neuron . y , neuron . size , neuron . activationLevel , neuron . timeInSeconds , neuron . maxSteps ) ;
222
- break ;
223
- case 'NoteNeuron' :
224
- tempNeuron = new NoteNeuron ( neuron . x , neuron . y , neuron . size , neuron . activationLevel , neuron . noteMIDI ) ;
225
- break ;
226
-
227
- }
228
-
187
+ // Adds the neurons to the neuronLists without their connections
188
+ _ . each ( exampleNeuronNet , neuron => {
189
+ let tempNeuron = neuronInfo [ neuron . type ] . Create ( neuron ) ;
229
190
neuronList [ `${ neuron . x } ${ neuron . y } ` ] = tempNeuron ;
230
- } ) ;
191
+ } ) ;
231
192
232
- // Adds connections
233
- _ . each ( exampleNeuronNet , neuron => {
234
- let tempNeuron = neuronList [ `${ neuron . x } ${ neuron . y } ` ] ;
193
+ // Adds connections
194
+ _ . each ( exampleNeuronNet , neuron => {
195
+ let tempNeuron = neuronList [ `${ neuron . x } ${ neuron . y } ` ] ;
235
196
236
- _ . each ( neuron . outBounds , outBoundNeuron => {
237
- tempNeuron . outBoundConnection ( neuronList [ outBoundNeuron ] ) ;
238
- } ) ;
197
+ _ . each ( neuron . outBounds , outBoundNeuron => {
198
+ tempNeuron . outBoundConnection ( neuronList [ outBoundNeuron ] ) ;
199
+ } ) ;
239
200
240
- _ . each ( neuron . inBounds , inBoundNeuron => {
241
- tempNeuron . inBoundConnection ( neuronList [ inBoundNeuron ] ) ;
201
+ _ . each ( neuron . inBounds , inBoundNeuron => {
202
+ tempNeuron . inBoundConnection ( neuronList [ inBoundNeuron ] ) ;
203
+ } ) ;
242
204
} ) ;
243
- } ) ;
244
205
}
245
206
246
207
static saveExample ( ) {
@@ -251,22 +212,17 @@ class Neuron {
251
212
let neuronObj = { } ;
252
213
253
214
neuronObj . type = neuronType ;
254
- neuronObj . x = neuron . xCoord ;
255
- neuronObj . y = neuron . yCoord ;
215
+ neuronObj . x = neuron . x ;
216
+ neuronObj . y = neuron . y ;
256
217
neuronObj . size = neuron . size ;
257
218
neuronObj . activationLevel = neuron . activationLevel ;
258
219
neuronObj . outBounds = _ . keys ( neuron . outBoundConnections ) ;
259
220
neuronObj . inBounds = _ . keys ( neuron . inBoundConnections ) ;
260
221
261
- switch ( neuronType ) {
262
- case 'TimerNeuron' :
263
- neuronObj . timeInSeconds = neuron . timeInSeconds ;
264
- neuronObj . maxSteps = neuron . maxSteps ;
265
- break ;
266
- case 'NoteNeuron' :
267
- neuronObj . noteMIDI = neuron . note ;
268
- break ;
269
- }
222
+ // TODO FIX THIS
223
+ _ . each ( neuronInfo [ neuronType ] , extraInfo => {
224
+ neuronObj [ extraInfo ] = neuron [ extraInfo ] ;
225
+ } ) ;
270
226
271
227
neurons . push ( neuronObj ) ;
272
228
} ) ;
0 commit comments