-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.go
82 lines (72 loc) · 1.61 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
package main
import (
"errors"
)
type Item struct {
Name string
Action string
ItemForUse int
Contains []int
}
func FindItemByName(itemName string) (error, int, *Item) {
for index, itm := range Items {
if itm.Name == itemName {
return nil, index, itm
}
}
return errors.New("Item not found"), -1, nil
}
func OpenItem(pla *Character, itemName string) {
loc := LocationMap[player.CurrentLocation]
for _, itm := range loc.Items {
if Items[itm].Name == itemName {
if Items[itm].ItemForUse != 0 && PlayerHasItem(pla, Items[itm].ItemForUse) {
loc.Items = append(loc.Items, Items[itm].Contains...)
Items[itm].Contains = *new([]int)
} else {
Output("red", "Could not open the "+itemName)
return
}
} else {
Output("red", "Could not open the "+itemName)
}
}
}
func PlayerHasItem(pla *Character, itm int) bool {
for _, pitm := range pla.Items {
if pitm == itm {
return true
}
}
return false
}
func (it *Item) RemoveItemFromRoom(loc *Location) {
for index, itm := range loc.Items {
if Items[itm].Name == it.Name {
loc.Items = append(loc.Items[:index], loc.Items[index+1:]...)
}
}
}
func (it *Item) ItemInRoom(loc *Location) bool {
for _, itm := range loc.Items {
if Items[itm].Name == it.Name {
return true
}
}
return false
}
func (it *Item) ItemOnPlayer(pla *Character) bool {
for _, itm := range pla.Items {
if Items[itm].Name == it.Name {
return true
}
}
return false
}
func describeItems(player Character) {
l := LocationMap[player.CurrentLocation]
Output("You see:")
for _, itm := range l.Items {
Outputf("\t%s\n", Items[itm].Name)
}
}