forked from gophergala2016/gopher_typer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_level.go
155 lines (135 loc) · 3.46 KB
/
game_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
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
package gopherTyper
import (
"fmt"
"math"
"math/rand"
"strings"
"time"
tl "github.com/JoelOtter/termloop"
)
type gameLevel struct {
tl.Level
gt *GopherTyper
fg tl.Attr
bg tl.Attr
words []*word
currentWord *word
currentWordText *tl.Text
garbageText *tl.Text
garbageCollectEndsAt time.Time
}
func (l *gameLevel) Activate() {
l.Level = tl.NewBaseLevel(tl.Cell{Bg: l.bg, Fg: l.fg})
l.gt.stats.Garbage = 0
l.gt.game.AddEntity(&l.gt.console)
l.gt.console.SetText("")
numWords := l.gt.stats.LevelsCompleted + 1
w, h := l.gt.g.Screen().Size()
l.words = []*word{}
x := 0
y := 0
for i := 0; i < numWords; i++ {
str := l.gt.wordList[rand.Intn(len(l.gt.wordList))]
if len(str)+x > w {
x = 0
y++
}
w := newWord(x, y, str, tl.ColorRed, tl.ColorGreen, tl.ColorBlue, tl.ColorCyan)
l.AddEntity(w)
l.words = append(l.words, w)
x += len(w.str) + 2
}
l.currentWord = nil
l.currentWordText = tl.NewText(0, h-1, "", tl.ColorRed, tl.ColorBlue)
l.AddEntity(l.currentWordText)
l.garbageText = tl.NewText(w, h-1, "", tl.ColorRed, tl.ColorBlue)
l.AddEntity(l.garbageText)
l.AddEntity(tl.NewText(0, h-2, strings.Repeat("*", w), tl.ColorBlack, tl.ColorDefault))
for _, i := range l.gt.items {
i.Reset(l.gt)
}
l.gt.g.Screen().SetLevel(l)
}
func (l *gameLevel) Draw(screen *tl.Screen) {
l.Level.Draw(screen)
if time.Now().After(l.garbageCollectEndsAt) {
for _, i := range l.gt.items {
i.Tick(l)
}
}
sw, sh := screen.Size()
gameLost := false
gameWon := false
totalComplete := 0
for _, w := range l.words {
w.Update()
if !w.Complete() && w.y > sh-3 {
gameLost = true
}
if w.Complete() {
totalComplete++
}
}
if totalComplete == len(l.words) {
gameWon = true
}
if l.currentWord != nil && l.currentWord.Complete() {
l.currentWord = nil
}
var possibleWords []int
for i, w := range l.words {
if !w.Complete() && w.startedBy == 0 {
possibleWords = append(possibleWords, i)
}
}
if l.currentWord == nil && len(possibleWords) > 0 {
l.currentWord = l.words[possibleWords[rand.Intn(len(possibleWords))]]
l.currentWord.startedBy = pc
}
if l.gt.stats.GarbageCollect() {
l.gt.stats.Garbage = 0
if l.gt.stats.GoVersion < 1.5 {
l.garbageCollectEndsAt = time.Now().Add(time.Second * 3)
} else {
l.garbageCollectEndsAt = time.Now().Add(time.Second)
}
}
var msg string
if time.Now().Before(l.garbageCollectEndsAt) {
msg = fmt.Sprintf("COLLECTING GARBAGE: PLEASE WAIT")
l.garbageText.SetText(msg)
bgColor := tl.ColorBlue
if math.Remainder(float64(time.Now().Sub(l.garbageCollectEndsAt)), float64(time.Second)) > 0.5 {
bgColor = tl.ColorBlack
}
l.garbageText.SetColor(tl.ColorRed, bgColor)
l.garbageText.SetPosition(sw/2-len(msg)/2, 4)
} else {
msg = fmt.Sprintf("Garbage: %d ", l.gt.stats.Garbage)
l.garbageText.SetText(msg)
l.garbageText.SetColor(tl.ColorRed, tl.ColorBlue)
l.garbageText.SetPosition(sw-len(msg), sh-1)
}
if l.currentWord != nil {
l.currentWordText.SetText("Current Word: " + l.currentWord.str[l.currentWord.completedChars:])
} else {
l.currentWordText.SetText("")
}
// End conditions
if gameWon {
l.gt.goToEndWin()
}
if gameLost {
l.gt.goToEndFail()
}
}
func (l *gameLevel) Tick(e tl.Event) {
if e.Type == tl.EventKey {
if l.currentWord != nil {
l.currentWord.KeyDown(e.Ch)
}
}
}
func newGameLevel(g *GopherTyper, fg, bg tl.Attr) gameLevel {
return gameLevel{gt: g, fg: fg, bg: bg}
}