-
Notifications
You must be signed in to change notification settings - Fork 1
/
word.go
75 lines (64 loc) · 1.62 KB
/
word.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
package gopherTyper
import (
"time"
tl "github.com/JoelOtter/termloop"
)
type word struct {
str string
createdAt time.Time
v float64
startedBy int
completedChars int
deleteAt time.Time
x, y, baseY int
fgComplete, fgTodo tl.Attr
bgPlayer, bgGoroutine tl.Attr
}
const pc = -1
func newWord(x, y int, val string, fgComplete, fgTodo, bgPlayer, bgGoroutine tl.Attr) *word {
return &word{str: val, createdAt: time.Now(), v: 2, x: x, y: y, baseY: y, fgComplete: fgComplete, fgTodo: fgTodo, bgPlayer: bgPlayer, bgGoroutine: bgGoroutine}
}
func (w *word) Draw(s *tl.Screen) {
for i, ch := range w.str {
if w.startedBy == 0 {
s.RenderCell(w.x+i, w.y, &tl.Cell{Fg: w.fgTodo, Bg: tl.ColorDefault, Ch: ch})
} else {
var bg tl.Attr
if w.startedBy == pc {
bg = w.bgPlayer
} else {
bg = w.bgGoroutine
}
if i < w.completedChars {
s.RenderCell(w.x+i, w.y, &tl.Cell{Fg: w.fgComplete, Bg: bg, Ch: ch})
} else {
s.RenderCell(w.x+i, w.y, &tl.Cell{Fg: w.fgTodo, Bg: bg, Ch: ch})
}
}
}
}
func (w *word) Tick(e tl.Event) {
}
func (w *word) Complete() bool {
return w.completedChars == len(w.str)
}
func (w *word) Update() {
w.y = w.baseY + int((time.Now().Sub(w.createdAt)).Seconds()*w.v)
if w.Complete() {
w.bgPlayer = tl.AttrUnderline
w.bgGoroutine = tl.AttrUnderline
}
}
func (w *word) KeyDown(ch rune) {
found := false
for i, r := range w.str {
if i == w.completedChars && r == ch {
w.completedChars++
found = true
break
}
}
if !found {
w.createdAt = w.createdAt.Add(-1 * time.Second)
}
}