Hollywoo-flixel do polygon-based lighting

This commit is contained in:
2023-03-29 10:49:42 -06:00
parent e400d6d558
commit b5245f927c
4 changed files with 58 additions and 1 deletions

View File

@@ -18,9 +18,25 @@ import flixel.text.FlxText;
import flixel.system.FlxSound;
import flixel.FlxCamera;
import flixel.util.FlxTimer;
import flixel.math.FlxPoint;
import haxe.Constraints;
import kiss_flixel.SpriteTools;
import haxe.ds.Option;
using flixel.util.FlxSpriteUtil;
import openfl.display.BitmapData;
import openfl.display.BitmapDataChannel;
import openfl.geom.Rectangle;
import openfl.geom.Point;
@:build(kiss.Kiss.build())
class FlxDirector implements Director<FlxSprite, FlxScreenPosition, ActorFlxSprite, FlxSound, String, FlxSprite, FlxSound, FlxCamera, FlxLightSource> {}
class FlxDirector implements Director<FlxSprite, FlxScreenPosition, ActorFlxSprite, FlxSound, String, FlxSprite, FlxSound, FlxCamera, FlxLightSource> {
public static function blackAlphaMaskFlxSprite(sprite:FlxSprite, mask:FlxSprite, output:FlxSprite):FlxSprite
{
sprite.drawFrame();
var data:BitmapData = sprite.pixels.clone();
data.copyChannel(mask.pixels, new Rectangle(0, 0, sprite.width, sprite.height), new Point(), BitmapDataChannel.RED, BitmapDataChannel.ALPHA);
output.pixels = data;
return output;
}
}

View File

@@ -31,6 +31,8 @@
(setSprite.updateHitbox)
(setSprite.screenCenter))
(otherwise))
// TODO draw a sky background depending on time of day, int/ext
(FlxG.cameras.remove flxMovie.uiCamera false)
(FlxG.cameras.remove flxMovie.screenCamera false)
(FlxG.cameras.add camera)
@@ -45,6 +47,41 @@
(FlxG.state.remove set)
(cc))
(method :FlxSprite drawLight [:FlxLightSource source]
(lightSprite.drawPolygon source.points source.color (object color FlxColor.TRANSPARENT))
(lightMask.drawPolygon source.points FlxColor.BLACK (object color FlxColor.TRANSPARENT)))
(var EVENING_COLOR (FlxColor.fromRGBFloat 0.5 0 0.5 0.2))
(var NIGHT_COLOR (FlxColor.fromRGBFloat 0 0 0 0.5))
(prop &mut :FlxSprite lightSprite null)
(prop &mut :FlxSprite lightMask null)
(prop &mut :FlxSprite darkness null)
(prop &mut :FlxColor darkColor FlxColor.BLACK)
(method :Void showLighting [:SceneTime sceneTime :Array<FlxLightSource> lightSources :FlxCamera camera]
(set lightSprite (new FlxSprite))
(lightSprite.makeGraphic FlxG.width FlxG.height FlxColor.TRANSPARENT true)
(set lightMask (new FlxSprite))
(lightMask.makeGraphic FlxG.width FlxG.height FlxColor.WHITE true)
(set darkness (new FlxSprite))
// TODO handle morning better
(set darkColor (case sceneTime
(Evening EVENING_COLOR)
(Night NIGHT_COLOR)
(otherwise FlxColor.TRANSPARENT)))
(darkness.makeGraphic FlxG.width FlxG.height darkColor true)
(doFor source lightSources (drawLight source))
(blackAlphaMaskFlxSprite darkness lightMask darkness)
(set lightSprite.cameras [flxMovie.screenCamera])
(set darkness.alpha darkColor.alphaFloat)
(set darkness.cameras [flxMovie.screenCamera])
(FlxG.state.add darkness)
(FlxG.state.add lightSprite))
(method :Void hideLighting []
(FlxG.state.remove darkness)
(FlxG.state.remove lightSprite))
(method :Void cleanup [] 0)
(method :Option<AutoZConfig> autoZConfig [] (Some (object zPerLayer flxMovie.STAGE_BEHIND_DY frontLayer 0)))

View File

@@ -35,6 +35,8 @@ interface Director<Set:Cloneable<Set>, ScreenPosition, Actor, Sound, Song, Prop,
function autoZConfig():Option<AutoZConfig>;
function showSet(set:Set, time:SceneTime, perspective:ScenePerspective, appearance:Appearance, camera:Camera, cc:Continuation):Void;
function hideSet(set:Set, camera: Camera, cc:Continuation):Void;
function showLighting(sceneTime:SceneTime, lightSources:Array<LightSource>, camera:Camera):Void;
function hideLighting():Void;
function showCharacter(character:Character<Actor>, appearance:Appearance, camera:Camera, cc:Continuation):Void;
function hideCharacter(character:Character<Actor>, camera:Camera, cc:Continuation):Void;
function playSound(sound:Sound, volumeMod:Float, waitForEnd:Bool, cc:Continuation):Void;

View File

@@ -105,6 +105,7 @@
(if sceneKey
// hide current scene background
(let [currentScene (dictGet scenes sceneKey)]
(director.hideLighting)
(director.hideSet currentScene.set currentScene.camera
(makeCC
// hide current scene characters
@@ -122,6 +123,7 @@
(cc)))
(method _showScene [:Scene<Set,ScreenPosition,Actor,Prop,Camera> scene :Appearance appearance :Camera camera :Continuation cc]
(director.showLighting scene.time .elements (lightSources.get sceneKey) camera)
// Show current scene background
(director.showSet scene.set scene.time scene.perspective appearance camera
(makeCC