-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.go
390 lines (335 loc) · 10.7 KB
/
life.go
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
crrand "crypto/rand"
"fmt"
"image"
"image/color"
_ "image/png"
"math"
"math/big"
"math/rand"
"os"
"strconv"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"github.com/lucasb-eyer/go-colorful"
"golang.org/x/image/colornames"
"golang.org/x/image/font/basicfont"
)
type Particle struct {
x float64
y float64
vx float64
vy float64
}
type Group struct {
color color.RGBA
group []Particle
fuzetemp int
}
type Response struct {
state bool
index int
}
type Button struct {
name string
sprite *pixel.Sprite
x float64
y float64
scaleX float64
scaleY float64
rotate float64
action func()
}
type ParticleRules struct {
fuzionParticle1 int
fuzionParticle2 int
force float64
}
type ParticleCreation struct {
amount int
color color.RGBA
fuzionTemp int
}
var screenWidth float64 = 1600
var screenHeight float64 = 900
var tempature int = 0
var particleSize = 2
var red ParticleCreation = ParticleCreation{200, colornames.Red, 20}
var yellow ParticleCreation = ParticleCreation{200, colornames.Yellow, 40}
var green ParticleCreation = ParticleCreation{200, colornames.Green, 100}
var startingParticles = []ParticleCreation{red, yellow, green}
var isPauzed = false
var fuzionRules = []ParticleRules{}
var normalRules = []ParticleRules{}
var particles = []Group{}
var fuzionGroupIndexes = []int{}
var backgroundcolor color.RGBA = color.RGBA{0, 0, 0, 0}
var speedIndex float64 = 0.5
func calculateSpeedIndex() {
if tempature < 0 {
speedIndex -= 0.011
} else {
speedIndex += 0.011
}
if tempature <= -273 {
speedIndex = 0
}
}
func createRandom(number int, color color.RGBA, fuzetemp int) int {
var group = []Particle{}
for i := 0; i < number; i++ {
var particle = Particle{(rand.Float64() * screenWidth), (rand.Float64() * screenHeight), 0, 0}
group = append(group, particle)
}
particles = append(particles, Group{color, group, fuzetemp})
return len(particles) - 1
}
func createFuzionGroup(number int, color color.RGBA, fuzetemp int, x float64, y float64) int {
var group = []Particle{}
for i := 0; i < number; i++ {
var particle = Particle{x, y, 0, 0}
group = append(group, particle)
}
particles = append(particles, Group{color, group, fuzetemp})
fuzionGroupIndexes = append(fuzionGroupIndexes, len(particles)-1)
createFuzionRules()
return len(particles) - 1
}
func createFuzionRules() {
for i := 0; i < len(fuzionGroupIndexes); i++ {
for j := 0; j < len(particles); j++ {
if RandInt(1, 3) > 1 {
fuzionRules = append(fuzionRules, ParticleRules{i, j, (float64(RandInt(-12, 12)) * 0.03)})
}
}
}
}
func RemoveIndex(s []Particle, index int) []Particle {
return append(s[:index], s[index+1:]...)
}
func RandInt(lower, upper int) int {
seed, err := crrand.Int(crrand.Reader, big.NewInt(27))
if err == nil {
rand.Seed(seed.Int64())
}
rng := upper - lower
return rand.Intn(rng) + lower
}
func groupcolorInParticles(a color.RGBA, list []Group) Response {
for i := 0; i < len(list); i++ {
if list[i].color == a {
return Response{state: true, index: i}
}
}
return Response{state: false, index: 0}
}
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
func increaseTemp() {
tempature += 10
if tempature > 160 {
backgroundcolor.R += 5
}
// backgroundcolor.B -= 1
calculateSpeedIndex()
}
func decreaseTemp() {
if tempature > -273 {
tempature -= 10
calculateSpeedIndex()
if backgroundcolor.R <= 0 {
backgroundcolor.B += 1
} else {
backgroundcolor.R -= 1
}
}
}
func reset() {
fuzionRules = []ParticleRules{}
normalRules = []ParticleRules{}
particles = []Group{}
fuzionGroupIndexes = []int{}
tempature = 0
speedIndex = 0.5
backgroundcolor.R = 0
backgroundcolor.G = 0
backgroundcolor.B = 0
backgroundcolor.A = 0
for i := 0; i < len(startingParticles); i++ {
createRandom(startingParticles[i].amount, startingParticles[i].color, startingParticles[i].fuzionTemp)
}
for i := 0; i < len(particles); i++ {
for j := 0; j < len(particles); j++ {
if RandInt(1, 3) > 1 {
normalRules = append(normalRules, ParticleRules{i, j, (float64(RandInt(-12, 12)) * 0.03)})
}
}
}
}
func pauze() {
isPauzed = true
}
func start() {
isPauzed = false
}
func rule(groupIndex1 int, groupIndex2 int, g float64) {
for i := 0; i < len(particles[groupIndex1].group); i++ {
var fx float64 = 0
var fy float64 = 0
var fuze bool = false
var fuzeColor color.RGBA = colornames.Blue
for j := 0; j < len(particles[groupIndex2].group); j++ {
a := particles[groupIndex1].group[i]
b := particles[groupIndex2].group[j]
Ta := particles[groupIndex1].fuzetemp
Tb := particles[groupIndex2].fuzetemp
dx := a.x - b.x
dy := a.y - b.y
d := math.Sqrt(dx*dx + dy*dy)
if d > 0 && d < 3 && tempature >= Ta && tempature >= Tb && particles[groupIndex1].color != particles[groupIndex2].color {
particles[groupIndex2].group = RemoveIndex(particles[groupIndex2].group, j)
r, g, b, a := colorful.FastHappyColor().Clamped().RGBA()
fuzeColor.R = uint8(r)
fuzeColor.G = uint8(g)
fuzeColor.B = uint8(b)
fuzeColor.A = uint8(a)
fuze = true
}
if d > 0 && d < 60 {
F := g * 1 / d
fx += (F * dx)
fy += (F * dy)
}
}
if !fuze {
particles[groupIndex1].group[i].vx = (particles[groupIndex1].group[i].vx + fx) * speedIndex
particles[groupIndex1].group[i].vy = (particles[groupIndex1].group[i].vy + fy) * speedIndex
particles[groupIndex1].group[i].x += particles[groupIndex1].group[i].vx
particles[groupIndex1].group[i].y += particles[groupIndex1].group[i].vy
if particles[groupIndex1].group[i].x < 2 || particles[groupIndex1].group[i].x > screenWidth-2*float64(particleSize) {
particles[groupIndex1].group[i].vx *= -1
}
if particles[groupIndex1].group[i].y < 2 || particles[groupIndex1].group[i].y > screenHeight-2*float64(particleSize) {
particles[groupIndex1].group[i].vy *= -1
}
}
if fuze {
var knownParticle Response = groupcolorInParticles(fuzeColor, particles)
if knownParticle.state {
particles[knownParticle.index].group = append(particles[knownParticle.index].group, Particle{particles[groupIndex1].group[i].x, particles[groupIndex1].group[i].y, 0, 0})
particles[groupIndex1].group = RemoveIndex(particles[groupIndex1].group, i)
}
if !knownParticle.state {
createFuzionGroup(1, fuzeColor, (particles[groupIndex1].fuzetemp + particles[groupIndex2].fuzetemp), particles[groupIndex1].group[i].x, particles[groupIndex1].group[i].y)
particles[groupIndex1].group = RemoveIndex(particles[groupIndex1].group, i)
}
}
}
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "Artificial Life",
Bounds: pixel.R(0, 0, screenWidth, screenHeight),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
//text creation
basicAtlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
tempatureText := text.New(pixel.V(50, screenHeight-90), basicAtlas)
FuzionCountText := text.New(pixel.V(50, screenHeight-120), basicAtlas)
spritesheet, err := loadPicture("./sprites/menuButtons.png")
//button creation
buttons := []Button{}
var buttonFrames []pixel.Rect
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 18 {
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 16 {
buttonFrames = append(buttonFrames, pixel.R(x, y, x+18, y+16))
}
}
tempForwardButton := Button{"tempForwardButton", pixel.NewSprite(spritesheet, buttonFrames[0]), 220, (screenHeight - 87), 1, 1, 0, increaseTemp}
tempBackButton := Button{"tempBackButton", pixel.NewSprite(spritesheet, buttonFrames[4]), 140, (screenHeight - 87), 1, 1, 0, decreaseTemp}
resetButton := Button{"tempBackButton", pixel.NewSprite(spritesheet, buttonFrames[90]), 135, (screenHeight - 47), 1, 1, 0, reset}
pauzeButton := Button{"tempBackButton", pixel.NewSprite(spritesheet, buttonFrames[34]), 95, (screenHeight - 47), 1, 1, 0, pauze}
startButton := Button{"tempBackButton", pixel.NewSprite(spritesheet, buttonFrames[12]), 55, (screenHeight - 47), 1, 1, 0, start}
buttons = append(buttons, tempBackButton)
buttons = append(buttons, tempForwardButton)
buttons = append(buttons, resetButton)
buttons = append(buttons, pauzeButton)
buttons = append(buttons, startButton)
//generating starting particles
for i := 0; i < len(startingParticles); i++ {
createRandom(startingParticles[i].amount, startingParticles[i].color, startingParticles[i].fuzionTemp)
}
//aplying rules
for i := 0; i < len(particles); i++ {
for j := 0; j < len(particles); j++ {
normalRules = append(normalRules, ParticleRules{i, j, (float64(RandInt(-20, 20)) * 0.03)})
}
}
imd := imdraw.New(nil)
for !win.Closed() {
win.Clear(backgroundcolor)
//draw text
FuzionCountText.Draw(win, pixel.IM)
tempatureText.Draw(win, pixel.IM)
//draw particles
imd.Draw(win)
//draw buttons
for i := 0; i < len(buttons); i++ {
button := buttons[i]
sprite := button.sprite
sprite.Draw(win, pixel.IM.Moved(pixel.V(float64(button.x), float64(button.y))).Rotated(pixel.ZV, float64(button.rotate)).ScaledXY(pixel.ZV, pixel.V(button.scaleX, button.scaleY)))
}
if win.JustPressed(pixelgl.MouseButtonLeft) {
for i := 0; i < len(buttons); i++ {
if win.MousePosition().X < buttons[i].x+12 && win.MousePosition().X > buttons[i].x-12 && win.MousePosition().Y < buttons[i].y+12 && win.MousePosition().Y > buttons[i].y-12 {
buttons[i].action()
}
}
}
win.Update()
tempatureText.Clear()
FuzionCountText.Clear()
fmt.Fprintf(tempatureText, "Tempature %s 'C", strconv.Itoa(tempature))
fmt.Fprintf(FuzionCountText, "New fuzions: %s", strconv.Itoa(len(fuzionGroupIndexes)))
if !isPauzed {
for i := 0; i < len(fuzionRules); i++ {
rule(fuzionRules[i].fuzionParticle1, fuzionRules[i].fuzionParticle2, fuzionRules[i].force)
}
for i := 0; i < len(normalRules); i++ {
rule(normalRules[i].fuzionParticle1, normalRules[i].fuzionParticle2, normalRules[i].force)
}
//Cleaning screen between frames
imd.Reset()
imd.Clear()
//Change positions of particles for drawing
for i := 0; i < len(particles); i++ {
for j := 0; j < len(particles[i].group); j++ {
imd.Color = particles[i].color
imd.EndShape = imdraw.SharpEndShape
imd.Push(pixel.V(particles[i].group[j].x-float64(particleSize), particles[i].group[j].y-float64(particleSize)), pixel.V(particles[i].group[j].x+float64(particleSize), particles[i].group[j].y+float64(particleSize)))
imd.Rectangle(0)
}
}
}
}
}
func main() {
pixelgl.Run(run)
}