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

@@ -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,77 +1,65 @@
package konstructor
import (
"github.com/hajimehoshi/ebiten"
)
type Position struct {
X int
Y int
Z int
}
type RenderOptions struct {
Image string
Width int
Height int
}
type ObjectType struct {
ID string
RenderOptions RenderOptions
}
type Object struct {
ID string
Type ObjectType
Position Position
}
type NPCType struct {
ID string
RenderOptions RenderOptions
}
type NPC struct {
ID string
Type NPCType
Position Position
}
type Player struct {
ID string
RenderOptions RenderOptions
Position Position
}
type ItemType struct {
ID string
RenderOptions RenderOptions
}
type Item struct {
ID string
Type ItemType
Position Position
}
type Playground struct {
Background string
Objects []Object
NPCs []NPC
Items []Item
}
type Level struct {
ID string
Name string
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)
}
package entity
type Position struct {
X int
Y int
Z int
}
type RenderOptions struct {
Image string
Width int
Height int
}
type ObjectType struct {
ID string
RenderOptions RenderOptions
}
type Object struct {
ID string
Type ObjectType
Position Position
}
type NPCType struct {
ID string
RenderOptions RenderOptions
}
type NPC struct {
ID string
Type NPCType
Position Position
}
type Player struct {
ID string
RenderOptions RenderOptions
Position Position
}
type ItemType struct {
ID string
RenderOptions RenderOptions
}
type Item struct {
ID string
Type ItemType
Position Position
}
type Playground struct {
Background string
Objects []Object
NPCs []NPC
Items []Item
}
type Level struct {
ID string
Name string
Playgrounds []Playground
}

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 string

View File

@@ -1,73 +1,63 @@
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
}
package konstructor
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
}

View File

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

View File

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

View File

@@ -1,39 +1,10 @@
package konstructor
import (
"image/color"
"github.com/hajimehoshi/ebiten"
"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) {
dialog := e.Domain.GetDialog(e.KContext.Screen.Value)

View File

@@ -1,64 +1,35 @@
package konstructor
import (
"image/color"
"github.com/hajimehoshi/ebiten"
"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) {
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)
}
package konstructor
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)
}

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

@@ -1,53 +1,46 @@
package konstructor
import (
"bytes"
"image"
"io/ioutil"
"log"
"image/color"
_ "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
}
type FontLayout struct {
DPI float64
Size float64
Color color.Color
SelectedColor color.Color
}
func GetFontFace(layout 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
}
package konstructor
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
}