items
This commit is contained in:
@ -9,6 +9,6 @@ type Ammo struct {
|
||||
itemprops.DamageProfile
|
||||
}
|
||||
|
||||
func (a *Ammo) Type() string {
|
||||
func (a Ammo) Type() string {
|
||||
return ecs.AmmoComponent
|
||||
}
|
@ -6,6 +6,6 @@ type Armor struct {
|
||||
DefenceProfile struct{}
|
||||
}
|
||||
|
||||
func (a *Armor) Type() string {
|
||||
func (a Armor) Type() string {
|
||||
return ecs.ArmorComponent
|
||||
}
|
||||
|
33
engine/items/backpack.go
Normal file
33
engine/items/backpack.go
Normal file
@ -0,0 +1,33 @@
|
||||
package items
|
||||
|
||||
import "lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
|
||||
type Backpack struct {
|
||||
MaxNumber int
|
||||
MaxBulk int
|
||||
MaxMass int
|
||||
items []ecs.Entity
|
||||
}
|
||||
|
||||
func (b Backpack) Type() string {
|
||||
return ecs.BackpackComponent
|
||||
}
|
||||
|
||||
func (b *Backpack) HasFreeSpace(Bulk, Mass int) bool {
|
||||
totalBulk, totalMass := 0, 0
|
||||
for i, _ := range b.items {
|
||||
tmp := Controller.GetComponent(b.items[i], Carried{}.Type()).(Carried)
|
||||
carried := tmp
|
||||
totalBulk += carried.Bulk
|
||||
totalMass += carried.Mass
|
||||
}
|
||||
if totalMass >= b.MaxMass {
|
||||
//fixme return message along - 'too heavy'
|
||||
return false
|
||||
}
|
||||
if totalBulk >= b.MaxMass {
|
||||
//fixme return message along - 'doesnt fit to your backpack'
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
package items
|
||||
|
||||
import "lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
import (
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
"lab.zaar.be/thefish/alchemyst-go/engine/types"
|
||||
)
|
||||
|
||||
type CarriedFace interface {
|
||||
Drop()
|
||||
@ -12,16 +15,74 @@ type Carried struct {
|
||||
Bulk int //внешний размер, см3
|
||||
}
|
||||
|
||||
func (c *Carried) Type() string {
|
||||
func (c Carried) Type() string {
|
||||
return ecs.CarriedComponent
|
||||
}
|
||||
|
||||
func (c *Carried) Pickup() {}
|
||||
func (c *Carried) Drop() {}
|
||||
func (c Carried) Pickup(who, what ecs.Entity) {
|
||||
// check if im lying on ground
|
||||
if !Controller.HasComponent(what, ecs.CoordsComponent) {
|
||||
return
|
||||
}
|
||||
// something inexistent on map trying to pickup an item?!
|
||||
if !Controller.HasComponent(who, ecs.CoordsComponent) {
|
||||
//todo log error - investigate this situation
|
||||
return
|
||||
}
|
||||
//check if who and what are on the same tile
|
||||
whoCoords := Controller.GetComponent(who, ecs.CoordsComponent).(types.Coords)
|
||||
whatCoords := Controller.GetComponent(what, ecs.CoordsComponent).(types.Coords)
|
||||
if whoCoords != whatCoords {
|
||||
//todo log error - something strange happened
|
||||
return
|
||||
}
|
||||
//does not have inventory?
|
||||
if !Controller.HasComponent(who, ecs.BackpackComponent) {
|
||||
//todo send message - you cant carry items
|
||||
return
|
||||
}
|
||||
bp := Controller.GetComponent(who, Backpack{}.Type()).(Backpack)
|
||||
if !bp.HasFreeSpace(c.Bulk, c.Mass) {
|
||||
//todo send message - does not fit to your inventory
|
||||
return
|
||||
}
|
||||
//do not remove appearance
|
||||
//remove coords instead (does not exist on map anymore)
|
||||
Controller.RemoveComponent(what, ecs.CoordsComponent)
|
||||
bp.items = append(bp.items, what)
|
||||
}
|
||||
|
||||
func (c *Carried) GetMass() int {
|
||||
func (c Carried) Drop(who, what ecs.Entity) {
|
||||
var coords types.Coords
|
||||
// something inexistent on map trying to drop an item?!
|
||||
if !Controller.HasComponent(who, ecs.CoordsComponent) {
|
||||
//todo log error - investigate this situation
|
||||
return
|
||||
} else {
|
||||
coords = Controller.GetComponent(who, types.Coords{}.Type()).(types.Coords)
|
||||
}
|
||||
//does not have inventory?
|
||||
if !Controller.HasComponent(who, ecs.BackpackComponent) {
|
||||
//todo send message - you cant carry items
|
||||
return
|
||||
}
|
||||
|
||||
bp := Controller.GetComponent(who, Backpack{}.Type()).(Backpack)
|
||||
var dropped ecs.Entity
|
||||
bp.items, dropped = ecs.DeleteFromEntitySlice(bp.items, what)
|
||||
if dropped != what {
|
||||
//todo log error - something strange happened
|
||||
return
|
||||
}
|
||||
//Give dropped back coordinated
|
||||
Controller.AddComponent(dropped, coords)
|
||||
//Controller.UpdateComponent(what, coords.Type(), coords) //even we need that?
|
||||
Controller.UpdateComponent(who, bp.Type(), bp)
|
||||
}
|
||||
|
||||
func (c Carried) GetMass(what ecs.Entity) int {
|
||||
return c.Mass
|
||||
}
|
||||
func (c *Carried) GetBulk() int {
|
||||
func (c *Carried) GetBulk(what ecs.Entity) int {
|
||||
return c.Bulk
|
||||
}
|
9
engine/items/common.go
Normal file
9
engine/items/common.go
Normal file
@ -0,0 +1,9 @@
|
||||
package items
|
||||
|
||||
import "lab.zaar.be/thefish/alchemyst-go/engine/ecs"
|
||||
|
||||
var Controller ecs.Controller
|
||||
|
||||
func Init(ctrl ecs.Controller) {
|
||||
Controller = ctrl
|
||||
}
|
@ -6,6 +6,6 @@ type Ranged struct {
|
||||
RangeProfile struct{} //это зависимость дальности-скорости от характеристик и атрибутов
|
||||
}
|
||||
|
||||
func (r *Ranged) Type() string {
|
||||
func (r Ranged) Type() string {
|
||||
return ecs.RangedComponent
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ type UsableFace interface {
|
||||
type Usable struct {}
|
||||
|
||||
|
||||
func (u *Usable) Type() string {
|
||||
func (u Usable) Type() string {
|
||||
return ecs.UsableComponent
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,6 @@ type Wearable struct {
|
||||
Bodypart int //куда собстно одевать
|
||||
}
|
||||
|
||||
func (w *Wearable) Type() string {
|
||||
func (w Wearable) Type() string {
|
||||
return ecs.WearableComponent
|
||||
}
|
||||
|
Reference in New Issue
Block a user