Created
June 20, 2017 14:00
-
-
Save JoeCreates/11c2c7c627844b9d89e09b7d004ba102 to your computer and use it in GitHub Desktop.
CameraSprite: Like FlxSpriteGroup, but renders group to single buffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package lycan.util; | |
| import flixel.FlxBasic; | |
| import flixel.FlxCamera; | |
| import flixel.FlxG; | |
| import flixel.FlxSprite; | |
| import flixel.group.FlxGroup; | |
| import flixel.util.FlxCollision; | |
| import flixel.util.FlxColor; | |
| class CameraSprite extends FlxSprite { | |
| public var sourceCamera:FlxCamera; | |
| public var group:CameraGroup; | |
| var pointZero = new Point(); | |
| public function new(width:Float = 100, height:Float = 100, x:Float = 0, y:Float = 0, ?sourceCamera:FlxCamera) { | |
| super(x, y); | |
| if (sourceCamera == null) { | |
| sourceCamera = new FlxCamera(0, 0, Std.int(width), Std.int(height)); | |
| } | |
| this.sourceCamera = sourceCamera; | |
| this.width = width; | |
| this.height = height; | |
| makeGraphic(sourceCamera.width, sourceCamera.height, 0, true); | |
| group = new CameraGroup(); | |
| group.camera = sourceCamera; | |
| } | |
| @:access(flixel.FlxCamera) | |
| override public function draw():Void { | |
| pixels.fillRect(pixels.rect, FlxColor.TRANSPARENT); | |
| if (FlxG.renderTile) { | |
| sourceCamera.clearDrawStack(); | |
| sourceCamera.canvas.graphics.clear(); | |
| } | |
| group.draw(); | |
| if (FlxG.renderBlit) { | |
| pixels.copyPixels(sourceCamera.buffer, sourceCamera.buffer.rect, pointZero); | |
| dirty = true; | |
| } else { | |
| sourceCamera.render(); | |
| pixels.draw(sourceCamera.canvas); | |
| } | |
| super.draw(); | |
| } | |
| override public function update(dt:Float):Void { | |
| if (frameWidth != sourceCamera.width || frameHeight != sourceCamera.height) { | |
| makeGraphic(sourceCamera.width, sourceCamera.height, 0, true); | |
| } | |
| group.update(dt); | |
| super.update(dt); | |
| } | |
| } | |
| class CameraGroup extends FlxGroup { | |
| public function new() { | |
| super(); | |
| } | |
| override public function add(object:FlxBasic):FlxBasic { | |
| object.cameras = cameras; | |
| return super.add(object); | |
| } | |
| override public function insert(position:Int, object:FlxBasic):FlxBasic { | |
| object.cameras = cameras; | |
| return super.insert(position, object); | |
| } | |
| override function set_cameras(Value:Array<FlxCamera>):Array<FlxCamera> { | |
| for (m in members) { | |
| m.cameras = cameras; | |
| } | |
| return super.set_cameras(Value); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment