clean up inventory, add pickup and destroy

This commit is contained in:
stryan 2022-05-25 17:42:05 -04:00
parent b49afd9ffb
commit afc589380b
6 changed files with 61 additions and 12 deletions

View File

@ -4,7 +4,7 @@ name = "teaConverter"
displayName = "Tea Pulper" displayName = "Tea Pulper"
source = "tea" source = "tea"
output = "brick" output = "brick"
rate = 5 rate = 4
icon = "m" icon = "m"
[[converter.costs]] [[converter.costs]]
name = "tea" name = "tea"
@ -18,3 +18,6 @@ source = ""
output = "tea" output = "tea"
rate = 0 rate = 0
icon = "w" icon = "w"
[[converter.costs]]
name = "brick"
value = 3

View File

@ -110,6 +110,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
} }
return newMenuModel(res, placeMenu), nil return newMenuModel(res, placeMenu), nil
case ",":
m.s.Input("pickup")
case "x":
m.s.Input("destroy")
case "c": case "c":
var res []sim.ItemEntry var res []sim.ItemEntry
for k := range m.s.Player.Craftables { for k := range m.s.Player.Craftables {

View File

@ -48,10 +48,10 @@ func newConverter(k itemType, o *Player) *Converter {
//Tick one iteration //Tick one iteration
func (c *Converter) Tick() { func (c *Converter) Tick() {
if c.source == 0 { if c.source == 0 {
c.owner.Resources[c.output] = c.owner.Resources[c.output] + 1 c.owner.AddItem(c.output, 1)
} else if c.owner.Resources[c.source] > c.Rate { } else if c.owner.Resources[c.source] > c.Rate {
c.owner.Resources[c.source] = c.owner.Resources[c.source] - c.Rate c.owner.DelItem(c.source, c.Rate)
c.owner.Resources[c.output] = c.owner.Resources[c.output] + 1 c.owner.AddItem(c.output, 1)
} }
} }

View File

@ -9,7 +9,6 @@ import (
//Player is a player controlled mob //Player is a player controlled mob
type Player struct { type Player struct {
Resources map[itemType]int Resources map[itemType]int
Inventory map[itemType]int
Craftables map[itemType]struct{} Craftables map[itemType]struct{}
Techs map[TechID]Tech Techs map[TechID]Tech
CurrentTile *Tile CurrentTile *Tile
@ -22,6 +21,37 @@ func NewPlayer() *Player {
return &Player{Resources: make(map[itemType]int), Techs: make(map[TechID]Tech), Craftables: make(map[itemType]struct{})} return &Player{Resources: make(map[itemType]int), Techs: make(map[TechID]Tech), Craftables: make(map[itemType]struct{})}
} }
//AddItemByName adds the given amount of the item using the item name
func (p *Player) AddItemByName(name string, value int) {
obj := lookupByName(name)
if obj.Type() != emptyObject {
p.AddItem(obj.ID(), value)
}
}
//AddItem adds the given amount of the item
func (p *Player) AddItem(i itemType, value int) {
if v, ok := p.Resources[i]; ok {
p.Resources[i] = v + value
} else {
p.Resources[i] = value
}
}
//DelItem removes the given ammount of the item
func (p *Player) DelItem(i itemType, value int) {
if v, ok := p.Resources[i]; ok {
p.Resources[i] = v - value
}
}
//DelItemByName removes the given ammount of the item using the item name
func (p *Player) DelItemByName(name string, value int) {
obj := lookupByName(name)
if obj.Type() != emptyObject {
p.DelItem(obj.ID(), value)
}
}
func (p *Player) String() string { func (p *Player) String() string {
var res string var res string
res += "Resources: \n" res += "Resources: \n"

View File

@ -38,6 +38,11 @@ func (p *Pod) Place(item item, x, y int) bool {
return false return false
} }
//Delete removes an item from a tile
func (p *Pod) Delete(x, y int) {
p.Tiles[x][y].Building = nil
}
//MovePlayer swaps player tiles //MovePlayer swaps player tiles
func (p *Pod) MovePlayer(x, y, s, t int) *Tile { func (p *Pod) MovePlayer(x, y, s, t int) *Tile {
if oob(x) || oob(y) || oob(s) || oob(t) { if oob(x) || oob(y) || oob(s) || oob(t) {

View File

@ -35,8 +35,8 @@ func NewSimulator() *Simulator {
panic("Loaded items but nothing in global items table") panic("Loaded items but nothing in global items table")
} }
pod.Place(newResource(lookupByName("tea").ID()), 4, 4) pod.Place(newResource(lookupByName("tea").ID()), 4, 4)
player.Resources[itemType(1)] = 30 player.AddItem(itemType(1), 30)
player.Resources[itemType(3)] = 5 player.AddItem(itemType(3), 5)
pod.Tiles[0][0].User = player pod.Tiles[0][0].User = player
player.Announce("Game started") player.Announce("Game started")
return &Simulator{pod, player, 0, 0, 0, make(chan bool)} return &Simulator{pod, player, 0, 0, 0, make(chan bool)}
@ -63,11 +63,18 @@ func (s *Simulator) Input(cmd string) {
build := cur.Building.(*Resource) build := cur.Building.(*Resource)
prod := build.Get() prod := build.Get()
if prod.Kind != 0 && prod.Value > 0 { if prod.Kind != 0 && prod.Value > 0 {
s.Player.Resources[prod.Kind] = s.Player.Resources[prod.Kind] + prod.Value s.Player.AddItem(prod.Kind, prod.Value)
s.Player.Announce(fmt.Sprintf("Gathered %v %v", prod.Value, GlobalItems[prod.Kind].Describe())) s.Player.Announce(fmt.Sprintf("Gathered %v %v", prod.Value, GlobalItems[prod.Kind].Describe()))
} }
} }
} }
case "pickup":
if cur.Building != nil {
s.Player.AddItem(cur.Building.ID(), 1)
s.Place.Delete(s.Px, s.Py)
}
case "destroy":
s.Place.Delete(s.Px, s.Py)
case "place": case "place":
if len(cmdS) < 2 { if len(cmdS) < 2 {
return return
@ -84,14 +91,14 @@ func (s *Simulator) Input(cmd string) {
obj2 := obj.(Converter) obj2 := obj.(Converter)
res := s.Place.Place(newConverter(obj2.ID(), s.Player), s.Px, s.Py) res := s.Place.Place(newConverter(obj2.ID(), s.Player), s.Px, s.Py)
if res { if res {
s.Player.Resources[obj2.ID()] = s.Player.Resources[obj2.ID()] - 1 s.Player.DelItem(obj2.ID(), 1)
} }
case resourceObject: case resourceObject:
obj2 := obj.(Resource) obj2 := obj.(Resource)
if obj2.Buildable { if obj2.Buildable {
res := s.Place.Place(newResource(obj2.ID()), s.Px, s.Py) res := s.Place.Place(newResource(obj2.ID()), s.Px, s.Py)
if res { if res {
s.Player.Resources[obj2.ID()] = s.Player.Resources[obj2.ID()] - 1 s.Player.DelItem(obj2.ID(), 1)
} }
} }
} }
@ -115,9 +122,9 @@ func (s *Simulator) Input(cmd string) {
} }
if i == len(obj2.Costs) { if i == len(obj2.Costs) {
for _, v := range obj2.Costs { for _, v := range obj2.Costs {
s.Player.Resources[lookupByName(v.Name).ID()] = s.Player.Resources[lookupByName(v.Name).ID()] - v.Value s.Player.DelItemByName(v.Name, v.Value)
} }
s.Player.Resources[lookupByName(obj2.String()).ID()] = s.Player.Resources[lookupByName(obj2.String()).ID()] + 1 s.Player.AddItemByName(obj2.String(), 1)
} }
} }