spacetea/simulator/resource.go

95 lines
1.6 KiB
Go

package simulator
import (
"github.com/BurntSushi/toml"
)
//Resource is a game resource; can be planted
type Resource struct {
Id itemType `toml:"itemid"`
Name string `toml:"name"`
DisplayName string `toml:"displayName"`
Flavour string `toml:"flavour"`
Buildable bool `toml:"buildable"`
Rate int `toml:"rate"`
Icon string `toml:"icon"`
value int
growth int
}
type resources struct {
Resource []Resource
}
func newResource(k itemType) *Resource {
var res Resource
if template, ok := GlobalItems[k]; ok {
temp := template.(Resource)
res.DisplayName = temp.DisplayName
res.Flavour = temp.Flavour
res.Id = k
res.Name = temp.Name
res.Buildable = temp.Buildable
res.Rate = temp.Rate
res.Flavour = temp.Flavour
res.Icon = temp.Icon
res.value = 0
res.growth = 0
return &res
}
return &Resource{}
}
func (r *Resource) Tick() {
if !r.Buildable {
return
}
r.growth++
if r.growth > r.Rate {
r.value++
r.growth = 0
}
}
func (r *Resource) Get() Produce {
var pro Produce
pro.Value = r.value
pro.Kind = r.Id
r.value = 0
return pro
}
func (r Resource) String() string {
return r.Name
}
func (r Resource) Render() string {
return r.Icon
}
func (r Resource) Describe() string {
return r.DisplayName
}
func (r Resource) Description() string {
return r.Flavour
}
func loadResources(filename string) {
var res resources
_, err := toml.DecodeFile(filename, &res)
if err != nil {
panic(err)
}
for _, v := range res.Resource {
newItem(v.Id, v)
}
}
func (r Resource) ID() itemType {
return r.Id
}
func (r Resource) Type() ObjectType {
return resourceObject
}