-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
314 lines (268 loc) · 5.26 KB
/
main.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
package main
import (
"errors"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/vilmibm/smudge/game"
)
const (
minHeight = 10
minWidth = 12
animInterval = time.Millisecond * 300
)
type characterCell struct {
game.GameObject
HasSpread bool
Ignited bool
HP int
}
func (c *characterCell) Update() {
if c.HP <= 0 {
if !c.HasSpread {
c.Spread()
}
c.Game.AddDrawable(newSmoke(c.Game, c.Point()))
c.Game.Destroy(c)
return
}
if c.Ignited {
fireColor := tcell.NewRGBColor(240, int32(rand.Intn(110)+60), 20)
so := c.Game.Style.Foreground(fireColor)
c.StyleOverride = &so
c.HP--
if !c.HasSpread {
if rand.Intn(10) < 8 {
c.Spread()
}
}
}
}
func (c *characterCell) Ignite() {
c.Ignited = true
}
func (c *characterCell) Spread() {
c.HasSpread = true
cells := c.Game.FilterGameObjects(func(d game.Drawable) bool {
var o *characterCell
var ok bool
if o, ok = d.(*characterCell); !ok {
return false
}
if o.Ignited {
return false
}
r := game.NewRay(c.Point(), o.Point())
if r.Length() == 0 {
return false
}
return r.Length() == 2
})
if len(cells) == 0 {
return
}
if len(cells) == 1 {
cell := cells[0].(*characterCell)
cell.Ignite()
return
}
for _, d := range cells {
cell := d.(*characterCell)
if rand.Intn(100) < 25 {
cell.Ignite()
}
}
}
type smoke struct {
game.GameObject
HP int
}
func (s *smoke) Update() {
if s.HP == 0 || s.Y < 0 {
s.Game.Destroy(s)
return
}
spriteSheet := "....++++####"
s.Sprite = string(spriteSheet[s.HP-1])
color := int32(rand.Intn(120) + 60)
so := s.Game.Style.Foreground(tcell.NewRGBColor(color, color, color))
s.StyleOverride = &so
s.HP--
s.Y--
s.X += rand.Intn(3) - 1
}
func newSmoke(g *game.Game, p game.Point) *smoke {
so := g.Style.Foreground(tcell.NewRGBColor(160, 160, 160))
return &smoke{
GameObject: game.GameObject{
X: p.X, Y: p.Y,
W: 1, H: 1,
Sprite: "*",
Game: g,
StyleOverride: &so,
},
HP: 12,
}
}
func _main(sourceFiles []string) (err error) {
sources, err := parseFiles(sourceFiles)
if err != nil {
return err
}
s, err := tcell.NewScreen()
if err != nil {
return err
}
if err = s.Init(); err != nil {
return err
}
defer s.Fini()
defStyle := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)
quit := make(chan struct{})
blow := make(chan struct{})
go inputLoop(s, blow, quit)()
w, h := s.Size()
if w < minWidth || h < minHeight {
return errors.New("terminal is too small i'm sorry")
}
gg := &game.Game{
Screen: s,
Style: defStyle,
MaxWidth: w,
}
smudgeWidth := w / 3
rand.Seed(time.Now().Unix())
sourceIx := 0
sourcePointers := map[int]int{}
sourceColors := map[int]int{}
for i := range sources {
sourcePointers[i] = 0
sourceColors[i] = rand.Intn(120) + 60
}
nextChar := func() (string, tcell.Color) {
color := int32(sourceColors[sourceIx])
char := "x"
source := sources[sourceIx]
nIx := sourcePointers[sourceIx]
if nIx < len(source) {
char = strings.TrimSpace(string(source[nIx]))
if char == "" {
char = "+"
}
sourcePointers[sourceIx]++
}
sourceIx++
if sourceIx >= len(sources) {
sourceIx = 0
}
return char, tcell.NewRGBColor(color, color, color)
}
for y := 0; y < h; y++ {
for x := smudgeWidth; x < smudgeWidth*2; x++ {
char, color := nextChar()
so := defStyle.Foreground(color)
c := &characterCell{
GameObject: game.GameObject{
X: x, Y: y,
W: 1, H: 1,
Sprite: char,
Game: gg,
StyleOverride: &so,
},
HP: 20,
}
if y == 0 {
c.Ignited = true
}
gg.AddDrawable(c)
}
}
reignite := func() {
topCells := gg.FilterGameObjects(func(d game.Drawable) bool {
var cell *characterCell
var ok bool
if cell, ok = d.(*characterCell); !ok {
return false
}
return !cell.Ignited
})
for ix, d := range topCells {
if ix == 10 {
break
}
cell := d.(*characterCell)
cell.Ignite()
}
}
var quitting bool
for {
select {
case <-quit:
quitting = true
case <-blow:
reignite()
case <-time.After(animInterval):
}
if quitting {
break
}
s.Clear()
gg.Update()
gg.Draw()
s.Show()
}
return nil
}
func inputLoop(s tcell.Screen, blow chan struct{}, quit chan struct{}) func() {
return func() {
for {
s.Show()
ev := s.PollEvent()
switch ev := ev.(type) {
case *tcell.EventResize:
s.Sync()
case *tcell.EventKey:
if ev.Key() == tcell.KeyEnter {
blow <- struct{}{}
}
if ev.Key() == tcell.KeyEscape || ev.Key() == tcell.KeyCtrlC {
close(quit)
}
}
}
}
}
func parseFiles(filenames []string) ([]string, error) {
if len(filenames) == 0 {
return nil, fmt.Errorf("no files provided")
}
out := []string{}
errs := []error{}
for _, f := range filenames {
bs, err := os.ReadFile(f)
if err != nil {
errs = append(errs, err)
continue
}
out = append(out, string(bs))
}
if len(out) == 0 {
errMsg := "failed to read any files; errors encountered: "
for _, e := range errs {
errMsg += e.Error() + " "
}
return nil, fmt.Errorf(errMsg)
}
return out, nil
}
func main() {
err := _main(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}