melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

Custom tile type

Open nvlbg opened this issue 14 years ago • 1 comments

So what I didn't find in the framework is how to add custom tile types. For example, there is no way some tiles to be "solid" for some objects but not "solid" for other objects. I think this would be a nice feature for the framework.

P.S.: I actually have implemented one extra tile type like this:

In the constructor of TMXTileset there is a line which tells whether the tile is solid (isSolid). I've added a check to see not only if the tile type is "solid" but if it is "water" in my case. So if the type is "water" isSolid is true.

Then, in the object class for the object that can go through water I override the updateMovement method and there if the object is colliding with a tile (I pretty much looked through the original updateMovement method) I check if the tile type is water. If it is - I do nothing, if it isn't - the tile must be solid, so I stop the object.

I couldn't implement this feature by myself because I'm not that deep into the framework. I don't think that this is how custom tile types should be implemented (by editing the source code of melonJS). What I think could be a "hacky solution" is when we consider if the tile type is solid, we don't only check if the tile type (from tiled) is "solid" but if it is not ["breakeable", "lslope", "rslope", "platform"].

I'm sorry for my bad English. I hope what I described was clear.

nvlbg avatar Aug 30 '12 09:08 nvlbg

Yes, and the supported collision shapes are not really flexible enough to support different shapes. For example, tiles that are only "half solid" on the top, bottom, or sides ... or "inverted slopes". Or even completely different shapes like polygons and circles.

The first thing that's needed is modifying this line: https://github.com/obiot/melonJS/blob/master/src/level/TMXTileset.js#L278 to set isCollidable true when tileProp.type is set:

tileProp.isCollidable = (tileProp.type ? true : false);

This will allow arbitrarily setting the type property in Tiled, eg. type = water, to set the isCollidable flag in the TMXTileset ... me.TiledLayer.checkCollision() checks for that property to determine whether a collision should be handled by me.Entity.updateMovement() -- Which only handles the default solid/slope/ladder/platform/breakable tile types, leaving it up to user code to handle the arbitrary types like "water". Example:

var collision = this.updateMovement();
if (collision.yprop.type.toLowerCase() === "water") {
    // Handle water collisions
}

In this way, melonJS provides just the baseline collision shapes/tile types, and it's up to the game to handle the rest. (Also I think maybe the TMXTileset constructor should apply the toLowerCase() method to the type property so it doesn't have to be done everywhere... like in this user code!)

FYI, I tried this a while back, and got discouraged because it's so much work to implement many different tile types. But if you only need a few extras, this is a great solution. Here's the relevant post for reference: https://groups.google.com/forum/?fromgroups=#!topic/melonjs/AGNm_LmkUUM

parasyte avatar Sep 03 '12 22:09 parasyte