Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by hasman

Hello,

How would I find out the index number of a particular tile in tileset?

I'm making a game in which the player has a certain number of movement points at the beginning of each turn. Movement points are subtracted from the player according to the terrain the player goes over: for example roads might have a movement cost of -1 etc.

I've created a tiled level in Weltmeister. My understanding is that as the tiles in the tilemap are indexed from 1 onwards, if I can get the tile's index number (in the tilemap) for the tile the player is on, I can then calculate the movement cost.

I've looked at the Map and BackgroundMap documentation for this but can't find something I can use.

Thanks for your help.

1 decade ago by tunglxx226

Well, idk if there's an official way supported by impactjs to do that, but pretty much you can do it on your own, even though it'd be a lil ugly.

So you have your tile size, your character's position, so you can calculate the index of the tile by

indexX = Math.floor(character.pos.x / tile.size.x);
indexY = Math.floor(character.pos.y / tile.size.y);

It's not the runnable code, just the idea.
Hope that help.

1 decade ago by Joncom

Yep, tunglxx226 has the right idea.

Keep in mind that to access the tiles, I believe it's a little backwards from what you would expect in that Y comes before X. Like this:

ig.game.backgroundMaps[0].data[y][x]

1 decade ago by hasman

Thanks for your replies, tunglxx226 and Joncom!

One clarification, because I might not have explained this properly.

tunglxx226: that code would give me the x,y position of the tile on the actual level, right?
I want to access the metadata of the tile to figure out its position on the tilemap so I can calculate the movement cost from that tile's terrain type.

Joncom: the documentation says that
 ig.game.backgroundMaps[0].data[y][x]	

returns the "2D data array for this map...", which means the (x,y) position of the tile on the level, right and not the tile's position in the tilemap -- which is what I want.

Ok, is there any way that I can embed each tile type's movement cost in Weltmeister and retrieve it?

Thanks,
Has

1 decade ago by Joncom

No, not without some heavy modification of Weltmeister which would be unnecessary.

However, perhaps you can do this: in your main.js create a variable with assigned costs to various tiles.

costs: {
    1: 5, // first tile in your tilesheet
    2: 10, // second tile...
    3: 5,
    4: 5,
    etc...
}

Now you have stored the cost values somewhere for each tile.

So now all you have to do in order to check what the cost is for tile at 10, 7 is:

var x = 10;
var y = 7;
tile_used = ig.game.backgroundMaps[0].data[y][x];
cost_of_tile = ig.game.costs[tile_used];

Ta-da! Does that help?

1 decade ago by hasman

Joncom, what an elegant solution!

Thank you so very much, it works perfectly.

Has

1 decade ago by Joncom

You're welcome. Glad you go that sorted out!
Page 1 of 1
« first « previous next › last »