last_light/game/item/inventory.go

96 lines
1.7 KiB
Go
Raw Normal View History

2024-05-21 23:08:51 +03:00
package item
2024-05-03 13:46:32 +03:00
2024-05-21 23:08:51 +03:00
import (
"mvvasilev/last_light/engine"
)
2024-05-03 13:46:32 +03:00
2024-05-06 20:43:35 +03:00
type Inventory interface {
2024-05-21 23:08:51 +03:00
Items() []Item
2024-05-06 21:19:08 +03:00
Shape() engine.Size
2024-05-06 20:43:35 +03:00
Push(item Item) bool
2024-05-21 23:08:51 +03:00
Drop(x, y int) Item
ItemAt(x, y int) Item
2024-05-06 20:43:35 +03:00
}
type BasicInventory struct {
2024-05-21 23:08:51 +03:00
contents []Item
2024-05-06 21:19:08 +03:00
shape engine.Size
2024-05-03 13:46:32 +03:00
}
2024-05-06 21:19:08 +03:00
func CreateInventory(shape engine.Size) *BasicInventory {
2024-05-06 20:43:35 +03:00
inv := new(BasicInventory)
2024-05-03 13:46:32 +03:00
2024-05-21 23:08:51 +03:00
inv.contents = make([]Item, 0, shape.Height()*shape.Width())
2024-05-03 13:46:32 +03:00
inv.shape = shape
return inv
}
2024-05-21 23:08:51 +03:00
func (i *BasicInventory) Items() (items []Item) {
2024-05-03 13:46:32 +03:00
return i.contents
}
2024-05-06 21:19:08 +03:00
func (i *BasicInventory) Shape() engine.Size {
2024-05-03 13:46:32 +03:00
return i.shape
}
2024-05-21 23:08:51 +03:00
func (inv *BasicInventory) Push(i Item) (success bool) {
if len(inv.contents) == inv.shape.Area() {
2024-05-03 13:46:32 +03:00
return false
}
2024-05-21 23:08:51 +03:00
itemType := i.Type()
2024-05-03 13:46:32 +03:00
// Try to first find a matching item with capacity
2024-05-21 23:08:51 +03:00
for index, existingItem := range inv.contents {
2024-05-30 23:39:54 +03:00
if existingItem != nil && existingItem.Type().Id() == itemType.Id() {
2024-05-03 13:46:32 +03:00
if existingItem.Quantity()+1 > existingItem.Type().MaxStack() {
continue
}
2024-05-21 23:08:51 +03:00
it := CreateBasicItem(itemType, existingItem.Quantity()+1)
inv.contents[index] = &it
2024-05-03 13:46:32 +03:00
return true
}
}
// Next, try to find an intermediate empty slot to fit this item into
2024-05-21 23:08:51 +03:00
for index, existingItem := range inv.contents {
2024-05-03 13:46:32 +03:00
if existingItem == nil {
2024-05-21 23:08:51 +03:00
inv.contents[index] = i
2024-05-03 13:46:32 +03:00
return true
}
}
// Finally, just append the new item at the end
2024-05-21 23:08:51 +03:00
inv.contents = append(inv.contents, i)
2024-05-03 13:46:32 +03:00
return true
}
2024-05-21 23:08:51 +03:00
func (i *BasicInventory) Drop(x, y int) Item {
2024-05-03 13:46:32 +03:00
index := y*i.shape.Width() + x
if index > len(i.contents)-1 {
2024-05-06 20:43:35 +03:00
return nil
2024-05-03 13:46:32 +03:00
}
2024-05-06 20:43:35 +03:00
item := i.contents[index]
2024-05-03 13:46:32 +03:00
i.contents[index] = nil
2024-05-06 20:43:35 +03:00
return item
2024-05-03 13:46:32 +03:00
}
2024-05-21 23:08:51 +03:00
func (i *BasicInventory) ItemAt(x, y int) (item Item) {
2024-05-03 13:46:32 +03:00
index := y*i.shape.Width() + x
if index > len(i.contents)-1 {
return nil
}
return i.contents[index]
}