summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
Diffstat (limited to 'engine')
-rw-r--r--engine/Components/Packages/Package.go70
-rw-r--r--engine/Components/World/Descriptor.go29
-rw-r--r--engine/Components/World/Tile.go12
-rw-r--r--engine/Components/World/World.go11
-rw-r--r--engine/Dynamic/Common.go (renamed from engine/Components/Packages/Common.go)2
-rw-r--r--engine/Dynamic/Descriptors/Assets.go5
-rw-r--r--engine/Dynamic/Descriptors/Tile.go24
-rw-r--r--engine/Dynamic/Descriptors/World.go28
-rw-r--r--engine/Dynamic/Manager.go (renamed from engine/Components/Packages/Manager.go)16
-rw-r--r--engine/Dynamic/Package.go118
-rw-r--r--engine/Dynamic/Validation.go (renamed from engine/Components/Packages/Validation.go)2
-rw-r--r--engine/Packages/TestPackage/Description.json5
-rw-r--r--engine/Packages/TestPackage/Textures/Meta.json4
-rw-r--r--engine/Packages/TestPackage/Worlds/Main.json12
-rw-r--r--engine/Settings/AppVersion.go24
-rw-r--r--engine/Settings/Settings.go7
-rw-r--r--engine/main.go47
17 files changed, 244 insertions, 172 deletions
diff --git a/engine/Components/Packages/Package.go b/engine/Components/Packages/Package.go
deleted file mode 100644
index 9f2fdd2..0000000
--- a/engine/Components/Packages/Package.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package packages
-
-import (
- "encoding/json"
- "fmt"
- "os"
-
- world "github.com/DegustatorPonos/RuinesOfRafdolon/Components/World"
- settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
- rl "github.com/gen2brain/raylib-go/raylib"
-)
-
-type PackageDescription struct {
- Name string `json:"name"`
- Version uint64 `json:"version"`
- Type string `json:"type"`
- MinimumVersion *settings.AppVersion
- MaximumVersion *settings.AppVersion
-}
-
-type Package struct {
- Description PackageDescription `json:"description"`
- Worlds map[string]world.World `json:"-"`
-}
-
-func (base *Package) String() string {
- var outp, jsonErr = json.Marshal(base)
- if jsonErr != nil {
- return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
- }
- return string(outp)
-}
-
-func ReadPackage(dir os.DirEntry) (*Package, error) {
- var dirPath = ConcantinateFileLocation(settings.Current.PackagesLocation, dir.Name())
- var desc = PackageDescription{}
- if descErr := ReadValidJSONfromFile(ConcantinateFileLocation(dirPath, "Description.json"), &desc); descErr != nil {
- return nil, descErr
- }
- return &Package{
- Description: desc,
- }, nil
-}
-
-func (base *PackageDescription) IsValid() error {
- if base.Name == "" {
- return fmt.Errorf("Invalid package descriptor: the name cannot be empty")
- }
- if base.Version == 0{
- return fmt.Errorf("Invalid package descriptor: the version cannot be 0")
- }
- return nil
-}
-
-func loadWorldsFromDir(dirLocation string) (map[string]world.World, error) {
- var files, err = os.ReadDir(dirLocation)
- if err != nil {
- return nil, err
- }
- var outp = make(map[string]world.World)
- for _, v := range files {
- var new = world.World{}
- var loadErr = ReadJSONfromFile(ConcantinateFileLocation(dirLocation, v.Name()), &new)
- if loadErr != nil {
- rl.TraceLog(rl.LogWarning, "Failed to load the world %s: %s", v.Name(), loadErr)
- continue
- }
- }
- return outp, nil
-}
diff --git a/engine/Components/World/Descriptor.go b/engine/Components/World/Descriptor.go
deleted file mode 100644
index 553aa5b..0000000
--- a/engine/Components/World/Descriptor.go
+++ /dev/null
@@ -1,29 +0,0 @@
-package world
-
-import rl "github.com/gen2brain/raylib-go/raylib"
-
-type Descriptor struct {
- TileSize rl.Vector2
- Textures map[int]string `json:"textures"`
- WorldMap [][]TileDescriptor `json:"worldmap"`
-}
-
-func (base *Descriptor) GenerateMap() *World {
- var outp = &World {
- TileSize: base.TileSize,
- TextureNames: base.Textures,
- Tiles: make([][]Tile, len(base.WorldMap)),
- }
- for x := range base.WorldMap {
- outp.Tiles[x] = make([]Tile, len(base.WorldMap[x]))
- for y, tile := range base.WorldMap[x] {
- outp.Tiles[x][y] = &StandardTile {
- X: float32(x),
- Y: float32(y),
- ParentWorld: outp,
- Descriptor: tile,
- }
- }
- }
- return outp
-}
diff --git a/engine/Components/World/Tile.go b/engine/Components/World/Tile.go
index a630062..0ef3a00 100644
--- a/engine/Components/World/Tile.go
+++ b/engine/Components/World/Tile.go
@@ -1,6 +1,9 @@
package world
-import rl "github.com/gen2brain/raylib-go/raylib"
+import (
+ descriptors "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic/Descriptors"
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
// The square that will be displayed at the screen
type Tile interface {
@@ -12,7 +15,7 @@ type StandardTile struct {
X float32
Y float32
ParentWorld *World
- Descriptor TileDescriptor
+ Descriptor descriptors.TileDescriptor
}
func (base *StandardTile) Update() {
@@ -34,8 +37,3 @@ func (base *StandardTile) Draw() {
rl.White)
}
}
-
-type TileDescriptor struct {
- TextureId int `json:"textureid"`
- OveralyTextureId int `json:"overalytextureid"`
-}
diff --git a/engine/Components/World/World.go b/engine/Components/World/World.go
index 80ef7e9..d360adc 100644
--- a/engine/Components/World/World.go
+++ b/engine/Components/World/World.go
@@ -9,11 +9,12 @@ var Texture rl.Texture2D
// The scene full of tiles
type World struct {
- TileSize rl.Vector2
- TextureNames map[int]string
- Textures map[int]rl.Texture2D
- Tiles [][]Tile
- Camera rl.Camera2D
+ Name string `json:"name"`
+ TileSize rl.Vector2 `json:"tilesize"`
+ TextureNames map[int]string `json:"textures"`
+ Textures map[int]rl.Texture2D `json:"-"`
+ Tiles [][]Tile `json:"tiles"`
+ Camera rl.Camera2D `json:"-"`
}
func (base *World) IsValid() error {
diff --git a/engine/Components/Packages/Common.go b/engine/Dynamic/Common.go
index 0564f08..99cfdc2 100644
--- a/engine/Components/Packages/Common.go
+++ b/engine/Dynamic/Common.go
@@ -1,4 +1,4 @@
-package packages
+package dynamic
import (
"encoding/json"
diff --git a/engine/Dynamic/Descriptors/Assets.go b/engine/Dynamic/Descriptors/Assets.go
new file mode 100644
index 0000000..864761d
--- /dev/null
+++ b/engine/Dynamic/Descriptors/Assets.go
@@ -0,0 +1,5 @@
+package descriptors
+
+type AssetsDescriptor struct {
+ Textures map[int]string `json:"textures"`
+}
diff --git a/engine/Dynamic/Descriptors/Tile.go b/engine/Dynamic/Descriptors/Tile.go
new file mode 100644
index 0000000..09c8c62
--- /dev/null
+++ b/engine/Dynamic/Descriptors/Tile.go
@@ -0,0 +1,24 @@
+package descriptors
+
+import rl "github.com/gen2brain/raylib-go/raylib"
+
+// Represents a type of a tile that can be used in the world
+type TileDescriptor struct {
+ Id uint64 `json:"id"`
+ Position rl.Vector2 `json:"position"`
+ TextureId int `json:"textureid"`
+ OveralyTextureId int `json:"overalytextureid"`
+}
+
+func (base *TileDescriptor) IsValid() error {
+ return nil
+}
+
+// Maps the tiles by IDs
+func MapTileDescriptors(data []*TileDescriptor) map[uint64]*TileDescriptor {
+ var outp = make(map[uint64]*TileDescriptor)
+ for _, v := range data {
+ outp[v.Id] = v
+ }
+ return outp
+}
diff --git a/engine/Dynamic/Descriptors/World.go b/engine/Dynamic/Descriptors/World.go
new file mode 100644
index 0000000..00e17f3
--- /dev/null
+++ b/engine/Dynamic/Descriptors/World.go
@@ -0,0 +1,28 @@
+package descriptors
+
+import rl "github.com/gen2brain/raylib-go/raylib"
+
+type WorldDescriptor struct {
+ Name string
+ // Deprecated
+ TileSize rl.Vector2 `json:"-"`
+ FloorMap []FloorPiece
+}
+
+func (base *WorldDescriptor) IsValid() error {
+ return nil
+}
+
+// Maps the world descriptor by IDs
+func MapWorldDescriptors(data []*WorldDescriptor) map[string]*WorldDescriptor {
+ var outp = make(map[string]*WorldDescriptor)
+ for _, v := range data {
+ outp[v.Name] = v
+ }
+ return outp
+}
+
+type FloorPiece struct {
+ Position rl.Vector2
+ TextureId uint64
+}
diff --git a/engine/Components/Packages/Manager.go b/engine/Dynamic/Manager.go
index 8a2786b..7e5a567 100644
--- a/engine/Components/Packages/Manager.go
+++ b/engine/Dynamic/Manager.go
@@ -1,4 +1,4 @@
-package packages
+package dynamic
import (
"fmt"
@@ -30,19 +30,13 @@ func loadPackages() (*PackageManager, error) {
rl.TraceLog(rl.LogWarning, "Failed to load the module %v: %s", packagePath, packageErr.Error())
continue
}
+ newPackage.location = packagePath
- // Resource packages cannot contain worlds
- if newPackage.Description.Type == "story" {
- var worlds, worldsErr = loadWorldsFromDir(ConcantinateFileLocation(packagePath, "Worlds"))
- if worldsErr != nil {
- rl.TraceLog(rl.LogWarning, "Failed to load the world from the module %s: %s", v.Name(), worldsErr)
- } else {
- newPackage.Worlds = worlds
- }
- }
+ // newPackage.LoadTiles()
+ newPackage.LoadWorlds()
outp.AvaliablePackages[newPackage.Description.Name] = *newPackage
- rl.TraceLog(rl.LogError, "Loaded the module %s", newPackage.Description.Name)
+ rl.TraceLog(rl.LogInfo, "Loaded the module %s", newPackage.Description.Name)
}
return &outp, nil
}
diff --git a/engine/Dynamic/Package.go b/engine/Dynamic/Package.go
new file mode 100644
index 0000000..8cabc91
--- /dev/null
+++ b/engine/Dynamic/Package.go
@@ -0,0 +1,118 @@
+package dynamic
+
+import (
+ "encoding/json"
+ "fmt"
+ "os"
+
+ descriptors "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic/Descriptors"
+ settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+// The defenition of the package
+type PackageDescription struct {
+ Name string `json:"name"`
+ Version uint64 `json:"version"`
+ Type string `json:"type"`
+ MinimumVersion *settings.AppVersion
+ MaximumVersion *settings.AppVersion
+}
+
+func (base *PackageDescription) IsValid() error {
+ if base.Name == "" {
+ return fmt.Errorf("Invalid package descriptor: the name cannot be empty")
+ }
+ if base.Version == 0 {
+ return fmt.Errorf("Invalid package descriptor: the version cannot be 0")
+ }
+ if base.MinimumVersion == nil {
+ return fmt.Errorf("Invalid package descriptor: The package must specify the minimal version")
+ }
+ if !base.MinimumVersion.IsCompatible() || (base.MaximumVersion != nil && !base.MaximumVersion.IsLessThan(&settings.Current.Version)){
+ return fmt.Errorf("Invalid package descriptor: The package is made for the newer or older version")
+ }
+ return nil
+}
+
+// The dynamic collection of the things
+type Package struct {
+ Description PackageDescription
+ Worlds map[string]*descriptors.WorldDescriptor
+
+ location string
+}
+
+func (base *Package) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
+
+func ReadPackage(dir os.DirEntry) (*Package, error) {
+ var dirPath = ConcantinateFileLocation(settings.Current.PackagesLocation, dir.Name())
+ var desc = PackageDescription{}
+ if descErr := ReadValidJSONfromFile(ConcantinateFileLocation(dirPath, "Description.json"), &desc); descErr != nil {
+ return nil, descErr
+ }
+ return &Package{
+ Description: desc,
+ }, nil
+}
+
+func loadAssetsFromDir[T Validatable](dirLocation string, InitializeFunc func()T) ([]T, error) {
+ var files, err = os.ReadDir(dirLocation)
+ if err != nil {
+ return nil, err
+ }
+ var outp = make([]T, 0)
+ for _, v := range files {
+ var new = InitializeFunc()
+ var loadErr = ReadValidJSONfromFile(ConcantinateFileLocation(dirLocation, v.Name()), new)
+ if loadErr != nil {
+ rl.TraceLog(rl.LogWarning, "Failed to load the asset %s: %s", v.Name(), loadErr)
+ continue
+ }
+ outp = append(outp, new)
+ }
+ return outp, nil
+}
+
+func (base *Package) LoadWorlds() {
+ if base.Description.Type != "story" {
+ return
+ }
+ var worlds, loadErr = loadWorldsFromDir(ConcantinateFileLocation(base.location, "Worlds"))
+ if loadErr != nil {
+ rl.TraceLog(rl.LogWarning, "Failed to load world from the module %s: %s", base.location, loadErr)
+ } else {
+ base.Worlds = worlds
+ }
+}
+
+// func (base *Package) LoadTiles() {
+ // var tiles, loadErr = loadTilesFromDir(ConcantinateFileLocation(base.location, "Worlds"))
+ // if loadErr != nil {
+ // rl.TraceLog(rl.LogWarning, "Failed to load tiles from the module %s: %s", base.location, loadErr)
+ // } else {
+ // base.Tiles = tiles
+ // }
+// }
+
+func loadWorldsFromDir(dirLocation string) (map[string]*descriptors.WorldDescriptor, error) {
+ var loaded, err = loadAssetsFromDir(dirLocation, func() *descriptors.WorldDescriptor{ return &descriptors.WorldDescriptor{} })
+ if err != nil {
+ return nil, err
+ }
+ return descriptors.MapWorldDescriptors(loaded), nil
+}
+
+func loadTilesFromDir(dirLocation string) (map[uint64]*descriptors.TileDescriptor, error) {
+ var loaded, err = loadAssetsFromDir(dirLocation, func() *descriptors.TileDescriptor{ return &descriptors.TileDescriptor{} })
+ if err != nil {
+ return nil, err
+ }
+ return descriptors.MapTileDescriptors(loaded), nil
+}
diff --git a/engine/Components/Packages/Validation.go b/engine/Dynamic/Validation.go
index 04428d2..dfe1d5c 100644
--- a/engine/Components/Packages/Validation.go
+++ b/engine/Dynamic/Validation.go
@@ -1,4 +1,4 @@
-package packages
+package dynamic
type Validatable interface {
// If the object is returned invalid this function should return the reason
diff --git a/engine/Packages/TestPackage/Description.json b/engine/Packages/TestPackage/Description.json
index b0defb0..9fd287f 100644
--- a/engine/Packages/TestPackage/Description.json
+++ b/engine/Packages/TestPackage/Description.json
@@ -6,10 +6,5 @@
"MajorVersion": 0,
"MinorVersion": 0,
"Patch": 1
- },
- "MaximumVersion": {
- "MajorVersion": 0,
- "MinorVersion": 0,
- "Patch": 1
}
}
diff --git a/engine/Packages/TestPackage/Textures/Meta.json b/engine/Packages/TestPackage/Textures/Meta.json
new file mode 100644
index 0000000..6ca8df9
--- /dev/null
+++ b/engine/Packages/TestPackage/Textures/Meta.json
@@ -0,0 +1,4 @@
+{
+ "Avaliable": [
+ ]
+}
diff --git a/engine/Packages/TestPackage/Worlds/Main.json b/engine/Packages/TestPackage/Worlds/Main.json
index e69de29..afaa4df 100644
--- a/engine/Packages/TestPackage/Worlds/Main.json
+++ b/engine/Packages/TestPackage/Worlds/Main.json
@@ -0,0 +1,12 @@
+{
+ "Name": "MainWorld",
+ "FloorMap": [
+ {
+ "Position": {
+ "X": 1,
+ "Y": 0
+ },
+ "TextureId": 1
+ }
+ ]
+}
diff --git a/engine/Settings/AppVersion.go b/engine/Settings/AppVersion.go
new file mode 100644
index 0000000..d21fd1a
--- /dev/null
+++ b/engine/Settings/AppVersion.go
@@ -0,0 +1,24 @@
+package settings
+
+type AppVersion struct {
+ MajorVersion uint
+ MinorVersion uint
+ Patch uint
+ IsBeta bool
+}
+
+func (base *AppVersion) IsGreaterThan(other *AppVersion) bool {
+ return base.MajorVersion > other.MajorVersion || base.MinorVersion > other.MinorVersion || base.Patch > other.Patch
+}
+
+func (base *AppVersion) IsEqualTo(other *AppVersion) bool {
+ return base.MajorVersion == other.MajorVersion && base.MinorVersion == other.MinorVersion && base.Patch == other.Patch
+}
+
+func (base *AppVersion) IsLessThan(other *AppVersion) bool {
+ return base.MajorVersion < other.MajorVersion && base.MinorVersion < other.MinorVersion && base.Patch < other.Patch
+}
+
+func (base *AppVersion) IsCompatible() bool {
+ return Current.Version.MajorVersion >= base.MajorVersion && Current.Version.MinorVersion >= base.MinorVersion && Current.Version.Patch >= base.Patch
+}
diff --git a/engine/Settings/Settings.go b/engine/Settings/Settings.go
index a0490cc..f943203 100644
--- a/engine/Settings/Settings.go
+++ b/engine/Settings/Settings.go
@@ -6,13 +6,6 @@ import (
"os"
)
-type AppVersion struct {
- MajorVersion uint
- MinorVersion uint
- Patch uint
- IsBeta bool
-}
-
type Settings struct {
Version AppVersion
PackagesLocation string
diff --git a/engine/main.go b/engine/main.go
index b45ac2a..5468217 100644
--- a/engine/main.go
+++ b/engine/main.go
@@ -1,52 +1,27 @@
package main
import (
- packages "github.com/DegustatorPonos/RuinesOfRafdolon/Components/Packages"
- world "github.com/DegustatorPonos/RuinesOfRafdolon/Components/World"
+ "log"
+
coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects"
+ dynamic "github.com/DegustatorPonos/RuinesOfRafdolon/Dynamic"
render "github.com/DegustatorPonos/RuinesOfRafdolon/Render"
settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
- rl "github.com/gen2brain/raylib-go/raylib"
)
func main() {
settings.ReadSettings()
- packages.Init()
- // TEMPORARY SECTION
- // for _, v := range packages.Manager.AvaliablePackages {
- // fmt.Println(&v)
- // }
+ dynamic.Init()
- var descriptor = world.Descriptor {
- TileSize: rl.Vector2 { X: 32, Y: 32 },
- Textures: map[int]string{
- 0: "assets/tile.png",
- 1: "assets/water.png",
- 2: "assets/tree.png",
- },
- WorldMap: [][]world.TileDescriptor {
- []world.TileDescriptor {
- world.TileDescriptor{
- TextureId: 0,
- },
- world.TileDescriptor{
- TextureId: 0,
- OveralyTextureId: 2,
- },
- },
- []world.TileDescriptor {
- world.TileDescriptor{
- TextureId: 1,
- },
- world.TileDescriptor{
- TextureId: 1,
- },
- },
- },
+ // TEMPORARY SECTION
+ for k, v := range dynamic.Manager.AvaliablePackages {
+ log.Printf("%s: %s", k, &v)
}
- var field = descriptor.GenerateMap()
+ return
+
+ // var field = descriptor.GenerateMap()
var manager = coreobjects.InitSceneManager()
- render.InitWindow(manager, field)
+ render.InitWindow(manager, nil)
}