logic layer
This commit is contained in:
73
konstructor/controller.go
Executable file
73
konstructor/controller.go
Executable file
@@ -0,0 +1,73 @@
|
|||||||
|
package konstructor
|
||||||
|
|
||||||
|
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 Controller struct {
|
||||||
|
PressedKey ebiten.Key
|
||||||
|
KeyMap KeyMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) Watch() {
|
||||||
|
values := reflect.ValueOf(c.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) Clear() {
|
||||||
|
c.PressedKey = Controller{}.PressedKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) UpPressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Up
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) DownPressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Down
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) RightPressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Right
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) LeftPressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Left
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) Action0Pressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Action0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) Action1Pressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Action1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) Action2Pressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Action2
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) Action3Pressed() bool {
|
||||||
|
return c.PressedKey == c.KeyMap.Action3
|
||||||
|
}
|
||||||
@@ -1,26 +1,25 @@
|
|||||||
package konstructor
|
package konstructor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"image/color"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GameEngine struct {
|
type Engine struct {
|
||||||
Konstructor Konstructor
|
Konstructor *Konstructor
|
||||||
|
Logic *Logic
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GameEngine) Update(screen *ebiten.Image) error {
|
func (g *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||||
|
return g.Konstructor.Settings.Screen.Width, g.Konstructor.Settings.Screen.Height
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Engine) Update(screen *ebiten.Image) error {
|
||||||
g.Konstructor.Controller.Watch()
|
g.Konstructor.Controller.Watch()
|
||||||
|
g.Logic.Update(screen)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GameEngine) Draw(screen *ebiten.Image) {
|
func (g *Engine) Draw(screen *ebiten.Image) {
|
||||||
if g.Konstructor.Controller.UpPressed() {
|
g.Logic.Draw(screen)
|
||||||
screen.Fill(color.White)
|
g.Konstructor.Controller.Clear()
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GameEngine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
|
||||||
return g.Konstructor.Settings.Screen.Width, g.Konstructor.Settings.Screen.Height
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
package konstructor
|
|
||||||
|
|
||||||
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 Controller struct {
|
|
||||||
PressedKey ebiten.Key
|
|
||||||
KeyMap KeyMap
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) 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 *Controller) UpPressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Up
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) DownPressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Down
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) RightPressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Right
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) LeftPressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Left
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) Action0Pressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Action0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) Action1Pressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Action1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) Action2Pressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Action2
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k *Controller) Action3Pressed() bool {
|
|
||||||
return k.PressedKey == k.KeyMap.Action3
|
|
||||||
}
|
|
||||||
@@ -20,21 +20,23 @@ type ScreenSettings struct {
|
|||||||
|
|
||||||
type Settings struct {
|
type Settings struct {
|
||||||
Name string
|
Name string
|
||||||
Screen ScreenSettings
|
Screen *ScreenSettings
|
||||||
}
|
}
|
||||||
|
|
||||||
type Konstructor struct {
|
type Konstructor struct {
|
||||||
Context ContextInterface
|
|
||||||
Domain DomainInterface
|
Domain DomainInterface
|
||||||
Controller Controller
|
Controller *Controller
|
||||||
Settings Settings
|
Settings *Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k Konstructor) Init() {
|
func (k Konstructor) Init() {
|
||||||
ebiten.SetWindowSize(k.Settings.Screen.Width, k.Settings.Screen.Height)
|
ebiten.SetWindowSize(k.Settings.Screen.Width, k.Settings.Screen.Height)
|
||||||
ebiten.SetWindowTitle(k.Settings.Name)
|
ebiten.SetWindowTitle(k.Settings.Name)
|
||||||
if err := ebiten.RunGame(&GameEngine{
|
if err := ebiten.RunGame(&Engine{
|
||||||
Konstructor: k,
|
Konstructor: &k,
|
||||||
|
Logic: &Logic{
|
||||||
|
Konstructor: &k,
|
||||||
|
},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
20
konstructor/logic.go
Executable file
20
konstructor/logic.go
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
package konstructor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"image/color"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Logic struct {
|
||||||
|
Konstructor *Konstructor
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logic) Update(screen *ebiten.Image) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Logic) Draw(screen *ebiten.Image) {
|
||||||
|
if l.Konstructor.Controller.UpPressed() {
|
||||||
|
screen.Fill(color.White)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
func LoadImage(path string) *ebiten.Image {
|
func LoadImage(path string) *ebiten.Image {
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(path) //read the content of file
|
file, err := ioutil.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
13
main.go
13
main.go
@@ -8,7 +8,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
domain := domain.Domain{
|
konstructor := konstructor.Konstructor{
|
||||||
|
Domain: &domain.Domain{
|
||||||
Context: domain.Context{
|
Context: domain.Context{
|
||||||
Player: domain.Player{
|
Player: domain.Player{
|
||||||
Name: "Player One",
|
Name: "Player One",
|
||||||
@@ -17,10 +18,8 @@ func main() {
|
|||||||
Name: "John Doe",
|
Name: "John Doe",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
},
|
||||||
konstructor := konstructor.Konstructor{
|
Controller: &konstructor.Controller{
|
||||||
Domain: domain,
|
|
||||||
Controller: konstructor.Controller{
|
|
||||||
KeyMap: konstructor.KeyMap{
|
KeyMap: konstructor.KeyMap{
|
||||||
Up: ebiten.KeyUp,
|
Up: ebiten.KeyUp,
|
||||||
Down: ebiten.KeyDown,
|
Down: ebiten.KeyDown,
|
||||||
@@ -32,9 +31,9 @@ func main() {
|
|||||||
Action3: ebiten.KeyEscape,
|
Action3: ebiten.KeyEscape,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Settings: konstructor.Settings{
|
Settings: &konstructor.Settings{
|
||||||
Name: "Game",
|
Name: "Game",
|
||||||
Screen: konstructor.ScreenSettings{
|
Screen: &konstructor.ScreenSettings{
|
||||||
Width: 640,
|
Width: 640,
|
||||||
Height: 480,
|
Height: 480,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user