entity layer

This commit is contained in:
2023-07-04 22:58:23 +02:00
parent 93b2725c8a
commit e9c35f01ab
19 changed files with 364 additions and 353 deletions

View File

@@ -1,22 +1,22 @@
package domain package domain
import ( import (
"game/konstructor" "game/konstructor/entity"
"image/color" "image/color"
) )
func (d *Domain) InitDialog() { func (d *Domain) InitDialog() {
d.DialogMap = konstructor.DialogMap{ d.DialogMap = entity.DialogMap{
"TestDialog": { "TestDialog": {
Layout: konstructor.DialogLayout{ Layout: entity.DialogLayout{
ChoiceFont: konstructor.FontLayout{ ChoiceFont: entity.FontLayout{
DPI: 72, DPI: 72,
Size: 24, Size: 24,
Color: color.White, Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
}, },
}, },
Choices: []konstructor.DialogChoice{ Choices: []entity.DialogChoice{
{ {
ID: "one", ID: "one",
Label: "One", Label: "One",
@@ -30,11 +30,11 @@ func (d *Domain) InitDialog() {
} }
} }
func (d *Domain) GetDialog(name string) konstructor.Dialog { func (d *Domain) GetDialog(name string) entity.Dialog {
value, _ := d.DialogMap[name] value, _ := d.DialogMap[name]
return value return value
} }
func (d *Domain) SetDialog(name string, dialog konstructor.Dialog) { func (d *Domain) SetDialog(name string, dialog entity.Dialog) {
d.DialogMap[name] = dialog d.DialogMap[name] = dialog
} }

View File

@@ -1,14 +1,14 @@
package domain package domain
import ( import (
"game/konstructor" "game/konstructor/entity"
) )
type Domain struct { type Domain struct {
Context Context Context Context
MenuMap konstructor.MenuMap MenuMap entity.MenuMap
DialogMap konstructor.DialogMap DialogMap entity.DialogMap
Levels []konstructor.Level Levels []entity.Level
} }
func (d *Domain) Init() { func (d *Domain) Init() {

View File

@@ -1,28 +1,26 @@
package domain package domain
import ( import "game/konstructor/entity"
"game/konstructor"
)
func (d *Domain) InitLevel() { func (d *Domain) InitLevel() {
d.Levels = []konstructor.Level{ d.Levels = []entity.Level{
{ {
ID: "level_1", ID: "level_1",
Name: "Level I.", Name: "Level I.",
Playgrounds: []konstructor.Playground{ Playgrounds: []entity.Playground{
{ {
Background: "level_1_playground_1.png", Background: "level_1_playground_1.png",
Objects: []konstructor.Object{ Objects: []entity.Object{
{ {
ID: "test_object", ID: "test_object",
Position: konstructor.Position{ Position: entity.Position{
X: 10, X: 10,
Y: 10, Y: 10,
Z: 0, Z: 0,
}, },
Type: konstructor.ObjectType{ Type: entity.ObjectType{
ID: "test_object_type", ID: "test_object_type",
RenderOptions: konstructor.RenderOptions{ RenderOptions: entity.RenderOptions{
Image: "test_object.png", Image: "test_object.png",
Width: 30, Width: 30,
Height: 30, Height: 30,
@@ -36,6 +34,6 @@ func (d *Domain) InitLevel() {
} }
} }
func (d *Domain) GetLevel(index int) konstructor.Level { func (d *Domain) GetLevel(index int) entity.Level {
return d.Levels[index] return d.Levels[index]
} }

View File

@@ -2,24 +2,24 @@ package domain
import ( import (
"fmt" "fmt"
"game/konstructor" "game/konstructor/entity"
"image/color" "image/color"
"os" "os"
) )
func (d *Domain) InitMenu() { func (d *Domain) InitMenu() {
d.MenuMap = konstructor.MenuMap{ d.MenuMap = entity.MenuMap{
"MainMenu": { "MainMenu": {
CurrentSelected: 0, CurrentSelected: 0,
Layout: konstructor.MenuLayout{ Layout: entity.MenuLayout{
MenuItemFont: konstructor.FontLayout{ MenuItemFont: entity.FontLayout{
DPI: 72, DPI: 72,
Size: 24, Size: 24,
Color: color.White, Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
}, },
}, },
MenuItems: []konstructor.MenuItem{ MenuItems: []entity.MenuItem{
{ {
ID: "start", ID: "start",
Label: "Start Game", Label: "Start Game",
@@ -39,15 +39,15 @@ func (d *Domain) InitMenu() {
}, },
"GameMenu": { "GameMenu": {
CurrentSelected: 0, CurrentSelected: 0,
Layout: konstructor.MenuLayout{ Layout: entity.MenuLayout{
MenuItemFont: konstructor.FontLayout{ MenuItemFont: entity.FontLayout{
DPI: 72, DPI: 72,
Size: 24, Size: 24,
Color: color.White, Color: color.White,
SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100},
}, },
}, },
MenuItems: []konstructor.MenuItem{ MenuItems: []entity.MenuItem{
{ {
ID: "save", ID: "save",
Label: "Save Game", Label: "Save Game",
@@ -63,12 +63,12 @@ func (d *Domain) InitMenu() {
} }
} }
func (d *Domain) GetMenu(name string) konstructor.Menu { func (d *Domain) GetMenu(name string) entity.Menu {
value, _ := d.MenuMap[name] value, _ := d.MenuMap[name]
return value return value
} }
func (d *Domain) SetMenu(name string, menu konstructor.Menu) { func (d *Domain) SetMenu(name string, menu entity.Menu) {
d.MenuMap[name] = menu d.MenuMap[name] = menu
} }

View File

@@ -0,0 +1,14 @@
package entity
import "github.com/hajimehoshi/ebiten"
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
}

View File

@@ -0,0 +1,30 @@
package entity
import "image/color"
type DialogMap map[string]Dialog
type DialogLayout struct {
Background string
ChoiceFont FontLayout
}
type DialogChoice struct {
ID string
Label string
Handler func()
}
type Dialog struct {
CurrentSelected int
Layout DialogLayout
Choices []DialogChoice
}
func (dialog *Dialog) GetChoiceColor(i int) color.Color {
if dialog.CurrentSelected == i {
return dialog.Layout.ChoiceFont.SelectedColor
} else {
return dialog.Layout.ChoiceFont.Color
}
}

View File

@@ -0,0 +1,10 @@
package entity
import "image/color"
type FontLayout struct {
DPI float64
Size float64
Color color.Color
SelectedColor color.Color
}

View File

@@ -0,0 +1,30 @@
package entity
import "image/color"
type MenuMap map[string]Menu
type MenuLayout struct {
Background string
MenuItemFont FontLayout
}
type MenuItem struct {
ID string
Label string
Handler func()
}
type Menu struct {
CurrentSelected int
Layout MenuLayout
MenuItems []MenuItem
}
func (menu *Menu) GetMenuItemColor(i int) color.Color {
if menu.CurrentSelected == i {
return menu.Layout.MenuItemFont.SelectedColor
} else {
return menu.Layout.MenuItemFont.Color
}
}

View File

@@ -1,8 +1,4 @@
package konstructor package entity
import (
"github.com/hajimehoshi/ebiten"
)
type Position struct { type Position struct {
X int X int
@@ -67,11 +63,3 @@ type Level struct {
Name string Name string
Playgrounds []Playground Playgrounds []Playground
} }
func (e *Engine) PlaygroundDraw(screen *ebiten.Image) {
e.Domain.GetLevel(e.KContext.CurrentLevel)
}
func (e *Engine) PlaygroundUpdate() {
e.Domain.GetLevel(e.KContext.CurrentLevel)
}

View File

@@ -0,0 +1,11 @@
package entity
type ScreenSettings struct {
Width int
Height int
}
type Settings struct {
Name string
Screen *ScreenSettings
}

View File

@@ -1,4 +1,4 @@
package konstructor package entity
type KContextScreen struct { type KContextScreen struct {
Type string Type string

View File

@@ -2,26 +2,16 @@ package konstructor
import ( import (
"fmt" "fmt"
"game/konstructor/entity"
"reflect" "reflect"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/inpututil" "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 { type Controller struct {
PressedKey ebiten.Key PressedKey ebiten.Key
KeyMap KeyMap KeyMap entity.KeyMap
} }
func (c *Controller) Watch() { func (c *Controller) Watch() {

View File

@@ -1,6 +1,7 @@
package konstructor package konstructor
import ( import (
"game/konstructor/entity"
"os" "os"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
@@ -9,8 +10,8 @@ import (
type Engine struct { type Engine struct {
Domain DomainInterface Domain DomainInterface
Controller *Controller Controller *Controller
Settings *Settings Settings *entity.Settings
KContext *KContext KContext *entity.KContext
} }
func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) { func (e *Engine) Layout(outsideWidth, outsideHeight int) (int, int) {

View File

@@ -1,6 +1,7 @@
package konstructor package konstructor
import ( import (
"game/konstructor/entity"
"log" "log"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
@@ -11,28 +12,18 @@ type ContextInterface interface {
type DomainInterface interface { type DomainInterface interface {
Init() Init()
GetMenu(name string) Menu GetMenu(name string) entity.Menu
SetMenu(name string, menu Menu) SetMenu(name string, menu entity.Menu)
GetDialog(name string) Dialog GetDialog(name string) entity.Dialog
SetDialog(name string, menu Dialog) SetDialog(name string, menu entity.Dialog)
GetLevel(index int) Level GetLevel(index int) entity.Level
}
type ScreenSettings struct {
Width int
Height int
}
type Settings struct {
Name string
Screen *ScreenSettings
} }
type Konstructor struct { type Konstructor struct {
Domain DomainInterface Domain DomainInterface
Controller *Controller Controller *Controller
Settings *Settings Settings *entity.Settings
KContext *KContext KContext *entity.KContext
} }
func (k Konstructor) Init() { func (k Konstructor) Init() {

View File

@@ -1,39 +1,10 @@
package konstructor package konstructor
import ( import (
"image/color"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text" "github.com/hajimehoshi/ebiten/text"
) )
type DialogMap map[string]Dialog
type DialogLayout struct {
Background string
ChoiceFont FontLayout
}
type DialogChoice struct {
ID string
Label string
Handler func()
}
type Dialog struct {
CurrentSelected int
Layout DialogLayout
Choices []DialogChoice
}
func (dialog *Dialog) GetChoiceColor(i int) color.Color {
if dialog.CurrentSelected == i {
return dialog.Layout.ChoiceFont.SelectedColor
} else {
return dialog.Layout.ChoiceFont.Color
}
}
func (e *Engine) DialogDraw(screen *ebiten.Image) { func (e *Engine) DialogDraw(screen *ebiten.Image) {
dialog := e.Domain.GetDialog(e.KContext.Screen.Value) dialog := e.Domain.GetDialog(e.KContext.Screen.Value)

View File

@@ -1,39 +1,10 @@
package konstructor package konstructor
import ( import (
"image/color"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/text" "github.com/hajimehoshi/ebiten/text"
) )
type MenuMap map[string]Menu
type MenuLayout struct {
Background string
MenuItemFont FontLayout
}
type MenuItem struct {
ID string
Label string
Handler func()
}
type Menu struct {
CurrentSelected int
Layout MenuLayout
MenuItems []MenuItem
}
func (menu *Menu) GetMenuItemColor(i int) color.Color {
if menu.CurrentSelected == i {
return menu.Layout.MenuItemFont.SelectedColor
} else {
return menu.Layout.MenuItemFont.Color
}
}
func (e *Engine) MenuDraw(screen *ebiten.Image) { func (e *Engine) MenuDraw(screen *ebiten.Image) {
menu := e.Domain.GetMenu(e.KContext.Screen.Value) menu := e.Domain.GetMenu(e.KContext.Screen.Value)
face := GetFontFace(menu.Layout.MenuItemFont) face := GetFontFace(menu.Layout.MenuItemFont)

View File

@@ -0,0 +1,13 @@
package konstructor
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)
}

View File

@@ -2,11 +2,11 @@ package konstructor
import ( import (
"bytes" "bytes"
"game/konstructor/entity"
"image" "image"
"io/ioutil" "io/ioutil"
"log" "log"
"image/color"
_ "image/png" _ "image/png"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
@@ -35,14 +35,7 @@ func LoadImage(path string) *ebiten.Image {
return out return out
} }
type FontLayout struct { func GetFontFace(layout entity.FontLayout) font.Face {
DPI float64
Size float64
Color color.Color
SelectedColor color.Color
}
func GetFontFace(layout FontLayout) font.Face {
tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf) tt, _ := opentype.Parse(fonts.MPlus1pRegular_ttf)
face, _ := opentype.NewFace(tt, &opentype.FaceOptions{ face, _ := opentype.NewFace(tt, &opentype.FaceOptions{
Size: layout.DPI, Size: layout.DPI,

12
main.go
View File

@@ -3,15 +3,15 @@ package main
import ( import (
"game/domain" "game/domain"
"game/konstructor" "game/konstructor"
"game/konstructor/entity"
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
) )
func main() { func main() {
k := konstructor.Konstructor{ k := konstructor.Konstructor{
KContext: &konstructor.KContext{ KContext: &entity.KContext{
Screen: konstructor.KContextScreen{ Screen: entity.KContextScreen{
Type: "playground", Type: "playground",
Value: "", Value: "",
}, },
@@ -28,7 +28,7 @@ func main() {
}, },
}, },
Controller: &konstructor.Controller{ Controller: &konstructor.Controller{
KeyMap: konstructor.KeyMap{ KeyMap: entity.KeyMap{
Up: ebiten.KeyUp, Up: ebiten.KeyUp,
Down: ebiten.KeyDown, Down: ebiten.KeyDown,
Right: ebiten.KeyRight, Right: ebiten.KeyRight,
@@ -39,9 +39,9 @@ func main() {
Action3: ebiten.KeyEscape, Action3: ebiten.KeyEscape,
}, },
}, },
Settings: &konstructor.Settings{ Settings: &entity.Settings{
Name: "Game", Name: "Game",
Screen: &konstructor.ScreenSettings{ Screen: &entity.ScreenSettings{
Width: 640, Width: 640,
Height: 480, Height: 480,
}, },