forked from gophergala2016/gopher_typer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_level.go
124 lines (105 loc) · 2.98 KB
/
store_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
package gopherTyper
import (
"fmt"
"io/ioutil"
"time"
tl "github.com/JoelOtter/termloop"
)
type storeLevel struct {
tl.Level
gt *GopherTyper
bg tl.Attr
fg tl.Attr
items []item
currentItem int
}
func (l *storeLevel) refresh() {
l.Level = tl.NewBaseLevel(tl.Cell{Bg: l.bg, Fg: l.fg})
l.gt.store.AddEntity(&l.gt.console)
l.gt.console.SetText("")
w, h := l.gt.g.Screen().Size()
rect := tl.NewRectangle(10, 2, w-20, h-4, tl.ColorGreen)
l.AddEntity(rect)
store, _ := ioutil.ReadFile("data/store.txt")
c := tl.CanvasFromString(string(store))
l.AddEntity(tl.NewEntityFromCanvas(w/2-len(c)/2, 4, c))
msg := "Up/Down(j/k), Enter to purchase, N to return to the game"
l.AddEntity(tl.NewText(w/2-len(msg)/2, 10, msg, tl.ColorBlack, tl.ColorDefault))
msg = fmt.Sprintf("Cash: $%d", l.gt.stats.Dollars)
l.AddEntity(tl.NewText(14, 11, msg, tl.ColorBlack, tl.ColorDefault))
y := 12
for idx, i := range l.items {
i.Reset(l.gt)
x := 14
fg := tl.ColorBlack
if i.Price() > l.gt.stats.Dollars {
fg = tl.ColorRed
}
var price string
if l.currentItem == idx {
price = ">" + i.PriceDesc() + "<"
} else {
price = " " + i.PriceDesc()
}
l.AddEntity(tl.NewText(x, y, price, fg, tl.ColorDefault))
x += len(i.PriceDesc()) + 4
l.AddEntity(tl.NewText(x, y, i.Name(), tl.ColorBlue, tl.ColorDefault))
y++
}
desc := l.items[l.currentItem].Desc()
l.AddEntity(tl.NewText(14, y+1, desc, tl.ColorBlue, tl.ColorDefault))
y = 12
x := w - 30
msg = fmt.Sprintf("Goroutines: %d", len(l.gt.items))
l.AddEntity(tl.NewText(x, y, msg, tl.ColorBlue, tl.ColorDefault))
y++
msg = fmt.Sprintf("CPU Upgrades: %d", l.gt.stats.CPUUpgrades)
l.AddEntity(tl.NewText(x, y, msg, tl.ColorBlue, tl.ColorDefault))
y++
msg = fmt.Sprintf("Go Version: %0.1f", l.gt.stats.GoVersion)
l.AddEntity(tl.NewText(x, y, msg, tl.ColorBlue, tl.ColorDefault))
y++
l.gt.g.Screen().SetLevel(l)
}
func (l *storeLevel) Activate() {
l.currentItem = 0
l.refresh()
}
func (l *storeLevel) purchaseItem(id int) {
itm := l.items[id]
if itm.Price() <= l.gt.stats.Dollars {
if itm.Purchase(l) {
l.gt.items = append(l.gt.items, itm.Dupe())
l.gt.items[len(l.gt.items)-1].SetID(len(l.gt.items))
}
l.gt.stats.Dollars -= itm.Price()
}
}
func (l *storeLevel) Tick(e tl.Event) {
if e.Type == tl.EventKey {
if e.Key == tl.KeyArrowDown || e.Ch == 'j' {
l.currentItem = (l.currentItem + 1) % len(l.items)
} else if e.Key == tl.KeyArrowUp || e.Ch == 'k' {
l.currentItem = (l.currentItem - 1)
if l.currentItem < 0 {
l.currentItem = len(l.items) - 1
}
} else if e.Key == tl.KeyEnter || e.Ch == 'e' {
l.purchaseItem(l.currentItem)
} else if e.Ch == 'N' || e.Ch == 'n' {
l.gt.goToGame()
return
}
l.refresh()
}
}
func newBaseItems() []item {
return []item{
newGoroutineItem(150*time.Millisecond, 500*time.Millisecond),
&cpuUpgradeItem{},
&goUpgradeItem{},
}
}
func newStoreLevel(g *GopherTyper, fg, bg tl.Attr) storeLevel {
return storeLevel{gt: g, bg: bg, fg: fg, items: newBaseItems()}
}