rename presenter to constructor (memoriem of Last Ninja)

This commit is contained in:
2023-06-30 17:16:17 +02:00
parent bd63d00ad9
commit dec8ad2fe0
5 changed files with 11 additions and 11 deletions

33
constructor/engine.go Executable file
View File

@@ -0,0 +1,33 @@
package constructor
import (
"game/domain"
"image/color"
"github.com/hajimehoshi/ebiten"
)
const (
screenWidth = 320
screenHeight = 240
)
type GameEngine struct {
Context domain.Context
Keyboard Keyboard
}
func (g *GameEngine) Update(screen *ebiten.Image) error {
g.Keyboard.Watch()
return nil
}
func (g *GameEngine) Draw(screen *ebiten.Image) {
if g.Keyboard.UpPressed() {
screen.Fill(color.White)
}
}
func (g *GameEngine) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}

69
constructor/keyboard.go Executable file
View File

@@ -0,0 +1,69 @@
package constructor
import (
"fmt"
"reflect"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil"
)
type KeyMap struct {
Up ebiten.Key
Down ebiten.Key
Right ebiten.Key
Left ebiten.Key
Action0 ebiten.Key
Action1 ebiten.Key
Action2 ebiten.Key
Action3 ebiten.Key
}
type Keyboard struct {
PressedKey ebiten.Key
KeyMap KeyMap
}
func (k *Keyboard) Watch() {
values := reflect.ValueOf(k.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)
k.PressedKey = key
}
}
}
func (k *Keyboard) UpPressed() bool {
return k.PressedKey == k.KeyMap.Up
}
func (k *Keyboard) DownPressed() bool {
return k.PressedKey == k.KeyMap.Down
}
func (k *Keyboard) RightPressed() bool {
return k.PressedKey == k.KeyMap.Right
}
func (k *Keyboard) LeftPressed() bool {
return k.PressedKey == k.KeyMap.Left
}
func (k *Keyboard) Action0Pressed() bool {
return k.PressedKey == k.KeyMap.Action0
}
func (k *Keyboard) Action1Pressed() bool {
return k.PressedKey == k.KeyMap.Action1
}
func (k *Keyboard) Action2Pressed() bool {
return k.PressedKey == k.KeyMap.Action2
}
func (k *Keyboard) Action3Pressed() bool {
return k.PressedKey == k.KeyMap.Action3
}

29
constructor/presenter.go Executable file
View File

@@ -0,0 +1,29 @@
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)
}
}

34
constructor/utils.go Executable file
View File

@@ -0,0 +1,34 @@
package constructor
import (
"bytes"
"fmt"
"image"
"io/ioutil"
"log"
_ "image/png"
"github.com/hajimehoshi/ebiten"
)
func LoadImage(path string) *ebiten.Image {
file, err := ioutil.ReadFile(path) //read the content of file
if err != nil {
log.Fatal(err)
}
img, _, err := image.Decode(bytes.NewReader(file))
if err != nil {
fmt.Println("decode error")
log.Fatal(err)
}
out, err := ebiten.NewImageFromImage(img, 0)
if err != nil {
log.Fatal(err)
}
return out
}