-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknights.go
167 lines (146 loc) · 3.5 KB
/
knights.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
package modes
import (
"fmt"
"math/rand"
"github.com/hajimehoshi/ebiten"
"seebs.net/modus/g"
"seebs.net/modus/keys"
"seebs.net/modus/sound"
)
var knightMoves = []g.IVec{
{X: -2, Y: -1},
{X: -1, Y: -2},
{X: 1, Y: -2},
{X: 2, Y: -1},
{X: 2, Y: 1},
{X: 1, Y: 2},
{X: -1, Y: 2},
{X: -2, Y: 1},
}
func knightMove() g.IVec {
return knightMoves[int(rand.Int31n(int32(len(knightMoves))))]
}
// knightMode is one of the internal modes based on knight moves
type knightMode struct {
k int // knights
cycleTime int // number of ticks to go by between updates
}
const knightCycleTime = 10
var knightModes = []knightMode{
{k: 1, cycleTime: knightCycleTime},
{k: 2, cycleTime: knightCycleTime},
{k: 3, cycleTime: knightCycleTime},
{k: 4, cycleTime: knightCycleTime},
{k: 5, cycleTime: knightCycleTime},
{k: 6, cycleTime: knightCycleTime},
}
func init() {
for _, mode := range knightModes {
defaultList.Add(mode)
}
}
func (m knightMode) Name() string {
return fmt.Sprintf("knights%d", m.k)
}
func (m knightMode) Description() string {
return fmt.Sprintf("%d knights jumping", m.k)
}
func (m knightMode) New(gctx *g.Context, detail int, p *g.Palette) (Scene, error) {
return newKnightScene(m, gctx, detail, p)
}
type knight struct {
g.ILoc
P g.Paint
c *g.FloatingCellBase
}
func (k *knight) apply() {
if k.c != nil {
k.c.P = k.P
*k.c.X() = float32(k.X)
*k.c.Y() = float32(k.Y)
}
}
type knightScene struct {
nextKnight int
palette *g.Palette
gctx *g.Context
mode knightMode
knights []knight
detail int
gr *g.SquareGrid
cycle int
}
func newKnightScene(m knightMode, gctx *g.Context, detail int, p *g.Palette) (*knightScene, error) {
sc := &knightScene{mode: m, gctx: gctx, detail: detail, palette: p, knights: make([]knight, m.k)}
err := sc.Reset(detail, p)
if err != nil {
return nil, err
}
return sc, nil
}
func (s *knightScene) Mode() Mode {
return s.mode
}
func (s *knightScene) Reset(detail int, p *g.Palette) error {
_ = s.Hide()
s.palette = p
err := s.Display()
if err != nil {
return err
}
// and then reset the grid, and the knights, i guess?
return nil
}
func (s *knightScene) Display() error {
s.gr = s.gctx.NewSquareGrid(s.detail, 1, s.palette)
p := s.gr.Palette().Paint(0)
s.gr.Iterate(func(generic g.Grid, l g.ILoc, n int, c *g.Cell) {
c.P = p
})
for i := 0; i < s.mode.k; i++ {
c := s.gr.NewExtraCell().(*g.FloatingCellBase)
c.Cell.Scale = 0.7
l := s.gr.NewLoc()
*c.Loc() = g.FLoc{X: float32(l.X), Y: float32(l.Y)}
c.Cell.Alpha = 0
s.knights[i].c = c
s.knights[i].P = g.Paint(i)
s.knights[i].apply()
}
return nil
}
func (s *knightScene) Hide() error {
for i := 0; i < s.mode.k; i++ {
s.knights[i].c = nil
}
s.gr = nil
return nil
}
func (s *knightScene) Tick(voice *sound.Voice, km keys.Map) (bool, error) {
s.cycle = (s.cycle + 1) % s.mode.cycleTime
if s.cycle != 0 {
return false, nil
}
s.gr.Iterate(func(gr g.Grid, l g.ILoc, n int, c *g.Cell) {
c.IncAlpha(-0.001)
})
k := &s.knights[s.nextKnight]
k.ILoc, _ = s.gr.Add(k.ILoc, knightMove())
k.P = s.gr.IncP(k.ILoc, 2)
k.c.Cell.Alpha = 1
k.apply()
s.gr.IncAlpha(k.ILoc, 0.2)
voice.Play(int(s.gr.Cells[k.X][k.Y].P), 75)
s.gr.Splash(k.ILoc, 1, 1, func(gr g.Grid, l g.ILoc, n int, c *g.Cell) {
gr.IncP(l, 1)
c.IncAlpha(0.1)
})
s.nextKnight = (s.nextKnight + 1) % s.mode.k
return true, nil
}
func (s *knightScene) Draw(screen *ebiten.Image) error {
s.gctx.Render(screen, func(t *ebiten.Image, scale float32) {
s.gr.Draw(t, scale)
})
return nil
}