-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from eleniums/hackathon
Hackathon
- Loading branch information
Showing
30 changed files
with
1,088 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package assets | ||
|
||
import ( | ||
"github.com/eleniums/game-of-life-go/assets/fonts" | ||
"github.com/eleniums/game-of-life-go/assets/images" | ||
"github.com/faiface/pixel" | ||
"github.com/faiface/pixel/text" | ||
) | ||
|
||
var ( | ||
Icon16x16 pixel.Picture | ||
Title pixel.Picture | ||
CellMap pixel.Picture | ||
GrassMap pixel.Picture | ||
PixelAtlas *text.Atlas | ||
) | ||
|
||
func Load() error { | ||
var err error | ||
|
||
Icon16x16, err = images.Load(images.Icon16x16Data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
Title, err = images.Load(images.TitleData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
CellMap, err = images.Load(images.CellMapData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
GrassMap, err = images.Load(images.GrassMapData) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
PixelAtlas, err = fonts.Load(fonts.PixelData, 40) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package game | ||
|
||
type CellType int | ||
|
||
const ( | ||
CellType_Cross CellType = iota | ||
CellType_Plus | ||
CellType_Circle | ||
CellType_Dot | ||
) | ||
|
||
type Cell struct { | ||
Alive bool | ||
Type CellType | ||
} | ||
|
||
func (c *Cell) Clear() { | ||
c.Alive = false | ||
c.Type = CellType_Cross | ||
} | ||
|
||
func (c *Cell) Copy() *Cell { | ||
return &Cell{ | ||
Alive: c.Alive, | ||
Type: c.Type, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package game | ||
|
||
const ( | ||
GridMaxX = 288 | ||
GridMaxY = 288 | ||
) | ||
|
||
type CellGrid [][]*Cell | ||
|
||
func NewCellGrid() CellGrid { | ||
grid := make(CellGrid, GridMaxX) | ||
for x := 0; x < GridMaxX; x++ { | ||
grid[x] = make([]*Cell, GridMaxY) | ||
for y := 0; y < GridMaxY; y++ { | ||
grid[x][y] = &Cell{ | ||
Alive: false, | ||
Type: 0, | ||
} | ||
} | ||
} | ||
|
||
return grid | ||
} | ||
|
||
func (c CellGrid) Clear() { | ||
for x := range c { | ||
for y := range c[x] { | ||
c[x][y].Clear() | ||
} | ||
} | ||
} | ||
|
||
func (c CellGrid) Copy() CellGrid { | ||
grid := make(CellGrid, GridMaxX) | ||
for x := 0; x < GridMaxX; x++ { | ||
grid[x] = make([]*Cell, GridMaxY) | ||
for y := 0; y < GridMaxY; y++ { | ||
grid[x][y] = c[x][y].Copy() | ||
} | ||
} | ||
|
||
return grid | ||
} | ||
|
||
func (c CellGrid) IsAlive(x, y int) bool { | ||
if x < 0 || x >= GridMaxX || y < 0 || y >= GridMaxY { | ||
return false | ||
} | ||
|
||
return c[x][y].Alive | ||
} | ||
|
||
func (c CellGrid) CountNeighbors(x, y int) (count, cross, plus, circle, dot int) { | ||
for i := x - 1; i <= x+1; i++ { | ||
for j := y - 1; j <= y+1; j++ { | ||
if c.IsAlive(i, j) && !(i == x && j == y) { | ||
switch c[i][j].Type { | ||
case CellType_Cross: | ||
cross++ | ||
case CellType_Plus: | ||
plus++ | ||
case CellType_Circle: | ||
circle++ | ||
case CellType_Dot: | ||
dot++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
return cross + plus + circle + dot, cross, plus, circle, dot | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package game | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"time" | ||
) | ||
|
||
var ( | ||
Interval = 100 | ||
) | ||
|
||
type Manager struct { | ||
cells CellGrid | ||
buffer CellGrid | ||
memory CellGrid | ||
running bool | ||
ticker *time.Ticker | ||
} | ||
|
||
func NewManager() *Manager { | ||
return &Manager{ | ||
cells: NewCellGrid(), | ||
buffer: NewCellGrid(), | ||
memory: NewCellGrid(), | ||
running: false, | ||
} | ||
} | ||
|
||
func (m *Manager) Update() { | ||
if m.running { | ||
select { | ||
case <-m.ticker.C: | ||
// iterate over grid and apply rules | ||
for x := range m.cells { | ||
for y := range m.cells[x] { | ||
neighbors, cross, plus, circle, dot := m.cells.CountNeighbors(x, y) | ||
if m.cells[x][y].Alive { | ||
m.buffer[x][y].Type = m.cells[x][y].Type | ||
} else { | ||
m.buffer[x][y].Type = determineType(cross, plus, circle, dot) | ||
} | ||
m.buffer[x][y].Alive = applyRules(m.cells[x][y].Alive, neighbors) | ||
} | ||
} | ||
|
||
// swap active cells with buffer | ||
temp := m.cells | ||
m.cells = m.buffer | ||
m.buffer = temp | ||
default: | ||
} | ||
} | ||
} | ||
|
||
func (m *Manager) Start() { | ||
m.running = true | ||
m.ticker = time.NewTicker(time.Duration(Interval) * time.Millisecond) | ||
} | ||
|
||
func (m *Manager) Stop() { | ||
m.ticker.Stop() | ||
m.running = false | ||
} | ||
|
||
func (m *Manager) Clear() { | ||
m.cells.Clear() | ||
m.buffer.Clear() | ||
} | ||
|
||
func (m *Manager) Store() { | ||
m.memory = m.cells.Copy() | ||
} | ||
|
||
func (m *Manager) Reset() { | ||
m.cells = m.memory.Copy() | ||
} | ||
|
||
func (m *Manager) Save(path string) error { | ||
return save(m.cells, path) | ||
} | ||
|
||
func (m *Manager) Load(path string) error { | ||
cells, err := load(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
m.cells = cells | ||
m.buffer.Clear() | ||
m.memory = m.cells.Copy() | ||
|
||
return nil | ||
} | ||
|
||
func (m *Manager) Cells() CellGrid { | ||
return m.cells | ||
} | ||
|
||
func (m *Manager) Running() bool { | ||
return m.running | ||
} | ||
|
||
type storage struct { | ||
Cell *Cell | ||
X int | ||
Y int | ||
} | ||
|
||
func save(cells CellGrid, path string) error { | ||
compact := []*storage{} | ||
for x := range cells { | ||
for y := range cells[x] { | ||
if cells[x][y].Alive { | ||
compact = append(compact, &storage{ | ||
Cell: cells[x][y], | ||
X: x, | ||
Y: y, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
data, err := json.Marshal(compact) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = ioutil.WriteFile(path, data, 0644) | ||
|
||
return err | ||
} | ||
|
||
func load(path string) (CellGrid, error) { | ||
data, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var compact []*storage | ||
err = json.Unmarshal(data, &compact) | ||
|
||
cells := NewCellGrid() | ||
for _, v := range compact { | ||
cells[v.X][v.Y] = v.Cell | ||
} | ||
|
||
return cells, err | ||
} |
Oops, something went wrong.