|
@@ -8,16 +8,17 @@ type Ship struct {
|
|
|
Name string
|
|
|
Sprite Sprite // TODO: Make this a struct
|
|
|
Thumbnail string
|
|
|
- UUID string // We should not allow this to be changed (it might be significant to Endless Sky)
|
|
|
- Attributes map[string]interface{} // TODO: Make this a struct
|
|
|
- Outfits []string // TODO: Make this a struct
|
|
|
+ UUID string // We should not allow this to be changed (it might be significant to Endless Sky)
|
|
|
+ Attributes Attributes
|
|
|
+ Outfits OutfitList
|
|
|
Crew int
|
|
|
Fuel int
|
|
|
Shields int
|
|
|
Hull int
|
|
|
Pos Vec2
|
|
|
- Engines []string // TODO: Make this a struct
|
|
|
- Gun []string // TODO: Make this a struct
|
|
|
+ Engines []Engine
|
|
|
+ Guns []Gun
|
|
|
+ Turrets []Turret
|
|
|
Leak []string // TODO: Make this a struct
|
|
|
Explode []string // TODO: Make this a struct
|
|
|
Location Location
|
|
@@ -33,30 +34,35 @@ func (s *Ship) Parse(node *Node) error {
|
|
|
}
|
|
|
s.node = node
|
|
|
s.Model = node.Value()
|
|
|
- s.Attributes = make(map[string]interface{})
|
|
|
+ s.Attributes = Attributes{
|
|
|
+ Others: make(map[string]float64),
|
|
|
+ }
|
|
|
+ s.Engines = []Engine{}
|
|
|
+ s.Guns = []Gun{}
|
|
|
+ s.Turrets = []Turret{}
|
|
|
+ s.Outfits = OutfitList{}
|
|
|
for _, kid := range node.Children {
|
|
|
switch kid.Key() {
|
|
|
case "name":
|
|
|
s.Name = kid.Value()
|
|
|
case "sprite":
|
|
|
- s.Sprite.Parse(kid)
|
|
|
+ err := s.Sprite.Parse(kid)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
case "thumbnail":
|
|
|
s.Thumbnail = kid.Value()
|
|
|
case "uuid":
|
|
|
s.UUID = kid.Value()
|
|
|
case "attributes":
|
|
|
- for _, k := range kid.Children {
|
|
|
- if CanFloat(k.Value()) {
|
|
|
- s.Attributes[k.Key()] = k.ValueFloat64()
|
|
|
- } else if CanInt(k.Value()) {
|
|
|
- s.Attributes[k.Key()] = k.ValueInt()
|
|
|
- } else {
|
|
|
- s.Attributes[k.Key()] = k.Value()
|
|
|
- }
|
|
|
+ err := s.Attributes.Parse(kid)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
case "outfits":
|
|
|
- for _, k := range kid.Children {
|
|
|
- s.Outfits = append(s.Outfits, k.Line)
|
|
|
+ err := s.Outfits.Parse(kid)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
case "crew":
|
|
|
s.Crew = kid.ValueInt()
|
|
@@ -67,24 +73,149 @@ func (s *Ship) Parse(node *Node) error {
|
|
|
case "hull":
|
|
|
s.Hull = kid.ValueInt()
|
|
|
case "position":
|
|
|
- s.Pos.Parse(kid.Value())
|
|
|
+ err := s.Pos.Parse(kid.Value())
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
case "engine":
|
|
|
- s.Engines = append(s.Engines, kid.Line)
|
|
|
+ e := Engine{}
|
|
|
+ err := e.Parse(kid)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ s.Engines = append(s.Engines, e)
|
|
|
case "gun":
|
|
|
- s.Gun = append(s.Gun, kid.Value())
|
|
|
+ g := Gun{}
|
|
|
+ err := g.Parse(kid)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ s.Guns = append(s.Guns, g)
|
|
|
+ case "turret":
|
|
|
+ t := Turret{}
|
|
|
+ err := t.Parse(kid)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ s.Turrets = append(s.Turrets, t)
|
|
|
case "leak":
|
|
|
s.Leak = append(s.Leak, kid.Value())
|
|
|
case "explode":
|
|
|
s.Explode = append(s.Explode, kid.Value())
|
|
|
case "system":
|
|
|
- s.Location.Parse(kid)
|
|
|
+ if s.Location.System == "" {
|
|
|
+ s.Location.Parse(kid)
|
|
|
+ }
|
|
|
case "planet":
|
|
|
- s.Location.Parse(kid)
|
|
|
+ if s.Location.Planet == "" {
|
|
|
+ s.Location.Parse(kid)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (s *Ship) Update() error {
|
|
|
+ if s.node == nil {
|
|
|
+ return fmt.Errorf("no node, ship")
|
|
|
+ }
|
|
|
+ s.node.SetValue(s.Model)
|
|
|
+ err := s.Location.Update()
|
|
|
+ if err != nil {
|
|
|
+ return fmt.Errorf("ship, %v", err)
|
|
|
+ }
|
|
|
+ for _, kid := range s.node.Children {
|
|
|
+ switch kid.Key() {
|
|
|
+ case "name":
|
|
|
+ kid.SetValue(s.Name)
|
|
|
+ case "sprite":
|
|
|
+ err := s.Sprite.Update()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ case "thumbnail":
|
|
|
+ kid.SetValue(s.Thumbnail)
|
|
|
+ case "attributes":
|
|
|
+ err := s.Attributes.Update()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ case "outfits":
|
|
|
+ err := s.Outfits.Update()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ case "crew":
|
|
|
+ kid.SetValue(s.Crew)
|
|
|
+ case "fuel":
|
|
|
+ kid.SetValue(s.Fuel)
|
|
|
+ case "shields":
|
|
|
+ kid.SetValue(s.Shields)
|
|
|
+ case "hull":
|
|
|
+ kid.SetValue(s.Hull)
|
|
|
+ case "position":
|
|
|
+ kid.SetValue(s.Pos.String())
|
|
|
+ case "engine":
|
|
|
+ _, err := kid.DetachFromParent()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ case "gun":
|
|
|
+ _, err := kid.DetachFromParent()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ case "turret":
|
|
|
+ _, err := kid.DetachFromParent()
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, e := range s.Engines {
|
|
|
+ n := s.node.NewChild()
|
|
|
+ n.Set(e.Kind, e.Pos.String())
|
|
|
+ n1 := n.NewChild()
|
|
|
+ n1.Set("zoom", e.Zoom)
|
|
|
+ n1 = n.NewChild()
|
|
|
+ n1.Set("angle", e.Angle)
|
|
|
+ if e.Over {
|
|
|
+ n1 = n.NewChild()
|
|
|
+ n1.Line = "over"
|
|
|
+ } else {
|
|
|
+ n1 = n.NewChild()
|
|
|
+ n1.Line = "under"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, g := range s.Guns {
|
|
|
+ n := s.node.NewChild()
|
|
|
+ if g.Mount != "" {
|
|
|
+ n.Set("gun", fmt.Sprintf("%s %s", g.Pos.String(), g.Mount))
|
|
|
+ } else {
|
|
|
+ n.Set("gun", g.Pos.String())
|
|
|
+ }
|
|
|
+ if g.Over {
|
|
|
+ n1 := n.NewChild()
|
|
|
+ n1.Line = "over"
|
|
|
+ } else {
|
|
|
+ n1 := n.NewChild()
|
|
|
+ n1.Line = "under"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for _, t := range s.Turrets {
|
|
|
+ n := s.node.NewChild()
|
|
|
+ if t.Mount != "" {
|
|
|
+ n.Set("turret", fmt.Sprintf("%s %s", t.Pos.String(), t.Mount))
|
|
|
+ } else {
|
|
|
+ n.Set("turret", t.Pos.String())
|
|
|
+ }
|
|
|
+ if t.Over {
|
|
|
+ n1 := n.NewChild()
|
|
|
+ n1.Line = "over"
|
|
|
+ } else {
|
|
|
+ n1 := n.NewChild()
|
|
|
+ n1.Line = "under"
|
|
|
+ }
|
|
|
+ }
|
|
|
return nil
|
|
|
}
|