tile.go 1021 B

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