rot.js
rot.js copied to clipboard
Updating the position of a light
Suppose I want a light that follows the player, something like a torch. Obviously this needs to update as the player moves.
I initially accomplished this by using the clearLights() function every time the game updates, then creating a new light at the player's position, and lastly calling compute().
This works fine and dandy, however let's say I have many different lights throughout the game, some of which are static and non moving, others dynamic like the player's torch. Is there any more effective system than keeping a list of all lights and clearing them and then re-adding every game update?
I feel that if this support isn't already there(I may just not be seeing a way to do it that's already there), it could easily be added to the ROT.Lighting class.
Hi @Cleptomania,
the current API has no dedicated method for moving, that is correct.
The idea behind this design decision is that to actually move a light, you need to identify it somehow -- preferrably via its coordinates (as having multiple lights in one place is not tested, not expected and will probably yield strange/buggy results). But once you have your (old) coordinates, moving a light source boils down to
setLight(oldX, oldY, null);
setLight(newX, newY, myLightColor);
...and I thought that having a wrapper method for these two lines is superfluous.
What do you think?
Yeah that makes sense. I didn't think about doing it that way. I've made a Light class within my game that tracks the positions of a light and handles updating it and that seems to work well. Within my map I can just contain an array of all my Light objects and update the needed lights.