started tile serializing

This commit is contained in:
2019-11-10 04:29:59 +03:00
parent f442dc6921
commit 3251d00dac
8 changed files with 155 additions and 51 deletions

View File

@ -1,6 +1,8 @@
package types
import (
"bytes"
"fmt"
"github.com/gammazero/deque"
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -29,12 +31,12 @@ type DanceColorHolder struct {
B *cdeque
}
func (chd *DanceColorHolder) GetColor() uint32 {
func (dch DanceColorHolder) GetColor() uint32 {
return blt.ColorFromARGB(
chd.A,
chd.R.Next(),
chd.G.Next(),
chd.B.Next(),
dch.A,
dch.R.Next(),
dch.G.Next(),
dch.B.Next(),
)
}
@ -45,7 +47,7 @@ type PlainColorHolder struct {
B uint8
}
func (chb *PlainColorHolder) GetColor() uint32 {
func (chb PlainColorHolder) GetColor() uint32 {
return blt.ColorFromARGB(
chb.A,
chb.R,
@ -55,10 +57,10 @@ func (chb *PlainColorHolder) GetColor() uint32 {
}
type TileColorSet struct {
Fg ColorHolder
Bg ColorHolder
DarkFg ColorHolder
DarkBg ColorHolder
Fg ColorHolder `json:"fg"`
Bg ColorHolder `json:"bg"`
DarkFg ColorHolder `json:"darkfg"`
DarkBg ColorHolder `json:"darkbg"`
}
@ -70,13 +72,13 @@ type PlainGlyphHolder struct {
Glyph string
}
func (pch *PlainGlyphHolder) GetGlyph() string {
return pch.Glyph
func (pgh PlainGlyphHolder) GetGlyph() string {
return pgh.Glyph
}
type Appearance struct {
Glyph GlyphHolder `json:"char"`
ColorSet *TileColorSet `json:"colorSet"`
Glyph GlyphHolder `json:"glyph"`
ColorSet TileColorSet `json:"colorSet"`
}
func SingleColorRing(colorValue uint8) *cdeque {
@ -108,4 +110,51 @@ func FillColorRing(colorValue uint8, minGlow, maxGlow, step int) *cdeque {
func (app Appearance) Type() string {
return ecs.AppearanceComponent
}
func (app *Appearance) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString("{")
//glyph
buffer.WriteString(`"glyph":{`)
switch app.Glyph.(type) {
case PlainGlyphHolder:
buffer.WriteString(fmt.Sprintf(`"type":"plain", "chars":"%s"`, app.Glyph.GetGlyph()))
break
}
//note the comma
buffer.WriteString("},")
//color
buffer.WriteString(`"color":{`)
buffer.WriteString(getColorJson("fg", app.ColorSet.Fg) + ",")
buffer.WriteString(getColorJson("bg", app.ColorSet.Fg) + ",")
buffer.WriteString(getColorJson("darkfg", app.ColorSet.Fg) + ",")
buffer.WriteString(getColorJson("darkbg", app.ColorSet.Fg))
buffer.WriteString("}")
buffer.WriteString("}")
return buffer.Bytes(), nil
}
func (app *Appearance) UnmarshalJSON(buffer []byte) error {
return nil
}
func getColorJson(field string, holder ColorHolder) string {
result := ""
switch holder.(type) {
case PlainColorHolder:
result = fmt.Sprintf(`"plain":[%d,%d,%d,%d]`,
holder.(PlainColorHolder).A,
holder.(PlainColorHolder).R,
holder.(PlainColorHolder).G,
holder.(PlainColorHolder).B,
)
break
case DanceColorHolder:
return "" //fixme!!!
break
}
return fmt.Sprintf(`"%s":%s`, field, result)
}