constructor stucture fixes
This commit is contained in:
41
constructor/constructor.go
Executable file
41
constructor/constructor.go
Executable 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,33 +1,26 @@
|
|||||||
package constructor
|
package constructor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"game/domain"
|
|
||||||
"image/color"
|
"image/color"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
screenWidth = 320
|
|
||||||
screenHeight = 240
|
|
||||||
)
|
|
||||||
|
|
||||||
type GameEngine struct {
|
type GameEngine struct {
|
||||||
Context domain.Context
|
Constructor Constructor
|
||||||
Keyboard Keyboard
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GameEngine) Update(screen *ebiten.Image) error {
|
func (g *GameEngine) Update(screen *ebiten.Image) error {
|
||||||
g.Keyboard.Watch()
|
g.Constructor.Controller.Watch()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GameEngine) Draw(screen *ebiten.Image) {
|
func (g *GameEngine) Draw(screen *ebiten.Image) {
|
||||||
if g.Keyboard.UpPressed() {
|
if g.Constructor.Controller.UpPressed() {
|
||||||
screen.Fill(color.White)
|
screen.Fill(color.White)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GameEngine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
func (g *GameEngine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
return screenWidth, screenHeight
|
return g.Constructor.GameSettings.ScreenSettings.Width, g.Constructor.GameSettings.ScreenSettings.Height
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ type KeyMap struct {
|
|||||||
Action3 ebiten.Key
|
Action3 ebiten.Key
|
||||||
}
|
}
|
||||||
|
|
||||||
type Keyboard struct {
|
type Controller struct {
|
||||||
PressedKey ebiten.Key
|
PressedKey ebiten.Key
|
||||||
KeyMap KeyMap
|
KeyMap KeyMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) Watch() {
|
func (k *Controller) Watch() {
|
||||||
values := reflect.ValueOf(k.KeyMap)
|
values := reflect.ValueOf(k.KeyMap)
|
||||||
|
|
||||||
for i := 0; i < values.NumField(); i++ {
|
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
|
return k.PressedKey == k.KeyMap.Up
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) DownPressed() bool {
|
func (k *Controller) DownPressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Down
|
return k.PressedKey == k.KeyMap.Down
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) RightPressed() bool {
|
func (k *Controller) RightPressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Right
|
return k.PressedKey == k.KeyMap.Right
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) LeftPressed() bool {
|
func (k *Controller) LeftPressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Left
|
return k.PressedKey == k.KeyMap.Left
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) Action0Pressed() bool {
|
func (k *Controller) Action0Pressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Action0
|
return k.PressedKey == k.KeyMap.Action0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) Action1Pressed() bool {
|
func (k *Controller) Action1Pressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Action1
|
return k.PressedKey == k.KeyMap.Action1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) Action2Pressed() bool {
|
func (k *Controller) Action2Pressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Action2
|
return k.PressedKey == k.KeyMap.Action2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *Keyboard) Action3Pressed() bool {
|
func (k *Controller) Action3Pressed() bool {
|
||||||
return k.PressedKey == k.KeyMap.Action3
|
return k.PressedKey == k.KeyMap.Action3
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@ package constructor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"image"
|
"image"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
@@ -21,7 +20,6 @@ func LoadImage(path string) *ebiten.Image {
|
|||||||
|
|
||||||
img, _, err := image.Decode(bytes.NewReader(file))
|
img, _, err := image.Decode(bytes.NewReader(file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("decode error")
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
9
main.go
9
main.go
@@ -20,7 +20,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
constructor := constructor.Constructor{
|
constructor := constructor.Constructor{
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
Keyboard: constructor.Keyboard{
|
Controller: constructor.Controller{
|
||||||
KeyMap: constructor.KeyMap{
|
KeyMap: constructor.KeyMap{
|
||||||
Up: ebiten.KeyUp,
|
Up: ebiten.KeyUp,
|
||||||
Down: ebiten.KeyDown,
|
Down: ebiten.KeyDown,
|
||||||
@@ -32,6 +32,13 @@ func main() {
|
|||||||
Action3: ebiten.KeyEscape,
|
Action3: ebiten.KeyEscape,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
GameSettings: constructor.GameSettings{
|
||||||
|
Name: "Game",
|
||||||
|
ScreenSettings: constructor.ScreenSettings{
|
||||||
|
Width: 640,
|
||||||
|
Height: 480,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
constructor.Init()
|
constructor.Init()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user