merge controller to engine

This commit is contained in:
2023-07-04 23:48:00 +02:00
parent 374f7a9acc
commit 6041c26eaa
6 changed files with 51 additions and 59 deletions

View File

@@ -2,62 +2,56 @@ package engine
import ( import (
"fmt" "fmt"
"game/konstructor/entity"
"reflect" "reflect"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil" "github.com/hajimehoshi/ebiten/inpututil"
) )
type Controller struct { func (e *Engine) WatchKeyPress() {
PressedKey ebiten.Key values := reflect.ValueOf(e.KeyMap)
KeyMap entity.KeyMap
}
func (c *Controller) Watch() {
values := reflect.ValueOf(c.KeyMap)
for i := 0; i < values.NumField(); i++ { for i := 0; i < values.NumField(); i++ {
key := values.Field(i).Interface().(ebiten.Key) key := values.Field(i).Interface().(ebiten.Key)
if inpututil.IsKeyJustPressed(key) { if inpututil.IsKeyJustPressed(key) {
fmt.Printf("Key pressed: %s\n", key) fmt.Printf("Key pressed: %s\n", key)
c.PressedKey = key e.PressedKey = key
} }
} }
} }
func (c *Controller) Clear() { func (e *Engine) ClearKeyPresed() {
c.PressedKey = Controller{}.PressedKey e.PressedKey = Engine{}.PressedKey
} }
func (c *Controller) UpPressed() bool { func (e *Engine) UpPressed() bool {
return c.PressedKey == c.KeyMap.Up return e.PressedKey == e.KeyMap.Up
} }
func (c *Controller) DownPressed() bool { func (e *Engine) DownPressed() bool {
return c.PressedKey == c.KeyMap.Down return e.PressedKey == e.KeyMap.Down
} }
func (c *Controller) RightPressed() bool { func (e *Engine) RightPressed() bool {
return c.PressedKey == c.KeyMap.Right return e.PressedKey == e.KeyMap.Right
} }
func (c *Controller) LeftPressed() bool { func (e *Engine) LeftPressed() bool {
return c.PressedKey == c.KeyMap.Left return e.PressedKey == e.KeyMap.Left
} }
func (c *Controller) Action0Pressed() bool { func (e *Engine) Action0Pressed() bool {
return c.PressedKey == c.KeyMap.Action0 return e.PressedKey == e.KeyMap.Action0
} }
func (c *Controller) Action1Pressed() bool { func (e *Engine) Action1Pressed() bool {
return c.PressedKey == c.KeyMap.Action1 return e.PressedKey == e.KeyMap.Action1
} }
func (c *Controller) Action2Pressed() bool { func (e *Engine) Action2Pressed() bool {
return c.PressedKey == c.KeyMap.Action2 return e.PressedKey == e.KeyMap.Action2
} }
func (c *Controller) Action3Pressed() bool { func (e *Engine) Action3Pressed() bool {
return c.PressedKey == c.KeyMap.Action3 return e.PressedKey == e.KeyMap.Action3
} }

View File

