소스 검색

Adding updated new 2D project template, Player now holds a strong reference to current scene

Josh Engebretson 10 년 전
부모
커밋
9ce82e5d81

+ 9 - 10
Data/AtomicEditor/ProjectTemplates/Project2D/Resources/Components/Star.js

@@ -1,16 +1,15 @@
-var game = Atomic.game;
-var node = self.node;
+"atomic component";
 
-function start() {
-
-	var sprite2D = node.createComponent("StaticSprite2D");
-	sprite2D.sprite = game.getSprite2D("Sprites/star.png");
-	sprite2D.blendMode = Atomic.BLEND_ALPHA;
-	
+var inspectorFields = {
+  speed: 1.0
 }
 
-function update(timeStep) {	
+module.exports = function(self) {
+
+  self.update = function(timeStep) {
+
+    self.node.rotate2D(timeStep * 75 * self.speed);
 
-	node.roll(timeStep * 100);
+  }
 
 }

+ 45 - 0
Data/AtomicEditor/ProjectTemplates/Project2D/Resources/Scenes/Scene.scene

@@ -0,0 +1,45 @@
+<?xml version="1.0"?>
+<scene id="1">
+	<attribute name="Name" value="" />
+	<attribute name="Time Scale" value="1" />
+	<attribute name="Smoothing Constant" value="50" />
+	<attribute name="Snap Threshold" value="5" />
+	<attribute name="Elapsed Time" value="0" />
+	<attribute name="Next Replicated Node ID" value="364" />
+	<attribute name="Next Replicated Component ID" value="1978" />
+	<attribute name="Next Local Node ID" value="16778496" />
+	<attribute name="Next Local Component ID" value="16777216" />
+	<attribute name="Variables" />
+	<attribute name="Variable Names" value="" />
+	<component type="PhysicsWorld" id="1" />
+	<component type="Octree" id="2" />
+	<component type="DebugRenderer" id="3" />
+	<component type="Renderer2D" id="1976" />
+	<node id="361">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="Camera" />
+		<attribute name="Position" value="0 0 -5" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="Camera" id="1973">
+			<attribute name="Near Clip" value="0" />
+			<attribute name="Orthographic" value="true" />
+			<attribute name="Orthographic Size" value="8" />
+		</component>
+	</node>
+	<node id="363">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="Node" />
+		<attribute name="Position" value="0 0 0" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="StaticSprite2D" id="1975">
+			<attribute name="Sprite" value="Sprite2D;Sprites/star.png" />
+		</component>
+		<component type="JSComponent" id="1977">
+			<attribute name="ComponentFile" value="JSComponentFile;Components/Star.js" />
+		</component>
+	</node>
+</scene>

+ 1 - 23
Data/AtomicEditor/ProjectTemplates/Project2D/Resources/Scripts/main.js

@@ -1,25 +1,3 @@
-
 // This script is the main entry point of the game
-require("AtomicGame");
-
-Atomic.game.init(start, update);
-
-
-// called at the start of play
-function start() {
-
-	var game = Atomic.game;
-
-	// create a 2D scene
-	game.createScene2D();
-
-    var spaceNode = game.scene.createChild("Star");
-    spaceNode.createJSComponent("Star");
-
-}
-
-// called per frame
-function update(timeStep) {
-
 
-}
+Atomic.player.loadScene("Scenes/Scene.scene");

+ 50 - 1
Script/AtomicEditor/ui/frames/inspector/ComponentInspector.ts

@@ -134,6 +134,10 @@ class ComponentInspector extends Atomic.UISection {
             this.addSoundSourceUI(attrsVerticalLayout, component.typeName);
         }
 
+        if (component.typeName == "StaticSprite2D" || component.typeName == "AnimatedSprite2D") {
+            this.addSpriteUI(attrsVerticalLayout, component.typeName);
+        }
+
 
         var deleteButton = new Atomic.UIButton();
         deleteButton.text = "Delete Component";
@@ -328,7 +332,7 @@ class ComponentInspector extends Atomic.UISection {
 
         var numGeometries = staticModel.numGeometries;
         if (typeName == "Skybox") {
-          numGeometries = 1;
+            numGeometries = 1;
         }
 
         for (var x = 0; x < staticModel.numGeometries; x++) {
@@ -438,6 +442,51 @@ class ComponentInspector extends Atomic.UISection {
         });
 
 
+    }
+
+    addSpriteUI(layout: Atomic.UILayout, typeName: string) {
+
+        var spriteComponent = <Atomic.StaticSprite2D> this.component;
+
+        var o = InspectorUtils.createAttrEditFieldWithSelectButton("Sprite", layout);
+        var field = o.editField;
+        field.readOnly = true;
+        field.text = spriteComponent.sprite ? spriteComponent.sprite.name : "";
+
+        var select = o.selectButton;
+
+        select.onClick = () => {
+
+            // this should allow selecting of sprite sheets as well
+            EditorUI.getModelOps().showResourceSelection("Select Sprite", "TextureImporter", function(asset: ToolCore.Asset) {
+
+                spriteComponent.sprite = <Atomic.Sprite2D> Atomic.cache.getResource("Sprite2D", asset.path);
+                if (spriteComponent.sprite)
+                    field.text = spriteComponent.sprite.name;
+
+            });
+
+        }
+
+        // handle dropping of component on field
+        field.subscribeToEvent(field, "DragEnded", (ev: Atomic.DragEndedEvent) => {
+
+            if (ev.target == field) {
+
+                var importer = this.acceptAssetDrag("TextureImporter", ev);
+
+                if (importer) {
+
+                  spriteComponent.sprite = <Atomic.Sprite2D> Atomic.cache.getResource("Sprite2D", importer.asset.path);
+                  if (spriteComponent.sprite)
+                      field.text = spriteComponent.sprite.name;
+
+                }
+            }
+
+        });
+
+
     }
 
     addLightCascadeParametersUI(layout: Atomic.UILayout) {

+ 5 - 0
Script/AtomicEditor/ui/frames/inspector/CreateComponentButton.ts

@@ -8,6 +8,10 @@ audioCreateSource.addItem(new Atomic.UIMenuItem("SoundListener", "SoundListener"
 audioCreateSource.addItem(new Atomic.UIMenuItem("SoundSource", "SoundSource"));
 audioCreateSource.addItem(new Atomic.UIMenuItem("SoundSource3D", "SoundSource3D"));
 
+var _2DCreateSource = new Atomic.UIMenuItemSource();
+_2DCreateSource.addItem(new Atomic.UIMenuItem("StaticSprite2D", "StaticSprite2D"));
+_2DCreateSource.addItem(new Atomic.UIMenuItem("AnimatedSprite2D", "AnimatedSprite2D"));
+
 var geometryCreateSource = new Atomic.UIMenuItemSource();
 
 geometryCreateSource.addItem(new Atomic.UIMenuItem("StaticModel", "StaticModel"));
@@ -59,6 +63,7 @@ var componentCreateSource = new Atomic.UIMenuItemSource();
 
 var sources = {
     Audio: audioCreateSource,
+    "2D": _2DCreateSource,
     Geometry: geometryCreateSource,
     Logic: logicCreateSource,
     Navigation: navigationCreateSource,

+ 2 - 2
Source/AtomicJS/Javascript/JSScene.cpp

@@ -155,8 +155,8 @@ static int Node_GetComponents(duk_context* ctx)
 
     for (unsigned i = 0; i < dest.Size(); i++)
     {
-        js_push_class_object_instance(ctx, dest[i], "Component");
-        duk_put_prop_index(ctx, -2, i);
+        if (js_push_class_object_instance(ctx, dest[i], "Component"))
+            duk_put_prop_index(ctx, -2, i);
     }
 
     return 1;

+ 7 - 2
Source/AtomicJS/Javascript/JSVM.h

@@ -229,8 +229,13 @@ inline bool js_push_class_object_instance(duk_context* ctx, const RefCounted *in
     duk_push_pointer(ctx, (void*) instance->GetClassID());
     duk_get_prop(ctx, -2);
 
-    // if this is tripped, means the class hasn't been registered and shouldn't be trying to push it
-    assert(duk_is_object(ctx, -1));
+    // if not an object, this instance isn't not a scriptable class
+    // reset top and return false
+    if (!duk_is_object(ctx, -1))
+    {
+        duk_set_top(ctx, top);
+        return false;
+    }
 
     duk_get_prop_index(ctx, -1, 0);
     const char* package = duk_require_string(ctx, -1);

+ 15 - 2
Source/AtomicPlayer/Player.cpp

@@ -1,5 +1,8 @@
 
 #include <Atomic/IO/Log.h>
+
+#include <Atomic/Input/InputEvents.h>
+
 #include <Atomic/Resource/ResourceCache.h>
 #include <Atomic/Graphics/Renderer.h>
 #include <Atomic/Graphics/Camera.h>
@@ -14,6 +17,8 @@ Player::Player(Context* context) :
 {
     viewport_ = new Viewport(context_);
     GetSubsystem<Renderer>()->SetViewport(0, viewport_);
+
+    SubscribeToEvent(E_EXITREQUESTED, HANDLER(Player, HandleExitRequested));
 }
 
 Player::~Player()
@@ -21,6 +26,13 @@ Player::~Player()
 
 }
 
+void Player::HandleExitRequested(StringHash eventType, VariantMap& eventData)
+{
+    currentScene_ = 0;
+    viewport_ = 0;
+
+}
+
 Scene* Player::LoadScene(const String& filename, Camera *camera)
 {
     ResourceCache* cache = GetSubsystem<ResourceCache>();
@@ -31,14 +43,15 @@ Scene* Player::LoadScene(const String& filename, Camera *camera)
         return 0;
     }
 
-    Scene* scene = new Scene(context_);
+    SharedPtr<Scene> scene(new Scene(context_));
 
     if (!scene->LoadXML(*file))
     {
-        scene->ReleaseRef();
         return 0;
     }
 
+    currentScene_ = scene;
+
     if(!camera)
     {
         PODVector<Node*> cameraNodes;

+ 7 - 0
Source/AtomicPlayer/Player.h

@@ -23,8 +23,15 @@ public:
 
     Scene* LoadScene(const String& filename, Camera* camera = NULL);
 
+    Scene* GetCurrentScene() { return currentScene_; }
+
 private:
 
+    void HandleExitRequested(StringHash eventType, VariantMap& eventData);
+
+    // Strong reference
+    SharedPtr<Scene> currentScene_;
+
     SharedPtr<Viewport> viewport_;
 
 };