summaryrefslogtreecommitdiff
path: root/engine/Components
diff options
context:
space:
mode:
Diffstat (limited to 'engine/Components')
-rw-r--r--engine/Components/ResourceManager.go36
-rw-r--r--engine/Components/TextureManager.go54
-rw-r--r--engine/Components/World.go72
3 files changed, 162 insertions, 0 deletions
diff --git a/engine/Components/ResourceManager.go b/engine/Components/ResourceManager.go
new file mode 100644
index 0000000..19f0315
--- /dev/null
+++ b/engine/Components/ResourceManager.go
@@ -0,0 +1,36 @@
+package components
+
+import (
+ "encoding/json"
+ "fmt"
+
+ settings "github.com/DegustatorPonos/RuinesOfRafdolon/Settings"
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+var Resources ResourceManager
+
+type ResourceManager struct {
+ LoadedPackages map[string]*settings.AppVersion
+ Textures TextrueManager
+ Worlds map[string]*World
+}
+
+func InitManager() {
+ Resources = ResourceManager{
+ LoadedPackages: make(map[string]*settings.AppVersion),
+ Worlds: make(map[string]*World),
+ Textures: TextrueManager{
+ Textures: make([]*rl.Texture2D, 0),
+ NameToId: make(map[string]uint64),
+ },
+ }
+}
+
+func (base *ResourceManager) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
diff --git a/engine/Components/TextureManager.go b/engine/Components/TextureManager.go
new file mode 100644
index 0000000..27449b1
--- /dev/null
+++ b/engine/Components/TextureManager.go
@@ -0,0 +1,54 @@
+package components
+
+import (
+ "encoding/json"
+ "fmt"
+
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+
+type TextrueManager struct {
+ Textures []*rl.Texture2D
+ NameToId map[string]uint64
+}
+
+// Loads the texture in the VRAM
+func (base *TextrueManager) LoadTexture(path string, displayName string) {
+ var image = rl.LoadImage(path)
+ var texture = rl.LoadTextureFromImage(image)
+ var id = len(base.Textures)
+ base.Textures = append(base.Textures, &texture)
+ base.NameToId[displayName] = uint64(id)
+ rl.UnloadImage(image)
+}
+
+// Unloads all the textures from the VRAM
+func (base *TextrueManager) UnloadAll() {
+ for _, v := range base.Textures {
+ rl.UnloadTexture(*v)
+ }
+}
+
+func (base *TextrueManager) GetTexture(Id uint64) (*rl.Texture2D, error) {
+ if Id >= uint64(len(base.Textures)) {
+ return nil, fmt.Errorf("No texture loaded with that ID")
+ }
+ return base.Textures[Id], nil
+}
+
+func (base *TextrueManager) GetTextureByName(textureName string) (*rl.Texture2D, error) {
+ var id, exists = base.NameToId[textureName]
+ if !exists {
+ return nil, fmt.Errorf("No such texture in the system")
+ }
+ return base.GetTexture(id)
+}
+
+func (base *TextrueManager) String() string {
+ var outp, jsonErr = json.Marshal(base)
+ if jsonErr != nil {
+ return fmt.Sprintf("Failed to parse settings: %s", jsonErr.Error())
+ }
+ return string(outp)
+}
diff --git a/engine/Components/World.go b/engine/Components/World.go
new file mode 100644
index 0000000..0605703
--- /dev/null
+++ b/engine/Components/World.go
@@ -0,0 +1,72 @@
+package components
+
+import (
+ coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects"
+ rl "github.com/gen2brain/raylib-go/raylib"
+)
+
+// The scene implimetation that represents one playable scene
+type World struct {
+ Manager coreobjects.SceneManager
+ Name string
+ Floor []FloorTile
+
+ Camera *rl.Camera2D
+}
+
+func (base *World) Create(manager coreobjects.SceneManager) {
+ base.Manager = manager
+}
+
+func (base *World) Destroy() {
+}
+
+func (base *World) Update() {
+ base.handleFreecam()
+}
+
+func (base *World) handleFreecam() {
+ var speed = 150 * rl.GetFrameTime()
+ var zoomSpeed = 0.2 * rl.GetFrameTime()
+ if rl.IsKeyDown(rl.KeyW) {
+ base.Camera.Target.Y -= speed
+ }
+ if rl.IsKeyDown(rl.KeyS) {
+ base.Camera.Target.Y += speed
+ }
+ if rl.IsKeyDown(rl.KeyA) {
+ base.Camera.Target.X -= speed
+ }
+ if rl.IsKeyDown(rl.KeyD) {
+ base.Camera.Target.X += speed
+ }
+ if rl.IsKeyDown(rl.KeyEqual) {
+ base.Camera.Zoom += zoomSpeed
+ }
+ if rl.IsKeyDown(rl.KeyMinus) {
+ base.Camera.Zoom -= zoomSpeed
+ }
+}
+
+func (base *World) Draw() {
+ rl.BeginMode2D(*base.Camera)
+ defer rl.EndMode2D()
+ for _, v := range base.Floor {
+ v.Draw()
+ }
+}
+
+// The single texture drawn at level 0 of the world
+type FloorTile struct {
+ Position rl.Vector2
+ Texture *rl.Texture2D
+}
+
+func (base *FloorTile) Draw() {
+ rl.DrawTexture(
+ *base.Texture,
+ int32(base.Position.X),
+ int32(base.Position.Y),
+ rl.White,
+ )
+}