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
}