I've got a room that uses graphical room overlays for faux lighting effects. Whenever the player turns a light on, the game takes a copy of the "lit" background, places "lit" versions of object sprites on it, copies the transparency of a lighting mask sprite, then applies the resulting image to an overlay to create the illusion of lighting.
This itself works fine. The problem arises when using Overlay.Remove() to turn a light off.
If the overlays are removed in the order they were created, the lights continue to work as expected. But if, say, you turn on the overhead light, then open the fridge, then turn the overhead light off and on again - everything breaks. Like so:
I've tested it with up to five lights and this only occurs when removing the non-newest overlay. My best guess is that the overlays are getting jumbled up, somehow? At the 0:18 mark you can just barely see the lamp's beam clip with the lamp itself, which implies it's gotten the fridge's z value of 220 instead of it's usual 1.
Any suggestions would be greatly appreciated!
Relevant bits of code (apologies for the spaghetti, things got progressively more chaotic the more things I tried/deleted):
Room Code
Code: ags
Lighting Script
Code: ags
This itself works fine. The problem arises when using Overlay.Remove() to turn a light off.
If the overlays are removed in the order they were created, the lights continue to work as expected. But if, say, you turn on the overhead light, then open the fridge, then turn the overhead light off and on again - everything breaks. Like so:
I've tested it with up to five lights and this only occurs when removing the non-newest overlay. My best guess is that the overlays are getting jumbled up, somehow? At the 0:18 mark you can just barely see the lamp's beam clip with the lamp itself, which implies it's gotten the fridge's z value of 220 instead of it's usual 1.
Any suggestions would be greatly appreciated!
Relevant bits of code (apologies for the spaghetti, things got progressively more chaotic the more things I tried/deleted):
Room Code
Overlay* lLamp;
Overlay* lFridge;
function oSwitch_Interact()
{
if (kitchenState!="lit"){
oLamp.Visible=true;
oLamp.Graphic=223;
lLamp=TurnOn(lLamp, 224, 1);
kitchenState="lit";
}
else{
oLamp.Graphic=239;
kitchenState="unlit";
lLamp.Remove();
}
}
function oFridge_Interact()
{
if (!oFridge.isOpen()){
oFridge.Open();
lFridge=TurnOn(lFridge, 240, 220);
oFridge.Baseline=220;
}
else{
oFridge.Close();
oFridge.Baseline=1;
lFridge.Remove();
}
}
Lighting Script
DynamicSprite* litBG;
///Grabs the full "lit" version of the scene for later use
DynamicSprite* PrepLight(){
DynamicSprite* lightBG = DynamicSprite.CreateFromBackground(1, 0, 0, 320, 240);
DrawingSurface* surface = lightBG.GetDrawingSurface();
for (int i = 0; i<Room.ObjectCount; i++)
{
int sprite;
if (object[i].isOpen()) sprite = object[i].GetProperty("spriteOpen");
else sprite = object[i].GetProperty("spriteClose");
if (object[i].Visible && sprite>0){
surface.DrawImage(object[i].X, object[i].Y - Game.SpriteHeight[object[i].Graphic], sprite);
}
}
surface.Release();
return lightBG;
}
///Crops the lightBG to a sprite mask, applies it to an overlay, then returns the overlay.
Overlay* TurnOn(Overlay* light, int mask, int z){
PrepLight();
litBG=PrepLight();
litBG.CopyTransparencyMask(mask);
Overlay* toReturn = Overlay.CreateRoomGraphical(0, 0, litBG.Graphic, true, true);
toReturn.ZOrder=z;
return toReturn;
}