-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathitems.go
243 lines (213 loc) · 4.07 KB
/
items.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package rpg
import (
"fmt"
"log"
"math/rand"
"strconv"
"strings"
"time"
"github.com/aerth/rpc/librpg/common"
"github.com/faiface/pixel"
)
type Item struct {
Name string
Type ItemType
Properties ItemProperties
Effect func(Stats) Stats
Quantity uint64
}
type ItemProperties struct {
Weight uint8
Size pixel.Vec
}
func DefaultItemProperties() ItemProperties {
return ItemProperties{}
}
func (i Item) String() (s string) {
if i.Quantity > 1 {
s += strconv.FormatInt(int64(i.Quantity), 10)
s += " "
}
if i.Name != "" {
s += i.Name
if i.Effect != nil {
switch i.Type {
case ARMOR:
s += " (+defence)"
case POTION:
s += " (+magic)"
case WEAPON:
s += " (+attack)"
}
}
return s
}
s += i.Type.String()
return s
}
type ItemType int
const (
_ ItemType = iota
GOLD
POTION
FOOD
WEAPON
ARMOR
SPECIAL
)
func MakeGold(amount uint64) Item {
return Item{
Name: "gold",
Type: GOLD,
Quantity: amount,
}
}
func createLoot() Item {
item := Item{
Type: ItemType(rand.Intn(5)) + 1,
Quantity: 1,
}
return item
}
func createItemLoot() Item {
item := createLoot()
if item.Type == GOLD || item.Type == FOOD {
return createItemLoot()
}
return item
}
// just stack gold potions and food for now
func (i Item) Stack(items []Item) []Item {
var stacked []Item
var foods, potions, goldlvl uint64
//log.Println(items)
//log.Println("Totalnum:", len(items))
for _, item := range items {
//log.Println(item)
switch item.Type {
case FOOD:
foods += item.Quantity
case POTION:
potions += item.Quantity
case GOLD:
//log.Printf("adding %v and %v", goldlvl, item.Quantity)
goldlvl += item.Quantity
default:
//log.Printf("stacking item %s", item)
stacked = append(stacked, item)
//log.Printf("items are now %v", len(stacked))
}
}
switch i.Type {
default:
stacked = append(stacked, i)
case GOLD:
goldlvl += i.Quantity
case POTION:
potions += i.Quantity
case FOOD:
foods += i.Quantity
}
if potions > 0 {
stacked = append(stacked, Item{Type: POTION, Quantity: potions})
}
if foods > 0 {
stacked = append(stacked, Item{Type: FOOD, Quantity: foods})
}
if goldlvl > 0 {
stacked = append(stacked, MakeGold(goldlvl))
}
return stacked
}
func StackItems(itemsets ...[]Item) []Item {
var backpack []Item
for _, inventory := range itemsets {
for _, item := range inventory {
backpack = item.Stack(backpack)
}
}
return backpack
}
func FormatItemList(items []Item) string {
if len(items) == 0 {
return "none"
}
var s string
for i, item := range items {
if i%4 == 0 {
s += "\n"
}
s += item.String() + ", "
}
return strings.TrimSuffix(s, ", ")
}
func RandomLoot() []Item {
switch rand.Intn(10) {
default: // 0
return []Item{} // no loot
case 1, 2, 3, 4:
return []Item{MakeGold(uint64(rand.Intn(255) + 1))}
case 5, 6:
return []Item{createLoot()}
// case 8:
case 7, 8, 9:
return []Item{RandomMagicItem()}
}
}
func RandomMagicItem() Item {
// special item
item := createLoot()
switch item.Type {
case GOLD, FOOD, POTION:
default:
item.Name = fmt.Sprintf(GenerateItemName(), item.Type.String())
item.Effect = RandomItemEffect(item.Type)
}
return item
}
func RandomItemEffect(t ItemType) func(Stats) Stats {
fn := func(s Stats) Stats {
return s
}
switch t {
case ARMOR:
fn = func(s Stats) Stats {
s.Vitality += 0.1
return s
}
case WEAPON:
fn = func(s Stats) Stats {
s.Strength += 0.1
return s
}
case POTION:
fn = func(s Stats) Stats {
s.Intelligence += 0.1
return s
}
default:
fn = func(s Stats) Stats {
return s
}
}
return fn
}
func (w *World) NewLoot(location pixel.Vec, items []Item) {
if len(items) < 1 {
log.Println("not enough items to drop loot")
return
}
if location == pixel.ZV {
log.Println("warning: creating loot at 0,0 coordinates")
}
loot := &DObject{
Contains: items,
Type: D_LOOT,
Until: time.Now().Add(5 * time.Minute),
Object: common.Object{
Loc: location,
Rect: common.DefaultSpriteRectangle.Moved(location),
},
}
w.DObjects = append(w.DObjects, loot)
}