engine layer
This commit is contained in:
63
konstructor/engine/engine.controller.go
Normal file
63
konstructor/engine/engine.controller.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"game/konstructor/entity"
|
||||
"reflect"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/inpututil"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
PressedKey ebiten.Key
|
||||
KeyMap entity.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
|
||||
}
|
||||
53
konstructor/engine/engine.go
Normal file
53
konstructor/engine/engine.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"game/konstructor/entity"
|
||||
"os"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
Domain entity.DomainInterface
|
||||
Controller *Controller
|
||||
Settings *entity.Settings
|
||||
KContext *entity.KContext
|
||||
}
|
||||
|
||||
func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return e.Settings.Screen.Width, e.Settings.Screen.Height
|
||||
}
|
||||
|
||||
func (e *Engine) ScreenTypeIs(name string) bool {
|
||||
return e.KContext.Screen.Type == name
|
||||
}
|
||||
|
||||
func (e *Engine) Update(screen *ebiten.Image) error {
|
||||
e.Controller.Watch()
|
||||
if e.ScreenTypeIs("menu") {
|
||||
e.MenuUpdate()
|
||||
}
|
||||
if e.ScreenTypeIs("dialog") {
|
||||
e.DialogUpdate()
|
||||
}
|
||||
if e.ScreenTypeIs("playground") {
|
||||
e.PlaygroundUpdate()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Engine) Draw(screen *ebiten.Image) {
|
||||
if e.ScreenTypeIs("menu") {
|
||||
e.MenuDraw(screen)
|
||||
}
|
||||
if e.ScreenTypeIs("dialog") {
|
||||
e.DialogDraw(screen)
|
||||
}
|
||||
if e.ScreenTypeIs("playground") {
|
||||
e.PlaygroundDraw(screen)
|
||||
}
|
||||
if e.Controller.Action3Pressed() {
|
||||
os.Exit(1)
|
||||
}
|
||||
e.Controller.Clear()
|
||||
}
|
||||
46
konstructor/engine/engine.utils.go
Normal file
46
konstructor/engine/engine.utils.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"game/konstructor/entity"
|
||||
"image"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
_ "image/png"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/examples/resources/fonts"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/font/opentype"
|
||||
)
|
||||
|
||||
func LoadImage(path string) *ebiten.Image {
|
||||
|
||||
file, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
img, _, err := image.Decode(bytes.NewReader(file))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
out, err := ebiten.NewImageFromImage(img, 0)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func GetFontFace(layout entity.FontLayout) font.Face {
|
||||
tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf)
|
||||
face, _ := opentype.NewFace(tt, &opentype.FaceOptions{
|
||||
Size: layout.DPI,
|
||||
DPI: layout.Size,
|
||||
Hinting: font.HintingVertical,
|
||||
})
|
||||
return face
|
||||
}
|
||||
34
konstructor/engine/screen.dialog.go
Normal file
34
konstructor/engine/screen.dialog.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/text"
|
||||
)
|
||||
|
||||
func (e *Engine) DialogDraw(screen *ebiten.Image) {
|
||||
dialog := e.Domain.GetDialog(e.KContext.Screen.Value)
|
||||
|
||||
face := GetFontFace(dialog.Layout.ChoiceFont)
|
||||
|
||||
for i, choice := range dialog.Choices {
|
||||
offset := int(dialog.Layout.ChoiceFont.Size) * (i + 1)
|
||||
text.Draw(screen, choice.Label+"\n", face, 8, offset, dialog.GetChoiceColor(i))
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) DialogUpdate() {
|
||||
dialog := e.Domain.GetDialog(e.KContext.Screen.Value)
|
||||
|
||||
if e.Controller.UpPressed() && dialog.CurrentSelected != 0 {
|
||||
dialog.CurrentSelected--
|
||||
}
|
||||
|
||||
if e.Controller.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 {
|
||||
dialog.CurrentSelected++
|
||||
}
|
||||
|
||||
if e.Controller.Action0Pressed() {
|
||||
dialog.Choices[dialog.CurrentSelected].Handler()
|
||||
}
|
||||
e.Domain.SetDialog(e.KContext.Screen.Value, dialog)
|
||||
}
|
||||
35
konstructor/engine/screen.menu.go
Normal file
35
konstructor/engine/screen.menu.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/text"
|
||||
)
|
||||
|
||||
func (e *Engine) MenuDraw(screen *ebiten.Image) {
|
||||
menu := e.Domain.GetMenu(e.KContext.Screen.Value)
|
||||
face := GetFontFace(menu.Layout.MenuItemFont)
|
||||
|
||||
for i, menu_item := range menu.MenuItems {
|
||||
color := menu.GetMenuItemColor(i)
|
||||
offset := int(menu.Layout.MenuItemFont.Size) * (i + 1)
|
||||
text.Draw(screen, menu_item.Label+"\n", face, 8, offset, color)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) MenuUpdate() {
|
||||
menu := e.Domain.GetMenu(e.KContext.Screen.Value)
|
||||
|
||||
if e.Controller.UpPressed() && menu.CurrentSelected != 0 {
|
||||
menu.CurrentSelected--
|
||||
}
|
||||
|
||||
if e.Controller.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
|
||||
menu.CurrentSelected++
|
||||
}
|
||||
|
||||
if e.Controller.Action0Pressed() {
|
||||
menu.MenuItems[menu.CurrentSelected].Handler()
|
||||
}
|
||||
|
||||
e.Domain.SetMenu(e.KContext.Screen.Value, menu)
|
||||
}
|
||||
13
konstructor/engine/screen.playground.go
Normal file
13
konstructor/engine/screen.playground.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
func (e *Engine) PlaygroundDraw(screen *ebiten.Image) {
|
||||
e.Domain.GetLevel(e.KContext.CurrentLevel)
|
||||
}
|
||||
|
||||
func (e *Engine) PlaygroundUpdate() {
|
||||
e.Domain.GetLevel(e.KContext.CurrentLevel)
|
||||
}
|
||||
Reference in New Issue
Block a user