diff --git a/domain/dialog.go b/domain/dialog.go index 56d75cf..189dd9c 100644 --- a/domain/dialog.go +++ b/domain/dialog.go @@ -1,22 +1,22 @@ package domain import ( - "game/konstructor" + "game/konstructor/entity" "image/color" ) func (d *Domain) InitDialog() { - d.DialogMap = konstructor.DialogMap{ + d.DialogMap = entity.DialogMap{ "TestDialog": { - Layout: konstructor.DialogLayout{ - ChoiceFont: konstructor.FontLayout{ + Layout: entity.DialogLayout{ + ChoiceFont: entity.FontLayout{ DPI: 72, Size: 24, Color: color.White, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, }, }, - Choices: []konstructor.DialogChoice{ + Choices: []entity.DialogChoice{ { ID: "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] return value } -func (d *Domain) SetDialog(name string, dialog konstructor.Dialog) { +func (d *Domain) SetDialog(name string, dialog entity.Dialog) { d.DialogMap[name] = dialog } diff --git a/domain/domain.go b/domain/domain.go index ba941d1..bb73136 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -1,14 +1,14 @@ package domain import ( - "game/konstructor" + "game/konstructor/entity" ) type Domain struct { Context Context - MenuMap konstructor.MenuMap - DialogMap konstructor.DialogMap - Levels []konstructor.Level + MenuMap entity.MenuMap + DialogMap entity.DialogMap + Levels []entity.Level } func (d *Domain) Init() { diff --git a/domain/level.go b/domain/level.go index cc5acc5..26cfeee 100644 --- a/domain/level.go +++ b/domain/level.go @@ -1,28 +1,26 @@ package domain -import ( - "game/konstructor" -) +import "game/konstructor/entity" func (d *Domain) InitLevel() { - d.Levels = []konstructor.Level{ + d.Levels = []entity.Level{ { ID: "level_1", Name: "Level I.", - Playgrounds: []konstructor.Playground{ + Playgrounds: []entity.Playground{ { Background: "level_1_playground_1.png", - Objects: []konstructor.Object{ + Objects: []entity.Object{ { ID: "test_object", - Position: konstructor.Position{ + Position: entity.Position{ X: 10, Y: 10, Z: 0, }, - Type: konstructor.ObjectType{ + Type: entity.ObjectType{ ID: "test_object_type", - RenderOptions: konstructor.RenderOptions{ + RenderOptions: entity.RenderOptions{ Image: "test_object.png", Width: 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] } diff --git a/domain/menu.go b/domain/menu.go index f32a475..777c9a5 100644 --- a/domain/menu.go +++ b/domain/menu.go @@ -2,24 +2,24 @@ package domain import ( "fmt" - "game/konstructor" + "game/konstructor/entity" "image/color" "os" ) func (d *Domain) InitMenu() { - d.MenuMap = konstructor.MenuMap{ + d.MenuMap = entity.MenuMap{ "MainMenu": { CurrentSelected: 0, - Layout: konstructor.MenuLayout{ - MenuItemFont: konstructor.FontLayout{ + Layout: entity.MenuLayout{ + MenuItemFont: entity.FontLayout{ DPI: 72, Size: 24, Color: color.White, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, }, }, - MenuItems: []konstructor.MenuItem{ + MenuItems: []entity.MenuItem{ { ID: "start", Label: "Start Game", @@ -39,15 +39,15 @@ func (d *Domain) InitMenu() { }, "GameMenu": { CurrentSelected: 0, - Layout: konstructor.MenuLayout{ - MenuItemFont: konstructor.FontLayout{ + Layout: entity.MenuLayout{ + MenuItemFont: entity.FontLayout{ DPI: 72, Size: 24, Color: color.White, SelectedColor: color.RGBA{R: 0, G: 255, B: 0, A: 100}, }, }, - MenuItems: []konstructor.MenuItem{ + MenuItems: []entity.MenuItem{ { ID: "save", 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] return value } -func (d *Domain) SetMenu(name string, menu konstructor.Menu) { +func (d *Domain) SetMenu(name string, menu entity.Menu) { d.MenuMap[name] = menu } diff --git a/konstructor/entity/entity.controller.go b/konstructor/entity/entity.controller.go new file mode 100644 index 0000000..853f29a --- /dev/null +++ b/konstructor/entity/entity.controller.go @@ -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 +} diff --git a/konstructor/entity/entity.dialog.go b/konstructor/entity/entity.dialog.go new file mode 100644 index 0000000..8d71e7b --- /dev/null +++ b/konstructor/entity/entity.dialog.go @@ -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 + } +} diff --git a/konstructor/entity/entity.font.go b/konstructor/entity/entity.font.go new file mode 100644 index 0000000..0895596 --- /dev/null +++ b/konstructor/entity/entity.font.go @@ -0,0 +1,10 @@ +package entity + +import "image/color" + +type FontLayout struct { + DPI float64 + Size float64 + Color color.Color + SelectedColor color.Color +} diff --git a/konstructor/entity/entity.menu.go b/konstructor/entity/entity.menu.go new file mode 100644 index 0000000..903a6c6 --- /dev/null +++ b/konstructor/entity/entity.menu.go @@ -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 + } +} diff --git a/konstructor/playground.go b/konstructor/entity/entity.playground.go similarity index 72% rename from konstructor/playground.go rename to konstructor/entity/entity.playground.go index 4ae0dd0..ec6bd57 100644 --- a/konstructor/playground.go +++ b/konstructor/entity/entity.playground.go @@ -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 +} diff --git a/konstructor/entity/entity.settings.go b/konstructor/entity/entity.settings.go new file mode 100644 index 0000000..41e18d5 --- /dev/null +++ b/konstructor/entity/entity.settings.go @@ -0,0 +1,11 @@ +package entity + +type ScreenSettings struct { + Width int + Height int +} + +type Settings struct { + Name string + Screen *ScreenSettings +} diff --git a/konstructor/kcontext.go b/konstructor/entity/konstructor.kcontext.go similarity index 86% rename from konstructor/kcontext.go rename to konstructor/entity/konstructor.kcontext.go index e539031..ddf6a1c 100644 --- a/konstructor/kcontext.go +++ b/konstructor/entity/konstructor.kcontext.go @@ -1,4 +1,4 @@ -package konstructor +package entity type KContextScreen struct { Type string diff --git a/konstructor/controller.go b/konstructor/konstructor.controller.go similarity index 81% rename from konstructor/controller.go rename to konstructor/konstructor.controller.go index 5d6ccba..c9841c4 100644 --- a/konstructor/controller.go +++ b/konstructor/konstructor.controller.go @@ -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 +} diff --git a/konstructor/engine.go b/konstructor/konstructor.engine.go similarity index 91% rename from konstructor/engine.go rename to konstructor/konstructor.engine.go index 7e0a173..cf775ff 100644 --- a/konstructor/engine.go +++ b/konstructor/konstructor.engine.go @@ -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) { diff --git a/konstructor/konstructor.go b/konstructor/konstructor.go index 8e9f2d0..87a9491 100644 --- a/konstructor/konstructor.go +++ b/konstructor/konstructor.go @@ -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() { diff --git a/konstructor/dialog.go b/konstructor/konstructor.screen.dialog.go similarity index 63% rename from konstructor/dialog.go rename to konstructor/konstructor.screen.dialog.go index a9fdfa1..4c126c5 100644 --- a/konstructor/dialog.go +++ b/konstructor/konstructor.screen.dialog.go @@ -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) diff --git a/konstructor/menu.go b/konstructor/konstructor.screen.menu.go similarity index 61% rename from konstructor/menu.go rename to konstructor/konstructor.screen.menu.go index 74d0adc..83230b7 100644 --- a/konstructor/menu.go +++ b/konstructor/konstructor.screen.menu.go @@ -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) +} diff --git a/konstructor/konstructor.screen.playground.go b/konstructor/konstructor.screen.playground.go new file mode 100644 index 0000000..51f6a18 --- /dev/null +++ b/konstructor/konstructor.screen.playground.go @@ -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) +} diff --git a/konstructor/utils.go b/konstructor/konstructor.utils.go similarity index 75% rename from konstructor/utils.go rename to konstructor/konstructor.utils.go index c1f2bd8..291f7a0 100644 --- a/konstructor/utils.go +++ b/konstructor/konstructor.utils.go @@ -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 +} diff --git a/main.go b/main.go index 66b9657..3fb726c 100644 --- a/main.go +++ b/main.go @@ -3,15 +3,15 @@ package main import ( "game/domain" "game/konstructor" + "game/konstructor/entity" "github.com/hajimehoshi/ebiten" ) func main() { - k := konstructor.Konstructor{ - KContext: &konstructor.KContext{ - Screen: konstructor.KContextScreen{ + KContext: &entity.KContext{ + Screen: entity.KContextScreen{ Type: "playground", Value: "", }, @@ -28,7 +28,7 @@ func main() { }, }, Controller: &konstructor.Controller{ - KeyMap: konstructor.KeyMap{ + KeyMap: entity.KeyMap{ Up: ebiten.KeyUp, Down: ebiten.KeyDown, Right: ebiten.KeyRight, @@ -39,9 +39,9 @@ func main() { Action3: ebiten.KeyEscape, }, }, - Settings: &konstructor.Settings{ + Settings: &entity.Settings{ Name: "Game", - Screen: &konstructor.ScreenSettings{ + Screen: &entity.ScreenSettings{ Width: 640, Height: 480, },