Merge remote-tracking branch 'origin/master'
# Conflicts: # cmd/game/main.go # delaunay_test.go # engine/screens/devmenu.go # go.mod
This commit is contained in:
@ -1,7 +1,13 @@
|
||||
package items
|
||||
|
||||
import "lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
)
|
||||
var (
|
||||
ErrorInvTooHeavy = fmt.Errorf("too heavy")
|
||||
ErrorInvTooBulky = fmt.Errorf("too bulky")
|
||||
)
|
||||
type Backpack struct {
|
||||
MaxNumber int
|
||||
MaxBulk int
|
||||
@ -13,7 +19,7 @@ func (b Backpack) Type() string {
|
||||
return ecs.BackpackComponent
|
||||
}
|
||||
|
||||
func (b *Backpack) HasFreeSpace(Bulk, Mass int) bool {
|
||||
func (b *Backpack) HasFreeSpace(Bulk, Mass int) error {
|
||||
totalBulk, totalMass := 0, 0
|
||||
for i, _ := range b.items {
|
||||
tmp := Controller.GetComponent(b.items[i], Carried{}.Type()).(Carried)
|
||||
@ -22,14 +28,12 @@ func (b *Backpack) HasFreeSpace(Bulk, Mass int) bool {
|
||||
totalMass += carried.Mass
|
||||
}
|
||||
if totalMass >= b.MaxMass {
|
||||
//fixme return message along - 'too heavy'
|
||||
return false
|
||||
return ErrorInvTooHeavy
|
||||
}
|
||||
if totalBulk >= b.MaxMass {
|
||||
//fixme return message along - 'doesnt fit to your backpack'
|
||||
return false
|
||||
if totalBulk >= b.MaxBulk {
|
||||
return ErrorInvTooBulky
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Backpack) GetItems() []ecs.Entity {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package items
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/types"
|
||||
)
|
||||
@ -19,32 +20,33 @@ func (c Carried) Type() string {
|
||||
return ecs.CarriedComponent
|
||||
}
|
||||
|
||||
func (c Carried) Pickup(who, what ecs.Entity) {
|
||||
func (c Carried) Pickup(who, what ecs.Entity) error {
|
||||
// check if im lying on ground
|
||||
if !Controller.HasComponent(what, ecs.CoordsComponent) {
|
||||
return
|
||||
return fmt.Errorf("bug! item with no coords?!")
|
||||
}
|
||||
// something inexistent on map trying to pickup an item?!
|
||||
if !Controller.HasComponent(who, ecs.CoordsComponent) {
|
||||
//todo log error - investigate this situation
|
||||
return
|
||||
return fmt.Errorf("bug! actor with no coords?!")
|
||||
}
|
||||
//check if who and what are on the same tile
|
||||
//check if who and what are in adjacent tiles
|
||||
whoCoords := Controller.GetComponent(who, ecs.CoordsComponent).(types.Coords)
|
||||
whatCoords := Controller.GetComponent(what, ecs.CoordsComponent).(types.Coords)
|
||||
if whoCoords != whatCoords {
|
||||
if !whoCoords.IsAdjacentTo(&whatCoords) {
|
||||
//todo log error - something strange happened
|
||||
return
|
||||
return fmt.Errorf("bug! actor and item in inadjacent coords?!")
|
||||
}
|
||||
//does not have inventory?
|
||||
if !Controller.HasComponent(who, ecs.BackpackComponent) {
|
||||
//todo send message - you cant carry items
|
||||
return
|
||||
return fmt.Errorf("bug! actor cannot carry items")
|
||||
}
|
||||
bp := Controller.GetComponent(who, Backpack{}.Type()).(Backpack)
|
||||
if !bp.HasFreeSpace(c.Bulk, c.Mass) {
|
||||
err := bp.HasFreeSpace(c.Bulk, c.Mass)
|
||||
if err != nil {
|
||||
//todo send message - does not fit to your inventory
|
||||
return
|
||||
return err
|
||||
}
|
||||
//do not remove appearance
|
||||
//remove coords instead (does not exist on map anymore)
|
||||
@ -52,6 +54,7 @@ func (c Carried) Pickup(who, what ecs.Entity) {
|
||||
bp.items = append(bp.items, what)
|
||||
//fuck that, we need to update constantly
|
||||
Controller.UpdateComponent(who, ecs.BackpackComponent, bp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Carried) Drop(who, what ecs.Entity) {
|
||||
@ -90,13 +93,14 @@ func (c *Carried) GetBulk(what ecs.Entity) int {
|
||||
}
|
||||
|
||||
func FindCarriedUnder(who ecs.Entity) []ecs.Entity {
|
||||
coords := Controller.GetComponent(who, ecs.CoordsComponent).(types.Coords)
|
||||
carrieds := Controller.GetEntitiesWithComponent(ecs.CarriedComponent)
|
||||
pickerCoords := Controller.GetComponent(who, ecs.CoordsComponent).(types.Coords)
|
||||
// _И_ носимые _И_ имеющие координаты, т.е. где-то лежащие
|
||||
carrieds := Controller.GetEntitiesWithComponent(ecs.CarriedComponent, ecs.CoordsComponent)
|
||||
result := make([]ecs.Entity, 0)
|
||||
for _, ent := range carrieds {
|
||||
car := Controller.GetComponent(ent, ecs.CoordsComponent)
|
||||
if car == coords {
|
||||
result = append(result, ent)
|
||||
for _, carried := range carrieds {
|
||||
carriedCoords := Controller.GetComponent(carried, ecs.CoordsComponent).(types.Coords)
|
||||
if pickerCoords.IsAdjacentTo(&carriedCoords) {
|
||||
result = append(result, carried)
|
||||
}
|
||||
}
|
||||
return result
|
||||
|
Reference in New Issue
Block a user