diff --git a/flixel/input/FlxPointer.hx b/flixel/input/FlxPointer.hx index e06805edb6..bd30f4f9a9 100644 --- a/flixel/input/FlxPointer.hx +++ b/flixel/input/FlxPointer.hx @@ -15,6 +15,7 @@ class FlxPointer private var _globalScreenX:Int = 0; private var _globalScreenY:Int = 0; + private static var _cachedPoint:FlxPoint = new FlxPoint(); public function new() {} @@ -36,10 +37,9 @@ class FlxPointer { point = FlxPoint.get(); } - var screenPosition:FlxPoint = getScreenPosition(Camera); - point.x = screenPosition.x + Camera.scroll.x; - point.y = screenPosition.y + Camera.scroll.y; - screenPosition.put(); + getScreenPosition(Camera, _cachedPoint); + point.x = _cachedPoint.x + Camera.scroll.x; + point.y = _cachedPoint.y + Camera.scroll.y; return point; } @@ -106,10 +106,9 @@ class FlxPointer } else { - var point:FlxPoint = getPosition(); + getPosition(_cachedPoint); var object:FlxObject = cast ObjectOrGroup; - result = object.overlapsPoint(point, true, Camera); - point.put(); + result = object.overlapsPoint(_cachedPoint, true, Camera); } return result; @@ -140,14 +139,12 @@ class FlxPointer */ private function updatePositions():Void { - var screenPosition:FlxPoint = getScreenPosition(); - screenX = Std.int(screenPosition.x); - screenY = Std.int(screenPosition.y); - screenPosition.put(); + getScreenPosition(FlxG.camera, _cachedPoint); + screenX = Std.int(_cachedPoint.x); + screenY = Std.int(_cachedPoint.y); - var worldPosition = getWorldPosition(); - x = Std.int(worldPosition.x); - y = Std.int(worldPosition.y); - worldPosition.put(); + getWorldPosition(FlxG.camera, _cachedPoint); + x = Std.int(_cachedPoint.x); + y = Std.int(_cachedPoint.y); } } \ No newline at end of file diff --git a/flixel/input/keyboard/FlxKeyboard.hx b/flixel/input/keyboard/FlxKeyboard.hx index 3a1829c275..546c3dd368 100644 --- a/flixel/input/keyboard/FlxKeyboard.hx +++ b/flixel/input/keyboard/FlxKeyboard.hx @@ -166,13 +166,11 @@ class FlxKeyboard extends FlxKeyManager { var i:Int = 0; var l:Int = Record.length; - var o:CodeValuePair; - var o2:FlxKeyInput; while (i < l) { - o = Record[i++]; - o2 = getKey(o.code); + var o = Record[i++]; + var o2 = getKey(o.code); o2.current = o.value; } } diff --git a/flixel/math/FlxRect.hx b/flixel/math/FlxRect.hx index ef2b3197bf..e82314e801 100644 --- a/flixel/math/FlxRect.hx +++ b/flixel/math/FlxRect.hx @@ -367,14 +367,17 @@ class FlxRect implements IFlxPooled * @param rect Rectangle to check intersection againist. * @return The area of intersection of two rectangles. */ - public function intersection(rect:FlxRect):FlxRect + public function intersection(rect:FlxRect, ?result:FlxRect):FlxRect { + if (result == null) + result = FlxRect.get(); + var x0:Float = x < rect.x ? rect.x : x; var x1:Float = right > rect.right ? rect.right : right; if (x1 <= x0) { rect.putWeak(); - return FlxRect.get(0, 0, 0, 0); + return result; } var y0:Float = y < rect.y ? rect.y : y; @@ -382,11 +385,11 @@ class FlxRect implements IFlxPooled if (y1 <= y0) { rect.putWeak(); - return FlxRect.get(0, 0, 0, 0); + return result; } rect.putWeak(); - return FlxRect.get(x0, y0, x1 - x0, y1 - y0); + return result.set(x0, y0, x1 - x0, y1 - y0); } /** diff --git a/flixel/util/FlxCollision.hx b/flixel/util/FlxCollision.hx index 80d9e87636..4e9ff1314d 100644 --- a/flixel/util/FlxCollision.hx +++ b/flixel/util/FlxCollision.hx @@ -10,6 +10,9 @@ import flixel.FlxSprite; import flixel.group.FlxGroup; import flixel.math.FlxAngle; import flixel.math.FlxMath; +import flixel.math.FlxRect; +import flixel.math.FlxVector; +import flixel.math.FlxMatrix; import flixel.tile.FlxTileblock; /** @@ -21,15 +24,17 @@ import flixel.tile.FlxTileblock; class FlxCollision { // Optimization: Local static vars to reduce allocations - private static var pointA:Point = new Point(); - private static var pointB:Point = new Point(); - private static var centerA:Point = new Point(); - private static var centerB:Point = new Point(); - private static var matrixA:Matrix = new Matrix(); - private static var matrixB:Matrix = new Matrix(); - private static var testMatrix:Matrix = new Matrix(); - private static var boundsA:Rectangle = new Rectangle(); - private static var boundsB:Rectangle = new Rectangle(); + private static var pointA:FlxVector = new FlxVector(); + private static var pointB:FlxVector = new FlxVector(); + private static var centerA:FlxVector = new FlxVector(); + private static var centerB:FlxVector = new FlxVector(); + private static var matrixA:FlxMatrix = new FlxMatrix(); + private static var matrixB:FlxMatrix = new FlxMatrix(); + private static var testMatrix:FlxMatrix = new FlxMatrix(); + private static var boundsA:FlxRect = new FlxRect(); + private static var boundsB:FlxRect = new FlxRect(); + private static var intersect:FlxRect = new FlxRect(); + private static var flashRect:Rectangle = new Rectangle(); /** * A Pixel Perfect Collision check between two FlxSprites. It will do a bounds check first, and if that passes it will run a @@ -57,18 +62,20 @@ class FlxCollision if (considerRotation) { // find the center of both sprites - Contact.origin.copyToFlash(centerA); - Target.origin.copyToFlash(centerB); + Contact.origin.copyTo(centerA); + Target.origin.copyTo(centerB); // now make a bounding box that allows for the sprite to be rotated in 360 degrees - boundsA.x = (pointA.x + centerA.x - centerA.length); - boundsA.y = (pointA.y + centerA.y - centerA.length); - boundsA.width = centerA.length * 2; + var lengthA = centerA.length; + boundsA.x = (pointA.x + centerA.x - lengthA); + boundsA.y = (pointA.y + centerA.y - lengthA); + boundsA.width = lengthA * 2; boundsA.height = boundsA.width; - boundsB.x = (pointB.x + centerB.x - centerB.length); - boundsB.y = (pointB.y + centerB.y - centerB.length); - boundsB.width = centerB.length * 2; + var lengthB = centerB.length; + boundsB.x = (pointB.x + centerB.x - lengthB); + boundsB.y = (pointB.y + centerB.y - lengthB); + boundsB.width = lengthB * 2; boundsB.height = boundsB.width; } else @@ -84,9 +91,9 @@ class FlxCollision boundsB.height = Target.frameHeight; } - var intersect:Rectangle = boundsA.intersection(boundsB); + boundsA.intersection(boundsB, intersect.set()); - if (intersect.isEmpty() || intersect.width < 1 || intersect.height < 1) + if (intersect.isEmpty || intersect.width < 1 || intersect.height < 1) { return false; } @@ -152,8 +159,11 @@ class FlxCollision boundsB.width = overlapWidth; boundsB.height = overlapHeight; - var pixelsA = testA.getPixels(boundsA); - var pixelsB = testB.getPixels(boundsB); + boundsA.copyToFlash(flashRect); + var pixelsA = testA.getPixels(flashRect); + + boundsB.copyToFlash(flashRect); + var pixelsB = testB.getPixels(flashRect); var hit = false;