MichaelAquilina / Some-2D-RPG

Tinkering with Game Development
MIT License
28 stars 5 forks source link

Is there a way to easily change tiles on the map? #6

Closed behindcurtain3 closed 11 years ago

behindcurtain3 commented 11 years ago

I was just playing around with map scripting and was curious to try and explode a bridge when the player crosses it using a MapZone to trigger the event. I can't seem to figure out how to edit tiles, is this supported?

My other idea was to create bridge objects and just swap them out but I'm not sure how that would impact the collision detection. If the normal bridge didn't allow the player to cross water any longer this wouldn't work. Any ideas?

MichaelAquilina commented 11 years ago

Swapping tiles is technically supported at the moment but not implemented in a fashion that's easy to perform (never actually thought of such use case scenario). If you have a MapZone and some event handler, then you can access the map and change the data in the layer you wish to change. Structure and layout information about tiles is stored in TileLayer objects which are found in a TiledMap. Here is a very very rough pseudo code of what you would need to do:

public void MapZone_MapZoneHit(MapZone sender, List<Entity> intersectingEntities, TeeEngine engine) 
{
     if(intersectingEntities.Contains(engine.GetEntity("Player"))
     {
         engine.map.TileLayers[1][5, 10] = 40;      // In TileLayer 1, set the tile with coordinate (5, 10) to the tile with Gid 40.
     }
}

Finding out whats the id of the tile you wish to change to is probably the hardest part. Its not clean because you relying on index values to access the tile layer you want rather than the layer name. Also finding the appropriate Gid is a bit hard (but if you look at example_map.tmx in a text editor you should manage to figure it out eventually).

behindcurtain3 commented 11 years ago

Thanks for the response, I may look to implement a method like engine.Map.GetLayer("Bridges") I haven't looked at the engine map code much though. I think this kind of functionality would be really great for the engine!

As a side note, I'm not too familiar with the Tiled maps. Is there a link or easy way to explain how the collision detection is handled when loading different tilesets?

MichaelAquilina commented 11 years ago

I agree it would be a good feature to add to the engine :) I actually had started implementing it in that way, but because of the way the layers are drawn i had decided to stick to lists rather than dictionaries. I can either:

Ill have a look at it tonight or tomorrow depending if i get to chance to work on this repository today.

With regards to collision - currently the only collission detection code is found in the 'Hero' player (which is where i am placing most of my prototyping code before moving it to the standard game library once it has matured). Essentially, if the current tile the entity is on contains the property 'Impassable' then some simple collision response is performed. Have a look at the Hero's Update() loop towards the end to get an idea of how it works.

behindcurtain3 commented 11 years ago

I just pushed d3ed674 to a branch on my fork. I think it provides a decent solution without requiring larger changes.

MichaelAquilina commented 11 years ago

For now it should suffice. I will probably make improvements to the general structure of TileLayers and other TiledMap properties in the future.

MichaelAquilina commented 11 years ago

Im going to close this because the pull request given pretty much serves its purpose. As regards its time complexity of O(n) - it shouldnt be too much of a problem considering layers should always be kept to a minimum anyway and rarely exceeds over 10.