-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_inventory.go
63 lines (45 loc) · 1.13 KB
/
obj_inventory.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
package rogue
import "fmt"
// returns index in inventory of something edible
func (o *Obj) tryFindItemTypeInInventory(t string) (int, error) {
if len(o.Inventory) == 0 {
return -1, fmt.Errorf("Inventory is empty")
}
for idx, it := range o.Inventory {
if it.Type == t {
return idx, nil
}
}
return -1, fmt.Errorf("%s has no items of %s in inventory", o.Name, t)
}
func (o *Obj) addItemToInventory(item *Obj) {
o.Inventory = append(o.Inventory, item)
}
func (o *Obj) addToInventory(itemName string) {
spec := o.Island.getNpcSpecFromName(itemName)
i := o.Island.getNpcFromSpec(spec)
o.Inventory = append(o.Inventory, i)
}
func (o *Obj) removeFromInventory(index int) *Obj {
i := o.Inventory[index]
o.Inventory = append(o.Inventory[:index], o.Inventory[index+1:]...)
return i
}
func (o *Obj) hasItemTypeInInventory(t string) bool {
_, err := o.tryFindItemTypeInInventory(t)
if err != nil {
return false
}
return true
}
func (o *Obj) hasItemInInventory(itemName string) bool {
if len(o.Inventory) == 0 {
return false
}
for _, it := range o.Inventory {
if it.Name == itemName {
return true
}
}
return false
}