1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package main
- import "fmt"
- type Sprite struct {
- node *Node
- Img string
- Animated bool
- FrameRate int
- Delay int
- RandomStart bool
- }
- func (s *Sprite) Parse(node *Node) error {
- if node.Key() != "sprite" {
- return fmt.Errorf("expected 'sprite' node, invalid node")
- }
- s.Img = node.Value()
- s.node = node
- if node.Len() != 0 {
- s.Animated = true
- for _, kid := range node.Children {
- switch kid.Key() {
- case "frame rate", "frame time":
- s.FrameRate = kid.ValueInt()
- case "delay":
- s.Delay = kid.ValueInt()
- case "random start frame":
- s.RandomStart = true
- }
- }
- }
- return nil
- }
- func (s *Sprite) Update() error {
- if s.node == nil {
- return fmt.Errorf("no node, sprite")
- }
- s.node.RmAllChildren()
- s.node.SetValue(s.Img)
- if s.Animated {
- n := s.node.NewChild()
- n.Set("frame rate", s.FrameRate)
- n = s.node.NewChild()
- n.Set("delay", s.Delay)
- if s.RandomStart {
- n := s.node.NewChild()
- n.Line = n.Format("random start frame")
- }
- }
- return nil
- }
|