Skip to content

Commit f5169f7

Browse files
authored
Merge pull request #11 from soockee/master
fix: add entitiy definition support
2 parents ba1bbad + c14c04e commit f5169f7

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

ldtkgo.go

+48-10
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ type TileRect struct {
9090
Tileset *Tileset
9191
}
9292

93+
// An Entity represents an Entitydefintion as defined in the entities.
94+
type EntityDefinition struct {
95+
Identifier string `json:"identifier"` // Name of the Entity
96+
UID int `json:"uid"` // IID of the Entity
97+
Width int `json:"width"` // Width of the Entity in pixels
98+
Height int `json:"height"` // Height of the Entity in pixels
99+
Tags []string `json:"tags"` // Tags (categories) assigned to the Entity
100+
TileRect *TileRect `json:"tileRect"`
101+
PivotX float32 `json:"pivotX"`
102+
PivotY float32 `json:"pivotY"`
103+
}
104+
93105
// An Entity represents an Entity as placed in the LDtk level.
94106
type Entity struct {
95107
Identifier string `json:"__identifier"` // Name of the Entity
@@ -339,15 +351,16 @@ func (level *Level) PropertyByIdentifier(id string) *Property {
339351

340352
// Project represents a full LDtk Project, allowing you access to the Levels within as well as some project-level properties.
341353
type Project struct {
342-
WorldLayout string
343-
WorldGridWidth int
344-
WorldGridHeight int
345-
BGColorString string `json:"defaultLevelBgColor"`
346-
BGColor color.Color `json:"-"`
347-
JSONVersion string
348-
Levels []*Level
349-
Tilesets []*Tileset
350-
IntGridNames []string
354+
WorldLayout string
355+
WorldGridWidth int
356+
WorldGridHeight int
357+
BGColorString string `json:"defaultLevelBgColor"`
358+
BGColor color.Color `json:"-"`
359+
JSONVersion string
360+
Levels []*Level
361+
Tilesets []*Tileset
362+
IntGridNames []string
363+
EntityDefinitions []*EntityDefinition
351364
// JSONData string
352365
}
353366

@@ -398,7 +411,7 @@ func (project *Project) TilesetByIdentifier(identifier string) *Tileset {
398411
return nil
399412
}
400413

401-
// EntityByUUID returns the Entity by unique identifier specified, or nil if entity isn't found
414+
// EntityByIID returns the Entity by unique identifier specified, or nil if entity isn't found
402415
func (project *Project) EntityByIID(iid string) *Entity {
403416
for _, level := range project.Levels {
404417
for _, layer := range level.Layers {
@@ -412,6 +425,16 @@ func (project *Project) EntityByIID(iid string) *Entity {
412425
return nil
413426
}
414427

428+
// EntityDefinitionByIdentifier returns the EntityDefinition by unique identifier specified, or nil if entity isn't found
429+
func (project *Project) EntityDefinitionByIdentifier(identifier string) *EntityDefinition {
430+
for _, definition := range project.EntityDefinitions {
431+
if definition.Identifier == identifier {
432+
return definition
433+
}
434+
}
435+
return nil
436+
}
437+
415438
// Open loads the LDtk project from the filepath specified using the file system provided.
416439
// Open returns the Project and an error should the loading process fail (unable to find the file, unable to deserialize the JSON, etc).
417440
func Open(filepath string, fileSystem fs.FS) (*Project, error) {
@@ -559,6 +582,21 @@ func Read(data []byte) (*Project, error) {
559582
}
560583
}
561584

585+
entityDefinitions := []*EntityDefinition{}
586+
defsResult := gjson.Get(dataStr, `defs.entities`).Array()
587+
for _, def := range defsResult {
588+
b := []byte(def.Raw)
589+
entityDefinition := &EntityDefinition{}
590+
if err := json.Unmarshal(b, &entityDefinition); err != nil {
591+
return nil, err
592+
}
593+
if entityDefinition.TileRect != nil {
594+
entityDefinition.TileRect.Tileset = tilesetByUID[entityDefinition.TileRect.TilesetUID]
595+
}
596+
entityDefinitions = append(entityDefinitions, entityDefinition)
597+
}
598+
project.EntityDefinitions = entityDefinitions
599+
562600
return project, err
563601

564602
}

0 commit comments

Comments
 (0)