2019-11-07 13:30:37 +03:00

85 lines
2.2 KiB
Go

package screens
import (
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"lab.zaar.be/thefish/alchemyst-go/engine/ecs/systems"
"lab.zaar.be/thefish/alchemyst-go/engine/gamestate"
"lab.zaar.be/thefish/alchemyst-go/engine/mob/movement"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
)
type GameScreen struct {
mw *mainwindow.MainWindow
state *gamestate.GameState
vp *mainwindow.ViewPort
controller *ecs.Controller
}
func NewGameScreen(mw *mainwindow.MainWindow, state *gamestate.GameState, viewPort *mainwindow.ViewPort, controller *ecs.Controller) *GameScreen {
return &GameScreen{mw: mw, state: state, vp: viewPort, controller: controller}
}
func (ts *GameScreen) UseEcs() bool { return true }
func (ts *GameScreen) Enter() {
renderMobs := systems.MobRenderSystem{EntityController: ts.controller}
ts.controller.AddSystem(renderMobs, 1)
}
func (ts *GameScreen) Exit() {
//remove what we dont need
}
func (ts *GameScreen) HandleInput(input string) {
//ts.state.Do(func(){
switch input {
case "Up", "k", "8":
ts.walk(ts.state, 0, -1)
break
case "Down", "j", "2":
ts.walk(ts.state, 0, 1)
break
case "Left", "h", "4":
ts.walk(ts.state, -1, 0)
break
case "Right", "l", "6":
ts.walk(ts.state, 1, 0)
break
case "y", "7":
ts.walk(ts.state, -1, -1)
break
case "u", "9":
ts.walk(ts.state, 1, -1)
break
case "b", "1":
ts.walk(ts.state, -1, 1)
break
case "n", "3":
ts.walk(ts.state, 1, 1)
break
default:
ts.mw.GetLayer("base").ClearArea(0, 3, 40, 1)
ts.mw.GetLayer("base").Print(1, 3, "Key: "+input)
ts.mw.GetLayer("base").Print(1, 6, "█")
}
//})
}
func (ts *GameScreen) Render() {
ts.vp.Render(ts.state)
}
func (ts *GameScreen) walk(state *gamestate.GameState, dx, dy int) {
controller := state.Controller
coords := controller.GetComponent(state.Player, types.Coords{}.TypeOf()).(types.Coords)
newCoords := types.Coords{coords.X + dx, coords.Y + dy}
movable := controller.GetComponent(state.Player, movement.Moveable{}.TypeOf()).(movement.Moveable)
if !movable.IsBlocked(newCoords) {
controller.UpdateComponent(state.Player, types.Coords{}.TypeOf(), newCoords)
}
state.Redraw <- struct{}{}
state.FovRecompute <- struct{}{}
}