From 0ed8cfb839c9f9984bcbe2e78fdb93e360c4d940 Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Fri, 30 Jun 2023 17:44:19 +0200 Subject: [PATCH] constructor stucture fixes --- constructor/constructor.go | 41 ++++++++++++++++++++++++++++++++++++++ constructor/engine.go | 15 ++++---------- constructor/keyboard.go | 20 +++++++++---------- constructor/presenter.go | 29 --------------------------- constructor/utils.go | 2 -- main.go | 9 ++++++++- 6 files changed, 63 insertions(+), 53 deletions(-) create mode 100755 constructor/constructor.go delete mode 100755 constructor/presenter.go diff --git a/constructor/constructor.go b/constructor/constructor.go new file mode 100755 index 0000000..0460b43 --- /dev/null +++ b/constructor/constructor.go @@ -0,0 +1,41 @@ +package constructor + +import ( + "log" + + "github.com/hajimehoshi/ebiten" +) + +type ContextInterface interface { +} + +type DomainInterface interface { + Init() +} + +type ScreenSettings struct { + Width int + Height int +} + +type GameSettings struct { + Name string + ScreenSettings ScreenSettings +} + +type Constructor struct { + Context ContextInterface + Domain DomainInterface + Controller Controller + GameSettings GameSettings +} + +func (c Constructor) Init() { + ebiten.SetWindowSize(c.GameSettings.ScreenSettings.Width, c.GameSettings.ScreenSettings.Height) + ebiten.SetWindowTitle(c.GameSettings.Name) + if err := ebiten.RunGame(&GameEngine{ + Constructor: c, + }); err != nil { + log.Fatal(err) + } +} diff --git a/constructor/engine.go b/constructor/engine.go index b474fd0..efc80ae 100755 --- a/constructor/engine.go +++ b/constructor/engine.go @@ -1,33 +1,26 @@ package constructor import ( - "game/domain" "image/color" "github.com/hajimehoshi/ebiten" ) -const ( - screenWidth = 320 - screenHeight = 240 -) - type GameEngine struct { - Context domain.Context - Keyboard Keyboard + Constructor Constructor } func (g *GameEngine) Update(screen *ebiten.Image) error { - g.Keyboard.Watch() + g.Constructor.Controller.Watch() return nil } func (g *GameEngine) Draw(screen *ebiten.Image) { - if g.Keyboard.UpPressed() { + if g.Constructor.Controller.UpPressed() { screen.Fill(color.White) } } func (g *GameEngine) Layout(outsideWidth, outsideHeight int) (int, int) { - return screenWidth, screenHeight + return g.Constructor.GameSettings.ScreenSettings.Width, g.Constructor.GameSettings.ScreenSettings.Height } diff --git a/constructor/keyboard.go b/constructor/keyboard.go index c77e8f4..7ef7f76 100755 --- a/constructor/keyboard.go +++ b/constructor/keyboard.go @@ -19,12 +19,12 @@ type KeyMap struct { Action3 ebiten.Key } -type Keyboard struct { +type Controller struct { PressedKey ebiten.Key KeyMap KeyMap } -func (k *Keyboard) Watch() { +func (k *Controller) Watch() { values := reflect.ValueOf(k.KeyMap) for i := 0; i < values.NumField(); i++ { @@ -36,34 +36,34 @@ func (k *Keyboard) Watch() { } } -func (k *Keyboard) UpPressed() bool { +func (k *Controller) UpPressed() bool { return k.PressedKey == k.KeyMap.Up } -func (k *Keyboard) DownPressed() bool { +func (k *Controller) DownPressed() bool { return k.PressedKey == k.KeyMap.Down } -func (k *Keyboard) RightPressed() bool { +func (k *Controller) RightPressed() bool { return k.PressedKey == k.KeyMap.Right } -func (k *Keyboard) LeftPressed() bool { +func (k *Controller) LeftPressed() bool { return k.PressedKey == k.KeyMap.Left } -func (k *Keyboard) Action0Pressed() bool { +func (k *Controller) Action0Pressed() bool { return k.PressedKey == k.KeyMap.Action0 } -func (k *Keyboard) Action1Pressed() bool { +func (k *Controller) Action1Pressed() bool { return k.PressedKey == k.KeyMap.Action1 } -func (k *Keyboard) Action2Pressed() bool { +func (k *Controller) Action2Pressed() bool { return k.PressedKey == k.KeyMap.Action2 } -func (k *Keyboard) Action3Pressed() bool { +func (k *Controller) Action3Pressed() bool { return k.PressedKey == k.KeyMap.Action3 } diff --git a/constructor/presenter.go b/constructor/presenter.go deleted file mode 100755 index bc8fd49..0000000 --- a/constructor/presenter.go +++ /dev/null @@ -1,29 +0,0 @@ -package constructor - -import ( - "game/domain" - "log" - - "github.com/hajimehoshi/ebiten" -) - -type DomainInterface interface { - Init() -} - -type Constructor struct { - Context domain.Context - Domain DomainInterface - Keyboard Keyboard -} - -func (p Constructor) Init() { - ebiten.SetWindowSize(screenWidth*2, screenHeight*2) - ebiten.SetWindowTitle("Game") - if err := ebiten.RunGame(&GameEngine{ - Context: p.Context, - Keyboard: p.Keyboard, - }); err != nil { - log.Fatal(err) - } -} diff --git a/constructor/utils.go b/constructor/utils.go index 64fe486..237bd1f 100755 --- a/constructor/utils.go +++ b/constructor/utils.go @@ -2,7 +2,6 @@ package constructor import ( "bytes" - "fmt" "image" "io/ioutil" "log" @@ -21,7 +20,6 @@ func LoadImage(path string) *ebiten.Image { img, _, err := image.Decode(bytes.NewReader(file)) if err != nil { - fmt.Println("decode error") log.Fatal(err) } diff --git a/main.go b/main.go index 69dd1cd..49cc619 100755 --- a/main.go +++ b/main.go @@ -20,7 +20,7 @@ func main() { } constructor := constructor.Constructor{ Domain: domain, - Keyboard: constructor.Keyboard{ + Controller: constructor.Controller{ KeyMap: constructor.KeyMap{ Up: ebiten.KeyUp, Down: ebiten.KeyDown, @@ -32,6 +32,13 @@ func main() { Action3: ebiten.KeyEscape, }, }, + GameSettings: constructor.GameSettings{ + Name: "Game", + ScreenSettings: constructor.ScreenSettings{ + Width: 640, + Height: 480, + }, + }, } constructor.Init() }