-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaquarium.fg
265 lines (240 loc) · 7.17 KB
/
aquarium.fg
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!syntax:lispish
#===============================
#
# Preparation
# -----------
#
#===============================
.var innerWidth <-- .import innerWidth
.var innerHeight <-- .import innerHeight
{prepContinuous `
function between(n, mini, maxi) {return mini < n && n < maxi}
function collision(pos1, size1, pos2, size2) {
return -size2 <= (pos2-pos1) && (pos2-pos1) <= size1
}
function colliObj(obj1, obj2) {
return collision(v(obj1.x), v(obj1.width), v(obj2.x), v(obj2.width))
&& collision(v(obj1.y), v(obj1.height), v(obj2.y), v(obj2.height))
}
function setGender(fish, sexe) {
fish.sexe = sexe
fish.setAttribute('rx', 15*sexe)
}
`}
{js () `
const svg = document.getElementById('gameZone')
svg.setAttribute('width', innerWidth)
svg.setAttribute('height', innerHeight)
`}
{deffunc drawNewFish (p_w p_h p_r)
:displayNewSVGElementIn (+ '<rect style="display: none" width="' $p_w '" height="' $p_h '" rx="' $p_r '" x="0" y="0" />') 'svg'
}
{deffunc drawNewAlga ()
:displayNewSVGElementIn '<rect style="display: none" fill="#00ff80" width="10" height="0" x="0" y="0" />' 'svg'
}
{deffunc lifeOfAlga (p_lifePoints)
# birth
#==========
.var thisAlga <-- !drawNewAlga
{js (thisAlga p_lifePoints) `
const y = innerHeight * Math.random()
thisAlga.setAttribute('x', innerWidth * Math.random())
thisAlga.setAttribute('y', y)
thisAlga.setAttribute('height', innerHeight - y)
thisAlga.style.display = 'inline'
thisAlga.lifePoints = p_lifePoints
thisAlga.age = 0
`}
# after birth
#=============
.var life
.var newBirth
{par @life
# meeting management
#--------------------------------
%whileTrueAwaitFrame adapt thisAlga `
thisAlga.age += 1
thisAlga.lifePoints += 0.01
// Meeting with hungry fish
//-------------------------
for (const otherFish of events.get('oneFish')) {
// When some fishs newly collide, generate a bite
if (! thisAlga.eltCollision && colliObj(thisAlga, otherFish) ) {
thisAlga.eltCollision = otherFish
if (!otherFish.carnovire && otherFish.lifePoints < 7) {
thisAlga.lifePoints -= 10
otherFish.lifePoints += 3
}
// Mark the end of collision
} else if (thisAlga != otherFish && thisAlga.eltCollision === otherFish && ! colliObj(thisAlga, otherFish) ) {
thisAlga.eltCollision = null
}
}
if (thisAlga.lifePoints <= 0 || thisAlga.age > 1200) sugBreak('life')
if (thisAlga.lifePoints >= 10) {
thisAlga.lifePoints /= 2
sugAssign('newBirth', thisAlga.lifePoints)
}
`
# birth management
#--------------------------------
{whileTrue
# await a newBirth signal
.var lifePoints <-- :await newBirth beep
# add a new parallel branch in 'supplLifeOf' "par"
{spawn supplLifeOf
.lifeOfAlga $lifePoints
}
}
}
# death
#=============
{js (thisAlga) `
thisAlga.parentNode.removeChild(thisAlga)
`}
}
{deffunc lifeOfFish (p_race p_sexe)
# birth
#==========
.var thisFish <-- %drawNewFish 100 30 [15 * $p_sexe]
{js (thisFish p_race p_sexe) `
thisFish.style.fill = {
'Mérou': '#ff0000',
'Thon': '#ff8000',
'Poisson-clown': '#c06000',
'Sole': '#0000ff',
'Bar': '#00a0ff',
'Carpe': '#004080',
}[p_race]
thisFish.carnivore = ['Mérou', 'Thon', 'Poisson-clown'].includes(p_race)
thisFish.ageHermaphrodite = ['Mérou', 'Bar'].includes(p_race)
thisFish.opportunisticHermaphrodite = ['Sole', 'Poisson-clown'].includes(p_race)
thisFish.setAttribute('x', innerWidth * Math.random())
thisFish.setAttribute('y', innerHeight * Math.random())
thisFish.style.display = 'inline'
thisFish.dx = Math.floor(5*Math.random())-2
thisFish.dy = Math.floor(5*Math.random())-2
thisFish.race = p_race
thisFish.sexe = p_sexe
thisFish.eltCollision = null
thisFish.lifePoints = 10
thisFish.age = 0
`}
# after birth
#=============
.var move
.var newBirth
.var life
{par @life
# broadcasting the fish
#-----------------------
%whileTrueAwaitFrame send thisFish `
send('oneFish', thisFish)
`
# bouncing and meeting management
#--------------------------------
%whileTrueAwaitFrame adapt thisFish `
thisFish.age += 1
thisFish.lifePoints -= 0.01
if (thisFish.ageHermaphrodite && thisFish.age >= 450 && ! thisFish.genderChanged) {
thisFish.genderChanged = true
setGender(thisFish, 1 - thisFish.sexe)
}
if (thisFish.opportunisticHermaphrodite) thisFish.opportunisticChange = false
// Bouncing
//---------
if (v(thisFish.x) + v(thisFish.width) > innerWidth) thisFish.dx = - Math.abs(thisFish.dx)
if (v(thisFish.x) < 0) thisFish.dx = Math.abs(thisFish.dx)
if (v(thisFish.y) + v(thisFish.height) > innerHeight) thisFish.dy = - Math.abs(thisFish.dy)
if (v(thisFish.y) < 0) thisFish.dy = Math.abs(thisFish.dy)
// Meeting
//--------
for (const otherFish of events.get('oneFish')) {
// When some fishs newly collide, generate a new birth or a bite
if (thisFish != otherFish && ! thisFish.eltCollision && colliObj(thisFish, otherFish) ) {
thisFish.eltCollision = otherFish
if (between(thisFish.age, 100, 600) && between(otherFish.age, 100, 600) && thisFish.race == otherFish.race) {
if (thisFish.sexe == 0 && otherFish.sexe == 1) {
// send a 'New birth' signal
sugBip('newBirth')
} else if (thisFish.sexe == otherFish.sexe && thisFish.opportunisticHermaphrodite && ! otherFish.opportunisticChange) {
thisFish.opportunisticChange = true
setGender(thisFish, 1 - thisFish.sexe)
// send a 'New birth' signal
sugBip('newBirth')
}
}
if (thisFish.carnivore && thisFish.lifePoints < 5) {
thisFish.lifePoints += 4
otherFish.lifePoints -= 6
}
// Mark the end of collision
} else if (thisFish != otherFish && thisFish.eltCollision === otherFish && ! colliObj(thisFish, otherFish) ) {
thisFish.eltCollision = null
}
}
// end of life
//------------
if (thisFish.age > 900 || thisFish.lifePoints <= 0) sugBreak('life')
`
# birth management
#--------------------------------
{whileTrue
# await a newBirth signal
:await newBirth beep
# add a new parallel branch in 'supplLifeOf' "par"
{spawn supplLifeOf
(lifeOfFish
$p_race
{js () `return Math.floor(2*Math.random())`}
)
}
}
# movement of the fish
#----------------------
{whileTrueAwaitFrame @move play thisFish `
thisFish.setAttribute('x', v(thisFish.x) + thisFish.dx*delta/30)
thisFish.setAttribute('y', v(thisFish.y) + thisFish.dy*delta/30)
`}
# pausing management
#-------------------
{whileTrue
.awaitClickBeep $thisFish
.pause move
.awaitClickBeep $thisFish
.resume move
}
}
# death
#=============
{js (thisFish) `
thisFish.parentNode.removeChild(thisFish)
`}
}
#===============================
#
# Main program
# ------------
#
#===============================
!separateMicrostep
.var supplLifeOf
.var numAlga <-- :parRange 0 10
{par
*supplLifeOf
{foreachPar numAlga
.lifeOfAlga 5
}
(lifeOfFish 'Mérou' 0)
(lifeOfFish 'Mérou' 1)
(lifeOfFish 'Thon' 0)
(lifeOfFish 'Thon' 1)
(lifeOfFish 'Poisson-clown' 0)
(lifeOfFish 'Poisson-clown' 0)
(lifeOfFish 'Sole' 1)
(lifeOfFish 'Sole' 1)
(lifeOfFish 'Bar' 0)
(lifeOfFish 'Bar' 1)
(lifeOfFish 'Carpe' 0)
(lifeOfFish 'Carpe' 1)
}