ondras / rot.js

ROguelike Toolkit in JavaScript. Cool dungeon-related stuff, interactive manual, documentation, tests!
https://ondras.github.io/rot.js/hp/
BSD 3-Clause "New" or "Revised" License
2.33k stars 254 forks source link

Updating the position of a light #99

Open Cleptomania opened 8 years ago

Cleptomania commented 8 years ago

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.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/36493450-updating-the-position-of-a-light?utm_campaign=plugin&utm_content=tracker%2F297828&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F297828&utm_medium=issues&utm_source=github).
ondras commented 8 years ago

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?

Cleptomania commented 8 years ago

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.