constructor stucture fixes

This commit is contained in:
2023-06-30 17:44:19 +02:00
parent dec8ad2fe0
commit 0ed8cfb839
6 changed files with 63 additions and 53 deletions

41
constructor/constructor.go Executable file
View File

@@ -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)
}
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}
}

View File

@@ -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)
}

View File

@@ -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()
}