From ec9d3d9a73df0cd416d43e8540e18d5e45d46623 Mon Sep 17 00:00:00 2001 From: thefish Date: Sat, 26 Oct 2019 23:32:32 +0300 Subject: [PATCH] slight refactor, gofmt --- Makefile | 2 +- main.go => cmd/game/main.go | 67 ++++++++------- cmd/mapeditor/editor.go | 1 + engine/ecs/entity.go | 4 +- engine/fov/fov.go | 2 +- engine/gamemap/level.go | 64 ++++++++++++-- engine/gamemap/mapgen.go | 6 +- engine/gamemap/tile.go | 96 +++++++++++---------- engine/gamemap/types.go | 2 +- engine/types/ifaces.go | 9 ++ engine/types/rect.go | 47 +++++++++++ ui/keyinput.go | 4 +- ui/mainwindow/camera.go | 4 +- ui/mainwindow/layer.go | 42 ++++------ ui/mainwindow/{window.go => mainwindow.go} | 9 +- ui/mainwindow/primitives.go | 97 ---------------------- ui/mainwindow/uiwindow.go | 68 +++++++++++++++ util/config.go | 4 +- util/context.go | 2 +- util/rng.go | 12 +-- 20 files changed, 303 insertions(+), 239 deletions(-) rename main.go => cmd/game/main.go (86%) create mode 100644 cmd/mapeditor/editor.go create mode 100644 engine/types/ifaces.go create mode 100644 engine/types/rect.go rename ui/mainwindow/{window.go => mainwindow.go} (87%) delete mode 100644 ui/mainwindow/primitives.go create mode 100644 ui/mainwindow/uiwindow.go diff --git a/Makefile b/Makefile index ca1eb22..406bbfd 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ build: build.server # $(CWD)/bin/oapi-codegen --generate types,spec -o $(CWD)/api/inner/inner.gen.go $(CWD)/public/schema/api.yaml build.server: - $(GO) build -ldflags $(LDFLAGS) -o $(CWD)/bin/game $(CWD)/cmd/main.go + $(GO) build -ldflags $(LDFLAGS) -o $(CWD)/bin/game $(CWD)/cmd/game/main.go test: $(GO) test -v $(go list ./... | grep -v /vendor/) diff --git a/main.go b/cmd/game/main.go similarity index 86% rename from main.go rename to cmd/game/main.go index a28bfe0..6a49366 100644 --- a/main.go +++ b/cmd/game/main.go @@ -36,7 +36,6 @@ func main() { var logLevels = map[string]zerolog.Level{"debug": zerolog.DebugLevel, "info": zerolog.InfoLevel, "warn": zerolog.WarnLevel} var logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}).Level(logLevels[config.Verbosity]) - mainCtx := util.NewClientContext(config, &logger) mw := mainwindow.Init(mainCtx) @@ -78,12 +77,12 @@ func mainLoop(ctx util.ClientCtx) { menuLayer := mainwindow.AddLayer(2, "white") initRender := func() { - - baseLayer.WithColor("white").NewRect(3, 0, 17, 6).Fill() - baseLayer.WithColor("grey").NewRect(22, 0, 38, 6).Splash() - baseLayer.WithColor("blue").NewRect(3, 8, 57, 6).Fill() - menuLayer.WithColor("#99de7c26").NewRect(50, 1, 10, 10).Splash() - menuLayer.WithColor("#aa26edca").DrawWindow("Testing with long title", 0, 5, 40, 20) + //fixme!! + baseLayer.WithColor("white").NewWindow(3, 0, 17, 6).NoBorder() + baseLayer.WithColor("grey").NewWindow(22, 0, 38, 6).Splash() + baseLayer.WithColor("blue").NewWindow(3, 8, 57, 6).NoBorder() + menuLayer.WithColor("#99de7c26").NewWindow(50, 1, 10, 10).Splash() + menuLayer.WithColor("#aa26edca").NewWindow(0, 5, 40, 20).DoubleBordered("Testing with long title") blt.PrintExt(1, 6, 40, 19, 1, `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Odio morbi quis commodo odio aenean sed. Egestas sed sed risus pretium quam vulputate dignissim suspendisse in. Viverra nam libero justo laoreet sit amet cursus sit. Sed egestas egestas fringilla phasellus faucibus. Ultrices neque ornare aenean euismod elementum nisi quis eleifend. Eget lorem dolor sed viverra ipsum nunc aliquet bibendum. Egestas maecenas pharetra convallis posuere morbi. In hac habitasse platea dictumst quisque. Aenean vel elit scelerisque mauris pellentesque pulvinar. Enim diam vulputate ut pharetra sit amet. Vestibulum mattis ullamcorper velit sed. Ultrices dui sapien eget mi. Phasellus vestibulum lorem sed risus ultricies tristique. Dignissim sodales ut eu sem integer vitae. Donec massa sapien faucibus et molestie ac. Turpis nunc eget lorem dolor sed viverra ipsum. Lectus urna duis convallis convallis tellus id interdum velit. Neque vitae tempus quam pellentesque nec nam aliquam. Ornare suspendisse sed nisi lacus sed viverra tellus in hac. Orci a scelerisque purus semper eget duis at tellus. Est ullamcorper eget nulla facilisi etiam dignissim diam. Ipsum dolor sit amet consectetur adipiscing elit. Sed egestas egestas fringilla phasellus. Eget velit aliquet sagittis id consectetur purus ut faucibus pulvinar. Malesuada fames ac turpis egestas maecenas pharetra convallis posuere morbi. Pretium quam vulputate dignissim suspendisse in est. Sit amet tellus cras adipiscing. Viverra accumsan in nisl nisi scelerisque. @@ -95,8 +94,8 @@ Cursus in hac habitasse platea. Aliquet risus feugiat in ante metus dictum. Maec Sed euismod nisi porta lorem mollis aliquam ut porttitor leo. Ut tellus elementum sagittis vitae et leo duis ut diam. Elementum curabitur vitae nunc sed velit dignissim. Auctor elit sed vulputate mi sit. Consectetur adipiscing elit ut aliquam purus. Feugiat vivamus at augue eget arcu. Duis ut diam quam nulla porttitor massa id neque. Pharetra magna ac placerat vestibulum lectus mauris ultrices. Non sodales neque sodales ut etiam. Massa ultricies mi quis hendrerit dolor. Est sit amet facilisis magna etiam. Ornare suspendisse sed nisi lacus sed viverra tellus in. `) - bgLayer.WithColor("#77cfcfcf").NewRect(45, 5, 40, 40).Splash() - menuLayer.WithColor("#aaed26ca").DrawWindow("Transparent BG window test", 45, 5, 40, 40) + bgLayer.WithColor("#77cfcfcf").NewWindow(45, 5, 40, 40).Splash() + menuLayer.WithColor("#aaed26ca").NewWindow(45, 5, 40, 40).DoubleBordered("Transparent BG window test") blt.PrintExt(46, 6, 40, 39, 1, `Lorem mollis aliquam ut porttitor leo a diam sollicitudin tempor. Convallis tellus id interdum velit. Enim nunc faucibus a pellentesque. Tincidunt augue interdum velit euismod in pellentesque massa placerat duis. Leo duis ut diam quam nulla porttitor massa id. Eu feugiat pretium nibh ipsum consequat nisl. Eget est lorem ipsum dolor sit amet. Et sollicitudin ac orci phasellus egestas. Donec adipiscing tristique risus nec. Et molestie ac feugiat sed. Ante in nibh mauris cursus mattis molestie a iaculis at. Neque laoreet suspendisse interdum consectetur. Vitae et leo duis ut diam quam nulla. Sed ullamcorper morbi tincidunt ornare massa eget egestas purus viverra. Ornare lectus sit amet est placerat in egestas erat. Cursus in hac habitasse platea. Aliquet risus feugiat in ante metus dictum. Maecenas sed enim ut sem viverra aliquet eget sit. [color=orange]Vitae[/color] aliquet nec ullamcorper sit amet risus nullam. Scelerisque fermentum dui faucibus in ornare quam viverra orci sagittis. Sed tempus urna et pharetra pharetra massa massa ultricies. Est ultricies integer quis auctor. Volutpat est velit egestas dui id ornare arcu. Eget nunc lobortis mattis aliquam faucibus purus. Erat nam at lectus urna duis. @@ -115,30 +114,30 @@ Sed euismod nisi porta lorem mollis aliquam ut porttitor leo. Ut tellus elementu for { //fresh inputs to chan State.Do(func() { - var key, keycode = ui.ReadKey(ctx) - if keycode == blt.TK_CLOSE { - ctx.Logger().Warn().Msg("exiting on window close...") - State.exit <- struct{}{} - ctx.Logger().Warn().Msg("...done") - return - } - switch key { - case "F10": - blt.Close() - blt.Open() - blt.Set("window: size=100x47; font: ./resources/fonts-ttf/UbuntuMono-R.ttf, size=11;") - case "Ctrl+q": - fallthrough - case "Escape": - ctx.Logger().Info().Msg("exiting on quit command...") - State.exit <- struct{}{} - ctx.Logger().Info().Msg("...done") - return - default: - //blt.Print(1, 3, "Key: "+key) - State.input <- key - //baseLayer.Print(1, 4, "█") - } + var key, keycode = ui.ReadKey(ctx) + if keycode == blt.TK_CLOSE { + ctx.Logger().Warn().Msg("exiting on window close...") + State.exit <- struct{}{} + ctx.Logger().Warn().Msg("...done") + return + } + switch key { + case "F10": + blt.Close() + blt.Open() + blt.Set("window: size=100x47; font: ./resources/fonts-ttf/UbuntuMono-R.ttf, size=11;") + case "Ctrl+q": + fallthrough + case "Escape": + ctx.Logger().Info().Msg("exiting on quit command...") + State.exit <- struct{}{} + ctx.Logger().Info().Msg("...done") + return + default: + //blt.Print(1, 3, "Key: "+key) + State.input <- key + //baseLayer.Print(1, 4, "█") + } }) //read inputs, write to screen State.Do(func() { @@ -150,7 +149,7 @@ Sed euismod nisi porta lorem mollis aliquam ut porttitor leo. Ut tellus elementu } }) //update screen - State.Do(func(){ + State.Do(func() { blt.Refresh() }) } diff --git a/cmd/mapeditor/editor.go b/cmd/mapeditor/editor.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/cmd/mapeditor/editor.go @@ -0,0 +1 @@ +package main diff --git a/engine/ecs/entity.go b/engine/ecs/entity.go index 627d608..636470c 100644 --- a/engine/ecs/entity.go +++ b/engine/ecs/entity.go @@ -1,5 +1,3 @@ package ecs -type Entity struct { - ID int -} +type Entity int diff --git a/engine/fov/fov.go b/engine/fov/fov.go index c19616c..6fe8e03 100644 --- a/engine/fov/fov.go +++ b/engine/fov/fov.go @@ -85,4 +85,4 @@ func (f *FieldOfVision) RayCast(playerX, playerY int, gameMap *gamemap.Map) { func Round(f float64) float64 { return math.Floor(f + .5) -} \ No newline at end of file +} diff --git a/engine/gamemap/level.go b/engine/gamemap/level.go index d70beb8..9af8793 100644 --- a/engine/gamemap/level.go +++ b/engine/gamemap/level.go @@ -1,14 +1,60 @@ package gamemap -import "lab.zaar.be/thefish/alchemyst-go/engine/ecs" +import ( + "lab.zaar.be/thefish/alchemyst-go/engine/ecs" + "lab.zaar.be/thefish/alchemyst-go/util" +) + +var maxrooms = 100 +var mapWidth = 150 +var mapHeight = 100 type Level struct { - Name string - Branch string - Depth int + Name string + Branch string + Depth int MaxRooms int - Width int - Height int - Objects []ecs.Entity - Tiles [][]*Tile -} \ No newline at end of file + Width int + Height int + Objects []ecs.Entity + Tiles [][]*Tile +} + +func NewLevel(ctx util.ClientCtx, branch string, depth int) *Level { + l := &Level{ + Name: branch + string(depth), + Depth: depth, + MaxRooms: maxrooms, + Width: mapWidth, + Height: mapHeight, + } + + l.Tiles = make([][]*Tile, l.Width) + for i := range l.Tiles { + l.Tiles[i] = make([]*Tile, l.Height) + } + + return l +} + +func Generate(l *Level) (l *Level) { + + for i := 0; i < l.MaxRooms; i++ { + + } + + return l +} + +type Room struct { + x, y, w, h int +} + +func (self *Room) intersects(other *Room) bool { + if self.x <= (other.x+other.w) && + (self.x+self.w) >= other.x && + self.y <= (other.y+other.h) && + (self.y+self.h) >= other.y { + + } +} diff --git a/engine/gamemap/mapgen.go b/engine/gamemap/mapgen.go index ea69cf9..964b93f 100644 --- a/engine/gamemap/mapgen.go +++ b/engine/gamemap/mapgen.go @@ -4,12 +4,12 @@ type mapGen interface { generate(l *Level) *Level } -type defaultGen struct {} +type defaultGen struct{} func (d defaultGen) generate(l *Level) *Level { l.Tiles, rooms = addRooms(l) - l. Tiles = connectRooms(rooms) + l.Tiles = connectRooms(rooms) l.Objects = populate(rooms) return l @@ -17,4 +17,4 @@ func (d defaultGen) generate(l *Level) *Level { func addRooms(l *Level) { -} \ No newline at end of file +} diff --git a/engine/gamemap/tile.go b/engine/gamemap/tile.go index ce76a3b..fccc3c6 100644 --- a/engine/gamemap/tile.go +++ b/engine/gamemap/tile.go @@ -10,15 +10,15 @@ type ColorHolder struct { } type TileColorSet struct { - Fg func() uint32 - Bg func() uint32 - DarkFg func() uint32 - DarkBg func() uint32 + Fg func() uint32 + Bg func() uint32 + DarkFg func() uint32 + DarkBg func() uint32 current *ColorHolder } type Appearance struct { - Char string + Char string ColorSet *TileColorSet } @@ -34,30 +34,29 @@ func colordance(colorValue uint8, minGlow, maxGlow, step int) uint8 { type Tile struct { *Appearance - Name string + Name string Description string - BlocksPass bool + BlocksPass bool BlocksSight bool - Explored bool - MustDraw bool + Explored bool + MustDraw bool } - func NewWall() *Tile { return &Tile{ - Name: "Wall", + Name: "Wall", Description: "A dull rock wall", - BlocksPass: true, + BlocksPass: true, BlocksSight: true, - Explored: false, - MustDraw: false, + Explored: false, + MustDraw: false, Appearance: &Appearance{ Char: "#", ColorSet: &TileColorSet{ - Fg: func() uint32 {return blt.ColorFromARGB(255, 130,110,150)}, - Bg: func() uint32 {return blt.ColorFromARGB(255, 172,170,173)}, - DarkFg: func() uint32 {return blt.ColorFromARGB(255, 20,20,68)}, - DarkBg: func() uint32 {return blt.ColorFromARGB(255, 7,7,30)}, + Fg: func() uint32 { return blt.ColorFromARGB(255, 130, 110, 150) }, + Bg: func() uint32 { return blt.ColorFromARGB(255, 172, 170, 173) }, + DarkFg: func() uint32 { return blt.ColorFromARGB(255, 20, 20, 68) }, + DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, }, }, } @@ -65,79 +64,78 @@ func NewWall() *Tile { func NewFloor() *Tile { return &Tile{ - Name: "Floor", + Name: "Floor", Description: "Dusty rock floor", - BlocksPass: false, + BlocksPass: false, BlocksSight: false, - Explored: false, - MustDraw: false, + Explored: false, + MustDraw: false, Appearance: &Appearance{ Char: ".", ColorSet: &TileColorSet{ - Fg: func() uint32 {return blt.ColorFromARGB(255, 220,220,250)}, - Bg: func() uint32 {return blt.ColorFromARGB(255, 19,19,70)}, - DarkFg: func() uint32 {return blt.ColorFromARGB(255, 30,20,50)}, - DarkBg: func() uint32 {return blt.ColorFromARGB(255, 7,7,30)}, + Fg: func() uint32 { return blt.ColorFromARGB(255, 220, 220, 250) }, + Bg: func() uint32 { return blt.ColorFromARGB(255, 19, 19, 70) }, + DarkFg: func() uint32 { return blt.ColorFromARGB(255, 30, 20, 50) }, + DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, }, }, } } func NewWaterTile() *Tile { - ch := &ColorHolder{19,19,70} - return &Tile { - Name: "Water", + ch := &ColorHolder{19, 19, 70} + return &Tile{ + Name: "Water", Description: "Murky water", - BlocksPass: false, + BlocksPass: false, BlocksSight: false, - Explored: false, - MustDraw: true, //fixme debug + Explored: false, + MustDraw: true, //fixme debug Appearance: &Appearance{ Char: ".", ColorSet: &TileColorSet{ current: ch, - Fg: func() uint32 {return blt.ColorFromARGB(255, 220,220,250)}, + Fg: func() uint32 { return blt.ColorFromARGB(255, 220, 220, 250) }, Bg: func() uint32 { return blt.ColorFromARGB( 255, ch.R, - colordance(ch.G, 2, 42, 4 ), - colordance(ch.B, 180,229,12), + colordance(ch.G, 2, 42, 4), + colordance(ch.B, 180, 229, 12), ) }, - DarkFg: func() uint32 {return blt.ColorFromARGB(255, 30,20,50)}, - DarkBg: func() uint32 {return blt.ColorFromARGB(255, 7,7,30)}, + DarkFg: func() uint32 { return blt.ColorFromARGB(255, 30, 20, 50) }, + DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, }, }, } } func NewDeepWaterTile() *Tile { - ch := &ColorHolder{5,2,154} - return &Tile { - Name: "Deep Water", + ch := &ColorHolder{5, 2, 154} + return &Tile{ + Name: "Deep Water", Description: "Deep water", - BlocksPass: false, + BlocksPass: false, BlocksSight: false, - Explored: false, - MustDraw: true, //fixme debug + Explored: false, + MustDraw: true, //fixme debug Appearance: &Appearance{ Char: " ", ColorSet: &TileColorSet{ current: ch, - Fg: func() uint32 {return blt.ColorFromARGB(255, 220,220,250)}, + Fg: func() uint32 { return blt.ColorFromARGB(255, 220, 220, 250) }, Bg: func() uint32 { return blt.ColorFromARGB( 255, ch.R, - colordance(ch.G, 0, 15, 2 ), - colordance(ch.B, 120,180,5), + colordance(ch.G, 0, 15, 2), + colordance(ch.B, 120, 180, 5), ) }, - DarkFg: func() uint32 {return blt.ColorFromARGB(255, 30,20,50)}, - DarkBg: func() uint32 {return blt.ColorFromARGB(255, 7,7,30)}, + DarkFg: func() uint32 { return blt.ColorFromARGB(255, 30, 20, 50) }, + DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, }, }, } } - diff --git a/engine/gamemap/types.go b/engine/gamemap/types.go index db2fe44..816bf62 100644 --- a/engine/gamemap/types.go +++ b/engine/gamemap/types.go @@ -1,5 +1,5 @@ package gamemap type Coords struct { - x,y int + x, y int } diff --git a/engine/types/ifaces.go b/engine/types/ifaces.go new file mode 100644 index 0000000..6b56230 --- /dev/null +++ b/engine/types/ifaces.go @@ -0,0 +1,9 @@ +package types + +type Renderable interface { + Render() +} + +type Putable interface { + Put(x, y int, symbol interface{}) +} diff --git a/engine/types/rect.go b/engine/types/rect.go new file mode 100644 index 0000000..5bb0917 --- /dev/null +++ b/engine/types/rect.go @@ -0,0 +1,47 @@ +package types + +type Rect struct { + X, Y, W, H int +} + +type RectFill struct { + Top, Bottom, Left, Right, TopLeft, TopRight, BottomLeft, BottomRight, Body interface{} +} + +func NewRect(x, y, w, h int) *Rect { + return &Rect{x, y, w, h} +} + +func (r *Rect) RenderToLayer(fillage RectFill, layer Putable) { + + if fillage.Body != "" { + for i := r.X + 1; i < r.X+r.W; i++ { + for j := r.Y + 1; j < r.Y+r.H; j++ { + layer.Put(i, j, fillage.Body) + //lii.Put(i, j, "X"); + } + } + } + + for i := r.X + 1; i < r.X+r.W; i++ { + layer.Put(i, r.Y, fillage.Top) + //lii.Put(i, Y-1, "Q"); + layer.Put(i, r.Y+r.H, fillage.Bottom) + //lii.Put(i, Y+H, "H"); + } + + for j := r.Y + 1; j < r.Y+r.H; j++ { + layer.Put(r.X, j, fillage.Left) + //lii.Put(X-1, j, "U"); + layer.Put(r.X+r.W, j, fillage.Right) + //lii.Put(X+W, j, "M"); + } + + layer.Put(r.X, r.Y, fillage.TopLeft) + //lii.Put(X-1, Y-1, "T"); + layer.Put(r.X, r.Y+r.H, fillage.BottomLeft) + //lii.Put(X-1, Y+H, "q"); + layer.Put(r.X+r.W, r.Y, fillage.TopRight) + //lii.Put(X+W, Y-1, "L"); + layer.Put(r.X+r.W, r.Y+r.H, fillage.BottomRight) +} diff --git a/ui/keyinput.go b/ui/keyinput.go index 245e6c1..5bb1138 100644 --- a/ui/keyinput.go +++ b/ui/keyinput.go @@ -1,8 +1,8 @@ package ui import ( - "lab.zaar.be/thefish/alchemyst-go/util" - blt "lab.zaar.be/thefish/bearlibterminal" + "lab.zaar.be/thefish/alchemyst-go/util" + blt "lab.zaar.be/thefish/bearlibterminal" ) var modifiers = []int{blt.TK_SHIFT, blt.TK_ALT, blt.TK_CONTROL} diff --git a/ui/mainwindow/camera.go b/ui/mainwindow/camera.go index d23a32f..a6d816f 100644 --- a/ui/mainwindow/camera.go +++ b/ui/mainwindow/camera.go @@ -20,11 +20,11 @@ func (c *GameCamera) MoveCamera(targetX int, targetY int, mapWidth int, mapHeigh y = 0 } - if x > mapWidth - c.Width { + if x > mapWidth-c.Width { x = mapWidth - c.Width } - if y > mapHeight - c.Height { + if y > mapHeight-c.Height { y = mapHeight - c.Height } diff --git a/ui/mainwindow/layer.go b/ui/mainwindow/layer.go index 2575145..21873da 100644 --- a/ui/mainwindow/layer.go +++ b/ui/mainwindow/layer.go @@ -1,14 +1,11 @@ package mainwindow -import blt "lab.zaar.be/thefish/bearlibterminal" - -type LayerInterface interface { - Render() - Put(x, y int, symbol rune, color string) -} +import ( + blt "lab.zaar.be/thefish/bearlibterminal" +) type Layer struct { - idx int + idx int defaultColor uint32 } @@ -35,31 +32,28 @@ func (layer *Layer) after() *Layer { return layer } -func (layer Layer) Put(x,y int, symbol string) { - rnes := []rune(symbol) +func (layer Layer) Put(x, y int, symbol interface{}) { + if symbol == nil { + return + } + rnes := []rune(symbol.(string)) if (len(rnes)) > 0 { blt.Put(x, y, int(rnes[0])) } } -func (layer Layer) Print(x,y int, txt string) (w,h int) { - return blt.Print(x,y, txt) +func (layer Layer) Print(x, y int, txt string) (w, h int) { + return blt.Print(x, y, txt) } - -func (layer Layer) DrawWindow (title string, x, y, w, h int) { - if len(title) > (w -2) { - title = title[:(w-2)] - } - layer.NewRect(x,y,w,h).DrawBorder() - centerX := x + (w / 2) - layer.Print(centerX - (len(title) / 2) - 1, y, "╡" + title + "╞") -}; - -func (layer *Layer) Decorate (f func (args ...interface{})) func (args ...interface{}) { - return func (args ...interface{}) { +func (layer *Layer) Decorate(f func(args ...interface{})) func(args ...interface{}) { + return func(args ...interface{}) { layer.before() f(args) layer.after() } -} \ No newline at end of file +} + +func (layer *Layer) Render() { + +} diff --git a/ui/mainwindow/window.go b/ui/mainwindow/mainwindow.go similarity index 87% rename from ui/mainwindow/window.go rename to ui/mainwindow/mainwindow.go index 34f6582..744b862 100644 --- a/ui/mainwindow/window.go +++ b/ui/mainwindow/mainwindow.go @@ -2,13 +2,14 @@ package mainwindow import ( "fmt" + "lab.zaar.be/thefish/alchemyst-go/engine/types" "lab.zaar.be/thefish/alchemyst-go/util" blt "lab.zaar.be/thefish/bearlibterminal" ) type MainWindow struct { - ctx util.ClientCtx - layers []LayerInterface + ctx util.ClientCtx + layers []types.Renderable } func Init(ctx util.ClientCtx) *MainWindow { @@ -47,6 +48,6 @@ func (mw *MainWindow) Render() { } } -func (mw *MainWindow) AddLayer(li LayerInterface) { +func (mw *MainWindow) AddLayer(li types.Renderable) { mw.layers = append(mw.layers, li) -} \ No newline at end of file +} diff --git a/ui/mainwindow/primitives.go b/ui/mainwindow/primitives.go deleted file mode 100644 index c3b8699..0000000 --- a/ui/mainwindow/primitives.go +++ /dev/null @@ -1,97 +0,0 @@ -package mainwindow - -type Rect struct { - x,y,w,h int - layer *Layer -} - -type rectFill struct { - top, bottom, left, right, topLeft, topRight, bottomLeft, bottomRight, body string -} - -func (layer *Layer) NewRect(x,y,w,h int) *Rect { - return &Rect{x,y,w,h, layer} -} - -var noborder = rectFill{ - top: "▄", - bottom: "▀", - left: "▐", - right: "▌", - topLeft: "▗", - topRight: "▖", - bottomLeft: "▝", - bottomRight: "▘", - body: "█", -} - -var splash = rectFill{ - top: "█", - bottom: "█", - left: "█", - right: "█", - topLeft: "█", - topRight: "█", - bottomLeft: "█", - bottomRight: "█", - body: "█", -} - -var doubleBorder = rectFill { - top: "═", - bottom: "═", - left: "║", - right: "║", - topLeft: "╔", - topRight: "╗", - bottomLeft: "╚", - bottomRight: "╝", -} - -func (r *Rect) Fill() { - r.render(noborder) -} - -func (r *Rect) Splash() { - r.render(splash) -} - -func (r *Rect) DrawBorder() { - r.render(doubleBorder) -} - -func (r *Rect) render (fillage rectFill) { - - if fillage.body != "" { - for i := r.x + 1; i < r.x+r.w; i++ { - for j := r.y + 1; j < r.y+r.h; j++ { - r.layer.Put(i, j, fillage.body); - //lii.Put(i, j, "X"); - } - } - } - - for i := r.x + 1; i < r.x+r.w; i++ { - r.layer.Put(i, r.y, fillage.top); - //lii.Put(i, y-1, "Q"); - r.layer.Put(i, r.y+r.h, fillage.bottom); - //lii.Put(i, y+h, "H"); - } - - for j := r.y + 1; j < r.y+r.h; j++ { - r.layer.Put(r.x, j, fillage.left); - //lii.Put(x-1, j, "U"); - r.layer.Put(r.x+r.w, j, fillage.right); - //lii.Put(x+w, j, "M"); - } - - r.layer.Put(r.x, r.y, fillage.topLeft); - //lii.Put(x-1, y-1, "T"); - r.layer.Put(r.x, r.y+r.h, fillage.bottomLeft); - //lii.Put(x-1, y+h, "q"); - r.layer.Put(r.x+r.w, r.y, fillage.topRight); - //lii.Put(x+w, y-1, "L"); - r.layer.Put(r.x+r.w, r.y+r.h, fillage.bottomRight); - -}; - diff --git a/ui/mainwindow/uiwindow.go b/ui/mainwindow/uiwindow.go new file mode 100644 index 0000000..b01c6e2 --- /dev/null +++ b/ui/mainwindow/uiwindow.go @@ -0,0 +1,68 @@ +package mainwindow + +import "lab.zaar.be/thefish/alchemyst-go/engine/types" + +var noborder = types.RectFill{ + Top: "▄", + Bottom: "▀", + Left: "▐", + Right: "▌", + TopLeft: "▗", + TopRight: "▖", + BottomLeft: "▝", + BottomRight: "▘", + Body: "█", +} + +var splash = types.RectFill{ + Top: "█", + Bottom: "█", + Left: "█", + Right: "█", + TopLeft: "█", + TopRight: "█", + BottomLeft: "█", + BottomRight: "█", + Body: "█", +} + +var doubleBorder = types.RectFill{ + Top: "═", + Bottom: "═", + Left: "║", + Right: "║", + TopLeft: "╔", + TopRight: "╗", + BottomLeft: "╚", + BottomRight: "╝", +} + +type UiWindow struct { + *types.Rect + layer *Layer + fillage types.RectFill +} + +func (layer *Layer) NewWindow(x, y, w, h int) *UiWindow { + return &UiWindow{ + Rect: types.NewRect(x, y, w, h), + layer: layer, + } +} + +func (uiw *UiWindow) NoBorder() { + uiw.RenderToLayer(noborder, uiw.layer) +} + +func (uiw *UiWindow) Splash() { + uiw.RenderToLayer(splash, uiw.layer) +} + +func (uiw *UiWindow) DoubleBordered(title string) { + uiw.RenderToLayer(doubleBorder, uiw.layer) + if len(title) > (uiw.W - 2) { + title = title[:(uiw.W - 2)] + } + centerX := uiw.X + (uiw.W / 2) + uiw.layer.Print(centerX-(len(title)/2)-1, uiw.Y, "╡"+title+"╞") +} diff --git a/util/config.go b/util/config.go index ced68e3..4d6afd9 100644 --- a/util/config.go +++ b/util/config.go @@ -13,8 +13,8 @@ type Config struct { Version string `json:"version"` MainWindowSizeX int `json:"sizeX"` MainWindowSizeY int `json:"sizeY"` - Font string `json:"font"` - Verbosity string `json:"verbosity"` + Font string `json:"font"` + Verbosity string `json:"verbosity"` } func LoadConfig() *Config { diff --git a/util/context.go b/util/context.go index 1a2d19f..96efdf3 100644 --- a/util/context.go +++ b/util/context.go @@ -41,4 +41,4 @@ func (c *ClientCtx) Logger() *zerolog.Logger { //func FromContext(ctx context.Context) (*User, bool) { // u, ok := ctx.Value(userKey).(*User) // return u, ok -//} \ No newline at end of file +//} diff --git a/util/rng.go b/util/rng.go index a37b65a..cf9c2ac 100644 --- a/util/rng.go +++ b/util/rng.go @@ -34,7 +34,7 @@ func (rng *RNG) Uniform() float64 { } func (rng *RNG) UniformRange(a, b float64) float64 { - return a + rng.Uniform() * (b - a) + return a + rng.Uniform()*(b-a) } func (rng *RNG) Normal(mean, stddev float64) float64 { @@ -47,9 +47,9 @@ func (rng *RNG) Normal(mean, stddev float64) float64 { r = x*x + y*y } - result := x * math.Sqrt(-2 * math.Log(r) / r) + result := x * math.Sqrt(-2*math.Log(r)/r) - return mean + stddev * result + return mean + stddev*result } @@ -61,7 +61,7 @@ func (rng *RNG) Range(min, max int) int { if min == max { return min } else { - return rng.rand.Intn(max - min) + min + return rng.rand.Intn(max-min) + min } } @@ -69,7 +69,7 @@ func (rng *RNG) RangeNegative(min, max int) int { if min == max { return min } else { - return rng.rand.Intn(max - min + 1) + min + return rng.rand.Intn(max-min+1) + min } } @@ -91,4 +91,4 @@ func (rng *RNG) GetWeightedEntity(values map[int]int) int { } return -1 -} \ No newline at end of file +}