From 529f5a57492eb9c9250edc6d4913bcfbb95a996f Mon Sep 17 00:00:00 2001 From: thefish Date: Fri, 1 Nov 2019 18:21:27 +0300 Subject: [PATCH] reorganize --- cmd/game/main.go | 11 +++++--- engine/{types => gamestate}/gamestate.go | 10 +++++-- engine/mob/mob.go | 21 +++++++++++++++ engine/{types => mob}/player.go | 2 +- engine/screens/game.go | 8 +++--- engine/types/mob.go | 19 ------------- ui/mainwindow/viewport.go | 34 +++++++++++------------- 7 files changed, 56 insertions(+), 49 deletions(-) rename engine/{types => gamestate}/gamestate.go (65%) create mode 100644 engine/mob/mob.go rename engine/{types => mob}/player.go (67%) delete mode 100644 engine/types/mob.go diff --git a/cmd/game/main.go b/cmd/game/main.go index 4169c63..d9d32b9 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -5,6 +5,8 @@ import ( "github.com/rs/zerolog/log" "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" "lab.zaar.be/thefish/alchemyst-go/engine/gamemap/mapgens" + "lab.zaar.be/thefish/alchemyst-go/engine/gamestate" + "lab.zaar.be/thefish/alchemyst-go/engine/mob" "lab.zaar.be/thefish/alchemyst-go/engine/screens" "lab.zaar.be/thefish/alchemyst-go/engine/types" "lab.zaar.be/thefish/alchemyst-go/ui" @@ -37,7 +39,7 @@ func init() { // ... // } -var State = types.GameState{ +var State = gamestate.GameState{ Mainfunc: make(chan func()), Exit: make(chan struct{}, 1), Input: make(chan string, 1), @@ -61,7 +63,8 @@ func main() { //fixme level, rooms := mapgens.DefaultGen(gamemap.NewLevel(mainCtx, "test", 1)) - vp := mainwindow.NewViewPort(40, 0, 60, 47, level, mw.GetLayer("base")) + State.Level = level + vp := mainwindow.NewViewPort(40, 0, 60, 47, mw.GetLayer("base")) screenMgr := types.NewScreenManager(mainCtx) @@ -72,8 +75,8 @@ func main() { //fixme - player := &types.Player{ - Mob: types.Mob{ + player := &mob.Player{ + Mob: mob.Mob{ Appearance: &types.Appearance{ Glyph: &types.PlainGlyphHolder{"@"}, ColorSet: &types.TileColorSet{ diff --git a/engine/types/gamestate.go b/engine/gamestate/gamestate.go similarity index 65% rename from engine/types/gamestate.go rename to engine/gamestate/gamestate.go index 5478b65..d20b9ae 100644 --- a/engine/types/gamestate.go +++ b/engine/gamestate/gamestate.go @@ -1,4 +1,9 @@ -package types +package gamestate + +import ( + "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" + "lab.zaar.be/thefish/alchemyst-go/engine/mob" +) type GameState struct { Mainfunc chan func() @@ -7,7 +12,8 @@ type GameState struct { RawInput chan int FovRecompute chan struct{} Redraw chan struct{} - Player *Player + Level *gamemap.Level + Player *mob.Player } // do runs f on the main thread. diff --git a/engine/mob/mob.go b/engine/mob/mob.go new file mode 100644 index 0000000..21eef57 --- /dev/null +++ b/engine/mob/mob.go @@ -0,0 +1,21 @@ +package mob + +import "lab.zaar.be/thefish/alchemyst-go/engine/types" + +type Mob struct { + *types.Appearance + types.Coords + BlocksPass bool +} + +func (m *Mob) Walk(dx, dy int) { + +} + +func (m *Mob) Render() { + +} + +func (m *Mob) MoveToCoords(c types.Coords) { + +} \ No newline at end of file diff --git a/engine/types/player.go b/engine/mob/player.go similarity index 67% rename from engine/types/player.go rename to engine/mob/player.go index 052ec2a..c1b8920 100644 --- a/engine/types/player.go +++ b/engine/mob/player.go @@ -1,4 +1,4 @@ -package types +package mob type Player struct { Mob diff --git a/engine/screens/game.go b/engine/screens/game.go index 2740ff9..9a32e80 100644 --- a/engine/screens/game.go +++ b/engine/screens/game.go @@ -1,17 +1,17 @@ package screens import ( - "lab.zaar.be/thefish/alchemyst-go/engine/types" + "lab.zaar.be/thefish/alchemyst-go/engine/gamestate" "lab.zaar.be/thefish/alchemyst-go/ui/mainwindow" ) type GameScreen struct { mw *mainwindow.MainWindow - state *types.GameState + state *gamestate.GameState vp *mainwindow.ViewPort } -func NewGameScreen(mw *mainwindow.MainWindow, state *types.GameState, viewPort *mainwindow.ViewPort) *GameScreen { +func NewGameScreen(mw *mainwindow.MainWindow, state *gamestate.GameState, viewPort *mainwindow.ViewPort) *GameScreen { return &GameScreen{mw: mw, state: state, vp: viewPort} } @@ -44,8 +44,6 @@ func (ts *GameScreen) HandleInput(input string) { case "n", "3": ts.state.Player.Walk(-1, 3) break - - default: ts.mw.GetLayer("base").ClearArea(0, 3, 40, 1) ts.mw.GetLayer("base").Print(1, 3, "Key: "+input) diff --git a/engine/types/mob.go b/engine/types/mob.go deleted file mode 100644 index 67b7fd5..0000000 --- a/engine/types/mob.go +++ /dev/null @@ -1,19 +0,0 @@ -package types - -type Mob struct { - *Appearance - Coords - BlocksPass bool -} - -func (m *Mob) Walk(dx, dy int) { - -} - -func (m *Mob) Render() { - -} - -func (m *Mob) MoveToCoords(c Coords) { - -} \ No newline at end of file diff --git a/ui/mainwindow/viewport.go b/ui/mainwindow/viewport.go index 585ab04..ec15208 100644 --- a/ui/mainwindow/viewport.go +++ b/ui/mainwindow/viewport.go @@ -5,7 +5,7 @@ import ( "fmt" "lab.zaar.be/thefish/alchemyst-go/engine/fov" "lab.zaar.be/thefish/alchemyst-go/engine/fov/precomputed_shade" - "lab.zaar.be/thefish/alchemyst-go/engine/gamemap" + "lab.zaar.be/thefish/alchemyst-go/engine/gamestate" "lab.zaar.be/thefish/alchemyst-go/engine/types" "time" ) @@ -15,7 +15,6 @@ var NotInViewError = errors.New("not in ViewPort") type ViewPort struct { *types.Rect cameraCoords types.Coords - level *gamemap.Level layer *Layer Fov fov.Fov PlayerCoords types.Coords @@ -23,19 +22,18 @@ type ViewPort struct { animateTiles *time.Ticker } -func NewViewPort(x, y, w, h int, level *gamemap.Level, layer *Layer) *ViewPort { - //fixme +func NewViewPort(x, y, w, h int, layer *Layer) *ViewPort { + computedFov := precomputed_shade.NewPrecomputedShade(15) computedFov.Init() vp := ViewPort{ Rect: &types.Rect{x, y, w, h}, - level: level, layer: layer, Fov: computedFov, } - vp.PlayerTorchRadius = 14 - vp.animateTiles = time.NewTicker(time.Second / 10) + vp.PlayerTorchRadius = 9 + vp.animateTiles = time.NewTicker(time.Second / 12) return &vp } @@ -45,10 +43,10 @@ func (vp *ViewPort) Close() { vp.animateTiles = nil //free pointer to ticker } -func (vp *ViewPort) Move(state *types.GameState) { +func (vp *ViewPort) Move(state *gamestate.GameState) { c := &state.Player.Coords - + x := c.X - vp.Rect.W/2 y := c.Y - vp.Rect.H/2 @@ -58,11 +56,11 @@ func (vp *ViewPort) Move(state *types.GameState) { if y < 0 { y = 0 } - if x > vp.level.W-vp.W { - x = vp.level.W - vp.W - 1 + if x > state.Level.W-vp.W { + x = state.Level.W - vp.W - 1 } - if y > vp.level.H-vp.H { - x = vp.level.H - vp.H - 1 + if y > state.Level.H-vp.H { + x = state.Level.H - vp.H - 1 } if x != vp.cameraCoords.X || y != vp.cameraCoords.Y { state.FovRecompute <- struct{}{} @@ -84,7 +82,7 @@ func (vp *ViewPort) ToVPCoords(c types.Coords) (newCoords types.Coords, err erro var redraw = true var fovRecompute = true -func (vp *ViewPort) Listen(state types.GameState) { +func (vp *ViewPort) Listen(state gamestate.GameState) { for { select { case <-state.FovRecompute: @@ -97,7 +95,7 @@ func (vp *ViewPort) Listen(state types.GameState) { } } -func (vp *ViewPort) Render(state *types.GameState) { +func (vp *ViewPort) Render(state *gamestate.GameState) { vp.Move(state) @@ -105,7 +103,7 @@ func (vp *ViewPort) Render(state *types.GameState) { vp.layer.ClearRect(vp.Rect) fovRecompute = false redraw = true - vp.Fov.ComputeFov(vp.level, state.Player.Coords, vp.PlayerTorchRadius) + vp.Fov.ComputeFov(state.Level, state.Player.Coords, vp.PlayerTorchRadius) } if redraw { @@ -114,8 +112,8 @@ func (vp *ViewPort) Render(state *types.GameState) { for x := 0; x < vp.W; x++ { mapCoords := types.Coords{vp.cameraCoords.X + x, vp.cameraCoords.Y + y} - if vp.level.InBounds(mapCoords) { - tile := vp.level.GetTile(mapCoords) + if state.Level.InBounds(mapCoords) { + tile := state.Level.GetTile(mapCoords) if tile.Explored || tile.MustDraw || tile.Visible { vp.layer.PutToBase(x+vp.X, y+vp.Y, tile.GetChar(), tile.GetRawColor(), tile.GetRawBgColor()) }