started tile serialization

This commit is contained in:
thefish 2019-11-10 19:52:01 +03:00
parent 3251d00dac
commit b734e538f4
3 changed files with 99 additions and 18 deletions

View File

@ -0,0 +1,18 @@
package alchemyst_go
import (
"encoding/json"
"fmt"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"testing"
)
func TestSerializeTile (t *testing.T) {
wt := gamemap.NewWaterTile()
txt, err := json.Marshal(wt)
if err != nil {
t.Log(err)
t.Fail()
}
fmt.Printf("%s", txt)
}

View File

@ -55,6 +55,32 @@ func NewWall() *Tile {
}
}
func NewDecoratedWall() *Tile {
return &Tile{
Name: "Wall",
Description: "A dull rock wall",
BlocksPass: true,
BlocksSight: true,
Explored: false,
MustDraw: false,
Appearance: &Appearance{
Glyph: &PlainGlyphHolder{"#"},
ColorSet: TileColorSet{
Fg: &PlainColorHolder{255, 130, 110, 150},
//Bg: &PlainColorHolder{255, 172, 170, 173},
Bg: &DanceColorHolder{
255,
DeviatedColorRing(172, -15, 10),
DeviatedColorRing(170, -5, 15),
DeviatedColorRing(173, -10, 10),
},
DarkFg: &PlainColorHolder{255, 20, 20, 68},
DarkBg: &PlainColorHolder{255, 7, 7, 30},
},
},
}
}
func NewFloor() *Tile {
return &Tile{
Name: "Floor",

View File

@ -108,6 +108,16 @@ func FillColorRing(colorValue uint8, minGlow, maxGlow, step int) *cdeque {
return c
}
func DeviatedColorRing(colorValue uint8, minGlow, maxGlow int) *cdeque {
q := make([]uint8, 0)
color := int(colorValue)
color = crng.Range(minGlow, maxGlow) + color
q = append(q, colorValue)
c := &cdeque{}
c.PushBack(uint8(color))
return c
}
func (app Appearance) Type() string {
return ecs.AppearanceComponent
}
@ -116,24 +126,24 @@ func (app *Appearance) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString("{")
//glyph
buffer.WriteString(`"glyph":{`)
switch app.Glyph.(type) {
case PlainGlyphHolder:
if _, ok := app.Glyph.(*PlainGlyphHolder); ok {
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(getColorJson("bg", app.ColorSet.Bg) + ",")
buffer.WriteString(getColorJson("darkfg", app.ColorSet.DarkFg) + ",")
buffer.WriteString(getColorJson("darkbg", app.ColorSet.DarkBg))
buffer.WriteString("}")
buffer.WriteString("}")
fmt.Printf("\n\nbuffer: %s\n\n", buffer.String())
return buffer.Bytes(), nil
}
@ -143,18 +153,45 @@ func (app *Appearance) UnmarshalJSON(buffer []byte) error {
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,
if _, dch := holder.(*DanceColorHolder); dch {
result = fmt.Sprintf(`{"dance":{`)
result = result + detectRing("a", holder.(*DanceColorHolder).A) + ","
result = result + detectRing("r", holder.(*DanceColorHolder).R) + ","
result = result + detectRing("g", holder.(*DanceColorHolder).G) + ","
result = result + detectRing("b", holder.(*DanceColorHolder).B)
result = result + "}}"
}
if _, pch := holder.(*PlainColorHolder); pch {
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)
}
func detectRing(channel string, something interface{}) string {
result := ""
switch something.(type) {
case (int),(uint8):
result += fmt.Sprintf(`"%s":{"plain":%d}`, channel, something)
case (*cdeque):
fmt.Printf("%v", something)
if something.(*cdeque).Len() == 1 {
//fixme right now we can not distinct plain and deviated
result += fmt.Sprintf(`"%s":{"single":[%v]}`, channel, something.(*cdeque).Front())
} else {
result += fmt.Sprintf(`"%s":{"fill":[%v, %v, %v, %v]}`,
channel,
something.(*cdeque).Front(),
something.(*cdeque).Next(),
something.(*cdeque).Next(),
something.(*cdeque).Next(),
)
}
}
return result
}