-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvert.go
110 lines (86 loc) · 2.06 KB
/
advert.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
package main
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/text"
"golang.org/x/image/font/basicfont"
"math/rand"
"time"
)
const advertMessageSpacing = " "
const textSpeed = 5
var advertMessages = []string{
"Welcome to Beat Phaser. Try not to die, won't you?",
"Boogie",
"Y'know so much of the music we hear today is pre-programmed electronic disco, we never get a chance to hear master blues men practicing their craft anymore.",
"By the year 2006, the music known today as the blues will exist only in the classical records department of your local public library.",
"You're gonna carry that weight.",
"Eat, Sleep, Buy Stuff, Dance.",
"Hello World!",
"Ship it!",
"<3 <3 <3",
}
type advert struct {
pos pixel.Vec
atlas *text.Atlas
i float64
maxWidth int
text string
shownText string
tick <-chan time.Time
}
func (a *advert) init() {
a.atlas = text.NewAtlas(
basicfont.Face7x13,
text.ASCII,
)
for i := range advertMessages {
if len(advertMessages[i]) < a.maxWidth {
for j := len(advertMessages[i]); j < a.maxWidth; j++ {
advertMessages[i] += " "
}
}
}
a.text = advertMessages[0] + advertMessageSpacing
a.tick = time.Tick(time.Second * 20)
}
func (a *advert) pickRandomMessage() {
r := rand.Intn(len(advertMessages))
a.text = advertMessages[r] + advertMessageSpacing
a.shownText = ""
a.i = 0
}
func (a *advert) update(dt float64) {
i := int(a.i)
if i == len(a.text)-1 {
select {
case <-a.tick:
a.pickRandomMessage()
return
default:
}
}
if i+a.maxWidth < len(a.text) {
a.shownText = a.text[i : i+a.maxWidth]
a.i += dt * textSpeed
} else if i < len(a.text) {
a.shownText = a.text[i:]
a.i += dt * textSpeed
} else {
a.i = 0
}
j := 0
for len(a.shownText) < a.maxWidth && j < len(a.text) {
a.shownText += string(a.text[j])
j++
}
}
func (a *advert) draw(t pixel.Target) {
tx := text.New(a.pos, a.atlas)
tx.Color = pixel.RGB(235.0/255.0, 35.0/255.0, 208.0/255.0)
_, err := fmt.Fprintf(tx, a.shownText)
if err != nil {
panic(err)
}
tx.Draw(t, pixel.IM)
}