-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
265 lines (220 loc) · 6.5 KB
/
main.go
File metadata and controls
265 lines (220 loc) · 6.5 KB
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
package main
import (
"engo.io/ecs"
"engo.io/engo"
"engo.io/engo/common"
"fmt"
"image/color"
"strings"
)
const (
// Default cell dimension
CellSize = 8
// Default screen width dimension
ScreenWidth = 8 * 85
// Default screen height dimension
ScreenHeight = 8 * 85
)
var initialStatus Design
var cells []*Cell // Slice of cells to process
// FirstScene is de first and unique scene for this project
type FirstScene struct {
Pattern Design
}
// SystemsList represents a collection of Systems
type SystemsList struct {
RenderSystems *common.RenderSystem
LifeSystems *LifeSystem
}
// Type returns "FirstScene"
func (FirstScene) Type() string { return "FirstScene" }
// Preload pre-process assets an do actions before executes Setup
func (FirstScene) Preload() {}
// Setup generates the GOL cells grid, add the neighbors to each cell and draw
// the pattern to be processed.
func (scene FirstScene) Setup(w *ecs.World) {
common.SetBackground(color.White)
systems := SystemsList{
&common.RenderSystem{},
&LifeSystem{},
}
// Number of cells on screen
maxCells := (ScreenWidth * ScreenHeight) / (CellSize * CellSize)
// Number of cells per column
maxCols := ScreenWidth / CellSize
// Number of cells per row
maxRows := ScreenHeight / CellSize
// Generating cells
for i := 0; i < maxCells; i++ {
is := i * CellSize
row := is / ScreenWidth
col := (i + maxCols) % maxCols
x := col * CellSize
y := row * CellSize
// Cell position on the screen
cellPos := engo.Point{float32(x), float32(y)}
// New cell on previous defined position
cell := NewCell(cellPos)
// Cell added to the cells list
cells = append(cells, cell)
// Check if current cell is an active cell on the
position := engo.Point{float32(col), float32(row)}
cell.status = IsActiveCell(scene.Pattern, position)
systems.RenderSystems.AddByInterface(cell)
systems.LifeSystems.Add(
&cell.BasicEntity,
&cell.RenderComponent,
&cell.SpaceComponent,
&cell.AliveComponent,
)
}
// Adding neighbors to each cell if is possible.
for i, cell := range cells {
is := i * CellSize
row := is / ScreenWidth
col := (i + maxCols) % maxCols
// fmt.Println(i, col, row)
// W cell
if col > 0 {
cell.AddNeighbor(cells[i-1])
}
// N cell
if row > 0 {
cell.AddNeighbor(cells[i-maxCols])
}
// NW cell
if col > 0 && row > 0 {
cell.AddNeighbor(cells[i-maxCols-1])
}
// E cell
if col < maxCols-1 {
cell.AddNeighbor(cells[i+1])
}
// S cell
if row < maxRows-1 {
cell.AddNeighbor(cells[i+maxCols])
}
// SE cell
if col < maxCols-1 && row < maxRows-1 {
cell.AddNeighbor(cells[i+maxCols+1])
}
// NE cell
if col < maxCols-1 && row > 0 {
cell.AddNeighbor(cells[i-maxCols+1])
}
// SW cell
if row < maxRows-1 && col > 0 {
cell.AddNeighbor(cells[i+maxCols-1])
}
}
w.AddSystem(systems.RenderSystems)
w.AddSystem(systems.LifeSystems)
fmt.Println("Cells ", len(cells))
}
func init() {
/*
* ####################################################################
* # Six different ways to initialize the map. #
* # Each option use a custom tool build in the package that #
* # satisfies the Design interface, that is used to set up each #
* # cell according to the result of the querying for the status of #
* # the (x, y) position at the scene's setup function. #
* # #
* # Comment the default and uncomment the option that you wanna test #
* # or create a new one. #
* ####################################################################
*/
// Option #1: Simple glider in position 1, 1
//
// initialStatus = designs.NewSimpleGlider(engo.Point{1, 1})
// Option #2: Gosper Glider Gun in de position 4, 4
//
// initialStatus = NewGosperGliderGun(engo.Point{4, 4})
// Option #3: Simplex Noise generator with random values
//
// initialStatus = NewRandomSimplexNoise()
// Option #4: A custom Simplex Noise generator that every time will
// draw the same init status.
//
// initialStatus = &SimplexNoise{
// .7,
// .1,
// engo.Point{0,0},
// }
// Option #5: Alternate pattern [1 on/2 off]:
// x..x..
// .x..x.
// ..x..x
//
// initialStatus = &AlternatePattern{}
// Option #6: Tile pattern made of pulsars.
// - First uses a free form to make one element.
// - Then render the element on a canvas.
// - Duplicates the canvas and flips each one: x axis, y axis and boths.
// - Add the result patterns in a loop.
pattern := NewFreeForm(
engo.Point{2, 2},
)
pattern.Set(engo.Point{-2, -2}, true)
pattern.Set(engo.Point{2, 0}, true)
pattern.Set(engo.Point{3, 0}, true)
pattern.Set(engo.Point{4, 0}, true)
pattern.Set(engo.Point{0, 2}, true)
pattern.Set(engo.Point{5, 2}, true)
pattern.Set(engo.Point{0, 3}, true)
pattern.Set(engo.Point{5, 3}, true)
pattern.Set(engo.Point{0, 4}, true)
pattern.Set(engo.Point{5, 4}, true)
pattern.Set(engo.Point{2, 5}, true)
pattern.Set(engo.Point{3, 5}, true)
pattern.Set(engo.Point{4, 5}, true)
design := NewCanvas(engo.Point{8, 8})
design.RenderDesign(pattern)
design.StartPoint = engo.Point{0, 0}
a := *design
b := *design
c := *design
d := *design
b.FlipVertical()
c.FlipHorizontal()
d.FlipVertical()
d.FlipHorizontal()
mapContainer := NewMultiPattern()
for x := 0; x < ScreenWidth; x += 17 {
for y := 0; y < ScreenHeight; y += 17 {
nw := a
ne := b
sw := c
se := d
nw.StartPoint = engo.Point{float32(x), float32(y)}
ne.StartPoint = engo.Point{float32(x) + 9, float32(y)}
sw.StartPoint = engo.Point{float32(x), float32(y) + 9}
se.StartPoint = engo.Point{float32(x) + 9, float32(y) + 9}
mapContainer.AddElement(&nw)
mapContainer.AddElement(&ne)
mapContainer.AddElement(&sw)
mapContainer.AddElement(&se)
}
}
initialStatus = mapContainer
}
func main() {
consoleLeftMargin := strings.Repeat(" ", 20)
fmt.Println(consoleLeftMargin + " _ _ _ _ _ ")
fmt.Println(consoleLeftMargin + "|_|_|_|_|_|")
fmt.Println(consoleLeftMargin + "|_|_|*|_|_|")
fmt.Println(consoleLeftMargin + "|_|_|_|*|_|")
fmt.Println(consoleLeftMargin + "|_|*|*|*|_|")
fmt.Println(consoleLeftMargin + "|_|_|_|_|_|")
fmt.Println("")
fmt.Println("Golang Conway's GOL implementation using engo engine.")
fmt.Println("Author: @umarquez")
fmt.Println("")
opts := engo.RunOptions{
Title: "Conway's Game of Life",
Width: ScreenWidth, // 85 cols
Height: ScreenHeight, // 85 rows
Fullscreen: false,
}
engo.Run(opts, FirstScene{initialStatus})
}