GetMenu, SetMenu

This commit is contained in:
2023-07-04 20:10:14 +02:00
parent 1451bd90c0
commit 74272c09f2
6 changed files with 82 additions and 69 deletions

View File

@@ -7,9 +7,11 @@ import (
type Domain struct {
Context Context
MenuMap konstructor.MenuMap
}
func (d *Domain) Init() {
d.InitMenu()
}
func (d *Domain) GetDialog() konstructor.Dialog {

View File

@@ -2,9 +2,76 @@ package domain
import (
"fmt"
"game/konstructor"
"image/color"
"os"
)
func (d *Domain) InitMenu() {
d.MenuMap = konstructor.MenuMap{
"MainMenu": {
CurrentSelected: 0,
Layout: konstructor.MenuLayout{
MenuItemFont: konstructor.FontLayout{
DPI: 72,
Size: 24,
Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
},
},
MenuItems: []konstructor.MenuItem{
{
ID: "start",
Label: "Start Game",
Handler: d.StartGame,
},
{
ID: "load",
Label: "Load Game",
Handler: d.LoadtGame,
},
{
ID: "exit",
Label: "Exit Game",
Handler: d.ExitGame,
},
},
},
"GameMenu": {
CurrentSelected: 0,
Layout: konstructor.MenuLayout{
MenuItemFont: konstructor.FontLayout{
DPI: 72,
Size: 24,
Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
},
},
MenuItems: []konstructor.MenuItem{
{
ID: "save",
Label: "Save Game",
Handler: d.SaveGame,
},
{
ID: "close",
Label: "Close Game",
Handler: d.CloseGame,
},
},
},
}
}
func (d *Domain) GetMenu(name string) konstructor.Menu {
value, _ := d.MenuMap[name]
return value
}
func (d *Domain) SetMenu(name string, menu konstructor.Menu) {
d.MenuMap[name] = menu
}
func (d *Domain) CloseGame() {
fmt.Println("Close game")
}

View File

@@ -11,7 +11,6 @@ type Engine struct {
Controller *Controller
Settings *Settings
KContext *KContext
MenuMap MenuMap
}
func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {

View File

@@ -11,6 +11,8 @@ type ContextInterface interface {
type DomainInterface interface {
Init()
GetMenu(name string) Menu
SetMenu(name string, menu Menu)
GetDialog() Dialog
}
@@ -29,7 +31,6 @@ type Konstructor struct {
Controller *Controller
Settings *Settings
KContext *KContext
MenuMap MenuMap
}
func (k Konstructor) Init() {
@@ -37,7 +38,6 @@ func (k Konstructor) Init() {
ebiten.SetWindowSize(k.Settings.Screen.Width, k.Settings.Screen.Height)
ebiten.SetWindowTitle(k.Settings.Name)
if err := ebiten.RunGame(&Engine{
MenuMap: k.MenuMap,
KContext: k.KContext,
Domain: k.Domain,
Controller: k.Controller,

View File

@@ -35,7 +35,7 @@ func (menu *Menu) GetMenuItemColor(i int) color.Color {
}
func (e *Engine) MenuDraw(screen *ebiten.Image) {
menu := e.MenuMap[e.KContext.Screen.Value]
menu := e.Domain.GetMenu(e.KContext.Screen.Value)
face := GetFontFace(menu.Layout.MenuItemFont)
for i, menu_item := range menu.MenuItems {
@@ -46,7 +46,7 @@ func (e *Engine) MenuDraw(screen *ebiten.Image) {
}
func (e *Engine) MenuUpdate() {
menu := e.MenuMap[e.KContext.Screen.Value]
menu := e.Domain.GetMenu(e.KContext.Screen.Value)
if e.Controller.UpPressed() && menu.CurrentSelected != 0 {
menu.CurrentSelected--
@@ -60,5 +60,5 @@ func (e *Engine) MenuUpdate() {
menu.MenuItems[menu.CurrentSelected].Handler()
}
e.MenuMap[e.KContext.Screen.Value] = menu
e.Domain.SetMenu(e.KContext.Screen.Value, menu)
}

71
main.go
View File

@@ -3,14 +3,20 @@ package main
import (
"game/domain"
"game/konstructor"
"image/color"
"github.com/hajimehoshi/ebiten"
)
func main() {
d := domain.Domain{
k := konstructor.Konstructor{
KContext: &konstructor.KContext{
Screen: konstructor.KContextScreen{
Type: "menu",
Value: "MainMenu",
},
},
Domain: &domain.Domain{
Context: domain.Context{
Player: domain.Player{
Name: "Player One",
@@ -19,67 +25,6 @@ func main() {
Name: "John Doe",
},
},
}
k := konstructor.Konstructor{
KContext: &konstructor.KContext{
Screen: konstructor.KContextScreen{
Type: "dialog",
},
},
Domain: &d,
MenuMap: konstructor.MenuMap{
"MainMenu": {
CurrentSelected: 0,
Layout: konstructor.MenuLayout{
MenuItemFont: konstructor.FontLayout{
DPI: 72,
Size: 24,
Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
},
},
MenuItems: []konstructor.MenuItem{
{
ID: "start",
Label: "Start Game",
Handler: d.StartGame,
},
{
ID: "load",
Label: "Load Game",
Handler: d.LoadtGame,
},
{
ID: "exit",
Label: "Exit Game",
Handler: d.ExitGame,
},
},
},
"GameMenu": {
CurrentSelected: 0,
Layout: konstructor.MenuLayout{
MenuItemFont: konstructor.FontLayout{
DPI: 72,
Size: 24,
Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
},
},
MenuItems: []konstructor.MenuItem{
{
ID: "save",
Label: "Save Game",
Handler: d.SaveGame,
},
{
ID: "close",
Label: "Close Game",
Handler: d.CloseGame,
},
},
},
},
Controller: &konstructor.Controller{
KeyMap: konstructor.KeyMap{