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

View File

@@ -9,9 +9,10 @@ import (
type Engine struct {
Domain entity.DomainInterface
Controller *Controller
Settings *entity.Settings
KContext *entity.KContext
KeyMap entity.KeyMap
PressedKey ebiten.Key
}
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 {
e.Controller.Watch()
e.WatchKeyPress()
if e.ScreenTypeIs("menu") {
e.MenuUpdate()
}
@@ -46,8 +47,8 @@ func (e *Engine) Draw(screen *ebiten.Image) {
if e.ScreenTypeIs("playground") {
e.PlaygroundDraw(screen)
}
if e.Controller.Action3Pressed() {
if e.Action3Pressed() {
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() {
dialog := e.Domain.GetDialog(e.KContext.Screen.Value)
if e.Controller.UpPressed() && dialog.CurrentSelected != 0 {
if e.UpPressed() && dialog.CurrentSelected != 0 {
dialog.CurrentSelected--
}
if e.Controller.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 {
if e.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 {
dialog.CurrentSelected++
}
if e.Controller.Action0Pressed() {
if e.Action0Pressed() {
dialog.Choices[dialog.CurrentSelected].Handler()
}
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() {
menu := e.Domain.GetMenu(e.KContext.Screen.Value)
if e.Controller.UpPressed() && menu.CurrentSelected != 0 {
if e.UpPressed() && menu.CurrentSelected != 0 {
menu.CurrentSelected--
}
if e.Controller.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
if e.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
menu.CurrentSelected++
}
if e.Controller.Action0Pressed() {
if e.Action0Pressed() {
menu.MenuItems[menu.CurrentSelected].Handler()
}