summaryrefslogtreecommitdiff
path: root/engine/Dynamic/Descriptors/World.go
blob: f0328fdfbccaa372e34b211abed6a2665e2cff5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package descriptors

import (
	components "github.com/DegustatorPonos/RuinesOfRafdolon/Components"
	coreobjects "github.com/DegustatorPonos/RuinesOfRafdolon/CoreObjects"
	rl "github.com/gen2brain/raylib-go/raylib"
)

const WorldsDirName string = "Worlds"

type WorldDescriptor struct {
	Name string
	DefaultStartingPosition rl.Vector2
	// Deprecated
	TileSize rl.Vector2 `json:"-"` 
	FloorMap []FloorPiece
	StaticObjects []ObjectLink
}

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
	Texture string 
}

type ObjectLink struct {
	Object string
	Position rl.Vector2
}

// Transforms the world descriptor into the game world
// Assumes that objects and textures have already been loaded
func (base *WorldDescriptor) Parse() components.World {
	var outp = components.World {
		Name: base.Name,
		Floor: make([]components.FloorTile, 0, len(base.FloorMap)),
		Player: &components.Player{
			Texture: *components.Resources.Textures.Textures[3], // TODO: Change
			Position: base.DefaultStartingPosition,
		},
		StaticObjects: base.parseObjects(),

		Camera: &rl.Camera2D{
			Offset: rl.Vector2 {X: 0, Y: 0},
			Target: rl.Vector2 {X: 0, Y: 0},
			Rotation: 0,
			Zoom: 1,
		},
	}

	for _, v := range base.FloorMap {
		var texture, textureErr = components.Resources.Textures.GetTextureByName(v.Texture)
		if textureErr != nil {
			continue
		}
		var new = components.FloorTile {
			Position: v.Position,
			Texture: texture,
		}
		outp.Floor = append(outp.Floor, new)
	}
	return outp
}

func (base *WorldDescriptor) parseObjects() []coreobjects.GameObject {
	var outp = make([]coreobjects.GameObject, 0, len(base.StaticObjects))
	for _, v := range base.StaticObjects {
		var model, modelErr = components.Resources.GetObject(v.Object)
		if modelErr != nil {
			rl.TraceLog(rl.LogWarning, "Failed to parse an object in the world %s: %s", base.Name, modelErr.Error())
			continue
		}
		var clone = model.Clone()
		clone.MoveTo(v.Position.X, v.Position.Y)
		outp = append(outp, clone)
	}
	return outp
}