Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get the effect the light should apply to a given location
- *
- * @param map The tile map you're lighting (I'm assuming you've got something)
- * @param x The x coordinate of the location being considered for lighting
- * @param y The y coordinate of the location being considered for lighting
- * @param colouredLights True if we're supporting coloured lights
- *
- * @return The effect on a given location of the light in terms of colour components (all
- * the same if we don't support coloured lights)
- */
- public float[] getEffectAt(Map map, float x, float y, boolean colouredLights) {
- // first work out what propotion of the strength distance the light
- // is from the point. This is a value from 0-1 where 1 is the centre of the
- // light (i.e. full brightness) and 0 is the very edge (or outside) the lights
- // range
- float dx = (x - xpos);
- float dy = (y - ypos);
- // first work out how many steps along the line we're going to do
- // should really work out the distance here, but a Math.sqrt() is probably pretty expensive
- int steps = (int) ((Math.abs(dx) + Math.abs(dy)) * 4);
- // now work out how far on each move we want to go (using minus dx/dy here since the previous calculations worked it out
- // in the wrong direction)
- float stepX = (-dx / steps);
- float stepY = (-dy / steps);
- // start at the position of the light
- float px = x;
- float py = y;
- // move across the map checking the points
- for (int i=0;i<steps-1;i++) {
- if (map.blocked((int) px, (int) py)) {
- return 0;
- }
- // move to the next point
- px += stepX;
- py += stepY;
- }
- // once we reach this point we know the light isn't blocked
- // calculate the brightness
- float distance2 = (dx*dx)+(dy*dy);
- float effect = 1 - (distance2 / (strength*strength));
- if (effect < 0) {
- effect = 0;
- }
- // if we doing coloured lights then multiple the colour of the light
- // by the effect. Otherwise just use the effect for all components to
- // give white light
- if (colouredLights) {
- return new float[] {col.r * effect, col.g * effect, col.b * effect};
- } else {
- return new float[] {effect,effect,effect};
- }
- }
Add Comment
Please, Sign In to add comment