YardGnomeNinja Posted September 1, 2014 Share Posted September 1, 2014 To start, I'll say, there may be a better way than what I'm thinking, so I'm very open to suggestions. I'm making a Roguelike. I have rooms with attaching tunnels, a player that can move to free spaces, the VERY basics. I've reached the point of implementing an FOV (Field of View). I've got it in place for the most part. When the map is drawn, only the visible spaces within the player's FOV display their tiles, however when it comes to updating the display I'm getting nowhere, very...very... slowly. I've tried calling my "recalculateFOV" function in the Phaser "update" state, only to be fired when the player moves, then redrawing the entire map based on the results. This is agonizingly slow for some reason I can't quite decipher and just doesn't work properly. Then I thought maybe I needed to overwrite how Phaser handles drawing to get the job done. I Googled for any help on how to go about that and came up empty. I've read a couple of Phaser tutorials on creating a Roguelike, however the ones I've found left out handling FOV. Any help or suggestions would be greatly appreciated. Thanks! Link to comment Share on other sites More sharing options...
lewster32 Posted September 1, 2014 Share Posted September 1, 2014 As a roguelike, I assume it's turn-based? If so, the FOV calculations should only be happening once each time the player moves, rather than every update I'd imagine? You could also speed it up by having a maximum radius (in tiles), selecting all of the tiles in a square of size radius*2 (again in tiles) with the player at the centre, and looping over each of those tiles using Phaser.Math.distance(player.x, player.y, tile.x, tile.y) <= radius, checking to see if they're visible or not. If you're not fussed about the range being circular, you could even skip the distance check altogether and just use a square area for your field of view. If you ensure all tiles outside of the square are automatically marked as non-visible (or even just tiles in a square 1 tile bigger in each direction for maximum speed) you can prevent a 'fog of war' style effect whereby seen tiles may remain visible. this routine should be pretty fast and is pretty similar to how most roguelikes usually do this kind of thing. Link to comment Share on other sites More sharing options...
lewster32 Posted September 1, 2014 Share Posted September 1, 2014 A great algorithm for shadow casting in roguelikes is explained here: http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting That site has other algorithms and explanations too which can all be adapted to your Phaser game Link to comment Share on other sites More sharing options...
YardGnomeNinja Posted September 1, 2014 Author Share Posted September 1, 2014 Thanks, lwester32! Yes, I'm going turn-based with it and your suggestions are very similar to what I'm doing for the FOV calculation. The problem I'm having is updating the display. In my "update" version, whenever there was an input from the player that required FOV recalc, I would set a bool and have the update function track it. When it was set to true, the recalc was supposed to be called, the entire floor redrawn, and the recalc bool reset to false; I believe that didn't work because it appeared the update function was being called again before the FOV recalc barely began. I considered trying the "render" state, but I figured that would result the same or worse. Here's an example to help me explain: So here, the player starts in a fairly small room with what is probably a pillar to the NE, but there could be more wall behind it... I don't know until I move to the right and recalculate FOV and redraw the entire floor. Unfortunately, I simply don't know how to redraw the display. I apologize if this is ridiculous to start a topic over, but I've reached a mental block on this. Link to comment Share on other sites More sharing options...
YardGnomeNinja Posted September 2, 2014 Author Share Posted September 2, 2014 I just had a 'Duh' moment. Instead of messing with trying to redraw the display, I simply need to show/hide the spaces that are/are not in the current visible array. Link to comment Share on other sites More sharing options...
lewster32 Posted September 2, 2014 Share Posted September 2, 2014 Yeah, I think maybe you were expecting to have to do more work than you actually do in Phaser - a lot of this stuff (such as updating the renderer) is taken care of automatically, you just have to set visible = false or whatever your 'non-visible' routine needs to do. Link to comment Share on other sites More sharing options...
YardGnomeNinja Posted September 2, 2014 Author Share Posted September 2, 2014 That's exactly what I did! Thanks for helping me shift gears! Link to comment Share on other sites More sharing options...
Recommended Posts