@@ -90,6 +90,18 @@ type TileRect struct {
90
90
Tileset * Tileset
91
91
}
92
92
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
+
93
105
// An Entity represents an Entity as placed in the LDtk level.
94
106
type Entity struct {
95
107
Identifier string `json:"__identifier"` // Name of the Entity
@@ -339,15 +351,16 @@ func (level *Level) PropertyByIdentifier(id string) *Property {
339
351
340
352
// Project represents a full LDtk Project, allowing you access to the Levels within as well as some project-level properties.
341
353
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
351
364
// JSONData string
352
365
}
353
366
@@ -398,7 +411,7 @@ func (project *Project) TilesetByIdentifier(identifier string) *Tileset {
398
411
return nil
399
412
}
400
413
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
402
415
func (project * Project ) EntityByIID (iid string ) * Entity {
403
416
for _ , level := range project .Levels {
404
417
for _ , layer := range level .Layers {
@@ -412,6 +425,16 @@ func (project *Project) EntityByIID(iid string) *Entity {
412
425
return nil
413
426
}
414
427
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
+
415
438
// Open loads the LDtk project from the filepath specified using the file system provided.
416
439
// Open returns the Project and an error should the loading process fail (unable to find the file, unable to deserialize the JSON, etc).
417
440
func Open (filepath string , fileSystem fs.FS ) (* Project , error ) {
@@ -559,6 +582,21 @@ func Read(data []byte) (*Project, error) {
559
582
}
560
583
}
561
584
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
+
562
600
return project , err
563
601
564
602
}
0 commit comments