Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shuffle method doesn't preserve unique properties #484

Closed
jamesjsewell opened this issue Feb 23, 2018 · 16 comments
Closed

shuffle method doesn't preserve unique properties #484

jamesjsewell opened this issue Feb 23, 2018 · 16 comments
Milestone

Comments

@jamesjsewell
Copy link

jamesjsewell commented Feb 23, 2018

  • A bug in the API: maybe

the issue is described here by someone, they never got an answer
http://www.html5gamedevs.com/topic/12946-tilemap-shuffle/

"I'm having trouble using Phaser's TileMap.shuffle. When I use it, it shuffles the area of tiles like it should, but the tiles lose their unique properties after being shuffled. Is there some way I can shuffle tiles without losing collision properties? I'm using shuffle like this:

myTileMap.setCollision(tileID, true, layer); // set a few collisions

myTileMap.setTileIndexCallback(tileID, function (player, tile) { // I have two callbacks
// do stuff

            myTileMap.shuffle(tile.x, tile.y, 5, 5, layer);            // call shuffle
        }, game, walls);

When the callback runs, the 5x5 area on the map loses its specific tile properties. Inside the callback, I can re-set the collision tiles, but I can't rewrite the callback I am in. Is there another way I should be going about achieving this? Thank you."

@samme
Copy link
Collaborator

samme commented Feb 23, 2018

Try Tilemap#setPreventRecalculate.

@jamesjsewell
Copy link
Author

I will try that soon and let you know. Thanks for responding, btw the name samme is familiar, i guess you're pretty active in the phaser community? @samme

@jamesjsewell
Copy link
Author

this.theTileMap.setPreventRecalculate(true)
this.theTileMap.shuffle(1, 1, 4, 4, this.currentLayerIndex)
this.theTileMap.setPreventRecalculate(false)

i tried this and it isn't quite working

@jamesjsewell
Copy link
Author

I tried setting it to true, then shuffle, then set it to false, and the opposite and neither worked

@samme
Copy link
Collaborator

samme commented Feb 23, 2018

I tried something similar on https://samme.github.io/phaser-examples-mirror/tilemaps/shuffle%20tiles.html:

layer.debug = true
map.setCollisionByExclusion([30], true)

Then I clicked on the map to shuffle the tiles. The collision faces didn't change. I didn't use the recalculate argument or the setPreventRecalculate method.

Re. the original post: I haven't tried to reproduce that, but it's possible that calling shuffle inside a tile callback might give unexpected results.

@jamesjsewell
Copy link
Author

@samme im not sure i understand

@samme
Copy link
Collaborator

samme commented Feb 24, 2018

Maybe I misunderstood.

In https://codepen.io/samme/pen/vdzjXy, after you shuffle tiles, do you want the collision lines (green) to change or stay the same?

@jamesjsewell
Copy link
Author

@samme i'll elaborate, I'm using the same shuffle method and arguments. What i'm doing is in tiled, i have set properties on the tiles. like lets say one tile is a goal for the player to reach, before the game starts i can iterate through the tiles and store the tile that has a property of "tile_type: 'goal'" as a variable to reference during the game. when i use the shuffle method, VISUALLY the "goal" tile is in a different location, but, the tile that has that property is still in the original location of the goal tile before the shuffle.

@jamesjsewell
Copy link
Author

I did the shuffle before finding and storing the "goal" tile by the way.

@jamesjsewell
Copy link
Author

like the goal tile is now whatever random tile got put in it's place, so instead of looking like the goal, it's just blank ground. And the tile that visually is the goal tile, does not have that property anymore

@jamesjsewell
Copy link
Author

that's an example to illustrate what is happening

@jamesjsewell
Copy link
Author

https://jamesjsewell.github.io/sifter/ this is the game i'm making, i am trying to shuffle the inner 4 x 4 tiles. I start in tiled by creating a puzzle that is possible, and then have the app shuffle the puzzle for the user to solve

@samme
Copy link
Collaborator

samme commented Feb 25, 2018

OK. It looks like shuffle modifies tile indexes only, it doesn't actually move the tiles around, so that's why the tile data doesn't change as well.

swap works the same way.

You could try putTile and removeTile. Unfortunately you have to handle the shuffle part yourself.

@jamesjsewell
Copy link
Author

jamesjsewell commented Feb 25, 2018

@samme interesting, i suppose i could write a function to do that myself, maybe I could make a pr? I've never actually made a pull request to a project like this though

@samme
Copy link
Collaborator

samme commented Feb 25, 2018

If it works, you can make a PR. :)

It seems most of the tile mutators modify index only:

putTile uses Tile#copy so it modifies all the data as well.

@samme samme added this to the 2.10.4 milestone Apr 21, 2018
@samme
Copy link
Collaborator

samme commented Apr 21, 2018

Clarified the expected behavior in the Tilemap docs.

You can still add a new feature if you like.

@samme samme closed this as completed Apr 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants