forked from gophergala2016/gopher_typer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intro_level.go
73 lines (61 loc) · 1.77 KB
/
intro_level.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
package gopherTyper
import (
"io/ioutil"
"time"
tl "github.com/JoelOtter/termloop"
)
type introLevel struct {
tl.Level
gt *GopherTyper
pressAKeyText *tl.Text
needsRefresh bool
swapMessageTime time.Time
reverseText bool
}
func (l *introLevel) Activate() {
l.needsRefresh = true
l.gt.g.Screen().SetLevel(l)
}
func (l *introLevel) refresh() {
l.gt.intro.AddEntity(&l.gt.console)
l.gt.console.SetText("")
w, h := l.gt.g.Screen().Size()
quarterH := h / 4
rect := tl.NewRectangle(10, 2, w-20, h-4, tl.ColorCyan)
l.AddEntity(rect)
logo, _ := ioutil.ReadFile("data/logo.txt")
c := tl.CanvasFromString(string(logo))
logoEntity := tl.NewEntityFromCanvas(w/2-len(c)/2, quarterH, tl.CanvasFromString(string(logo)))
l.AddEntity(logoEntity)
msg := "Press any key to continue"
l.pressAKeyText = tl.NewText(w/2-len(msg)/2, h/2, msg, tl.ColorBlue|tl.AttrReverse, tl.ColorDefault)
l.AddEntity(l.pressAKeyText)
instructions, _ := ioutil.ReadFile("data/instructions.txt")
c = tl.CanvasFromString(string(instructions))
l.AddEntity(tl.NewEntityFromCanvas(w/2-len(c)/2, h/2+2, c))
l.needsRefresh = false
}
func (l *introLevel) Draw(screen *tl.Screen) {
if l.needsRefresh {
l.refresh()
}
if time.Now().After(l.swapMessageTime) {
if l.reverseText {
l.pressAKeyText.SetColor(tl.ColorBlue, tl.ColorDefault)
} else {
l.pressAKeyText.SetColor(tl.ColorBlue|tl.AttrReverse, tl.ColorDefault)
}
l.reverseText = !l.reverseText
l.swapMessageTime = time.Now().Add(500 * time.Millisecond)
}
l.Level.Draw(screen)
}
func (l *introLevel) Tick(event tl.Event) {
if event.Type == tl.EventKey {
l.gt.goToGame()
}
}
func newIntroLevel(g *GopherTyper, fg, bg tl.Attr) introLevel {
l := tl.NewBaseLevel(tl.Cell{Bg: bg, Fg: fg})
return introLevel{Level: l, gt: g}
}