Skip to content

Instantly share code, notes, and snippets.

@Geokureli
Created November 15, 2018 00:12
Show Gist options
  • Save Geokureli/70c5b5205df327e440060361e9e088c1 to your computer and use it in GitHub Desktop.
Save Geokureli/70c5b5205df327e440060361e9e088c1 to your computer and use it in GitHub Desktop.
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