Citrus-Engine
Citrus-Engine copied to clipboard
following multiple targets with the camera
Not sure how to add this for it to be efficient, but I think I can leave the code here for people to use (I'm sure some action sidescrolling games could benefit from it)
(untested with rotation - and I expect it to not work with it yet anyway)
public var targetsBounds:Rectangle = new Rectangle();
public var targetsCenter:Point = new Point();
public var targets:Vector.<Object> = new Vector.<Object>();
//init
camera.target = targetsCenter;
//update
var o:Object;
var left:Number;
var right:Number;
var top:Number;
var bottom:Number;
for each (o in targets)
{
if (!left || o.x < left)
left = o.x;
if (!top || o.y < top)
top = o.y;
if (!right || o.x > right)
right = o.x;
if (!bottom || o.y > bottom)
bottom = o.y;
}
targetsBounds.setTo(left, top, right - left, bottom - top);
targetsCenter.setTo(targetsBounds.left + targetsBounds.width * .5, targetsBounds.top + targetsBounds.height * .5);
//fit all targets on screen - TODO: add margins
camera.zoomFit(targetsBounds.width, targetsBounds.height);