-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
49 lines (42 loc) · 990 Bytes
/
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
package main
import "fmt"
type inventory struct {
*baseComponent
Capacity int
Items []*item
}
func newInventory(capacity int) *inventory {
return &inventory{
baseComponent: &baseComponent{
Parent: nil,
},
Capacity: capacity,
Items: []*item{},
}
}
func (c *inventory) GetEntities() []entity {
entities := make([]entity, 0, len(c.Items))
for _, i := range c.Items {
entities = append(entities, i)
}
return entities
}
func (c *inventory) SetEntities(entities []entity) {
items := make([]*item, 0, len(entities))
for _, e := range entities {
if i, ok := e.(*item); ok {
items = append(items, i)
}
}
c.Items = items
}
func (c *inventory) Drop(drop *item) {
for i, it := range c.Items {
if it == drop {
c.Items = append(c.Items[:i], c.Items[i+1:]...)
break
}
}
drop.Place(c.Parent.Entity().X, c.Parent.Entity().Y, c.GameMap())
c.Engine().MessageLog.AddMessage(fmt.Sprintf("You dropped the %s.", drop.Entity().Name), ColorWhite, true)
}