tile.go 1010 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. ToMap() map[string]any
  15. }
  16. func TileFromMap(m map[string]any) Tile {
  17. _, has := m["symbol"]
  18. _, has_s := m["symbols"]
  19. if !has_s && !has {
  20. log.Println("TileFromMap(m map[string]any) Missing 'symbol' or 'symbols'")
  21. return nil
  22. }
  23. if has && !has_s {
  24. t := &TileBasic{
  25. name: m["name"].(string),
  26. symbol: m["symbol"].(string),
  27. color: m["color"].(string),
  28. }
  29. return t
  30. } else if has_s && !has {
  31. syms := []string{}
  32. for _, s := range m["symbols"].([]any) {
  33. syms = append(syms, s.(string))
  34. }
  35. cols := []string{}
  36. for _, c := range m["colors"].([]any) {
  37. cols = append(cols, c.(string))
  38. }
  39. t := &TileAnimated{
  40. name: m["name"].(string),
  41. symbols: syms,
  42. colors: cols,
  43. }
  44. return t
  45. } else {
  46. log.Printf("TileFromMap(m map[string]any) Unknown Tile Type")
  47. return nil
  48. }
  49. }