Browse Source

Cleanups to createChildPrefab, also support loading a prefab directly on an existing node

Josh Engebretson 10 years ago
parent
commit
189e56c539
2 changed files with 25 additions and 23 deletions
  1. 2 1
      Script/Packages/Atomic/Scene.json
  2. 23 22
      Source/AtomicJS/Javascript/JSScene.cpp

+ 2 - 1
Script/Packages/Atomic/Scene.json

@@ -40,7 +40,8 @@
 			"getChildAtIndex(index:number):Node;",
 			"getChildAtIndex(index:number):Node;",
 			"createJSComponent(name:string, args?:{});",
 			"createJSComponent(name:string, args?:{});",
 			"getJSComponent(name:string):JSComponent;",
 			"getJSComponent(name:string):JSComponent;",
-			"createChildPrefab(childName:string, prefabPath:string);"
+			"createChildPrefab(childName:string, prefabPath:string):Node;",
+			"loadPrefab(prefabPath:string):boolean;"
 		],
 		],
 		"Scene" : [
 		"Scene" : [
 			"getMainCamera():Camera;"
 			"getMainCamera():Camera;"

+ 23 - 22
Source/AtomicJS/Javascript/JSScene.cpp

@@ -7,6 +7,7 @@
 #include <Atomic/IO/File.h>
 #include <Atomic/IO/File.h>
 #include <Atomic/Scene/Node.h>
 #include <Atomic/Scene/Node.h>
 #include <Atomic/Scene/Scene.h>
 #include <Atomic/Scene/Scene.h>
+#include <Atomic/Scene/PrefabComponent.h>
 #include <Atomic/Graphics/Camera.h>
 #include <Atomic/Graphics/Camera.h>
 
 
 #ifdef ATOMIC_3D
 #ifdef ATOMIC_3D
@@ -198,42 +199,41 @@ static int Node_SaveXML(duk_context* ctx)
     return 1;
     return 1;
 }
 }
 
 
-static int Node_CreateChildPrefab(duk_context* ctx)
+static int Node_LoadPrefab(duk_context* ctx)
 {
 {
-    const char* childName = duk_require_string(ctx, 0);
-    const char* prefabName = duk_require_string(ctx, 1);
+    const char* prefabName = duk_require_string(ctx, 0);
 
 
     duk_push_this(ctx);
     duk_push_this(ctx);
+    Node* node = js_to_class_instance<Node>(ctx, -1, 0);
 
 
-    Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
+    PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
+    prefabComponent->SetPrefabGUID(prefabName);
 
 
-    Node* prefabNode = parent->CreateChild(childName);
+    duk_push_boolean(ctx, 1);
+    return 1;
+
+}
 
 
-    ResourceCache* cache = parent->GetSubsystem<ResourceCache>();
-    XMLFile* xmlfile = cache->GetResource<XMLFile>(prefabName);
+static int Node_CreateChildPrefab(duk_context* ctx)
+{
+    const char* childName = duk_require_string(ctx, 0);
+    const char* prefabName = duk_require_string(ctx, 1);
 
 
-    prefabNode->LoadXML(xmlfile->GetRoot());
-    prefabNode->SetTemporary(true);
+    duk_push_this(ctx);
+    Node* parent = js_to_class_instance<Node>(ctx, -1, 0);
 
 
-    prefabNode->SetPosition(Vector3::ZERO);
-    prefabNode->SetRotation(Quaternion::IDENTITY);
+    Node* node = parent->CreateChild(childName);
 
 
-#ifdef ATOMIC_3D
-    PODVector<RigidBody*> bodies;
-    prefabNode->GetComponents<RigidBody>(bodies, true);
-    for (unsigned i = 0; i < bodies.Size(); i++)
-    {
-        RigidBody* body = bodies[i];
-        body->SetTransform(body->GetNode()->GetWorldPosition(), body->GetNode()->GetWorldRotation());
-    }
-#endif
+    PrefabComponent* prefabComponent = node->CreateComponent<PrefabComponent>();
+    prefabComponent->SetPrefabGUID(prefabName);
 
 
-    js_push_class_object_instance(ctx, prefabNode, "Node");
+    js_push_class_object_instance(ctx, node, "Node");
 
 
     return 1;
     return 1;
 
 
 }
 }
 
 
+
 static int Scene_LoadXML(duk_context* ctx)
 static int Scene_LoadXML(duk_context* ctx)
 {
 {
     JSVM* vm = JSVM::GetJSVM(ctx);
     JSVM* vm = JSVM::GetJSVM(ctx);
@@ -311,9 +311,10 @@ void jsapi_init_scene(JSVM* vm)
     duk_put_prop_string(ctx, -2, "getChildAtIndex");
     duk_put_prop_string(ctx, -2, "getChildAtIndex");
     duk_push_c_function(ctx, Node_SaveXML, 1);
     duk_push_c_function(ctx, Node_SaveXML, 1);
     duk_put_prop_string(ctx, -2, "saveXML");
     duk_put_prop_string(ctx, -2, "saveXML");
+    duk_push_c_function(ctx, Node_LoadPrefab, 1);
+    duk_put_prop_string(ctx, -2, "loadPrefab");
     duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
     duk_push_c_function(ctx, Node_CreateChildPrefab, 2);
     duk_put_prop_string(ctx, -2, "createChildPrefab");
     duk_put_prop_string(ctx, -2, "createChildPrefab");
-
     duk_pop(ctx);
     duk_pop(ctx);
 
 
     js_class_get_prototype(ctx, "Atomic", "Scene");
     js_class_get_prototype(ctx, "Atomic", "Scene");