@@ -9,9 +9,10 @@ import (
type Engine struct { type Engine struct {
Domain entity.DomainInterface Domain entity.DomainInterface
Controller *Controller
Settings *entity.Settings Settings *entity.Settings
KContext *entity.KContext KContext *entity.KContext
KeyMap entity.KeyMap
PressedKey ebiten.Key
} }
func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) { func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {
@@ -23,7 +24,7 @@ func (e *Engine) ScreenTypeIs(name string) bool {
} }
func (e *Engine) Update(screen *ebiten.Image) error { func (e *Engine) Update(screen *ebiten.Image) error {
e.Controller.Watch() e.WatchKeyPress()
if e.ScreenTypeIs("menu") { if e.ScreenTypeIs("menu") {
e.MenuUpdate() e.MenuUpdate()
} }
@@ -46,8 +47,8 @@ func (e *Engine) Draw(screen *ebiten.Image) {
if e.ScreenTypeIs("playground") { if e.ScreenTypeIs("playground") {
e.PlaygroundDraw(screen) e.PlaygroundDraw(screen)
} }
if e.Controller.Action3Pressed() { if e.Action3Pressed() {
os.Exit(1) os.Exit(1)
} }
e.Controller.Clear() e.ClearKeyPresed()
} }

View File

@@ -19,15 +19,15 @@ func (e *Engine) DialogDraw(screen *ebiten.Image) {
func (e *Engine) DialogUpdate() { func (e *Engine) DialogUpdate() {
dialog := e.Domain.GetDialog(e.KContext.Screen.Value) dialog := e.Domain.GetDialog(e.KContext.Screen.Value)
if e.Controller.UpPressed() && dialog.CurrentSelected != 0 { if e.UpPressed() && dialog.CurrentSelected != 0 {
dialog.CurrentSelected-- dialog.CurrentSelected--
} }
if e.Controller.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 { if e.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 {
dialog.CurrentSelected++ dialog.CurrentSelected++
} }
if e.Controller.Action0Pressed() { if e.Action0Pressed() {
dialog.Choices[dialog.CurrentSelected].Handler() dialog.Choices[dialog.CurrentSelected].Handler()
} }
e.Domain.SetDialog(e.KContext.Screen.Value, dialog) e.Domain.SetDialog(e.KContext.Screen.Value, dialog)

View File

@@ -19,15 +19,15 @@ func (e *Engine) MenuDraw(screen *ebiten.Image) {
func (e *Engine) MenuUpdate() { func (e *Engine) MenuUpdate() {
menu := e.Domain.GetMenu(e.KContext.Screen.Value) menu := e.Domain.GetMenu(e.KContext.Screen.Value)
if e.Controller.UpPressed() && menu.CurrentSelected != 0 { if e.UpPressed() && menu.CurrentSelected != 0 {
menu.CurrentSelected-- menu.CurrentSelected--
} }
if e.Controller.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 { if e.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
menu.CurrentSelected++ menu.CurrentSelected++
} }
if e.Controller.Action0Pressed() { if e.Action0Pressed() {
menu.MenuItems[menu.CurrentSelected].Handler() menu.MenuItems[menu.CurrentSelected].Handler()
} }

View File

@@ -9,7 +9,7 @@ import (
type Konstructor struct { type Konstructor struct {
Domain entity.DomainInterface Domain entity.DomainInterface
Controller *engine.Controller KeyMap entity.KeyMap
Settings *entity.Settings Settings *entity.Settings
KContext *entity.KContext KContext *entity.KContext
} }
@@ -29,7 +29,7 @@ func (k Konstructor) Run() {
ebiten.RunGame(&engine.Engine{ ebiten.RunGame(&engine.Engine{
KContext: k.KContext, KContext: k.KContext,
Domain: k.Domain, Domain: k.Domain,
Controller: k.Controller, KeyMap: k.KeyMap,
Settings: k.Settings, Settings: k.Settings,
}) })
} }

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"game/domain" "game/domain"
"game/konstructor" "game/konstructor"
"game/konstructor/engine"
"game/konstructor/entity" "game/konstructor/entity"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
@@ -13,8 +12,8 @@ func main() {
k := konstructor.Konstructor{ k := konstructor.Konstructor{
KContext: &entity.KContext{ KContext: &entity.KContext{
Screen: entity.KContextScreen{ Screen: entity.KContextScreen{
Type: "playground", Type: "menu",
Value: "", Value: "MainMenu",
}, },
CurrentLevel: 0, CurrentLevel: 0,
}, },
@@ -28,7 +27,6 @@ func main() {
}, },
}, },
}, },
Controller: &engine.Controller{
KeyMap: entity.KeyMap{ KeyMap: entity.KeyMap{
Up: ebiten.KeyUp, Up: ebiten.KeyUp,
Down: ebiten.KeyDown, Down: ebiten.KeyDown,
@@ -39,7 +37,6 @@ func main() {
Action2: ebiten.KeyControl, Action2: ebiten.KeyControl,
Action3: ebiten.KeyEscape, Action3: ebiten.KeyEscape,
}, },
},
Settings: &entity.Settings{ Settings: &entity.Settings{
Name: "Game", Name: "Game",
Screen: &entity.ScreenSettings{ Screen: &entity.ScreenSettings{