-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtif.go
453 lines (389 loc) · 8.76 KB
/
tif.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"fmt"
"math/rand"
"sync"
"time"
sprite "github.com/pdevine/go-asciisprite"
tm "github.com/pdevine/go-asciisprite/termbox"
)
var allSprites sprite.SpriteGroup
var Width int
var Height int
const (
LEFT = iota
RIGHT
BACKLEFT
BACKRIGHT
)
const DogCostume1 = `
dd
ddBBd
BBddd
dddddttt
dddtttttt
ttttttttt
Btttttttw
BBttwwttwBB
BBBtwBBwtwBB
BBBBtwBBwttwttBBB
BBBBttwwtttttttBB
BBBBttttttBtttttt
BBttttttttBtttt
tTtTttttt
tTtTttttt
dtTtTttttt
dtTttTtttT
dtTTTTTTtTT
ddtttttTdtT
dddddtTdtTT
d dtTT
d d d
d d`
const TableCostume1 = `
wwww
wBBwww
wwww w
dddwwwwwwdddddddddddd
ddddwwddddddddddddddd
ddddddddddddddddddddd
d d
d d
d d`
const BubbleCostumeBottom = `wwwwwwwww
wwwww
www
ww
w`
const SmokeCostume1 = `gggggggggggggggggggggggggggggggggGGGgggggg
ggggggggggggggggggggggggggggggGGGGGGGGgggg
gggggggggggggggggGGgggGgggGGGGGGGGGGGGGGGg
gggggggggggggggGGGGGGGGGGGGGGGGGGGGGGGGGGG
GGgggggggggggGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
GGGggggggggGGGGGGGGGGGGGGGGGGGGGGGGG GGGGG
GGGGGGGGGGGGGGGGGGGGGGGGGG GGG
GGGGGGGGGGGGGGGGG GGGGGGG G
GGGGGGGGGGGGGGGG GGG
GGGGGGGGGGG
GGGG`
type Dog struct {
sprite.BaseSprite
Timer int
TimeOut int
}
type Room struct {
sprite.BaseSprite
}
type Table struct {
sprite.BaseSprite
}
type Fire struct {
sprite.BaseSprite
Points int
mu sync.Mutex
buf [][]int
}
type Smoke struct {
sprite.BaseSprite
Timer int
TimeOut int
}
type Text struct {
sprite.BaseSprite
font *sprite.Font
}
func NewDog() *Dog {
d := &Dog{BaseSprite: sprite.BaseSprite{
Visible: true},
TimeOut: 100,
}
d.Init()
d.RegisterEvent("resizeScreen", func() {
d.X = Width/2 - 22
if Width/2 % 2 != 0 {
d.X++
}
d.Y = Height/2 + 8
if Height/2 % 2 != 0 {
d.Y++
}
})
surf := sprite.NewSurfaceFromString(DogCostume1, true)
d.BlockCostumes = append(d.BlockCostumes, &surf)
return d
}
func (d *Dog) Update() {
d.Timer++
if d.Timer == d.TimeOut {
allSprites.TriggerEvent("saythisisfine")
}
}
func NewRoom() *Room {
r := &Room{BaseSprite: sprite.BaseSprite{
Visible: true},
}
r.Init()
r.RegisterEvent("resizeScreen", func() {
surf := sprite.NewSurface(Width, 34, false)
surf.Line(0, surf.Height-1, 30, surf.Height-1, 'X')
surf.Line(30, surf.Height-1, 30, 2, 'X')
surf.Line(30, 2, 50, 0, 'X')
surf.Line(50, 0, 50, surf.Height-1, 'X')
surf.Line(50, surf.Height-1, surf.Width-1, surf.Height-1, 'X')
r.BlockCostumes = []*sprite.Surface{&surf}
r.Y = Height/2 - surf.Height/2
})
return r
}
func NewTable() *Table {
t := &Table{BaseSprite: sprite.BaseSprite{
Visible: true},
}
t.Init()
t.RegisterEvent("resizeScreen", func() {
t.X = Width/2 + 10
t.Y = Height/2 + 22
})
surf := sprite.NewSurfaceFromString(TableCostume1, false)
t.BlockCostumes = append(t.BlockCostumes, &surf)
return t
}
func NewSmoke() *Smoke {
s := &Smoke{BaseSprite: sprite.BaseSprite{
Visible: true},
TimeOut: 1,
}
s.Init()
s.RegisterEvent("resizeScreen", func() {
surf := sprite.NewSurfaceFromString(SmokeCostume1, true)
yOff := 10
bigSurf := sprite.NewSurface(Width*2, surf.Height+yOff, true)
// fill top part w/ silver
for y := 0; y < yOff; y++ {
for x := 0; x < bigSurf.Width; x++ {
bigSurf.Blocks[y][x] = 'g'
}
}
for cnt := 0; cnt < Width*2 / surf.Width; cnt++ {
bigSurf.Blit(surf, cnt*surf.Width, yOff)
}
s.BlockCostumes = []*sprite.Surface{&bigSurf}
})
return s
}
func (s *Smoke) Update() {
s.Timer++
if s.Timer > s.TimeOut {
s.X--
s.Timer = 0
}
if s.X <= -Width/2 {
s.X = 0
}
}
func NewFire(side int) *Fire {
f := &Fire{BaseSprite: sprite.BaseSprite{
X: 0,
Y: 0,
Visible: true},
Points: 5,
}
f.Init()
if side == BACKLEFT || side == BACKRIGHT {
f.Points = 7
}
f.RegisterEvent("flamesHigher", func() {
f.Points -= 1
if f.Points == 3 {
f.Points = 4
}
})
f.RegisterEvent("flamesLower", func() {
f.Points += 1
if f.Points > 20 {
f.Points = 20
}
})
f.RegisterEvent("resizeScreen", func() {
f.mu.Lock()
f.buf = make([][]int, Height+1)
for cnt := range f.buf {
f.buf[cnt] = make([]int, Width/2+1)
}
f.mu.Unlock()
switch side {
case LEFT:
f.X = 0
f.Y = 0
case RIGHT:
f.X = Width/2 + 25
f.Y = 0
case BACKLEFT:
f.X = 0
f.Y = -Height/2 + 20
case BACKRIGHT:
f.X = Width/2
f.Y = -Height/2 + 20
}
surf := sprite.NewSurface(Width/2, Height, true)
f.BlockCostumes = []*sprite.Surface{&surf}
})
return f
}
func (f *Fire) Update() {
f.mu.Lock()
defer f.mu.Unlock()
// Screen hasn't properly initialized, so don't bother updating
if len(f.buf) < Height || len(f.buf[Height-1]) < Width/2 {
return
}
for cnt := 0; cnt < int(Width/2 / f.Points); cnt++ {
f.buf[Height-1][rand.Intn(Width/2)] = 65
}
surf := sprite.NewSurface(Width/2, Height, true)
for h := 0; h < Height; h++ {
for w := 0; w < Width/2; w++ {
if rand.Intn(2) == 0 {
if w > 0 {
f.buf[h][w] += f.buf[h][w-1]
}
} else {
f.buf[h][w] += f.buf[h][w+1]
}
f.buf[h][w] += f.buf[h+1][w]
f.buf[h][w] += f.buf[h+1][w+1]
f.buf[h][w] /= 4
if f.buf[h][w] > 15 {
surf.Blocks[h][w] = 'r'
} else if f.buf[h][w] > 9 {
surf.Blocks[h][w] = 'o'
} else if f.buf[h][w] > 4 {
surf.Blocks[h][w] = 'y'
}
}
}
f.BlockCostumes = []*sprite.Surface{&surf}
}
func NewText() *Text {
t := &Text{BaseSprite: sprite.BaseSprite{
X: Width/2,
Y: 30,
Visible: false},
font: sprite.NewPakuFont(),
}
t.Init()
t.RegisterEvent("resizeScreen", func() {
t.X = Width/2
if Width/2 % 2 != 0 {
t.X++
}
t.Y = Height/2 - 20
if Height/2 % 2 != 0 {
t.Y++
}
})
w := 70
h := 20
bgSurf := sprite.NewSurface(w, h+6, true)
for y := 1; y < h-1; y++ {
for x := 0; x < bgSurf.Width; x++ {
bgSurf.Blocks[y][x] = 'w'
}
}
surf := sprite.NewSurfaceFromString(t.font.BuildString("This is fine."), true)
bb := sprite.NewSurfaceFromString(BubbleCostumeBottom, true)
bgSurf.Line(2, 0, w-2, 0, 'w')
bgSurf.Line(2, h-1, w-2, h-1, 'w')
bgSurf.Blit(surf, w/2-surf.Width/2, h/2-surf.Height/2)
bgSurf.Blit(bb, 12, bgSurf.Height-bb.Height)
t.BlockCostumes = append(t.BlockCostumes, &bgSurf)
t.RegisterEvent("saythisisfine", func() {
t.Visible = true
})
return t
}
func setPalette() {
sprite.ColorMap['o'] = tm.Color214
sprite.ColorMap['y'] = tm.Color226
sprite.ColorMap['r'] = tm.Color197
sprite.ColorMap['d'] = tm.Color52
sprite.ColorMap['B'] = tm.ColorBlack
sprite.ColorMap['w'] = tm.ColorWhite
sprite.ColorMap['t'] = tm.Color173
sprite.ColorMap['T'] = tm.Color130
sprite.ColorMap['g'] = tm.ColorSilver
sprite.ColorMap['G'] = tm.ColorGray
sprite.ColorMap['X'] = tm.ColorBlack
}
func main() {
// XXX - Wait a bit until the terminal is properly initialized
time.Sleep(500 * time.Millisecond)
err := tm.Init()
if err != nil {
fmt.Printf("No terminal was detected. If this is in a docker container, use `docker run -it`\n")
panic(err)
}
defer tm.Close()
w, h := tm.Size()
Width = w*2
Height = h*2
setPalette()
allSprites.Init(Width, Height, true)
allSprites.Background = tm.Color187
eventQueue := make(chan tm.Event)
go func() {
for {
eventQueue <- tm.PollEvent()
}
}()
//r := NewRoom()
d := NewDog()
t := NewTable()
s := NewSmoke()
text := NewText()
f1 := NewFire(LEFT)
f2 := NewFire(RIGHT)
f3 := NewFire(BACKLEFT)
f4 := NewFire(BACKRIGHT)
//allSprites.Sprites = append(allSprites.Sprites, r)
allSprites.Sprites = append(allSprites.Sprites, f3)
allSprites.Sprites = append(allSprites.Sprites, f4)
allSprites.Sprites = append(allSprites.Sprites, d)
allSprites.Sprites = append(allSprites.Sprites, t)
allSprites.Sprites = append(allSprites.Sprites, s)
allSprites.Sprites = append(allSprites.Sprites, f1)
allSprites.Sprites = append(allSprites.Sprites, f2)
allSprites.Sprites = append(allSprites.Sprites, text)
mainloop:
for {
tm.Clear(tm.Color187, tm.Color187)
select {
case ev := <-eventQueue:
if ev.Type == tm.EventKey {
if ev.Key == tm.KeyCtrlC || ev.Key == tm.KeyEsc || ev.Ch == 'q' {
break mainloop
}
if ev.Key == tm.KeyArrowUp {
allSprites.TriggerEvent("flamesHigher")
} else if ev.Key == tm.KeyArrowDown {
allSprites.TriggerEvent("flamesLower")
}
} else if ev.Type == tm.EventResize {
// Don't try to initialize anything if the terminal isn't ready -- another
// EventResize will happen once the terminal is properly initialized
if ev.Width == 0 || ev.Height == 0 {
continue
}
Width = ev.Width*2
Height = ev.Height*2
allSprites.Init(Width, Height, true)
allSprites.Background = tm.Color187
allSprites.TriggerEvent("resizeScreen")
}
default:
allSprites.Update()
allSprites.Render()
time.Sleep(60 * time.Millisecond)
}
}
}