AS-Library für Kollision "übersetzen" in Autoit

  • Hallihallo,

    gerade bin ich fertig geworden mit der "Übersetzung" einer ActionScript Library in Autoit. Da ich Autoit noch nicht "doll" kann, möchte ich euch bitten einmal über den Code zu schauen. Besonders ab Zeile 283 bis 326. Da habe ich die Funktion drawAt noch nicht so richtig hinbekommen.

    Achtung!: Es mag zwar nach viel Code aussehn, ist es aber nicht! Ich habe nur den Code aus dem ASScript in das AutoitScript als Kommentar, zur jeweiligen Funktion, eingefügt.

    "Übersetztes":

    Spoiler anzeigen
    [autoit]


    #cs
    private var __x:Number;
    private var __y:Number;
    private var __normal:Vector;
    private var __len:Number;
    private var bInvalidLen:Boolean;
    private var bInvalidNormal:Boolean;
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Global $__x
    Global $__y
    Global $__normal = Vector($__x, $__y)
    Global $__len
    Global $bInvalidLen
    Global $bInvalidNormal

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * constructor
    * initializes x and y, invalidates length and normal
    * @param x Number
    * @param y Number
    */
    function Vector(x:Number, y:Number){
    this.__x = x;
    this.__y = y;
    this.bInvalidLen = true;
    this.bInvalidNormal = true;
    }
    #ce
    Func Vector($f__x, $f__y)
    $__x = $f__x
    $__y = $f__y
    $bInvalidLen = True
    $bInvalidNormal = True
    EndFunc ;==>Vector

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    / * *
    * Updates this vectors' x and y values
    * @param x Number
    * @param y Number
    * /
    public function setVector(x:Number, y:Number) {
    this.__x = x;
    this.__y = y;
    this.bInvalidLen = True;
    this.bInvalidNormal = True;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func setVector($f__x, $f__y)
    $__x = $f__x
    $__y = $f__y
    $bInvalidLen = True
    $bInvalidNormal = True
    EndFunc ;==>setVector

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Calculates the dot product of this vector and vetor v
    * @param v Vector
    * @return returns a new vector
    */
    public function dot(v:Vector){
    return this.x * v.x + this.y * v.y;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func dot($f__x, $f__y)
    Return $__x * $f__x + $__y * $f__y
    EndFunc ;==>dot

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Calculates the cross product of this vector and number n
    * @param n Number
    * @return returns a new vector
    */
    public function cross(n:Number){
    return new Vector(this.x * n, this.y * n);
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func cross($fn)
    Return Vector($f__x * $fn, $f__y * $fn)
    EndFunc ;==>cross

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Adds vector v to this vector
    * @param v Vector
    * @return returns a new vector
    */
    public function plus(v:Vector){
    return new Vector(this.x + v.x, this.y + v.y);
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func plus($f__x, $f__y)
    Return Vector($__x + $f__x, $__y + $f__y)
    EndFunc ;==>plus

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Substracts vector v from this vector
    * @param v Vector
    * @return returns a new vector
    */
    public function minus (v:Vector){
    return new Vector(this.x - v.x, this.y - v.y);
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func minus($f__x, $f__y)
    Return Vector($__x - $f__x, $__y - $f__y)
    EndFunc ;==>minus

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Returns inverted vector
    * @return returns a new vector
    */
    public function getInverted (){
    return new Vector(-this.x, -this.y);
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func getInverted()
    Return Vector(-$__x, -$__y)
    EndFunc ;==>getInverted

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Returns a normalized version of this vector
    * @return returns a new vector or null
    */
    public function getNormalized () {
    if (this.len == 0){
    return null;
    } else {
    return new Vector(this.__x / this.len, this.__y / this.len)
    }
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func getNormalized()
    If len() == 0 Then
    Return 0
    Else
    Return Vector($__x / len(), $__y / len())
    EndIf
    EndFunc ;==>getNormalized

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Inverts this vector
    * @return nothing
    */
    public function invert (){
    this.__x *= -1;
    this.__y *= -1;
    this.bInvalidNormal = true;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func invert()
    $__x = $__x * - 1
    $__y = $__y * - 1
    $bInvalidNormal = True
    EndFunc ;==>invert

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Normalizes this vector
    * @return nothing
    */
    public function normalize(Void):Void {
    if (this.len == 0){
    this.__x = 0;
    this.__y = 0;
    } else {
    this.__x /= this.len;
    this.__y /= this.len;
    }
    this.bInvalidLen = true;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func normalize()
    If len() == 0 Then
    $__x = 0
    $__y = 0
    Else
    $__x = $__x / len()
    $__y = $__y / len()
    EndIf
    EndFunc ;==>normalize

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    public function set x (n:Number) {
    this.__x = n;
    this.bInvalidLen = true;
    this.bInvalidNormal = true;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func set_x($fn)
    $__x = $fn
    $bInvalidLen = True
    $bInvalidNormal = True
    EndFunc ;==>set_x

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    public function set y (n:Number) {
    this.__y = n;
    this.bInvalidLen = true;
    this.bInvalidNormal = true;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func set_y($fn)
    $__y = $fn
    $bInvalidLen = True
    $bInvalidNormal = True
    EndFunc ;==>set_y

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    public function get x (){
    return this.__x;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func get_x()
    Return $__x
    EndFunc ;==>get_x

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    public function get y (){
    return this.__y;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func get_y()
    Return $__y
    EndFunc ;==>get_y

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    public function get normal (){
    if (this.bInvalidNormal) this.updateNormal();
    return this.__normal;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func get_normal()
    If $bInvalidLen = updateNormal() Then Return $__normal
    EndFunc ;==>get_normal

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    public function get len (){
    if (this.bInvalidLen) this.updateLen();
    return this.__len;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func get_len()
    If $bInvalidLen = updateLen() Then Return $__normal
    EndFunc ;==>get_len

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    //////////////////////////// misc public functions //////////////////////////

    [/autoit] [autoit][/autoit] [autoit]

    public function toString(Void):String {
    return "Vector(" + this.__x + ", " + this.__y + ")";
    }

    [/autoit] [autoit][/autoit] [autoit]

    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func toString()
    Return "Vector(" & $__x & "," & $__y & ")"
    EndFunc ;==>toString

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    /**
    * Draws the vector, useful when debugging
    * @param mc MovieClip to draw onto
    * @param px start point x coordinate
    * @param py start point y coordinate
    * @param scale optional scale
    * @param col optional color
    */
    public function drawAt(mc:MovieClip, px:Number, py:Number, scale:Number, col:Number){
    !> if (scale == undefined) scale = 1;
    !> if (col == undefined) col = 0xFF0000;
    !> var ex, ey;
    !> ex = px + this.__x * scale;
    !> ey = py + this.__y * scale;
    mc.lineStyle(1, col, 100);
    mc.moveTo(px, py);
    mc.lineTo(ex, ey);
    // draw arrow
    !> var tmpVector = new Vector(px, py).plus(this.getNormalized().cross(this.len*scale-6));
    !> var nVector = this.normal;
    nVector.normalize();
    nVector = nVector.cross(4);
    var tmpVector = tmpVector.plus(nVector);
    mc.moveTo(tmpVector.x, tmpVector.y);
    mc.lineTo(ex, ey);
    var tmpVector = tmpVector.plus(nVector.cross(-2));
    mc.lineTo(tmpVector.x, tmpVector.y);
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func drawAt($px, $py,$scale,$col)
    Local $ex, $ey, $tmpVector, $nVector

    [/autoit] [autoit][/autoit] [autoit]

    If $scale <> IsDeclared Then $scale = 1
    If $col <> IsDeclared Then $col = 0xFF0000

    [/autoit] [autoit][/autoit] [autoit]

    $ex = $px + $__x * $scale
    $ey = $py + $__y * $scale

    [/autoit] [autoit][/autoit] [autoit]

    $tmpVector = Vector($px,$py) + getNormalized() + cross($__len*$scale-6)
    $nVector = get_normal()

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    //////////////////////////// private functions //////////////////////////

    [/autoit] [autoit][/autoit] [autoit]

    private function updateLen() {
    this.__len = Math.sqrt(this.__x*this.__x + this.__y*this.__y);
    this.bInvalidLen = false;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func updateLen()
    $__len = Sqrt($__x * $__x + $__y * $__y)
    $bInvalidLen = False
    EndFunc ;==>updateLen

    [/autoit] [autoit][/autoit] [autoit]

    #cs
    private function updateNormal() {
    this.__normal = new Vector(-this.__y/this.len, this.__x/this.len);
    this.bInvalidNormal = false;
    }
    #ce

    [/autoit] [autoit][/autoit] [autoit]

    Func updateNormal()
    $__normal = Vector(($__y * - 1) / len(), ($__x * - 1) / len())
    EndFunc ;==>updateNormal

    [/autoit]

    Normale AS-Library:

    Spoiler anzeigen

    Der Quellcode stammt von folgender Seite: http://www.johanvanmol.org/content/view/45/37/1/7/</a>

    Ihr würdet mir echt einen großen Gefallen tun, den nur so lerne ich weiter ;) :rock:
    Einen schönen Tag noch - Vectorix