Ticket #2394: vector_prototype.2.diff

File vector_prototype.2.diff, 9.3 KB (added by sanderd17, 10 years ago)
  • binaries/data/mods/public/globalscripts/vector.js

     
     1/////////////////////////////////////////////////////////////////////
     2//  Vector2D
     3//
     4//  Class for representing and manipulating 2D vectors
     5//
     6/////////////////////////////////////////////////////////////////////
     7
     8// TODO: Type errors if v not instanceof Vector classes
     9// TODO: Possibly implement in C++
     10
     11function Vector2D(x, y)
     12{
     13    if (arguments.length == 2)
     14    {
     15        this.set(x, y);
     16    }
     17    else
     18    {
     19        this.set(0, 0);
     20    }
     21}
     22
     23Vector2D.prototype.set = function(x, y)
     24{
     25    this.x = x;
     26    this.y = y;
     27};
     28
     29Vector2D.prototype.add = function(v)
     30{
     31    return new Vector2D(this.x + v.x, this.y + v.y);
     32};
     33
     34Vector2D.prototype.sub = function(v)
     35{
     36    return new Vector2D(this.x - v.x, this.y - v.y);
     37};
     38
     39Vector2D.prototype.mult = function(f)
     40{
     41    return new Vector2D(this.x * f, this.y * f);
     42};
     43
     44Vector2D.prototype.div = function(f)
     45{
     46    return new Vector2D(this.x / f, this.y / f);
     47};
     48
     49Vector2D.prototype.dot = function(v)
     50{
     51    return this.x * v.x + this.y * v.y;
     52};
     53
     54Vector2D.prototype.lengthSquared = function()
     55{
     56    return this.dot(this);
     57};
     58
     59Vector2D.prototype.length = function()
     60{
     61    return Math.sqrt(this.lengthSquared());
     62};
     63
     64Vector2D.prototype.normalize = function()
     65{
     66    var mag = this.length();
     67    if (!mag)
     68        return;
     69   
     70    this.x /= mag;
     71    this.y /= mag;
     72};
     73
     74const Vector2Dprototype = Vector2D.prototype;
     75
     76/////////////////////////////////////////////////////////////////////
     77//  Vector3D
     78//
     79//  Class for representing and manipulating 3D vectors
     80//
     81/////////////////////////////////////////////////////////////////////
     82
     83function Vector3D(x, y, z)
     84{
     85    if (arguments.length == 3)
     86    {
     87        this.set(x, y, y);
     88    }
     89    else
     90    {
     91        this.set(0, 0, 0);
     92    }
     93}
     94
     95Vector3D.prototype.set = function(x, y, z)
     96{
     97    this.x = x;
     98    this.y = y;
     99    this.z = z;
     100};
     101
     102Vector3D.prototype.add = function(v)
     103{
     104    return new Vector3D(this.x + v.x, this.y + v.y, this.z + v.z);
     105};
     106
     107Vector3D.prototype.sub = function(v)
     108{
     109    return new Vector3D(this.x - v.x, this.y - v.y, this.z - v.z);
     110};
     111
     112Vector3D.prototype.mult = function(f)
     113{
     114    return new Vector3D(this.x * f, this.y * f, this.z * f);
     115};
     116
     117Vector3D.prototype.div = function(f)
     118{
     119    return new Vector3D(this.x / f, this.y / f, this.z / f);
     120};
     121
     122Vector3D.prototype.dot = function(v)
     123{
     124    return this.x * v.x + this.y * v.y + this.z * v.z;
     125};
     126
     127Vector3D.prototype.lengthSquared = function()
     128{
     129    return this.dot(this);
     130};
     131
     132Vector3D.prototype.length = function()
     133{
     134    return Math.sqrt(this.lengthSquared());
     135};
     136
     137Vector3D.prototype.normalize = function()
     138{
     139    var mag = this.length();
     140    if (!mag)
     141        return;
     142   
     143    this.x /= mag;
     144    this.y /= mag;
     145    this.z /= mag;
     146};
     147
     148const Vector3Dprototype = Vector3D.prototype;
  • binaries/data/mods/public/maps/random/rmgen/vector.js

     
    1 /////////////////////////////////////////////////////////////////////
    2 //  Vector2D
    3 //
    4 //  Class for representing and manipulating 2D vectors
    5 //
    6 /////////////////////////////////////////////////////////////////////
    7 
    8 // TODO: Type errors if v not instanceof Vector classes
    9 // TODO: Possibly implement in C++
    10 
    11 function Vector2D(x, y)
    12 {
    13     if (arguments.length == 2)
    14     {
    15         this.set(x, y);
    16     }
    17     else
    18     {
    19         this.set(0, 0);
    20     }
    21 }
    22 
    23 Vector2D.prototype.set = function(x, y)
    24 {
    25     this.x = x;
    26     this.y = y;
    27 };
    28 
    29 Vector2D.prototype.add = function(v)
    30 {
    31     return new Vector2D(this.x + v.x, this.y + v.y);
    32 };
    33 
    34 Vector2D.prototype.sub = function(v)
    35 {
    36     return new Vector2D(this.x - v.x, this.y - v.y);
    37 };
    38 
    39 Vector2D.prototype.mult = function(f)
    40 {
    41     return new Vector2D(this.x * f, this.y * f);
    42 };
    43 
    44 Vector2D.prototype.div = function(f)
    45 {
    46     return new Vector2D(this.x / f, this.y / f);
    47 };
    48 
    49 Vector2D.prototype.dot = function(v)
    50 {
    51     return this.x * v.x + this.y * v.y;
    52 };
    53 
    54 Vector2D.prototype.lengthSquared = function()
    55 {
    56     return this.dot(this);
    57 };
    58 
    59 Vector2D.prototype.length = function()
    60 {
    61     return sqrt(this.lengthSquared());
    62 };
    63 
    64 Vector2D.prototype.normalize = function()
    65 {
    66     var mag = this.length();
    67    
    68     this.x /= mag;
    69     this.y /= mag;
    70 };
    71 
    72 /////////////////////////////////////////////////////////////////////
    73 //  Vector3D
    74 //
    75 //  Class for representing and manipulating 3D vectors
    76 //
    77 /////////////////////////////////////////////////////////////////////
    78 
    79 function Vector3D(x, y, z)
    80 {
    81     if (arguments.length == 3)
    82     {
    83         this.set(x, y, y);
    84     }
    85     else
    86     {
    87         this.set(0, 0, 0);
    88     }
    89 }
    90 
    91 Vector3D.prototype.set = function(x, y, z)
    92 {
    93     this.x = x;
    94     this.y = y;
    95     this.z = z;
    96 };
    97 
    98 Vector3D.prototype.add = function(v)
    99 {
    100     return new Vector3D(this.x + v.x, this.y + v.y, this.z + v.z);
    101 };
    102 
    103 Vector3D.prototype.sub = function(v)
    104 {
    105     return new Vector3D(this.x - v.x, this.y - v.y, this.z - v.z);
    106 };
    107 
    108 Vector3D.prototype.mult = function(f)
    109 {
    110     return new Vector3D(this.x * f, this.y * f, this.z * f);
    111 };
    112 
    113 Vector3D.prototype.div = function(f)
    114 {
    115     return new Vector3D(this.x / f, this.y / f, this.z / f);
    116 };
    117 
    118 Vector3D.prototype.dot = function(v)
    119 {
    120     return this.x * v.x + this.y * v.y + this.z * v.z;
    121 };
    122 
    123 Vector3D.prototype.lengthSquared = function()
    124 {
    125     return this.dot(this);
    126 };
    127 
    128 Vector3D.prototype.length = function()
    129 {
    130     return sqrt(this.lengthSquared());
    131 };
    132 
    133 Vector3D.prototype.normalize = function()
    134 {
    135     var mag = this.length();
    136    
    137     this.x /= mag;
    138     this.y /= mag;
    139     this.z /= mag;
    140 };
    141 
  • binaries/data/mods/public/simulation/components/Player.js

     
    664664        cmpGUIInterface.PushNotification(notification);
    665665};
    666666
     667Player.prototype.OnUpdate = function(msg)
     668{
     669    var cmpRangeManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager);
     670    var ents = cmpRangeManager.GetEntitiesByPlayer(this.GetPlayerID());
     671    var positions = [];
     672    for each (var ent in ents)
     673        positions.push(Engine.QueryInterface(ent, IID_Position));
     674
     675    var pos2d;
     676    var pos3d;
     677    var sum2d = 0;
     678    var sum3d = 0;
     679    var oTime = +(new Date());
     680    for (var i = 0; i < 40; i++)
     681    {
     682        for each (var cmpPosition in positions)
     683        {
     684            pos2d = cmpPosition.GetPosition2D();
     685            sum2d += pos2d.x + pos2d.y;
     686            pos3d = cmpPosition.GetPosition();
     687            sum3d += pos3d.x + pos3d.y + pos3d.z;
     688        }
     689    }
     690    var nTime = +(new Date());
     691    warn("time:" + (nTime - oTime) + ", numbers: " + sum2d + " " + sum3d);
     692};
     693
    667694Engine.RegisterComponentType(IID_Player, "Player", Player);
  • source/scriptinterface/ScriptInterface.cpp

     
    627627            return false;
    628628        }
    629629    }
    630 
     630    jsval proto;
     631    if (JS_GetProperty(m->m_cx, JS_GetGlobalObject(m->m_cx), "Vector2Dprototype", &proto))
     632        vector2Dprototype = JSVAL_TO_OBJECT(proto);
     633    if (JS_GetProperty(m->m_cx, JS_GetGlobalObject(m->m_cx), "Vector3Dprototype", &proto))
     634        vector3Dprototype = JSVAL_TO_OBJECT(proto);
    631635    return true;
    632636}
    633637
     
    10331037    return ok ? true : false;
    10341038}
    10351039
    1036 
    10371040bool ScriptInterface::Eval(const char* code)
    10381041{
    10391042    jsval rval;
  • source/scriptinterface/ScriptInterface.h

     
    9191     */
    9292    static shared_ptr<ScriptRuntime> CreateRuntime(int runtimeSize = DEFAULT_RUNTIME_SIZE);
    9393
     94    JSObject* vector2Dprototype;
     95    JSObject* vector3Dprototype;
     96
    9497    /**
    9598     * Constructor.
    9699     * @param nativeScopeName Name of global object that functions (via RegisterFunction) will
  • source/simulation2/scripting/EngineScriptConversions.cpp

     
    163163
    164164template<> jsval ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, const CFixedVector3D& val)
    165165{
    166     JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
     166    // apply the Vector3D prototype to the return value;
     167    ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx);
     168    JSObject* obj = JS_NewObject(cx, NULL, pCxPrivate->pScriptInterface->vector3Dprototype, NULL);
     169
    167170    if (!obj)
    168171        return JSVAL_VOID;
    169172
     
    197200
    198201template<> jsval ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, const CFixedVector2D& val)
    199202{
    200     JSObject* obj = JS_NewObject(cx, NULL, NULL, NULL);
     203    // apply the Vector2D prototype to the return value
     204    ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx);
     205    JSObject* obj = JS_NewObject(cx, NULL, pCxPrivate->pScriptInterface->vector2Dprototype, NULL);
     206
    201207    if (!obj)
    202208        return JSVAL_VOID;
    203209