aboutsummaryrefslogtreecommitdiff
path: root/internal/object
diff options
context:
space:
mode:
Diffstat (limited to 'internal/object')
-rw-r--r--internal/object/item.go62
-rw-r--r--internal/object/item_store.go38
-rw-r--r--internal/object/object.go8
-rw-r--r--internal/object/store.go38
4 files changed, 146 insertions, 0 deletions
diff --git a/internal/object/item.go b/internal/object/item.go
new file mode 100644
index 0000000..67a2998
--- /dev/null
+++ b/internal/object/item.go
@@ -0,0 +1,62 @@
+package object
+
+import "strings"
+
+type EquipSlot string
+
+const (
+ SlotHead EquipSlot = "head"
+ SlotNeck EquipSlot = "neck"
+ SlotTorso EquipSlot = "torso"
+ SlotLegs EquipSlot = "legs"
+ SlotHands EquipSlot = "hands"
+ SlotFeet EquipSlot = "feet"
+ SlotBack EquipSlot = "back"
+ SlotAmmo EquipSlot = "ammo"
+ SlotMainHand EquipSlot = "main_hand"
+ SlotOffHand EquipSlot = "off_hand"
+ SlotRing EquipSlot = "ring"
+)
+
+type WeaponType string
+
+const (
+ WeaponMelee WeaponType = "melee"
+ WeaponRanged WeaponType = "ranged"
+ WeaponTechnology WeaponType = "technology"
+)
+
+type ItemDef struct {
+ ID string `yaml:"id"`
+ Name string `yaml:"name"`
+ Aliases []string `yaml:"aliases"`
+ Description string `yaml:"description"`
+ Value int `yaml:"value"` // credits
+ Stackable bool `yaml:"stackable"`
+ EquipSlot EquipSlot `yaml:"equip_slot"`
+ WeaponType WeaponType `yaml:"weapon_type"`
+ Stats ItemStats `yaml:"stats"`
+ Speed int `yaml:"speed"` // ticks between attacks
+ Toolbelt bool `yaml:"toolbelt"` // can go on toolbelt
+}
+
+type ItemStats struct {
+ AttackBonus int `yaml:"attack_bonus"`
+ StrengthBonus int `yaml:"strength_bonus"`
+ DefenseBonus int `yaml:"defense_bonus"`
+ ScienceBonus int `yaml:"science_bonus"`
+ TechnologyBonus int `yaml:"technology_bonus"`
+}
+
+func (d *ItemDef) MatchesName(input string) bool {
+ lower := strings.ToLower(input)
+ if strings.ToLower(d.Name) == lower {
+ return true
+ }
+ for _, a := range d.Aliases {
+ if strings.ToLower(a) == lower {
+ return true
+ }
+ }
+ return false
+}
diff --git a/internal/object/item_store.go b/internal/object/item_store.go
new file mode 100644
index 0000000..6d62143
--- /dev/null
+++ b/internal/object/item_store.go
@@ -0,0 +1,38 @@
+package object
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "gopkg.in/yaml.v3"
+)
+
+type ItemStore struct {
+ dataDir string
+ cache map[string]*ItemDef
+}
+
+func NewItemStore(dataDir string) *ItemStore {
+ return &ItemStore{
+ dataDir: dataDir,
+ cache: make(map[string]*ItemDef),
+ }
+}
+
+func (s *ItemStore) Load(id string) (*ItemDef, error) {
+ if def, ok := s.cache[id]; ok {
+ return def, nil
+ }
+ path := filepath.Join(s.dataDir, "items", id+".yaml")
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return nil, fmt.Errorf("read item %s: %w", id, err)
+ }
+ var def ItemDef
+ if err := yaml.Unmarshal(data, &def); err != nil {
+ return nil, fmt.Errorf("parse item %s: %w", id, err)
+ }
+ s.cache[id] = &def
+ return &def, nil
+}
diff --git a/internal/object/object.go b/internal/object/object.go
new file mode 100644
index 0000000..3b8741a
--- /dev/null
+++ b/internal/object/object.go
@@ -0,0 +1,8 @@
+package object
+
+type ObjectDef struct {
+ ID string `yaml:"id"`
+ Name string `yaml:"name"`
+ Behavior string `yaml:"behavior"`
+ Props map[string]any `yaml:"props"`
+}
diff --git a/internal/object/store.go b/internal/object/store.go
new file mode 100644
index 0000000..a884601
--- /dev/null
+++ b/internal/object/store.go
@@ -0,0 +1,38 @@
+package object
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "gopkg.in/yaml.v3"
+)
+
+type ObjectStore struct {
+ dataDir string
+ cache map[string]*ObjectDef
+}
+
+func NewObjectStore(dataDir string) *ObjectStore {
+ return &ObjectStore{
+ dataDir: dataDir,
+ cache: make(map[string]*ObjectDef),
+ }
+}
+
+func (s *ObjectStore) Load(id string) (*ObjectDef, error) {
+ if def, ok := s.cache[id]; ok {
+ return def, nil
+ }
+ path := filepath.Join(s.dataDir, "objects", fmt.Sprintf("%s.yaml", id))
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return nil, fmt.Errorf("read object %s: %w", id, err)
+ }
+ var def ObjectDef
+ if err := yaml.Unmarshal(data, &def); err != nil {
+ return nil, fmt.Errorf("parse object %s: %w", id, err)
+ }
+ s.cache[id] = &def
+ return &def, nil
+}