瀏覽代碼

v1.0.0-dev/server): Added Tile Catalog

The Catalog will allow the game to define tiles and animated tiles.

A number can be used instead of a full sizes structure for optimization.
Steve Thielemann 1 年之前
父節點
當前提交
68eb81b780
共有 9 個文件被更改,包括 355 次插入0 次删除
  1. 46 0
      Client/version.go
  2. 79 0
      Server/catalog.go
  3. 2 0
      Server/go.mod
  4. 2 0
      Server/go.sum
  5. 51 0
      Server/main.go
  6. 57 0
      Server/tile.go
  7. 39 0
      Server/tile_animated.go
  8. 33 0
      Server/tile_basic.go
  9. 46 0
      Server/version.go

+ 46 - 0
Client/version.go

@@ -0,0 +1,46 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os/exec"
+	"strings"
+)
+
+// 1.0.0-dev
+const (
+	VERSION_MAJ = 1
+	VERSION_MIN = 0
+	VERSION_PAT = 0
+	VERSION_TAG = "dev"
+)
+
+func GitVersion() string {
+	var (
+		cmd    *exec.Cmd
+		err    error
+		stdout []byte
+	)
+	cmd = exec.Command("git", "describe",
+		"--abbrev=8",
+		"--long",
+		"--tags",
+		"--dirty",
+		"--always",
+		"--match",
+		"v[0-9]*")
+	stdout, err = cmd.Output()
+	if err != nil {
+		log.Println("Err (GitVersion):", err)
+		return ""
+	}
+	return strings.TrimSpace(string(stdout))
+}
+
+func Version() string {
+	var hash string = GitVersion()
+	if hash != "" {
+		return fmt.Sprintf("%d.%d.%d-%s#%s", VERSION_MAJ, VERSION_MIN, VERSION_PAT, VERSION_TAG, hash)
+	}
+	return fmt.Sprintf("%d.%d.%d-%s", VERSION_MAJ, VERSION_MIN, VERSION_PAT, VERSION_TAG)
+}

+ 79 - 0
Server/catalog.go

@@ -0,0 +1,79 @@
+package main
+
+import (
+	"encoding/json"
+	"os"
+)
+
+type Catalog struct {
+	Tiles []Tile
+}
+
+func (c *Catalog) LoadFile(filename string) error {
+	pay, err := os.ReadFile(filename)
+	if err != nil {
+		return err
+	}
+	file := []map[string]any{}
+	err = json.Unmarshal(pay, &file)
+	if err != nil {
+		return err
+	}
+	for _, m := range file {
+		t := TileFromMap(m)
+		if t == nil {
+			continue
+		}
+		c.Tiles = append(c.Tiles, t)
+	}
+	return nil
+}
+
+func (c *Catalog) FindByName(name string) (int, bool) {
+	for idx, t := range c.Tiles {
+		if t.Name() == name {
+			return idx, true
+		}
+	}
+	return 0, false
+}
+
+func (c *Catalog) Symbol(idx int) string {
+	return c.Tiles[idx].Symbol()
+}
+
+func (c *Catalog) Color(idx int) string {
+	return c.Tiles[idx].Color()
+}
+
+func (c *Catalog) Next(idx int) {
+	c.Tiles[idx].Next()
+}
+
+func (c *Catalog) MakeTemplate(filename string) error {
+	pay, err := json.MarshalIndent([]map[string]any{
+		{
+			"name":   "template",
+			"symbol": "0",
+			"color":  "BRI BRO ON BLA",
+		},
+		{
+			"name": "animated-template",
+			"symbols": []string{
+				"1",
+				"2",
+				"3",
+			},
+			"colors": []string{
+				"BRI RED ON BLA",
+				"BRI GRE ON BLA",
+				"BRI CYA ON BLA",
+			},
+		},
+	}, "", "  ")
+	if err != nil {
+		return err
+	}
+	err = os.WriteFile(filename, pay, 0666)
+	return err
+}

+ 2 - 0
Server/go.mod

@@ -1,3 +1,5 @@
 module git.red-green.com/david/core-server
 
 go 1.21.0
+
+require git.red-green.com/david/point2d v1.0.1 // indirect

+ 2 - 0
Server/go.sum

@@ -0,0 +1,2 @@
+git.red-green.com/david/point2d v1.0.1 h1:E9VbOj68fIsGN8erBz1Juy/WMexYxGasj4h8D+XkTxo=
+git.red-green.com/david/point2d v1.0.1/go.mod h1:A0psWOXN81tQhgw8QuRFPsSc3UsnSehzjMicg89PLxE=

+ 51 - 0
Server/main.go

