• 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 [SOLVED] Shadow with window around player

YoSniper

Member
[EDIT] I have figured out the problem. It was an issue with x and y relativity, as well as the size of the surface I was using.

Hello all,

I've posted about shadows and light sources before, but now I'm trying to create an object that darkens the area around the player the farther out you get. Ideally I don't want to create a surface that encompasses the entire 7200x600 room.

I've written the following code based off of a previous tutorial, but it does not presently work.

The idea is to have the shadow fade to dark around the player after it is created, and then an external trigger caused it to light up again before it is destroyed.

Create
Code:
///Create shadow surface from which light beams will be subtracted

alpha = 0;
state = "FADE IN";

surface = surface_create(1600, 1200);
surface_set_target(surface);
draw_clear_alpha(c_black, 0);
surface_reset_target();
Destroy
Code:
///Clean up the surface

surface_free(surface);
Step
Code:
///State management

if not game_paused() {
    switch(state) {
        case "FADE IN":
            alpha += 0.02;
            if alpha >= 1 {
                alpha = 1;
                state = "ON";
            }
            break;
      
        case "FADE OUT":
            alpha -= 0.02;
            if alpha <= 0 {
                instance_destroy();
            }
            break;
    }
}

///Set x and y

if instance_exists(Player) {
    x = Player.x;
    y = Player.y - 8;
} else {
    instance_destroy();
    exit;
}

///Manage light sources in surface

surface_set_target(surface);

//Draw all-encompassing shadow
var max_alpha;
if World.difficulty[World.save_slot] == "EASY" {
    max_alpha = 0.4;
} else {
    max_alpha = 0.6;
}
draw_sprite_ext(spr_whiteout, 0, x - 800, y - 600, 50, 38, 0, c_black, alpha * max_alpha);

//Draw circles of light around player
draw_set_blend_mode(bm_subtract);

var ii;
draw_set_color(c_white);
for(ii = 0; ii < 5; ii += 1) {
    draw_set_alpha(1 - ii / 5);
    draw_circle(x, y, 64 + 16 * ii, false);
}
draw_set_alpha(1);

//Reset targets
draw_set_blend_mode(bm_normal);
surface_reset_target();
Draw
Code:
///Draw shadow surface

if surface_exists(surface) {
    draw_surface(surface, 0, 0);
}
 
Last edited:
Top