Browse Source

v1.0.0-dev/server): Added "update" reasons

These "update" reasons will call .Next() and perform updates on screen.

Common "update" values:
- "move player" (Call .Next() and redraw each time the player moves)
- "time <duration>" (Call .Next() and redraw if needed, given the duration)
- "ping" (Call .Next() and redraw each time a ping packet is received)
Steve Thielemann 1 year ago
parent
commit
150e9f9626

+ 3 - 0
Server/catalog.go

@@ -89,6 +89,9 @@ func (c *Catalog) MakeTemplate(filename string) error {
 				"BRI GRE ON BLA",
 				"BRI CYA ON BLA",
 			},
+			"update": []string{
+				"time 150ms",
+			},
 		},
 	}, "", "  ")
 	if err != nil {

+ 17 - 0
Server/main.go

@@ -5,6 +5,8 @@ import (
 	"log"
 	"os"
 	"path/filepath"
+	"strings"
+	"time"
 )
 
 func main() {
@@ -40,6 +42,21 @@ func main() {
 				}
 				t.Next()
 			}
+			timed := time.Duration(0)
+			for _, u := range t.Update() {
+				if strings.HasPrefix(u, "time ") {
+					part := strings.ReplaceAll(u, "time ", "")
+					work, err := time.ParseDuration(part)
+					if err != nil {
+						continue
+					}
+					timed += work
+				}
+			}
+			if timed != time.Duration(0) {
+				fmt.Printf("A %3d %-30s (%d) %s %s\n", idx, t.Name(), t.Len(), symbols, timed)
+				continue
+			}
 			fmt.Printf("A %3d %-30s (%d) %s\n", idx, t.Name(), t.Len(), symbols)
 		}
 	}

+ 8 - 1
Server/tile.go

@@ -17,6 +17,7 @@ type Tile interface {
 	Color() string
 
 	Next()
+	Update() []string
 	Len() int
 
 	ToMap() map[string]any
@@ -29,6 +30,7 @@ func TileFromMap(m map[string]any) Tile {
 		log.Println("TileFromMap(m map[string]any) Missing 'symbol' or 'symbols'")
 		return nil
 	}
+	_, has_u := m["update"]
 	if has && !has_s {
 		t := &TileBasic{
 			name:   m["name"].(string),
@@ -36,7 +38,7 @@ func TileFromMap(m map[string]any) Tile {
 			color:  m["color"].(string),
 		}
 		return t
-	} else if has_s && !has {
+	} else if has_s && !has && has_u {
 		syms := []string{}
 		for _, s := range m["symbols"].([]any) {
 			syms = append(syms, s.(string))
@@ -45,10 +47,15 @@ func TileFromMap(m map[string]any) Tile {
 		for _, c := range m["colors"].([]any) {
 			cols = append(cols, c.(string))
 		}
+		ups := []string{}
+		for _, u := range m["update"].([]any) {
+			ups = append(ups, u.(string))
+		}
 		t := &TileAnimated{
 			name:    m["name"].(string),
 			symbols: syms,
 			colors:  cols,
+			update:  ups,
 		}
 		return t
 	} else {

+ 6 - 0
Server/tile_animated.go

@@ -5,6 +5,7 @@ type TileAnimated struct {
 	idx     int
 	symbols []string
 	colors  []string
+	update  []string // when .Next() get's called ("move player", "time 100ms", "move monster")
 }
 
 func (t *TileAnimated) Name() string {
@@ -30,6 +31,10 @@ func (t *TileAnimated) Next() {
 	}
 }
 
+func (t *TileAnimated) Update() []string {
+	return t.update
+}
+
 func (t *TileAnimated) Len() int {
 	return len(t.symbols)
 }
@@ -39,5 +44,6 @@ func (t *TileAnimated) ToMap() map[string]any {
 		"name":    t.name,
 		"symbols": t.symbols,
 		"colors":  t.colors,
+		"update":  t.update,
 	}
 }

+ 4 - 0
Server/tile_basic.go

@@ -24,6 +24,10 @@ func (t *TileBasic) Color() string {
 
 func (t *TileBasic) Next() {}
 
+func (t *TileBasic) Update() []string {
+	return nil
+}
+
 func (t *TileBasic) Len() int {
 	return 1
 }

+ 57 - 0
content/tiles/0_base.json

@@ -23,6 +23,9 @@
       "Red Green",
       "Red Green",
       "Red Green"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -38,6 +41,9 @@
       "-",
       "\\",
       "|"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -53,6 +59,9 @@
       "-",
       "\\",
       "|"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -68,6 +77,9 @@
       "-",
       "\\",
       "|"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -83,6 +95,9 @@
       "-",
       "\\",
       "|"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -98,6 +113,9 @@
       "-",
       "\\",
       "|"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -113,6 +131,9 @@
       "-",
       "\\",
       "|"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -136,6 +157,9 @@
       ">",
       "|",
       "<"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -159,6 +183,9 @@
       ">",
       "|",
       "<"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -182,6 +209,9 @@
       ">",
       "|",
       "<"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -205,6 +235,9 @@
       ">",
       "|",
       "<"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -228,6 +261,9 @@
       ">",
       "|",
       "<"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -251,6 +287,9 @@
       ">",
       "|",
       "<"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -262,6 +301,9 @@
     "symbols": [
       "X",
       "+"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -273,6 +315,9 @@
     "symbols": [
       "X",
       "+"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -284,6 +329,9 @@
     "symbols": [
       "X",
       "+"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -295,6 +343,9 @@
     "symbols": [
       "X",
       "+"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -306,6 +357,9 @@
     "symbols": [
       "X",
       "+"
+    ],
+    "update": [
+      "time 150ms"
     ]
   },
   {
@@ -317,6 +371,9 @@
     "symbols": [
       "X",
       "+"
+    ],
+    "update": [
+      "time 150ms"
     ]
   }
 ]

+ 4 - 16
content/tiles/2_terrain.json

@@ -40,25 +40,13 @@
     "symbol": "^"
   },
   {
-    "colors": [
-      "BRI CYA ON BLU",
-      "CYA ON BLU"
-    ],
+    "color": "BRI CYA ON BLU",
     "name": "water-shallow",
-    "symbols": [
-      "~",
-      "~"
-    ]
+    "symbol": "~"
   },
   {
-    "colors": [
-      "BRI BLU ON BLU",
-      "BLA ON BLU"
-    ],
+    "color": "BRI BLU ON BLU",
     "name": "water-deep",
-    "symbols": [
-      "~",
-      "~"
-    ]
+    "symbol": "~"
   }
 ]

+ 3 - 0
content/tiles/4_interior.json

@@ -23,6 +23,9 @@
     "symbols": [
       "#",
       "#"
+    ],
+    "update": [
+      "time 225ms"
     ]
   },
   {

+ 3 - 0
content/tiles/5_constructs.json

@@ -73,6 +73,9 @@
     "symbols": [
       "%",
       "/"
+    ],
+    "update": [
+      "time 400ms"
     ]
   }
 ]