Spawning entities in from a tiled tilemap

Post your help queries and engine programming questions here

Spawning entities in from a tiled tilemap

Postby Riowind » Sat Sep 17, 2016 1:26 am

Right now I load in a json file with the igeTiledComponent to display the different layers and create some static collisions. My question is how do I spawn in or create entities based off a specific layer in tiled, or maybe even an object layer?

I don't know if this is the right way to do it but as of now i find my respawn coordinates by grabbing random numbers until they aren't within my collision layer's _mapData. I found this in one of the examples when they were creating AI, but when I have more layers to sort through the logic gets insane and it's not usable in practice.

So is there a way to put down some objects in tiled, maybe even a rectangle, and designate it to be made into an entity once ige reads it?

Thank you!
Riowind
 
Posts: 8
Joined: Thu Oct 29, 2015 7:55 pm

Re: Spawning entities in from a tiled tilemap

Postby m0dE » Sat Sep 17, 2016 1:53 am

Use IgeCellSheet!

e.g.

Code: Select all
var tiles = new IgeCellSheet('./tilesheet.png', 20, 20);  // there are 20x20 tiles in the tilesheet
var specificTile = tiles.textureFromCell(25); // this selects tile (x: 5, y:1)

entity = new IgeEntityBox2d();
entity.texture(specificTile)


*Code above has not been tested
m0dE
 
Posts: 6
Joined: Fri Aug 26, 2016 4:36 am

Re: Spawning entities in from a tiled tilemap

Postby Riowind » Mon Sep 26, 2016 4:28 am

I figured it out. In Tiled I made a object in an object layer named "entity" and gave the object a type of one of an existing entity's name

Code: Select all
ige.addComponent(IgeTiledComponent)
   .tiled.loadJson(Test_150x150, function (layerArray, layersById) {

   // Create collision boxes from the layers in the map
   ige.box2d.staticsFromMap(layersById.collision);

   // Objects' type must be manually set to class name
   // This is an Object layer
   var entities = layersById['entity'].objects;
   for (var ent in entities) {
      if (entities.hasOwnProperty(ent)) {
         var entityData = entities[ent];

         if (entityData.type) {
            new this[entityData.type]()
               .translateTo(entityData.x, entityData.y, 0)
               .mount(self.scene1)
         } else {
            console.log("Object type not set. Give it a type in Tiled")
         }
      }
   }
Riowind
 
Posts: 8
Joined: Thu Oct 29, 2015 7:55 pm


Return to Help & Questions

Who is online

Users browsing this forum: No registered users and 1 guest
cron