-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
game.go
135 lines (123 loc) · 2.93 KB
/
game.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
package main
import (
"fmt"
"time"
"github.com/gdamore/tcell"
"github.com/sachaos/go-life/preset"
)
type Game struct {
screen tcell.Screen
board *Board
themes []Theme
themeIndex int
presets []preset.Preset
presetIndex int
event chan Event
hide bool
stop bool
ticker *time.Ticker
}
func (g *Game) display() {
g.screen.Clear()
bst := tcell.StyleDefault.Background(g.themes[g.themeIndex].BackGround)
for i, row := range g.board.State() {
for j, cell := range row {
st := tcell.StyleDefault.Background(g.themes[g.themeIndex].Color(cell.LiveTime()))
if cell.State() {
g.screen.SetCell(j*2, i, st, ' ')
g.screen.SetCell(j*2+1, i, st, ' ')
} else {
g.screen.SetCell(j*2, i, bst, ' ')
g.screen.SetCell(j*2+1, i, bst, ' ')
}
}
}
}
func (g *Game) Loop() error {
for {
g.display()
select {
case ev := <-g.event:
switch ev.Type {
case switchState:
if (ev.X < g.board.width) && (ev.Y < g.board.height) && (ev.X >= 0) && (ev.Y >= 0) {
{
g.board.Get(ev.X, ev.Y).Switch()
}
}
case putPreset:
g.board.Set(ev.X, ev.Y, g.presets[g.presetIndex].Cells)
case resize:
stopState := g.stop
g.stop = true
width, height := g.screen.Size()
g.board.Resize(width/2, height)
g.stop = stopState
case step:
g.board.Next()
g.screen.Show()
case done:
return nil
case reset:
stopState := g.stop
g.stop = true
g.board.Random()
g.stop = stopState
case switchStop:
g.switchStop()
case clear:
g.board.Init()
g.screen.Show()
case switchPreset:
g.switchPreset()
case switchHide:
g.switchHide()
case switchTheme:
g.switchTheme()
default:
return fmt.Errorf(ev.Type)
}
case <-g.ticker.C:
if !g.stop {
g.board.Next()
} else if !g.hide {
g.displayMessage()
}
g.screen.Show()
}
}
}
func putString(s tcell.Screen, x, y int, str string) {
st := tcell.StyleDefault.Background(tcell.ColorGray).Foreground(tcell.ColorWhite)
for i, byte := range str {
s.SetCell(x+i, y, st, byte)
}
}
func (g *Game) switchHide() {
g.hide = !g.hide
}
func (g *Game) switchStop() {
g.stop = !g.stop
}
func (g *Game) displayMessage() {
_, height := g.screen.Size()
putString(g.screen, 0, 0, "SPC: start, Enter: next, c: clear, r: random, h: hide this message & status")
putString(g.screen, 0, 1, "LeftClick: switch state, RightClick: insert preset")
putString(g.screen, 0, 2, fmt.Sprintf("p: switch preset, Current: \"%s\"", g.presets[g.presetIndex].Name))
putString(g.screen, 0, 3, fmt.Sprintf("t: switch theme, Current: \"%s\"", g.themes[g.themeIndex].Name))
putString(g.screen, 0, height-1, fmt.Sprintf("Time: %d", g.board.Time()))
}
func (g *Game) switchPreset() {
if g.presetIndex < len(g.presets)-1 {
g.presetIndex++
} else {
g.presetIndex = 0
}
}
func (g *Game) switchTheme() {
if g.themeIndex < len(g.themes)-1 {
g.themeIndex++
} else {
g.themeIndex = 0
}
}