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

@@ -2,6 +2,10 @@ package domain
import "game/konstructor" import "game/konstructor"
const (
Level1Playground1 konstructor.PlaygroundID = "level_1_playground_1"
)
func (d *Domain) InitLevel() { func (d *Domain) InitLevel() {
d.Levels = []konstructor.Level{ d.Levels = []konstructor.Level{
{ {
@@ -9,8 +13,9 @@ func (d *Domain) InitLevel() {
Name: "Level I.", Name: "Level I.",
Playgrounds: []konstructor.Playground{ Playgrounds: []konstructor.Playground{
{ {
ID: Level1Playground1,
Render: konstructor.Render{ Render: konstructor.Render{
Image: "assets/images/playgrounds/level_1_playground_1.png", Image: konstructor.GetPlaygroundImagePath(Level1Playground1),
}, },
Platforms: []konstructor.Platform{ Platforms: []konstructor.Platform{
{ {

View File

@@ -11,7 +11,7 @@ func (d *Domain) InitItemType() {
SwordItemType: { SwordItemType: {
ID: "sword", ID: "sword",
Render: konstructor.Render{ Render: konstructor.Render{
Image: "assets/images/items/sword.png", Image: konstructor.GetItemTypeImagePath(SwordItemType),
}, },
}, },
} }

View File

@@ -3,14 +3,14 @@ package domain
import "game/konstructor" import "game/konstructor"
const ( const (
TestNPCType konstructor.NPCTypeMapKey = "test" TestNPCType konstructor.NPCTypeMapKey = "test_npc"
) )
func (d *Domain) InitNPCType() { func (d *Domain) InitNPCType() {
d.NPCTypeMap = konstructor.NPCTypeMap{ d.NPCTypeMap = konstructor.NPCTypeMap{
TestNPCType: { TestNPCType: {
Render: konstructor.Render{ Render: konstructor.Render{
Image: "assets/images/npcs/test_npc.png", Image: konstructor.GetNPCTypeImagePath(TestNPCType),
}, },
}, },
} }

View File

@@ -3,7 +3,7 @@ package domain
import "game/konstructor" import "game/konstructor"
const ( const (
TestPlatformType konstructor.PlatformTypeMapKey = "test" TestPlatformType konstructor.PlatformTypeMapKey = "test_platform"
) )
func (d *Domain) InitPlatformType() { func (d *Domain) InitPlatformType() {
@@ -11,7 +11,7 @@ func (d *Domain) InitPlatformType() {
TestPlatformType: konstructor.PlatformType{ TestPlatformType: konstructor.PlatformType{
ID: "test_platform_type", ID: "test_platform_type",
Render: konstructor.Render{ Render: konstructor.Render{
Image: "assets/images/platforms/test_platform.png", Image: konstructor.GetPlatformTypeImagePath(TestPlatformType),
Width: 30, Width: 30,
Height: 30, Height: 30,
}, },

View File

@@ -8,13 +8,18 @@ import (
) )
type Render struct { type Render struct {
Image string Image string
Width int Width int
Height int Height int
Visible bool Visible bool
cachedImage image.Image
} }
func (ro *Render) GetImage() image.Image { func (ro *Render) GetImage() image.Image {
if ro.cachedImage != nil {
return ro.cachedImage
}
file, err := ioutil.ReadFile(ro.Image) file, err := ioutil.ReadFile(ro.Image)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@@ -24,5 +29,6 @@ func (ro *Render) GetImage() image.Image {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
ro.cachedImage = img
return 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 Type ItemType
Position Position Position Position
} }
func GetItemTypeImagePath(name ItemTypeMapKey) string {
return GetObjectDirectory(ItemObjectType) + string(name) + ".png"
}

View File

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

View File

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

View File

@@ -1,5 +1,7 @@
package konstructor package konstructor
type PlaygroundID string
type Position struct { type Position struct {
X int X int
Y int Y int
@@ -7,6 +9,7 @@ type Position struct {
} }
type Playground struct { type Playground struct {
ID PlaygroundID
Render Render Render Render
Platforms []Platform Platforms []Platform
NPCs []NPC NPCs []NPC
@@ -18,3 +21,7 @@ type Level struct {
Name string Name string
Playgrounds []Playground Playgrounds []Playground
} }
func GetPlaygroundImagePath(name PlaygroundID) string {
return "assets/images/playgrounds/" + string(name) + ".png"
}