resolve conflicts

This commit is contained in:
anton.gurov
2019-11-05 12:42:05 +03:00
33 changed files with 1004 additions and 360 deletions

View File

@ -3,8 +3,13 @@ package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"lab.zaar.be/thefish/alchemyst-go/engine/ecs"
"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"
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -22,23 +27,6 @@ func init() {
runtime.LockOSThread()
}
type GameState struct {
mainfunc chan func()
exit chan struct{}
input chan string
rawInput chan int
}
// do runs f on the main thread.
func (*GameState) Do(f func()) {
done := make(chan struct{}, 1)
State.mainfunc <- func() {
f()
done <- struct{}{}
}
<-done
}
//we can run logic in separate goroutines
//
// go doSometing(State,...)
@ -47,17 +35,18 @@ func (*GameState) Do(f func()) {
// func doSomething(State main.GameState, args...) {
// ...
// State.Do(func() {
// ...do stuff in main thread, ie render something
// ...do stuff in main thread
// })
// ...
// }
// Use this trick CAREFULLY, cause closures may cause memleaks
var State = GameState{
mainfunc: make(chan func()),
exit: make(chan struct{}, 1),
input: make(chan string, 1),
rawInput: make(chan int, 1),
var State = gamestate.GameState{
Mainfunc: make(chan func()),
Exit: make(chan struct{}, 1),
Input: make(chan string, 1),
RawInput: make(chan int, 1),
FovRecompute: make(chan struct{}, 1),
Redraw: make(chan struct{}, 1),
}
func main() {
@ -79,29 +68,53 @@ func main() {
vp.Render()
go decodeInput(mainCtx, mw.GetLayer("base"))
go vp.Listen(State)
controller := ecs.NewController()
controller.MapComponentClass("coords", types.Coords{})
controller.MapComponentClass("appearance", types.Appearance{})
controller.MapComponentClass("mob", mob.Mob{})
player := controller.CreateEntity([]ecs.Component{})
controller.AddComponent(player, &types.Appearance{
Glyph: &types.PlainGlyphHolder{"@"},
ColorSet: &types.TileColorSet{
Fg: &types.PlainColorHolder{255, 255, 255, 255},
},
})
controller.AddComponent(player, rooms[0].Center) //implicit Coords
render := mob.MobRenderSystem{EntityController: controller}
controller.AddSystem(render, 1)
//but every call to bearlibterminal must be wrapped to closure and passed to mainfunc
var exit = false
for !exit {
select {
case State.rawInput <- ui.ReadKeyCode():
case State.RawInput <- ui.ReadKeyCode():
break
case pressed := <-State.input:
mw.GetLayer("base").ClearArea(0, 3, 40, 1)
mw.GetLayer("base").Print(1, 3, "Key: "+pressed)
mw.GetLayer("base").Print(1, 6, "█")
case pressed := <-State.Input:
screenMgr.CurrentScreen.HandleInput(pressed)
break
//case f := <-State.mainfunc:
// f()
// break
case <-State.exit:
//case f := <-State.mainfunc:
// f()
// break
case <-State.Exit:
mainCtx.Logger().Warn().Msg("quitting NOW")
exit = true
break
// не оставляйте default в бесконесчном select {} - сожрет всё CPU
// не оставляйте default в бесконесчном select {} - сожрет всё CPU
default:
vp.Render()
screenMgr.CurrentScreen.Render()
blt.Refresh()
}
@ -117,23 +130,21 @@ func setupLayers(mainwindow *mainwindow.MainWindow) {
func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
var exit = false
//for some reason blt's input queue gots spammed with 0xE0 on start.
//with this crutch we can wait out this WindowCloseEvent burst.
var waitForStartingWindowCloseBurst = true
for !exit{
var waitForWCspam = true
for !exit {
select {
case keycode := <-State.rawInput:
case keycode := <-State.RawInput:
if keycode == blt.TK_NONE {
continue
}
if keycode == blt.TK_CLOSE && !waitForStartingWindowCloseBurst {
if keycode == blt.TK_CLOSE && !waitForWCspam {
ctx.Logger().Warn().Msg("exiting on window close...")
State.exit <- struct{}{}
State.Exit <- struct{}{}
ctx.Logger().Warn().Msg("...done")
return
}
var pressed= ""
var isModifier, _= util.InArray(keycode, modifiers)
var pressed = ""
var isModifier, _ = util.InArray(keycode, modifiers)
if !isModifier {
pressed = ui.Scancodemap[keycode]
@ -150,6 +161,7 @@ func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
//global hotkeys
switch pressed {
//fixme testing only
case "F10":
State.Do(func() {
blt.Set("window: size=100x47; font: ./resources/fonts-ttf/UbuntuMono-R.ttf, size=11;")
@ -158,13 +170,15 @@ func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
fallthrough
case "Escape":
ctx.Logger().Info().Msg("exiting on quit command...")
State.exit <- struct{}{}
State.Exit <- struct{}{}
ctx.Logger().Info().Msg("...done")
exit = true
return
default:
waitForStartingWindowCloseBurst = false
State.input <- pressed
if pressed != "" {
waitForWCspam = false;
State.Input <- pressed
}
}
}
}