From 3251d00dacc2448d04c7ba32c1f34672c8b26c39 Mon Sep 17 00:00:00 2001 From: thefish Date: Sun, 10 Nov 2019 04:29:59 +0300 Subject: [PATCH] started tile serializing --- TODO | 44 ++++++++++++++++++++++ cmd/game/main.go | 12 +++--- engine/gamemap/TODO | 3 -- engine/gamemap/tile.go | 11 ++---- engine/items/carried.go | 9 +++++ engine/items/useable.go | 10 +++++ engine/types/appearance.go | 77 +++++++++++++++++++++++++++++++------- util/util.go | 40 +++++++++----------- 8 files changed, 155 insertions(+), 51 deletions(-) create mode 100644 TODO delete mode 100644 engine/gamemap/TODO create mode 100644 engine/items/carried.go create mode 100644 engine/items/useable.go diff --git a/TODO b/TODO new file mode 100644 index 0000000..947f9b4 --- /dev/null +++ b/TODO @@ -0,0 +1,44 @@ +Assets and i18n: + - move tile settings to json, generate from there (part of prefabs) + - prepare all interface entries for i18n + - all texts (terrain, mobs, quests) also in at least 2 languages + +ECS & engine: + - implement time queue (how to deal with closures?) + - move all rendering to systems + - try to move input handling to systems + +Dungeon and branches: + General: + - river! (dig_bezier) + - erosion (?) + - global map of valley + Prefabs: + - load prefabs + - compose from gens and prefabs + - editor for prefabs + +Combat: + - generate skeleton / intesines / muscle / eyes&ears & fingers from templates + - serializable + - config in outer files + - mass + - damage from skill / mass / speed / material density + - no hitpoints! blood is the life source + +Items: + - pickup + - drop + - use + +Mobs: + basic: + - place mobs + - move mobs + advanced: + - ai + - dijkstra maps + +Quest engine: + - look at parsers like URQL etc + - distorted Aschenputtel story / partisans / rapist prince / Grey Mountains \ No newline at end of file diff --git a/cmd/game/main.go b/cmd/game/main.go index cdbec24..6474f0e 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -82,10 +82,12 @@ func main() { controller := ecs.NewController() - controller.MapComponentClass("coords", types.Coords{}) - controller.MapComponentClass("appearance", types.Appearance{}) - controller.MapComponentClass("mob", mob.Mob{}) - controller.MapComponentClass("moveable", movement.Moveable{}) + controller.MapComponentClass(ecs.CoordsComponent, types.Coords{}) + controller.MapComponentClass(ecs.AppearanceComponent, types.Appearance{}) + controller.MapComponentClass(ecs.MobComponent, mob.Mob{}) + controller.MapComponentClass(ecs.MoveableComponent, movement.Moveable{}) + controller.MapComponentClass(ecs.CarriedComponent, movement.Moveable{}) + controller.MapComponentClass(ecs.UsableComponent, movement.Moveable{}) moveable := movement.Moveable{ Controller: controller, @@ -172,7 +174,7 @@ func main() { controller.AddComponent(player, &types.Appearance{ Glyph: &types.PlainGlyphHolder{"@"}, - ColorSet: &types.TileColorSet{ + ColorSet: types.TileColorSet{ Fg: &types.PlainColorHolder{255, 255, 255, 255}, }, }) diff --git a/engine/gamemap/TODO b/engine/gamemap/TODO deleted file mode 100644 index b6d1783..0000000 --- a/engine/gamemap/TODO +++ /dev/null @@ -1,3 +0,0 @@ -- load prefabs -- compose from gens and prefabs -- editor for prefabs \ No newline at end of file diff --git a/engine/gamemap/tile.go b/engine/gamemap/tile.go index 3a5aae2..8ee0ced 100644 --- a/engine/gamemap/tile.go +++ b/engine/gamemap/tile.go @@ -10,7 +10,6 @@ type Tile struct { Description string `json:"desc"` BlocksPass bool `json:"blocksPass"` BlocksSight bool `json:"blocksSight"` - Colordance bool `json:"colordance"` Explored bool MustDraw bool Visible bool @@ -46,7 +45,7 @@ func NewWall() *Tile { MustDraw: false, Appearance: &Appearance{ Glyph: &PlainGlyphHolder{"#"}, - ColorSet: &TileColorSet{ + ColorSet: TileColorSet{ Fg: &PlainColorHolder{255, 130, 110, 150}, Bg: &PlainColorHolder{255, 172, 170, 173}, DarkFg: &PlainColorHolder{255, 20, 20, 68}, @@ -66,7 +65,7 @@ func NewFloor() *Tile { MustDraw: false, Appearance: &Appearance{ Glyph: &PlainGlyphHolder{"."}, - ColorSet: &TileColorSet{ + ColorSet: TileColorSet{ Fg: &PlainColorHolder{255, 220, 220, 250}, Bg: &PlainColorHolder{255, 19, 19, 70}, DarkFg: &PlainColorHolder{255, 30, 20, 50}, @@ -85,11 +84,10 @@ func NewWaterTile() *Tile { BlocksSight: false, Explored: false, MustDraw: true, //fixme debug - Colordance: true, Appearance: &Appearance{ Glyph: &PlainGlyphHolder{" "}, - ColorSet: &TileColorSet{ + ColorSet: TileColorSet{ Fg: &PlainColorHolder{255, 220, 220, 250}, Bg: &DanceColorHolder{ 255, @@ -113,10 +111,9 @@ func NewDeepWaterTile() *Tile { BlocksSight: false, Explored: false, MustDraw: true, //fixme debug - Colordance: true, Appearance: &Appearance{ Glyph: &PlainGlyphHolder{" "}, - ColorSet: &TileColorSet{ + ColorSet: TileColorSet{ Fg: &PlainColorHolder{255, 220, 220, 250}, Bg: &DanceColorHolder{ 255, diff --git a/engine/items/carried.go b/engine/items/carried.go new file mode 100644 index 0000000..ef4ecad --- /dev/null +++ b/engine/items/carried.go @@ -0,0 +1,9 @@ +package items + +import "lab.zaar.be/thefish/alchemyst-go/engine/ecs" + +type Carried struct {} + +func (c *Carried) Type() string { + return ecs.CarriedComponent +} diff --git a/engine/items/useable.go b/engine/items/useable.go new file mode 100644 index 0000000..dd8efaf --- /dev/null +++ b/engine/items/useable.go @@ -0,0 +1,10 @@ +package items + +import "lab.zaar.be/thefish/alchemyst-go/engine/ecs" + +type Useable struct {} + + +func (u *Useable) Type() string { + return ecs.UsableComponent +} \ No newline at end of file diff --git a/engine/types/appearance.go b/engine/types/appearance.go index 074d5aa..f0eb440 100644 --- a/engine/types/appearance.go +++ b/engine/types/appearance.go @@ -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) } \ No newline at end of file diff --git a/util/util.go b/util/util.go index 1bb68f1..152ca31 100644 --- a/util/util.go +++ b/util/util.go @@ -1,9 +1,5 @@ package util -import ( - "reflect" -) - func IntInSlice(needle int, haystack[]int) (exists bool, index int) { exists = false index = -1 @@ -16,21 +12,21 @@ func IntInSlice(needle int, haystack[]int) (exists bool, index int) { } //left here for historical reasons -func InArray(val interface{}, array interface{}) (exists bool, index int) { - exists = false - index = -1 - - switch reflect.TypeOf(array).Kind() { - case reflect.Slice: - s := reflect.ValueOf(array) - - for i := 0; i < s.Len(); i++ { - if reflect.DeepEqual(val, s.Index(i).Interface()) == true { - index = i - exists = true - return - } - } - } - return -} \ No newline at end of file +//func InArray(val interface{}, array interface{}) (exists bool, index int) { +// exists = false +// index = -1 +// +// switch reflect.TypeOf(array).Kind() { +// case reflect.Slice: +// s := reflect.ValueOf(array) +// +// for i := 0; i < s.Len(); i++ { +// if reflect.DeepEqual(val, s.Index(i).Interface()) == true { +// index = i +// exists = true +// return +// } +// } +// } +// return +//} \ No newline at end of file