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 }