pick up in adjacent tiles, fix bulk check
This commit is contained in:
@ -124,7 +124,7 @@ func (ps *precomputedShade) FindByCoords(c types.Coords) (int, *Cell, error) {
|
||||
|
||||
func (ps *precomputedShade) IsInFov(coords types.Coords) bool {
|
||||
rc := ps.fromLevelCoords(coords)
|
||||
if rc.X == 0 && rc.Y ==0 {return true}
|
||||
if rc.X == 0 && rc.Y ==0 {return true}
|
||||
_, cell, err := ps.FindByCoords(rc)
|
||||
if err != nil {
|
||||
return false
|
||||
|
@ -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,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
|
||||
|
@ -58,6 +58,7 @@ func (ts *GameScreen) Exit() {
|
||||
//remove what we dont need
|
||||
}
|
||||
|
||||
//fixme kry names to action constants!
|
||||
func (ts *GameScreen) HandleInput(input string) {
|
||||
//ts.state.Do(func(){
|
||||
switch input {
|
||||
@ -104,9 +105,18 @@ func (ts *GameScreen) HandleInput(input string) {
|
||||
} else {
|
||||
//call pickup in selected
|
||||
cc := items.Controller.GetComponent(carrieds[0], ecs.CarriedComponent).(items.Carried)
|
||||
items.Carried.Pickup(cc, ts.state.Player, carrieds[0])
|
||||
}
|
||||
err := items.Carried.Pickup(cc, ts.state.Player, carrieds[0])
|
||||
if err != nil {
|
||||
// Message with error
|
||||
//gameLog.Log.Error(err)
|
||||
//@fixme!
|
||||
appctx.Logger(ts.ctx).Warn().Err(err)
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
//log picked up
|
||||
//gameLog.Log.Message(err)
|
||||
break;
|
||||
|
||||
case "i":
|
||||
|
@ -63,6 +63,8 @@ func (is *InventoryScreen) Enter() {
|
||||
is.prepared.Prepare(is)
|
||||
}
|
||||
|
||||
//fixme key names to action constants!
|
||||
//fixme unify scrolling controls!
|
||||
func (is *InventoryScreen) HandleInput(input string) {
|
||||
if strings.Contains(string(runeIndex), strings.Replace(input, "Shift+", "", -1)) {
|
||||
if strings.Contains("Shift+", input) {
|
||||
@ -76,7 +78,7 @@ func (is *InventoryScreen) HandleInput(input string) {
|
||||
return
|
||||
}
|
||||
switch input {
|
||||
case "Up":
|
||||
case "Up", "k":
|
||||
is.cursor = is.cursor - 1
|
||||
if is.cursor < 0 {
|
||||
is.cursor = 0
|
||||
@ -88,7 +90,7 @@ func (is *InventoryScreen) HandleInput(input string) {
|
||||
}
|
||||
}
|
||||
break
|
||||
case "Down":
|
||||
case "Down", "j":
|
||||
is.cursor = is.cursor + 1
|
||||
if is.cursor >= len(is.prepared) {
|
||||
is.cursor = len(is.prepared) - 1
|
||||
|
@ -19,6 +19,8 @@ func (ts *TitleScreen) UseEcs() bool { return false }
|
||||
func (ts *TitleScreen) Enter() {
|
||||
blt.Clear()
|
||||
}
|
||||
|
||||
//fixme key names to action constants!
|
||||
func (ts *TitleScreen) HandleInput(input string) {
|
||||
switch input {
|
||||
case "n":
|
||||
|
Reference in New Issue
Block a user