/
world.go
201 lines (172 loc) · 4.37 KB
/
world.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package world
import (
"fmt"
"github.com/faiface/pixel"
"github.com/faiface/pixel/text"
log "github.com/sirupsen/logrus"
"golang.org/x/image/font/basicfont"
"math"
"reflect"
)
type World struct {
Time, Gold float64
Items, KeyItems []ItemIndex
//Party check world_extended.go
Icons Icons
}
type ItemIndex struct {
Id, Count int
}
func Create() *World {
w := &World{
Time: 0,
Gold: 0,
Items: make([]ItemIndex, 0),
KeyItems: make([]ItemIndex, 0),
Icons: IconsDB,
}
//temp user items in inventory
//w.Items = append(w.Items, ItemIndex{Id: 1, Count: 2})
//w.Items = append(w.Items, ItemIndex{Id: 2, Count: 1})
//w.Items = append(w.Items, ItemIndex{Id: 3, Count: 1})
//w.KeyItems = append(w.KeyItems, ItemIndex{Id: 4, Count: 1})
return w
}
func (w *World) AddItem(itemId, count int) {
if _, ok := ItemsDB[itemId]; !ok {
log.Fatal(fmt.Sprintf("Item ID {%v} does not exists in DB", itemId))
}
for i := range w.Items {
//Does it already exist in World
if w.Items[i].Id == itemId {
w.Items[i].Count += count
return
}
}
//Add new
w.Items = append(w.Items, ItemIndex{
Id: itemId,
Count: count,
})
}
func (w *World) RemoveItem(itemId, count int) {
if _, ok := ItemsDB[itemId]; !ok {
log.Fatal(fmt.Sprintf("Item ID {%v} does not exists in DB", itemId))
}
for i := len(w.Items) - 1; i >= 0; i-- {
//Does it already exist in World
if w.Items[i].Id == itemId {
w.Items[i].Count -= count
}
if w.Items[i].Count <= 0 {
w.removeItemFromArray(i)
return
}
}
}
func (w *World) removeItemFromArray(index int) {
if len(w.Items) == 1 {
w.Items = make([]ItemIndex, 0)
return
}
w.Items[index], w.Items[0] = w.Items[0], w.Items[index]
w.Items = w.Items[1:]
}
func (w World) hasKeyItem(itemId int) bool {
for _, v := range w.KeyItems {
if v.Id == itemId {
return true
}
}
return false
}
func (w *World) AddKeyItem(itemId int) {
if w.hasKeyItem(itemId) {
//if already exists we dont add again
return
}
w.KeyItems = append(w.KeyItems, ItemIndex{Id: itemId, Count: 1})
}
func (w *World) RemoveKeyItem(itemId int) {
if !w.hasKeyItem(itemId) {
return
}
w.removeKeyItemFromArray(itemId)
}
func (w *World) removeKeyItemFromArray(index int) {
if len(w.KeyItems) == 1 {
w.Items = make([]ItemIndex, 0)
return
}
w.KeyItems[index], w.KeyItems[0] = w.KeyItems[0], w.KeyItems[index]
w.KeyItems = w.KeyItems[1 : len(w.KeyItems)-1]
}
func (w *World) Update(dt float64) {
w.Time = w.Time + dt
}
func (w World) TimeAsString() string {
time := w.Time
hours := math.Floor(time / 3600)
minutes := math.Ceil(math.Mod(time, 3600)/60) - 1
seconds := int(time) % 60
return fmt.Sprintf("%v:%v:%v", hours, minutes, seconds)
}
func (w World) GoldAsString() string {
return fmt.Sprintf("%v", w.Gold)
}
func (w World) GetItemsAsStrings() []string {
var items []string
for _, item := range w.Items {
items = append(items, fmt.Sprintf("%s, (%v)", ItemsDB[item.Id].Name, item.Count))
}
return items
}
func (w World) GetKeyItemsAsStrings() []string {
var items []string
for _, item := range w.KeyItems {
items = append(items, fmt.Sprintf("%s, (%v)", ItemsDB[item.Id].Name, item.Count))
}
return items
}
func (w World) DrawItem(a ...interface{}) {
//renderer pixel.Target, x, y float64, itemIdx ItemIndex
rendererV := reflect.ValueOf(a[0])
renderer := rendererV.Interface().(pixel.Target)
xV := reflect.ValueOf(a[1])
x := xV.Interface().(float64)
yV := reflect.ValueOf(a[2])
y := yV.Interface().(float64)
itemIdxV := reflect.ValueOf(a[3])
itemIdx := itemIdxV.Interface().(ItemIndex)
itemDef := ItemsDB[itemIdx.Id]
iconsSize := 16.0
basicAtlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
textPos := pixel.V(x+iconsSize, y)
textBase := text.New(textPos, basicAtlas)
fmt.Fprintln(textBase, fmt.Sprintf("%-6s (%v)", itemDef.Name, itemIdx.Count))
textBase.Draw(renderer, pixel.IM)
iconPos := pixel.V(x+5, y+(iconsSize/2))
iconSprite := w.Icons.Get(itemDef.Icon)
iconSprite.Draw(renderer, pixel.IM.Moved(iconPos))
}
func (w *World) HasKey(id int) bool {
for _, v := range w.KeyItems {
if v.Id == id {
return true
}
}
return false
}
func (w *World) Get(idx ItemIndex) Item {
return ItemsDB[idx.Id]
}
func (w *World) FilterItems(predicate ItemType) []ItemIndex {
list := make([]ItemIndex, 0)
for _, v := range w.Items {
item := ItemsDB[v.Id]
if item.ItemType == predicate {
list = append(list, v)
}
}
return list
}