-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachine.coffee
211 lines (190 loc) · 5.02 KB
/
statemachine.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
((exports) ->
NUM_ROWS = 18
NUM_COLS = 24
cells = {}
class Particle
constructor: (@row, @col, @state, @direction, lifetime) ->
@lifetime = lifetime ? 200
excited: 0
excite: -> @excited = 1
decay: -> @excited = 0
kill: -> @lifetime = 0
move: ->
if @state is 1 # On a standard cell
unless @excited # Normal Horiz/Vert particle
switch @direction
when 1 then @col++
when 2 then @row++
when 4 then @col--
when 8 then @row--
else throw new Error "Don't know where to go! Normal particle: [#{@row},#{@col}], direction #{@direction}"
else # Energetic particle
switch @direction
when 1 then
when 4 then @col--
when 16
@row--
@col--
when 64 then @row--
else throw new Error "Don't know where to go! Energetic particle, normal space: [#{@row},#{@col}], direction #{@direction}"
@state = 2 # Move into high-energy
else # On a high-energy cell
switch @direction
when 1
@row++
@col++
when 4 then @row++
when 16 then
when 64 then @col++
@state = 1 # Moving back to standard cell
if @lifetime > 0 then @lifetime-- # Reduce lifetime by 1, unless lifetime=-1
reverse: ->
unless @excited
@direction = @direction << 2
if @direction > 8 then @direction = @direction >> 4
else
@direction = @direction << 4
if @direction > 64 then @direction = @direction >> 8
checkObstacles: (repeat=false)->
unless @excited
if (@row is 1 and @direction is 8) or
(@row is NUM_ROWS and @direction is 2) or
(@col is 1 and @direction is 4) or
(@col is NUM_COLS and @direction is 1)
unless repeat then @reverse() # Grid boundary detection, normal particles
else @lifetime = 0
else if cells["#{@row}_#{@col}_1"].walls & @direction
unless repeat then @reverse()
else @lifetime = 0
else if @state is 1
if (@row is 1 and (@direction is 16 or @direction is 64)) or
(@row is NUM_ROWS and (@direction is 1 or @direction is 4)) or
(@col is 1 and (@direction is 4 or @direction is 16)) or
(@col is NUM_COLS and (@direction is 1 or @direction is 64))
@reverse() # Grid boundary detection, excited particles
class Cell
constructor: (@row, @col, @state) ->
split: false
walls: 0 # 1=east, 2=south, 4=west, 8=north
active: false
shape: null
sound: false
# ckpcw: this addSound method should get called when the client receives a message a new sound has been added
# the audio stuff should bind to the @sound model property changes
addSound: (sound) ->
@sound = sound
# ckpcw: this method gets called on "deactivate" click
# it should somehow notify the audio code to clean up the sound
removeSound: ->
@sound = false
activate: ->
deactivate: ->
setInstrument: (parameters) ->
select: (state=true) ->
if state
if cells.selected? then cells.selected.select(false)
@shape.attr stroke: select_color, 'stroke-width': 4
cells.selected = @
Phon.Elements.$paper.trigger 'cell-selected', [@]
else
@shape.attr stroke: "#000", 'stroke-width': 1
activate: ->
#phon.activate @row, @col
# show activation pending cell state (until server responds & sets)
deactivate: ->
#phon.deactivate @row, @col
# show deactivation pending state (until server responds & sets)
setInstrument: (parameters) ->
#phon.setInstrument @row, @col, parameters
# show instrument settings pending state (until server responds & sets)
# occupy: (state) ->
# if state is true
# @shape.attr fill: particle_color
# else
# @shape.attr fill: cell_colors[@state]
class Emitter
class StateHash
constructor: ->
@h = {}
@lastBeat = []
@thisBeat = []
add: (particle) ->
index = "#{particle.row}_#{particle.col}_#{particle.state}"
if not @h[index]
@h[index] = cells[index]
#@h[index].particles = []
#@h[index].sums = [0,0]
@thisBeat.push index
@h[index].sums[particle.excited] += particle.direction
@h[index].particles.push particle
reset: ->
@h = {}
@lastBeat = @thisBeat
@thisBeat = []
exports.init = ->
cells = {}
for row in [1..NUM_ROWS]
for col in [1..NUM_COLS]
cells["#{row}_#{col}_1"] = new Cell row, col, 1
return cells
exports.emitters = ->
emitters = [
name: "1_7"
life: 8
index: 0
active: true
,
name: "1_14"
life: 16
index: 0
active: true
,
name: "1_21"
life: 32
index: 0
active: true
,
name: "5_24"
life: 16
index: 0
active: true
,
name: "8_1"
life: 8
index: 0
active: true
,
name: "12_24"
life: 16
index: 0
active: true
,
name: "15_1"
life: 32
index: 0
active: true
,
name: "18_4"
life: 16
index: 0
active: true
,
name: "18_11"
life: 8
index: 0
active: true
,
name: "18_18"
life: 16
index: 0
active: true
]
emitList = {}
for cell in emitters
emitList[cell.name] = {
life: cell.life
index: cell.index
active: true
}
return emitList
)(exports)