Impact icon indicating copy to clipboard operation
Impact copied to clipboard

Get surrounding tiles x,y position and check if passable

Open rontrek opened this issue 6 years ago • 13 comments
trafficstars

How do I get the x and y position of the surrounding tiles of a player? I am trying to place a NPC beside a vacant tile and to take specific action, not collide or moving at the exact position of the player.

rontrek avatar Aug 30 '19 12:08 rontrek

First calculate the position of the player based on his pos.x, pos.y, size.x, and size.y, and then just add or subtract 1` tile depending if you want left, right, up, or down.

Joncom avatar Aug 31 '19 10:08 Joncom

Thanks @Joncom, but that would just be with position only and not knowing about the info of the tile if it is passable. Basically, I would like to get info on the tile if can be walkable and then get the position to move to it. I read somewhere if you can get the tile index? you can get more info about the tile.

rontrek avatar Sep 01 '19 00:09 rontrek

You could also combine it with checking ig.game.collisionMap.data to see which tiles are passable. In general, a collision tile with value of 0 is passable, while a tile with a value of 1 is solid.

Joncom avatar Sep 01 '19 01:09 Joncom

Thanks, I can see now the results. I understand now that the numbering is also from the collision map tile set image that is used if I'm not mistaken.

Does getTile and setTile only works on collision maps? I tried using on my LevelMyMap variable that is used in loadLevel() but getting undefined errors.

Can I also get info of a tile if it is also occupied by an Entity?

rontrek avatar Sep 01 '19 09:09 rontrek

getTile and setTile work on every kind of map, however note that they take absolute/pixel, not tile, coordinates, and then calculate which tile that is.

Joncom avatar Sep 01 '19 12:09 Joncom

An entity on a tile won’t affect the collision map data. Entity collisions are a different thing.

Joncom avatar Sep 01 '19 12:09 Joncom

I got it now, thanks for the info.

So is there a way to know if a tile is occupied by an Entity? I need my entity to look ahead if a tile is occupied by another entity so it can decide to move to another tile.

rontrek avatar Sep 02 '19 02:09 rontrek

You could loop through the entities array and check if each entity (based on its pos and size) is currently overlapping the tile you’re interested in.

Joncom avatar Sep 02 '19 02:09 Joncom

Thanks. I see some examples on how to loop specific entities, but how do you check for a tile overlap vs entity? Is there a command or can you give a snippet example?

rontrek avatar Sep 02 '19 07:09 rontrek

Take a look at the code inside ig.Entity.touches. The premise is the same whether you’re checking for entity vs tile, or entity vs entity. Just pretend the tile is an entity with a pos where the tile starts, and a width and height of tilesize.

Joncom avatar Sep 02 '19 07:09 Joncom

Thanks. I'm getting somewhere now, but still having issues with it.

btw, any thoughts on this diagonal issue thing? I'm tying my entity routines with hurik's a star plugin that you have commented a few days ago.

rontrek avatar Sep 03 '19 12:09 rontrek

Haven’t had a chance to look at that yet, and it’s been a long time since I played with that plugin so I can’t really suggest much off the top of my head.

Joncom avatar Sep 03 '19 15:09 Joncom

I see, how about your grid based movement plugin? Any tips on how to move NPCs?

I'm actually trying to make it work with the a star plugin, perhaps you can also give some pointers.

Thanks.

rontrek avatar Sep 05 '19 02:09 rontrek