@@ -0,0 +1,51 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+	"path/filepath"
+)
+
+func main() {
+	c := &Catalog{}
+	_, err := os.Stat("content")
+	if err != nil {
+		os.Mkdir("content", 0775)
+		_, err = os.Stat(filepath.Join("content", "tiles"))
+		if err != nil {
+			os.Mkdir(filepath.Join("content", "tiles"), 0775)
+		}
+		err = c.MakeTemplate(filepath.Join("content", "tiles", "0_template.json"))
+		if err != nil {
+			log.Printf("MakeTemplate: %v", err)
+			return
+		}
+	}
+	err = c.LoadFile(filepath.Join("content", "tiles", "0_template.json"))
+	if err != nil {
+		_, err = os.Stat(filepath.Join("content", "tiles"))
+		if err != nil {
+			os.MkdirAll(filepath.Join("content", "tiles"), 0775)
+		}
+		err = c.MakeTemplate(filepath.Join("content", "tiles", "0_template.json"))
+		if err != nil {
+			log.Printf("MakeTemplate: %v", err)
+			return
+		}
+		err = c.LoadFile(filepath.Join("content", "tiles", "0_template.json"))
+		if err != nil {
+			log.Printf("LoadFile: %v", err)
+			return
+		}
+	}
+	fmt.Printf("Tiles: %d\n", len(c.Tiles))
+	for idx, t := range c.Tiles {
+		switch t.Type() {
+		case BasicTile:
+			fmt.Printf("  %3d %s\n", idx, t.Name())
+		case AnimatedTile:
+			fmt.Printf("A %3d %s\n", idx, t.Name())
+		}
+	}
+}

+ 57 - 0
Server/tile.go

@@ -0,0 +1,57 @@
+package main
+
+import "log"
+
+type TileType uint8
+
+const (
+	BasicTile TileType = iota
+	AnimatedTile
+)
+
+type Tile interface {
+	Name() string
+	Type() TileType
+
+	Symbol() string
+	Color() string
+
+	Next()
+
+	ToMap() map[string]any
+}
+
+func TileFromMap(m map[string]any) Tile {
+	_, has := m["symbol"]
+	_, has_s := m["symbols"]
+	if !has_s && !has {
+		log.Println("TileFromMap(m map[string]any) Missing 'symbol' or 'symbols'")
+		return nil
+	}
+	if has && !has_s {
+		t := &TileBasic{
+			name:   m["name"].(string),
+			symbol: m["symbol"].(string),
+			color:  m["color"].(string),
+		}
+		return t
+	} else if has_s && !has {
+		syms := []string{}
+		for _, s := range m["symbols"].([]any) {
+			syms = append(syms, s.(string))
+		}
+		cols := []string{}
+		for _, c := range m["colors"].([]any) {
+			cols = append(cols, c.(string))
+		}
+		t := &TileAnimated{
+			name:    m["name"].(string),
+			symbols: syms,
+			colors:  cols,
+		}
+		return t
+	} else {
+		log.Printf("TileFromMap(m map[string]any) Unknown Tile Type")
+		return nil
+	}
+}

+ 39 - 0
Server/tile_animated.go

@@ -0,0 +1,39 @@
+package main
+
+type TileAnimated struct {
+	name    string
+	idx     int
+	symbols []string
+	colors  []string
+}
+
+func (t *TileAnimated) Name() string {
+	return t.name
+}
+
+func (t *TileAnimated) Type() TileType {
+	return AnimatedTile
+}
+
+func (t *TileAnimated) Symbol() string {
+	return t.symbols[t.idx]
+}
+
+func (t *TileAnimated) Color() string {
+	return t.colors[t.idx]
+}
+
+func (t *TileAnimated) Next() {
+	t.idx++
+	if t.idx >= len(t.symbols) {
+		t.idx = 0
+	}
+}
+
+func (t *TileAnimated) ToMap() map[string]any {
+	return map[string]any{
+		"name":    t.name,
+		"symbols": t.symbols,
+		"colors":  t.colors,
+	}
+}

+ 33 - 0
Server/tile_basic.go

@@ -0,0 +1,33 @@
+package main
+
+type TileBasic struct {
+	name   string
+	symbol string
+	color  string
+}
+
+func (t *TileBasic) Name() string {
+	return t.name
+}
+
+func (t *TileBasic) Type() TileType {
+	return BasicTile
+}
+
+func (t *TileBasic) Symbol() string {
+	return t.symbol
+}
+
+func (t *TileBasic) Color() string {
+	return t.color
+}
+
+func (t *TileBasic) Next() {}
+
+func (t *TileBasic) ToMap() map[string]any {
+	return map[string]any{
+		"name":   t.name,
+		"symbol": t.symbol,
+		"color":  t.color,
+	}
+}

+ 46 - 0
Server/version.go

@@ -0,0 +1,46 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os/exec"
+	"strings"
+)
+
+// 1.0.0-dev
+const (
+	VERSION_MAJ = 1
+	VERSION_MIN = 0
+	VERSION_PAT = 0
+	VERSION_TAG = "dev"
+)
+
+func GitVersion() string {
+	var (
+		cmd    *exec.Cmd
+		err    error
+		stdout []byte
+	)
+	cmd = exec.Command("git", "describe",
+		"--abbrev=8",
+		"--long",
+		"--tags",
+		"--dirty",
+		"--always",
+		"--match",
+		"v[0-9]*")
+	stdout, err = cmd.Output()
+	if err != nil {
+		log.Println("Err (GitVersion):", err)
+		return ""
+	}
+	return strings.TrimSpace(string(stdout))
+}
+
+func Version() string {
+	var hash string = GitVersion()
+	if hash != "" {
+		return fmt.Sprintf("%d.%d.%d-%s#%s", VERSION_MAJ, VERSION_MIN, VERSION_PAT, VERSION_TAG, hash)
+	}
+	return fmt.Sprintf("%d.%d.%d-%s", VERSION_MAJ, VERSION_MIN, VERSION_PAT, VERSION_TAG)
+}