pick up in adjacent tiles, fix bulk check

This commit is contained in:
2020-09-24 23:47:36 +03:00
parent bf13c9c7a2
commit d2b22f4760
10 changed files with 53 additions and 31 deletions

View File

@ -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 {

View File

@ -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,13 @@ func (c *Carried) GetBulk(what ecs.Entity) int {
}
func FindCarriedUnder(who ecs.Entity) []ecs.Entity {
coords := Controller.GetComponent(who, ecs.CoordsComponent).(types.Coords)
pickerCoords := Controller.GetComponent(who, ecs.CoordsComponent).(types.Coords)
carrieds := Controller.GetEntitiesWithComponent(ecs.CarriedComponent)
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