Advertisement
Guest User

Problem Machine simple 2d lighting

a guest
Mar 29th, 2015
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.83 KB | None | 0 0
  1. class LightSystem extends Sprite
  2. {
  3.     public var lights:Array<LightSource>;
  4.     public var ambient:AmbientLight;
  5.    
  6.     public function new()
  7.     {
  8.         super();
  9.         lights = [];
  10.         ambient = new AmbientLight();
  11.     }
  12.    
  13.     public function render(camera:Point, zoom:Float, width:Float, height:Float):Void
  14.     {          
  15.         graphics.clear();
  16.         if (ambient != null)
  17.             ambient.draw(graphics, width, height);
  18.         for (l in lights)
  19.             l.draw(graphics, camera);
  20.     }
  21. }
  22.  
  23. class AmbientLight
  24. {
  25.     public var color:Color;
  26.     public var intensity:Float;
  27.     public var pattern:LightPattern;
  28.    
  29.     public function new(?color:Color, intensity:Float = 0, ?pattern:LightPattern)
  30.     {
  31.         if (color != null)
  32.             this.color = color;
  33.         else
  34.             this.color = Color.createFromRGB(0, 0, 0);
  35.         this.intensity = intensity;
  36.         this.pattern = pattern;
  37.     }
  38.    
  39.     public function draw(target:Graphics, width:Float, height:Float):Void
  40.     {
  41.         if (intensity == 0)
  42.             return;
  43.         if (pattern != null)
  44.         {
  45.             var m:Matrix = new Matrix();
  46.             m.translate(pattern.offset.x, pattern.offset.y);
  47.             m.scale(pattern.scale, pattern.scale);
  48.             target.beginBitmapFill(pattern.data, m, true);
  49.         }
  50.         else
  51.         {
  52.             target.beginFill(color.raw, intensity);
  53.         }
  54.         target.drawRect(0, 0, width, height);
  55.     }
  56. }
  57.  
  58. class LightSource
  59. {
  60.     public var position:Point;
  61.     public var innerColor:Color;
  62.     public var innerRadius:Float;
  63.     public var outerColor:Color;
  64.     public var outerRadius:Float;
  65.     public var intensity:Float;
  66.     public var pattern:LightPattern;
  67.    
  68.     public function new()
  69.     {
  70.         position = new Point();
  71.         innerColor = Color.createFromRGB(0xFF, 0xFF, 0xFF);
  72.         innerRadius = 100;
  73.         outerColor = Color.createFromRGB(0xFF, 0xFF, 0xFF);
  74.         outerRadius = 500;
  75.         intensity = 0.5;
  76.         pattern = null;
  77.     }
  78.    
  79.     public function draw(target:Graphics, camera:Point):Void
  80.     {
  81.         //TODO: Early exit if light is off-screen
  82.         if (pattern != null)
  83.         {
  84.             var m:Matrix = new Matrix();
  85.             m.translate(position.x - camera.x - outerRadius + pattern.offset.x, position.y - camera.y - outerRadius + pattern.offset.y);
  86.             m.scale(pattern.scale, pattern.scale);
  87.             target.beginBitmapFill(pattern.data, m, true);
  88.         }
  89.         else
  90.         {
  91.             var m:Matrix = new Matrix();
  92.             m.createGradientBox(outerRadius * 2, outerRadius * 2, 0, position.x - camera.x - outerRadius, position.y - camera.y - outerRadius);
  93.             target.beginGradientFill(GradientType.RADIAL, [innerColor.raw, outerColor.raw], [intensity, 0],
  94.                 [Math.floor((innerRadius / outerRadius) * 0xFF), 0xFF], m);
  95.         }
  96.         target.drawCircle(position.x - camera.x, position.y - camera.y, outerRadius);
  97.     }
  98. }
  99.  
  100. class LightPattern
  101. {
  102.     public var data(default, null):BitmapData;
  103.     public var scale(default, default):Float;
  104.     public var offset(default, default):Point;
  105.  
  106.     public function new(data:BitmapData, scale:Float, offset:Point)
  107.     {
  108.         this.data = data;
  109.         this.scale = scale;
  110.         this.offset = offset;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement