tile.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package main
  2. import "log"
  3. type TileType uint8
  4. const (
  5. BasicTile TileType = iota
  6. AnimatedTile
  7. )
  8. type Tile interface {
  9. Name() string
  10. Type() TileType
  11. Symbol() string
  12. Color() string
  13. Next()
  14. Update() []string
  15. Len() int
  16. ToMap() map[string]any
  17. }
  18. func TileFromMap(m map[string]any) Tile {
  19. _, has := m["symbol"]
  20. _, has_s := m["symbols"]
  21. if !has_s && !has {
  22. log.Println("TileFromMap(m map[string]any) Missing 'symbol' or 'symbols'")
  23. return nil
  24. }
  25. _, has_u := m["update"]
  26. if has && !has_s {
  27. t := &TileBasic{
  28. name: m["name"].(string),
  29. symbol: m["symbol"].(string),
  30. color: m["color"].(string),
  31. }
  32. return t
  33. } else if has_s && !has && has_u {
  34. syms := []string{}
  35. for _, s := range m["symbols"].([]any) {
  36. syms = append(syms, s.(string))
  37. }
  38. cols := []string{}
  39. for _, c := range m["colors"].([]any) {
  40. cols = append(cols, c.(string))
  41. }
  42. ups := []string{}
  43. for _, u := range m["update"].([]any) {
  44. ups = append(ups, u.(string))
  45. }
  46. t := &TileAnimated{
  47. name: m["name"].(string),
  48. symbols: syms,
  49. colors: cols,
  50. update: ups,
  51. }
  52. return t
  53. } else {
  54. log.Printf("TileFromMap(m map[string]any) Unknown Tile Type")
  55. return nil
  56. }
  57. }