* Return an Array of instantiated objects, based on the map object definition. * Objects are created using the Tiled object factory registry, which dispatches * to the appropriate factory based on the object's class, name, or structure. * Custom factories can be registered via {@link registerT
(flatten)
| 487 | * @returns {Renderable[]} Array of Objects |
| 488 | */ |
| 489 | getObjects(flatten) { |
| 490 | const objects = []; |
| 491 | let targetContainer; |
| 492 | |
| 493 | // parse the map for objects |
| 494 | this.readMapObjects(this.data); |
| 495 | |
| 496 | for (let g = 0; g < this.objectGroups.length; g++) { |
| 497 | const group = this.objectGroups[g]; |
| 498 | |
| 499 | // check if this is the collision shape group |
| 500 | const isCollisionGroup = group.name |
| 501 | .toLowerCase() |
| 502 | .includes(COLLISION_GROUP); |
| 503 | |
| 504 | if (flatten === false) { |
| 505 | // create a new container |
| 506 | targetContainer = new Container(0, 0, this.width, this.height); |
| 507 | |
| 508 | // tiled uses 0,0 by default |
| 509 | targetContainer.anchorPoint.set(0, 0); |
| 510 | |
| 511 | // set additional properties |
| 512 | targetContainer.name = group.name; |
| 513 | targetContainer.pos.z = group.z; |
| 514 | targetContainer.setOpacity(group.opacity); |
| 515 | targetContainer.blendMode = group.blendMode; |
| 516 | |
| 517 | // disable auto-sort and auto-depth |
| 518 | targetContainer.autoSort = false; |
| 519 | targetContainer.autoDepth = false; |
| 520 | } |
| 521 | |
| 522 | // iterate through the group and add all objects into their |
| 523 | // corresponding target Container |
| 524 | for (let o = 0; o < group.objects.length; o++) { |
| 525 | const settings = group.objects[o]; |
| 526 | |
| 527 | // Tiled uses 0,0 by default |
| 528 | if (typeof settings.anchorPoint === "undefined") { |
| 529 | settings.anchorPoint = { x: 0, y: 0 }; |
| 530 | } |
| 531 | // convert to melonJS renderable argument name |
| 532 | if (typeof settings.tintcolor !== "undefined") { |
| 533 | settings.tint = parseTintColor(settings.tintcolor); |
| 534 | } |
| 535 | |
| 536 | // instantiate the object based on its type |
| 537 | const obj = createTMXObject(settings, this); |
| 538 | |
| 539 | // configure collision on unnamed objects in collision groups. |
| 540 | // Factory output uses `bodyDef` (engine-portable, declarative). |
| 541 | // We rewrite the def fields so when the world auto-registers |
| 542 | // the body, it picks up WORLD_SHAPE / static. Legacy objects |
| 543 | // that still carry a pre-constructed `body` are patched |
| 544 | // in-place for backward compatibility. |
| 545 | if (isCollisionGroup && !settings.name) { |
| 546 | if (obj.bodyDef) { |
no test coverage detected