Wednesday, August 7, 2019

An example of a simple data structure that I use in a project.

The trick of all this is that you can use data structures inside the hax code, while getting only the variables in the output. So far, variables are quickly processed by any engine)
So you do not have to worry about whether it is slow. You should only describe them and use:

/**

 * ...

 * @author Gavolot

 */

@:forward abstract Vec2f(Vec2f_impl) {

    public inline function new(x:Float, y:Float) this = { x:x, y:y };

 public inline function SqrtMagnitude() {

  return Math.sqrt((this.x * this.x) + (this.y * this.y));

 }

 public inline function SqrMagnitude() {

  return (this.x * this.x) + (this.y * this.y);

 }



    @:op(A + B) public inline function add(v:Vec2f) return new Vec2f(this.x + v.x, this.y + v.y);

 @:op(A + B) public inline function addInt(v:Int) return new Vec2f(this.x + v, this.y + v);

 @:op(A + B) public inline function addFloat(v:Float) return new Vec2f(this.x + v, this.y + v);

 

 @:op(A - B) public inline function minus(v:Vec2f) return new Vec2f(this.x - v.x, this.y - v.y);

 @:op(A - B) public inline function minusInt(v:Int) return new Vec2f(this.x - v, this.y - v);

 @:op(A - B) public inline function minusFloat(v:Float) return new Vec2f(this.x - v, this.y - v);

 

 @:op(A * B) public inline function multiply(v:Vec2f) return new Vec2f(this.x * v.x, this.y * v.y);

 @:op(A * B) public inline function multiplyInt(v:Int) return new Vec2f(this.x * v, this.y * v);

 @:op(A * B) public inline function multiplyFloat(v:Float) return new Vec2f(this.x * v, this.y * v);

 

 @:op(A / B) public inline function divide(v:Vec2f) return new Vec2f(Std.int(this.x / v.x), this.y / v.y);

 @:op(A / B) public inline function divideInt(v:Int) return new Vec2f(Std.int(this.x / v), this.y / v);

 @:op(A / B) public inline function divideFloat(v:Float) return new Vec2f(this.x / v, this.y / v);

 

 @:op(A == B) public inline function equally(v:Vec2f) : Bool return (this.x == v.x && this.y == v.y);

 @:op(A != B) public inline function notEqually(v:Vec2f) : Bool return (this.x != v.x || this.y != v.y);

}

@:nativeGen typedef Vec2f_impl = { x:Float, y:Float }



What's going on here? Yes, just a data structure is created, which you can then use in the code as an object, v1 = new Vec2f (5.0, 10.0);
and inside in the generated code it will look like:

No comments:

Post a Comment