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
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
type GameEngine struct {
|
||||
Konstructor Konstructor
|
||||
type Engine struct {
|
||||
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.Logic.Update(screen)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GameEngine) Draw(screen *ebiten.Image) {
|
||||
if g.Konstructor.Controller.UpPressed() {
|
||||
screen.Fill(color.White)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GameEngine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return g.Konstructor.Settings.Screen.Width, g.Konstructor.Settings.Screen.Height
|
||||
func (g *Engine) Draw(screen *ebiten.Image) {
|
||||
g.Logic.Draw(screen)
|
||||
g.Konstructor.Controller.Clear()
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
Name string
|
||||
Screen ScreenSettings
|
||||
Screen *ScreenSettings
|
||||
}
|
||||
|
||||
type Konstructor struct {
|
||||
Context ContextInterface
|
||||
Domain DomainInterface
|
||||
Controller Controller
|
||||
Settings Settings
|
||||
Controller *Controller
|
||||
Settings *Settings
|
||||
}
|
||||
|
||||
func (k Konstructor) Init() {
|
||||
ebiten.SetWindowSize(k.Settings.Screen.Width, k.Settings.Screen.Height)
|
||||
ebiten.SetWindowTitle(k.Settings.Name)
|
||||
if err := ebiten.RunGame(&GameEngine{
|
||||
Konstructor: k,
|
||||
if err := ebiten.RunGame(&Engine{
|
||||
Konstructor: &k,
|
||||
Logic: &Logic{
|
||||
Konstructor: &k,
|
||||
},
|
||||
}); err != nil {
|
||||
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 {
|
||||
|
||||
file, err := ioutil.ReadFile(path) //read the content of file
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
25
main.go
25
main.go
@@ -8,19 +8,18 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
domain := domain.Domain{
|
||||
Context: domain.Context{
|
||||
Player: domain.Player{
|
||||
Name: "Player One",
|
||||
},
|
||||
User: domain.User{
|
||||
Name: "John Doe",
|
||||
konstructor := konstructor.Konstructor{
|
||||
Domain: &domain.Domain{
|
||||
Context: domain.Context{
|
||||
Player: domain.Player{
|
||||
Name: "Player One",
|
||||
},
|
||||
User: domain.User{
|
||||
Name: "John Doe",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
konstructor := konstructor.Konstructor{
|
||||
Domain: domain,
|
||||
Controller: konstructor.Controller{
|
||||
Controller: &konstructor.Controller{
|
||||
KeyMap: konstructor.KeyMap{
|
||||
Up: ebiten.KeyUp,
|
||||
Down: ebiten.KeyDown,
|
||||
@@ -32,9 +31,9 @@ func main() {
|
||||
Action3: ebiten.KeyEscape,
|
||||
},
|
||||
},
|
||||
Settings: konstructor.Settings{
|
||||
Settings: &konstructor.Settings{
|
||||
Name: "Game",
|
||||
Screen: konstructor.ScreenSettings{
|
||||
Screen: &konstructor.ScreenSettings{
|
||||
Width: 640,
|
||||
Height: 480,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user