pick up in adjacent tiles, fix bulk check

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

View File

@ -33,7 +33,7 @@ func init() {
//we can run logic in separate goroutines
//
// go doSometing(State,...)
// go doSomething(State,...)
//
//and there we go like this:
// func doSomething(State main.GameState, args...) {

View File

@ -25,7 +25,7 @@ func TestDelaunay(t *testing.T) {
{types.Coords{10, 10,}, types.Coords{30, 10}},
}
result := delaunay.GetMst(coords, 100, 100)
result := delaunay.GetMst(coords, 100, 100, -1)
for idx, _ := range result {
if result[idx] != expected[idx] {

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

View File

@ -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":

View File

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

View File

@ -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":

1
go.mod
View File

@ -5,5 +5,6 @@ go 1.12
require (
github.com/gammazero/deque v0.0.0-20190521012701-46e4ffb7a622
github.com/rs/zerolog v1.15.0
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc
lab.zaar.be/thefish/bearlibterminal v0.0.0-20191018101635-dd37bbc90d77
)

View File

@ -5,7 +5,7 @@
Почему это важно
---
Про сборку под разные ОС я даже уюеждать не буду - аудитория рогаликов мало того что крохотная, так еще и сильно
Про сборку под разные ОС я даже убеждать не буду - аудитория рогаликов мало того что крохотная, так еще и сильно
сегментирована по осям. Go почти бесплатно дает вам возможность сборки под все мажорные оси, пользуйтесь этим - и
потенциально в разы больше народа ознакомится с вашим творением.