Created
November 15, 2018 00:12
-
-
Save Geokureli/70c5b5205df327e440060361e9e088c1 to your computer and use it in GitHub Desktop.
This file contains 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
class Point { | |
static var _pool:Array<Point> = []; | |
public var x:Float; | |
public var y:Float; | |
var _weak:Bool = false; | |
public function new (x = 0.0, y = 0.0) { set(x, y); } | |
inline public function set(x = 0.0, y = 0.0):Point { | |
this.x = x; | |
this.y = y; | |
return this; | |
} | |
inline function weaken():Point { | |
_weak = true; | |
return this; | |
} | |
inline public function put():Void { _pool.push(this); } | |
inline public function putWeak():Void { if (_weak) put(); } | |
inline static public function get(x = 0.0, y = 0.0):Point { | |
return (_pool.length == 0 ? new Point() : _pool.shift()).set(x, y); | |
} | |
inline static public function weak(x = 0.0, y = 0.0):Point { | |
return Point.get(x, y).weaken(); | |
} | |
} | |
@:forward | |
abstract Vector(Point) from Point to Point { | |
public var length(get, never):Float; | |
inline public function new (x = 0.0, y = 0.0) { this = Point.get(x, y); } | |
inline function get_lengthSquared():Float { return this.x * this.x + this.y * this.y; } | |
inline static public function get(x = 0.0, y = 0.0):Vector { return cast Point.get(x, y); } | |
inline static public function weak(x = 0.0, y = 0.0):Vector { return cast Point.weak(x, y); } | |
} | |
class Test { | |
static function main() { | |
Point.weak().putWeak(); | |
trace(Vector.get(4, 3).lengthSquared); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment