forked from gophergala2016/gopher_typer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
173 lines (158 loc) · 3.53 KB
/
item.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package gopherTyper
import (
"fmt"
"math/rand"
"time"
)
type item interface {
Name() string
Desc() string
Price() int
PriceDesc() string
SetID(int)
Reset(gt *GopherTyper)
Purchase(g *storeLevel) bool
Dupe() item
Tick(g *gameLevel)
}
type goroutineItem struct {
wakeAt time.Time
baseWait time.Duration
waitRange time.Duration
currentWord *word
id int
cpuUpgrades int
price int
}
func (i *goroutineItem) Name() string {
return "Goroutine"
}
func (i *goroutineItem) Desc() string {
return "Add a goroutine to help type words for you"
}
func (i *goroutineItem) Price() int {
return i.price
}
func (i *goroutineItem) PriceDesc() string {
return fmt.Sprintf("$%d", i.Price())
}
func (i *goroutineItem) Tick(gl *gameLevel) {
if time.Now().After(i.wakeAt) {
if i.currentWord == nil {
var possibleWords []int
for i, w := range gl.words {
if gl.currentWord != gl.words[i] && !w.Complete() && w.startedBy == 0 {
possibleWords = append(possibleWords, i)
}
}
if len(possibleWords) > 0 {
i.currentWord = gl.words[possibleWords[rand.Intn(len(possibleWords))]]
i.currentWord.completedChars++
i.currentWord.startedBy = i.id
}
} else {
i.currentWord.completedChars++
gl.gt.stats.Garbage++
if i.currentWord.Complete() {
i.currentWord = nil
}
}
i.sleep()
}
}
func (i *goroutineItem) sleep() {
i.wakeAt = time.Now().Add(i.baseWait/time.Duration(i.cpuUpgrades) + time.Duration(rand.Intn(int(i.waitRange))))
}
func (i *goroutineItem) SetID(id int) {
i.id = id
}
func (i *goroutineItem) Reset(gt *GopherTyper) {
i.currentWord = nil
i.cpuUpgrades = gt.stats.CPUUpgrades
i.price = 1000
for _, itm := range gt.items {
if itm.Name() == i.Name() {
i.price *= 2
}
}
}
func (i *goroutineItem) Dupe() item {
var dupe goroutineItem
dupe = *i
return &dupe
}
func (i *goroutineItem) Purchase(l *storeLevel) bool {
return true
}
func newGoroutineItem(waitRange, baseWait time.Duration) *goroutineItem {
item := goroutineItem{waitRange: waitRange, baseWait: baseWait, cpuUpgrades: 1}
item.sleep()
return &item
}
type cpuUpgradeItem struct {
id int
price int
}
func (i *cpuUpgradeItem) Name() string {
return "CPU Upgrade"
}
func (i *cpuUpgradeItem) Desc() string {
return "Make your goroutines go faster"
}
func (i *cpuUpgradeItem) Price() int {
return i.price
}
func (i *cpuUpgradeItem) PriceDesc() string {
return fmt.Sprintf("$%d", i.Price())
}
func (i *cpuUpgradeItem) Tick(gl *gameLevel) {
}
func (i *cpuUpgradeItem) SetID(id int) {
i.id = id
}
func (i *cpuUpgradeItem) Reset(gt *GopherTyper) {
i.price = 2000 * gt.stats.CPUUpgrades
}
func (i *cpuUpgradeItem) Purchase(l *storeLevel) bool {
l.gt.stats.CPUUpgrades++
return false
}
func (i *cpuUpgradeItem) Dupe() item {
var dupe cpuUpgradeItem
dupe = *i
return &dupe
}
type goUpgradeItem struct {
id int
price int
}
func (i *goUpgradeItem) Name() string {
return "Go Upgrade"
}
func (i *goUpgradeItem) Desc() string {
return "Improves garbage collection performance"
}
func (i *goUpgradeItem) Price() int {
return i.price
}
func (i *goUpgradeItem) PriceDesc() string {
return fmt.Sprintf("$%d", i.Price())
}
func (i *goUpgradeItem) Tick(gl *gameLevel) {
}
func (i *goUpgradeItem) SetID(id int) {
i.id = id
}
func (i *goUpgradeItem) Reset(gt *GopherTyper) {
i.price = int(1000 * gt.stats.GoVersion)
}
func (i *goUpgradeItem) Purchase(l *storeLevel) bool {
l.gt.stats.GoVersion += 0.1
l.gt.stats.GarbageFreq += 3
return false
}
func (i *goUpgradeItem) Dupe() item {
var dupe goUpgradeItem
dupe = *i
return &dupe
}