phoboslab / Impact

HTML5 Game Engine
MIT License
1.99k stars 204 forks source link

Get surrounding tiles x,y position and check if passable #62

Open rontrek opened 5 years ago

rontrek commented 5 years ago

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.

Joncom commented 5 years ago

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

rontrek commented 5 years ago

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.

Joncom commented 5 years ago

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.

rontrek commented 5 years ago

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?

Joncom commented 5 years ago

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 commented 5 years ago

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

rontrek commented 5 years ago

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.

Joncom commented 5 years ago

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.

rontrek commented 5 years ago

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?

Joncom commented 5 years ago

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.

rontrek commented 5 years ago

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.

Joncom commented 5 years ago

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.

rontrek commented 5 years ago

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.