-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
85 lines (63 loc) · 1.61 KB
/
player.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
package main
type Player struct {
name string
Place *Place
hasInventory bool
inventory []*GameItem
}
func createPlayer(w *World) *Player {
p := &Player{}
return p
}
func (p *Player) addToInventory(item *GameItem) string {
if p.hasInventory {
p.inventory = append(p.inventory, item)
p.Place.removeGameItem(item)
return "предмет добавлен в инвентарь: " + item.getName()
}
return "некуда класть"
}
func (p *Player) wearGameItem(item *GameItem) string {
p.inventory = append(p.inventory, item)
for _, action := range item.actions {
if i, ok := action.(WearGameItemAction); ok {
return i.Execute(item, p)
}
}
return "нечего надеть"
}
func (p *Player) takeGameItem(item *GameItem) string {
p.inventory = append(p.inventory, item)
for _, action := range item.actions {
if i, ok := action.(TakeGameItemAction); ok {
return i.Execute(item, p)
}
}
return "нельзя взять"
}
func (p *Player) GetItemNames() []string {
gameItemNames := make([]string, len(p.inventory))
for i, gameItem := range p.inventory {
gameItemNames[i] = gameItem.getName()
}
return gameItemNames
}
func (p *Player) getItemByName(name string) (*GameItem, bool) {
if (!p.hasInventory) {
return nil, false
}
for _, gameItem := range p.inventory {
if gameItem.getName() == name {
return gameItem, true
}
}
return nil, false
}
func (p *Player) applyGameItem(item *GameItem) string {
for _, action := range item.actions {
if i, ok := action.(ApplyGameItemAction); ok {
return i.Execute(item, p)
}
}
return "нечего применить"
}