merge controller to engine
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
type Konstructor struct {
|
||||
Domain entity.DomainInterface
|
||||
Controller *engine.Controller
|
||||
KeyMap entity.KeyMap
|
||||
Settings *entity.Settings
|
||||
KContext *entity.KContext
|
||||
}
|
||||
@@ -29,7 +29,7 @@ func (k Konstructor) Run() {
|
||||
ebiten.RunGame(&engine.Engine{
|
||||
KContext: k.KContext,
|
||||
Domain: k.Domain,
|
||||
Controller: k.Controller,
|
||||
KeyMap: k.KeyMap,
|
||||
Settings: k.Settings,
|
||||
})
|
||||
}
|
||||
|
||||
7
main.go
7
main.go
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"game/domain"
|
||||
"game/konstructor"
|
||||
"game/konstructor/engine"
|
||||
"game/konstructor/entity"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
@@ -13,8 +12,8 @@ func main() {
|
||||
k := konstructor.Konstructor{
|
||||
KContext: &entity.KContext{
|
||||
Screen: entity.KContextScreen{
|
||||
Type: "playground",
|
||||
Value: "",
|
||||
Type: "menu",
|
||||
Value: "MainMenu",
|
||||
},
|
||||
CurrentLevel: 0,
|
||||
},
|
||||
@@ -28,7 +27,6 @@ func main() {
|
||||
},
|
||||
},
|
||||
},
|
||||
Controller: &engine.Controller{
|
||||
KeyMap: entity.KeyMap{
|
||||
Up: ebiten.KeyUp,
|
||||
Down: ebiten.KeyDown,
|
||||
@@ -39,7 +37,6 @@ func main() {
|
||||
Action2: ebiten.KeyControl,
|
||||
Action3: ebiten.KeyEscape,
|
||||
},
|
||||
},
|
||||
Settings: &entity.Settings{
|
||||
Name: "Game",
|
||||
Screen: &entity.ScreenSettings{
|
||||
|
||||
Reference in New Issue
Block a user