spacetea/simulator/converter.go

106 lines
2.0 KiB
Go
Raw Normal View History

2022-05-19 17:09:46 -04:00
package simulator
import (
2022-05-25 16:45:23 -04:00
"github.com/BurntSushi/toml"
2022-05-19 17:09:46 -04:00
)
//Converter is a object that converts one item into another per tick
type Converter struct {
2022-05-25 16:45:23 -04:00
Id itemType `toml:"itemid"`
Name string `toml:"name"`
DisplayName string `toml:"displayName"`
Icon string `toml:"icon"`
Rate int `toml:"rate"`
source itemType
SourceName string `toml:"source"`
output itemType
OutputName string `toml:"output"`
owner *Player
Costs []CraftCost
}
type converters struct {
Converter []Converter
}
//ID returns id
func (c Converter) ID() itemType {
return c.Id
2022-05-19 17:09:46 -04:00
}
func newConverter(k itemType, o *Player) *Converter {
2022-05-25 16:45:23 -04:00
var res Converter
if template, ok := GlobalItems[k]; ok {
temp := template.(Converter)
res.DisplayName = temp.DisplayName
res.Name = temp.Name
res.Icon = temp.Icon
res.source = lookupByName(temp.SourceName).ID()
res.output = lookupByName(temp.OutputName).ID()
res.Rate = temp.Rate
res.Costs = temp.Costs
res.owner = o
return &res
2022-05-19 17:09:46 -04:00
}
2022-05-25 16:45:23 -04:00
return &Converter{}
2022-05-19 17:09:46 -04:00
}
//Tick one iteration
func (c *Converter) Tick() {
2022-05-25 16:45:23 -04:00
if c.source == 0 {
c.owner.AddItem(c.output, 1)
2022-05-25 16:45:23 -04:00
} else if c.owner.Resources[c.source] > c.Rate {
c.owner.DelItem(c.source, c.Rate)
c.owner.AddItem(c.output, 1)
2022-05-19 17:09:46 -04:00
}
}
2022-05-25 16:45:23 -04:00
func (c Converter) String() string {
return c.Name
}
func (c Converter) Render() string {
return c.Icon
2022-05-19 17:09:46 -04:00
}
//Describe returns human useful string
2022-05-25 16:45:23 -04:00
func (c Converter) Describe() string {
return c.DisplayName
2022-05-19 17:09:46 -04:00
}
//Type returns consumer
2022-05-25 16:45:23 -04:00
func (c Converter) Type() ObjectType {
2022-05-19 17:09:46 -04:00
return consumerObject
}
2022-05-25 16:45:23 -04:00
func loadConverters(filename string) {
var res converters
_, err := toml.DecodeFile(filename, &res)
if err != nil {
panic(err)
}
for _, v := range res.Converter {
newItem(v.ID(), v)
}
2022-05-19 17:09:46 -04:00
}
type converterEntry struct {
id itemType
rate int
name string
source itemType
output itemType
}
func (c converterEntry) Name() string {
return c.name
}
func (c converterEntry) Render() string {
return "m"
}
func (c converterEntry) ID() string {
return c.id.String()
}