• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Lighting help plz

V

Vanthex

Guest
Hi
This is my game lol:

As you can see how the lighting works. I want to make it so that everytime i create or destroy a block the lighting is updated. As you can see, the block the player is standing on is still dark, which is wrong because light from above should make it bright.

Here is my code:
scr_alpha is a script that returns a value of how dark a block should be:
Code:
var a = 0;

for (var i = 1; i <= 4; i++) {
    if (place_meeting(x, y - (sprite_height * i), obj_tile)) {
        a += 1;
    }
}

if (a > 0) {
    a = (a * 25) / 100;
}

return a;
Then in the object called obj_tile, in the create event the value is set to a variable called alpha:
Code:
alpha = scr_alpha();
In the draw event of the object, i then draw the tile with the correct brightness:
Code:
//Draw self
draw_self();

//Draw darkness
draw_sprite_ext(sprite_index, image_index, x, y, 1, 1, 0, c_black, alpha);
My questions is how do I loop these procedures? OR either how do I make it so that it only updates the lighting when a block is created or destroyed? ( I used left mouse button released and right mouse button released)
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I'd re-think the problem completely. How? Use a ds_grid!

So, you create a ds_grid that is the width/height of the room (in blocks) and then clear it to 1. This will now be used to set the alpha of every block.... so in your PLAYER object you simply update the ds_grid every time the player moves so that a aradius about the player is lighter. Something like this then:

Code:
// PLAYER CREATE EVENT
light_grid = ds_grid_create(room_width / BOX_WIDTH, room_height / BOX_HEIGHT); // change the BOX values to the required for the grid
ds_grid_clear(light_grid, 1);
radius = 5; // light radius in grid cells around player, change as required

// PLAYER STEP EVENT
if x != xprevious || y != yprevious
{
ds_grid_clear(light_grid, 1);
var a = 1 / radius;
var new_alpha = a;
var r = radius;
repeat(radius)
    {
    ds_grid_set_disk(light_grid, floor(x / BOX_WIDTH), floor(y / BOX_WIDTH), r--, 1 - new_alpha);
    new_alpha -= a;
    }
with (obj_Wall)
    {
    alpha = other.light_grid[# floor(x / BOX_WIDTH), floor(y / BOX_WIDTH)];
    }
}

// DESTROY EVENT / ROOM END EVENT
ds_grid_destroy(light_grid);
You can make BOX_WIDTH/HEIGHT constants to make things more obvious, but that's how I'd do it... all the code is contained in the Player object, and it will only update if the player object actually moves.

Hope that helps!

:)
 
Top