move engine to engines/easy_ebitengine
This commit is contained in:
57
konstructor/engines/easy_ebitengine/engine.controller.go
Normal file
57
konstructor/engines/easy_ebitengine/engine.controller.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/inpututil"
|
||||
)
|
||||
|
||||
func (e *Engine) WatchKeyPress() {
|
||||
values := reflect.ValueOf(e.Settings.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)
|
||||
e.PressedKey = key
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) ClearKeyPresed() {
|
||||
e.PressedKey = Engine{}.PressedKey
|
||||
}
|
||||
|
||||
func (e *Engine) UpPressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Up.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) DownPressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Down.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) RightPressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Right.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) LeftPressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Left.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) Action0Pressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Action0.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) Action1Pressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Action1.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) Action2Pressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Action2.(ebiten.Key)
|
||||
}
|
||||
|
||||
func (e *Engine) Action3Pressed() bool {
|
||||
return e.PressedKey == e.Settings.KeyMap.Action3.(ebiten.Key)
|
||||
}
|
||||
55
konstructor/engines/easy_ebitengine/engine.go
Normal file
55
konstructor/engines/easy_ebitengine/engine.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"game/konstructor"
|
||||
"os"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
type Engine struct {
|
||||
Domain konstructor.DomainInterface
|
||||
Settings *konstructor.Settings
|
||||
KContext *konstructor.KContext
|
||||
PressedKey ebiten.Key
|
||||
}
|
||||
|
||||
func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {
|
||||
return e.Settings.Screen.Width, e.Settings.Screen.Height
|
||||
}
|
||||
|
||||
func (e *Engine) Update(screen *ebiten.Image) error {
|
||||
e.WatchKeyPress()
|
||||
if e.KContext.ScreenTypeIs(konstructor.MenuScreenType) {
|
||||
e.MenuUpdate()
|
||||
}
|
||||
if e.KContext.ScreenTypeIs(konstructor.DialogScreenType) {
|
||||
e.DialogUpdate()
|
||||
}
|
||||
if e.KContext.ScreenTypeIs(konstructor.PlaygroundScreenType) {
|
||||
e.PlaygroundUpdate()
|
||||
}
|
||||
if e.KContext.ScreenTypeIs(konstructor.InventoryScreenType) {
|
||||
e.InventoryUpdate()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *Engine) Draw(screen *ebiten.Image) {
|
||||
if e.KContext.ScreenTypeIs(konstructor.MenuScreenType) {
|
||||
e.MenuDraw(screen)
|
||||
}
|
||||
if e.KContext.ScreenTypeIs(konstructor.DialogScreenType) {
|
||||
e.DialogDraw(screen)
|
||||
}
|
||||
if e.KContext.ScreenTypeIs(konstructor.PlaygroundScreenType) {
|
||||
e.PlaygroundDraw(screen)
|
||||
}
|
||||
if e.KContext.ScreenTypeIs(konstructor.InventoryScreenType) {
|
||||
e.InventoryDraw(screen)
|
||||
}
|
||||
if e.Action3Pressed() {
|
||||
os.Exit(1)
|
||||
}
|
||||
e.ClearKeyPresed()
|
||||
}
|
||||
19
konstructor/engines/easy_ebitengine/engine.utils.go
Normal file
19
konstructor/engines/easy_ebitengine/engine.utils.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"game/konstructor"
|
||||
"log"
|
||||
|
||||
_ "image/png"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
func (e *Engine) GetImage(render_options konstructor.Render) *ebiten.Image {
|
||||
img := render_options.GetImage()
|
||||
out, err := ebiten.NewImageFromImage(img, 0)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
25
konstructor/engines/easy_ebitengine/engine.wrapper.go
Normal file
25
konstructor/engines/easy_ebitengine/engine.wrapper.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"game/konstructor"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
type EngineWrapper struct {
|
||||
Engine Engine
|
||||
}
|
||||
|
||||
func (ew *EngineWrapper) Init(options konstructor.EngineArgs) {
|
||||
ew.Engine = Engine{
|
||||
KContext: options.KContext,
|
||||
Domain: options.Domain,
|
||||
Settings: options.Settings,
|
||||
}
|
||||
ebiten.SetWindowSize(options.Settings.Screen.Width, options.Settings.Screen.Height)
|
||||
ebiten.SetWindowTitle(options.Settings.Name)
|
||||
}
|
||||
|
||||
func (ew *EngineWrapper) Run() {
|
||||
ebiten.RunGame(&ew.Engine)
|
||||
}
|
||||
34
konstructor/engines/easy_ebitengine/screen.dialog.go
Normal file
34
konstructor/engines/easy_ebitengine/screen.dialog.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/text"
|
||||
)
|
||||
|
||||
func (e *Engine) DialogUpdate() {
|
||||
dialog := e.Domain.GetDialog(e.KContext.ScreenValue)
|
||||
|
||||
if e.UpPressed() && dialog.CurrentSelected != 0 {
|
||||
dialog.CurrentSelected--
|
||||
}
|
||||
|
||||
if e.DownPressed() && dialog.CurrentSelected != len(dialog.Choices)-1 {
|
||||
dialog.CurrentSelected++
|
||||
}
|
||||
|
||||
if e.Action0Pressed() {
|
||||
dialog.Choices[dialog.CurrentSelected].Handler()
|
||||
}
|
||||
e.Domain.SetDialog(e.KContext.ScreenValue, dialog)
|
||||
}
|
||||
|
||||
func (e *Engine) DialogDraw(screen *ebiten.Image) {
|
||||
dialog := e.Domain.GetDialog(e.KContext.ScreenValue)
|
||||
|
||||
face := dialog.Layout.ChoiceFont.GetFontFace()
|
||||
|
||||
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))
|
||||
}
|
||||
}
|
||||
25
konstructor/engines/easy_ebitengine/screen.inventory.go
Normal file
25
konstructor/engines/easy_ebitengine/screen.inventory.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"game/konstructor"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
func (e *Engine) InventoryUpdate() {
|
||||
}
|
||||
|
||||
func (e *Engine) InventoryDraw(screen *ebiten.Image) {
|
||||
}
|
||||
|
||||
func (e *Engine) AddToInventory(item *konstructor.Item) {
|
||||
e.Domain.AddToInventory(item)
|
||||
}
|
||||
|
||||
func (e *Engine) RemoveFromInventory(item *konstructor.Item) {
|
||||
e.Domain.RemoveFromInventory(item)
|
||||
}
|
||||
|
||||
func (e *Engine) UseInventoryItem(item *konstructor.Item) {
|
||||
e.Domain.UseInventoryItem(item)
|
||||
}
|
||||
35
konstructor/engines/easy_ebitengine/screen.menu.go
Normal file
35
konstructor/engines/easy_ebitengine/screen.menu.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/text"
|
||||
)
|
||||
|
||||
func (e *Engine) MenuUpdate() {
|
||||
menu := e.Domain.GetMenu(e.KContext.ScreenValue)
|
||||
|
||||
if e.UpPressed() && menu.CurrentSelected != 0 {
|
||||
menu.CurrentSelected--
|
||||
}
|
||||
|
||||
if e.DownPressed() && menu.CurrentSelected != len(menu.MenuItems)-1 {
|
||||
menu.CurrentSelected++
|
||||
}
|
||||
|
||||
if e.Action0Pressed() {
|
||||
menu.MenuItems[menu.CurrentSelected].Handler()
|
||||
}
|
||||
|
||||
e.Domain.SetMenu(e.KContext.ScreenValue, menu)
|
||||
}
|
||||
|
||||
func (e *Engine) MenuDraw(screen *ebiten.Image) {
|
||||
menu := e.Domain.GetMenu(e.KContext.ScreenValue)
|
||||
face := menu.Layout.MenuItemFont.GetFontFace()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
44
konstructor/engines/easy_ebitengine/screen.playground.go
Normal file
44
konstructor/engines/easy_ebitengine/screen.playground.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package easy_ebitengine
|
||||
|
||||
import (
|
||||
"game/konstructor"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
func (e *Engine) PlaygroundUpdate() {
|
||||
level := e.Domain.GetLevel(e.KContext.CurrentLevel)
|
||||
e.Domain.Process(konstructor.DomainProcessArgs{
|
||||
Level: &level,
|
||||
KContext: e.KContext,
|
||||
})
|
||||
}
|
||||
|
||||
func (e *Engine) PlaygroundDraw(screen *ebiten.Image) {
|
||||
e.PlaygroundBackgroundDraw(screen)
|
||||
e.PlaygroundObjectsDraw(screen)
|
||||
}
|
||||
|
||||
func (e *Engine) PlaygroundBackgroundDraw(screen *ebiten.Image) {
|
||||
playground := e.GetPlayground()
|
||||
render := playground.Render
|
||||
screen.DrawImage(e.GetImage(render), &ebiten.DrawImageOptions{})
|
||||
}
|
||||
|
||||
func (e *Engine) PlaygroundObjectsDraw(screen *ebiten.Image) {
|
||||
playground := e.GetPlayground()
|
||||
geoM := ebiten.GeoM{}
|
||||
for _, object := range playground.Objects {
|
||||
render := object.Type.Render
|
||||
geoM.Reset()
|
||||
geoM.Translate(float64(object.Position.X), float64(object.Position.Y))
|
||||
screen.DrawImage(e.GetImage(render), &ebiten.DrawImageOptions{
|
||||
GeoM: geoM,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) GetPlayground() konstructor.Playground {
|
||||
level := e.Domain.GetLevel(e.KContext.CurrentLevel)
|
||||
return level.Playgrounds[e.KContext.CurrentPlayground]
|
||||
}
|
||||
Reference in New Issue
Block a user