From 7ef126f02bd04a3f3bee832a58e8e3406769182f Mon Sep 17 00:00:00 2001 From: Zsolt Tasnadi Date: Fri, 7 Jul 2023 14:00:56 +0200 Subject: [PATCH] image path helper functions --- domain/level.go | 7 ++++++- domain/type.item.go | 2 +- domain/type.npc.go | 4 ++-- domain/type.platform.go | 4 ++-- konstructor/layout.render.go | 14 ++++++++++---- konstructor/object.go | 21 +++++++++++++++++++++ konstructor/object.item.go | 4 ++++ konstructor/object.npc.go | 4 ++++ konstructor/object.platform.go | 4 ++++ konstructor/screen.playground.go | 7 +++++++ 10 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 konstructor/object.go diff --git a/domain/level.go b/domain/level.go index 4fe70fc..a54af51 100644 --- a/domain/level.go +++ b/domain/level.go @@ -2,6 +2,10 @@ package domain import "game/konstructor" +const ( + Level1Playground1 konstructor.PlaygroundID = "level_1_playground_1" +) + func (d *Domain) InitLevel() { d.Levels = []konstructor.Level{ { @@ -9,8 +13,9 @@ func (d *Domain) InitLevel() { Name: "Level I.", Playgrounds: []konstructor.Playground{ { + ID: Level1Playground1, Render: konstructor.Render{ - Image: "assets/images/playgrounds/level_1_playground_1.png", + Image: konstructor.GetPlaygroundImagePath(Level1Playground1), }, Platforms: []konstructor.Platform{ { diff --git a/domain/type.item.go b/domain/type.item.go index b2c230b..29a38b1 100644 --- a/domain/type.item.go +++ b/domain/type.item.go @@ -11,7 +11,7 @@ func (d *Domain) InitItemType() { SwordItemType: { ID: "sword", Render: konstructor.Render{ - Image: "assets/images/items/sword.png", + Image: konstructor.GetItemTypeImagePath(SwordItemType), }, }, } diff --git a/domain/type.npc.go b/domain/type.npc.go index e07050f..ca56268 100644 --- a/domain/type.npc.go +++ b/domain/type.npc.go @@ -3,14 +3,14 @@ package domain import "game/konstructor" const ( - TestNPCType konstructor.NPCTypeMapKey = "test" + TestNPCType konstructor.NPCTypeMapKey = "test_npc" ) func (d *Domain) InitNPCType() { d.NPCTypeMap = konstructor.NPCTypeMap{ TestNPCType: { Render: konstructor.Render{ - Image: "assets/images/npcs/test_npc.png", + Image: konstructor.GetNPCTypeImagePath(TestNPCType), }, }, } diff --git a/domain/type.platform.go b/domain/type.platform.go index 9541d26..6ab91dc 100644 --- a/domain/type.platform.go +++ b/domain/type.platform.go @@ -3,7 +3,7 @@ package domain import "game/konstructor" const ( - TestPlatformType konstructor.PlatformTypeMapKey = "test" + TestPlatformType konstructor.PlatformTypeMapKey = "test_platform" ) func (d *Domain) InitPlatformType() { @@ -11,7 +11,7 @@ func (d *Domain) InitPlatformType() { TestPlatformType: konstructor.PlatformType{ ID: "test_platform_type", Render: konstructor.Render{ - Image: "assets/images/platforms/test_platform.png", + Image: konstructor.GetPlatformTypeImagePath(TestPlatformType), Width: 30, Height: 30, }, diff --git a/konstructor/layout.render.go b/konstructor/layout.render.go index 7264a2c..c73a1e9 100644 --- a/konstructor/layout.render.go +++ b/konstructor/layout.render.go @@ -8,13 +8,18 @@ import ( ) type Render struct { - Image string - Width int - Height int - Visible bool + Image string + Width int + Height int + Visible bool + cachedImage image.Image } func (ro *Render) GetImage() image.Image { + if ro.cachedImage != nil { + return ro.cachedImage + } + file, err := ioutil.ReadFile(ro.Image) if err != nil { log.Fatal(err) @@ -24,5 +29,6 @@ func (ro *Render) GetImage() image.Image { if err != nil { log.Fatal(err) } + ro.cachedImage = img return img } diff --git a/konstructor/object.go b/konstructor/object.go new file mode 100644 index 0000000..2e7db60 --- /dev/null +++ b/konstructor/object.go @@ -0,0 +1,21 @@ +package konstructor + +type ObjectType string + +const ( + ItemObjectType ObjectType = "item" + NPCObjectType ObjectType = "npc" + PlatformObjectType ObjectType = "platform" +) + +type ObjectDirectoryMap map[ObjectType]string + +var ObjectDirectories = ObjectDirectoryMap{ + ItemObjectType: "items", + NPCObjectType: "npcs", + PlatformObjectType: "platforms", +} + +func GetObjectDirectory(name ObjectType) string { + return "assets/images/" + ObjectDirectories[name] + "/" +} diff --git a/konstructor/object.item.go b/konstructor/object.item.go index ded21a0..a0046a1 100644 --- a/konstructor/object.item.go +++ b/konstructor/object.item.go @@ -14,3 +14,7 @@ type Item struct { Type ItemType Position Position } + +func GetItemTypeImagePath(name ItemTypeMapKey) string { + return GetObjectDirectory(ItemObjectType) + string(name) + ".png" +} diff --git a/konstructor/object.npc.go b/konstructor/object.npc.go index 9d4b027..1e655b3 100644 --- a/konstructor/object.npc.go +++ b/konstructor/object.npc.go @@ -14,3 +14,7 @@ type NPC struct { Type NPCType Position Position } + +func GetNPCTypeImagePath(name NPCTypeMapKey) string { + return GetObjectDirectory(NPCObjectType) + string(name) + ".png" +} diff --git a/konstructor/object.platform.go b/konstructor/object.platform.go index da72856..63b4df7 100644 --- a/konstructor/object.platform.go +++ b/konstructor/object.platform.go @@ -14,3 +14,7 @@ type Platform struct { Type PlatformType Position Position } + +func GetPlatformTypeImagePath(name PlatformTypeMapKey) string { + return GetObjectDirectory(PlatformObjectType) + string(name) + ".png" +} diff --git a/konstructor/screen.playground.go b/konstructor/screen.playground.go index 9643fd4..ba4bd15 100644 --- a/konstructor/screen.playground.go +++ b/konstructor/screen.playground.go @@ -1,5 +1,7 @@ package konstructor +type PlaygroundID string + type Position struct { X int Y int @@ -7,6 +9,7 @@ type Position struct { } type Playground struct { + ID PlaygroundID Render Render Platforms []Platform NPCs []NPC @@ -18,3 +21,7 @@ type Level struct { Name string Playgrounds []Playground } + +func GetPlaygroundImagePath(name PlaygroundID) string { + return "assets/images/playgrounds/" + string(name) + ".png" +}