Ticket #2394: vector_prototype.3.diff
File vector_prototype.3.diff, 9.3 KB (added by , 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 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 Math.sqrt(this.lengthSquared()); 62 }; 63 64 Vector2D.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 74 const Vector2Dprototype = Vector2D.prototype; 75 76 ///////////////////////////////////////////////////////////////////// 77 // Vector3D 78 // 79 // Class for representing and manipulating 3D vectors 80 // 81 ///////////////////////////////////////////////////////////////////// 82 83 function 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 95 Vector3D.prototype.set = function(x, y, z) 96 { 97 this.x = x; 98 this.y = y; 99 this.z = z; 100 }; 101 102 Vector3D.prototype.add = function(v) 103 { 104 return new Vector3D(this.x + v.x, this.y + v.y, this.z + v.z); 105 }; 106 107 Vector3D.prototype.sub = function(v) 108 { 109 return new Vector3D(this.x - v.x, this.y - v.y, this.z - v.z); 110 }; 111 112 Vector3D.prototype.mult = function(f) 113 { 114 return new Vector3D(this.x * f, this.y * f, this.z * f); 115 }; 116 117 Vector3D.prototype.div = function(f) 118 { 119 return new Vector3D(this.x / f, this.y / f, this.z / f); 120 }; 121 122 Vector3D.prototype.dot = function(v) 123 { 124 return this.x * v.x + this.y * v.y + this.z * v.z; 125 }; 126 127 Vector3D.prototype.lengthSquared = function() 128 { 129 return this.dot(this); 130 }; 131 132 Vector3D.prototype.length = function() 133 { 134 return Math.sqrt(this.lengthSquared()); 135 }; 136 137 Vector3D.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 148 const Vector3Dprototype = Vector3D.prototype; -
binaries/data/mods/public/maps/random/rmgen/vector.js
1 /////////////////////////////////////////////////////////////////////2 // Vector2D3 //4 // Class for representing and manipulating 2D vectors5 //6 /////////////////////////////////////////////////////////////////////7 8 // TODO: Type errors if v not instanceof Vector classes9 // 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 else18 {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 // Vector3D74 //75 // Class for representing and manipulating 3D vectors76 //77 /////////////////////////////////////////////////////////////////////78 79 function Vector3D(x, y, z)80 {81 if (arguments.length == 3)82 {83 this.set(x, y, y);84 }85 else86 {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
664 664 cmpGUIInterface.PushNotification(notification); 665 665 }; 666 666 667 Player.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 667 694 Engine.RegisterComponentType(IID_Player, "Player", Player); -
source/scriptinterface/ScriptInterface.cpp
627 627 return false; 628 628 } 629 629 } 630 630 jsval proto; 631 if (JS_GetProperty(m->m_cx, JS_GetGlobalObject(m->m_cx), "Vector2Dprototype", &proto)) 632 vector2Dprototype = CScriptValRooted(m->m_cx, proto); 633 if (JS_GetProperty(m->m_cx, JS_GetGlobalObject(m->m_cx), "Vector3Dprototype", &proto)) 634 vector3Dprototype = CScriptValRooted(m->m_cx, proto); 631 635 return true; 632 636 } 633 637 … … 1033 1037 return ok ? true : false; 1034 1038 } 1035 1039 1036 1037 1040 bool ScriptInterface::Eval(const char* code) 1038 1041 { 1039 1042 jsval rval; -
source/scriptinterface/ScriptInterface.h
91 91 */ 92 92 static shared_ptr<ScriptRuntime> CreateRuntime(int runtimeSize = DEFAULT_RUNTIME_SIZE); 93 93 94 CScriptValRooted vector2Dprototype; 95 CScriptValRooted vector3Dprototype; 96 94 97 /** 95 98 * Constructor. 96 99 * @param nativeScopeName Name of global object that functions (via RegisterFunction) will -
source/simulation2/scripting/EngineScriptConversions.cpp
163 163 164 164 template<> jsval ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, const CFixedVector3D& val) 165 165 { 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, JSVAL_TO_OBJECT(pCxPrivate->pScriptInterface->vector3Dprototype.get()), NULL); 169 167 170 if (!obj) 168 171 return JSVAL_VOID; 169 172 … … 197 200 198 201 template<> jsval ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, const CFixedVector2D& val) 199 202 { 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, JSVAL_TO_OBJECT(pCxPrivate->pScriptInterface->vector2Dprototype.get()), NULL); 206 201 207 if (!obj) 202 208 return JSVAL_VOID; 203 209