image path helper functions

This commit is contained in:
2023-07-07 14:00:56 +02:00
parent cb9175cef3
commit 7ef126f02b
10 changed files with 61 additions and 10 deletions

View File

@@ -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
}

21
konstructor/object.go Normal file
View File

@@ -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] + "/"
}

View File

@@ -14,3 +14,7 @@ type Item struct {
Type ItemType
Position Position
}
func GetItemTypeImagePath(name ItemTypeMapKey) string {
return GetObjectDirectory(ItemObjectType) + string(name) + ".png"
}

View File

@@ -14,3 +14,7 @@ type NPC struct {
Type NPCType
Position Position
}
func GetNPCTypeImagePath(name NPCTypeMapKey) string {
return GetObjectDirectory(NPCObjectType) + string(name) + ".png"
}

View File

@@ -14,3 +14,7 @@ type Platform struct {
Type PlatformType
Position Position
}
func GetPlatformTypeImagePath(name PlatformTypeMapKey) string {
return GetObjectDirectory(PlatformObjectType) + string(name) + ".png"
}

View File

@@ -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"
}