Shaddock Heath 10 роки тому
батько
коміт
a0b5685e7a

+ 3 - 0
CONTRIBUTORS.md

@@ -2,3 +2,6 @@
 
 ##### **standtall007**
 * RobomanPlatformer
+
+#### **shaddockh**
+* EventLoop

+ 0 - 0
EventLoop/EventLoop.atomic


+ 53 - 0
EventLoop/README.md

@@ -0,0 +1,53 @@
+## Example of using an Event Loop ##
+
+Including an EventLoop allows you to schedule actions to occur in the future, or 
+actions to occur every # of milliseconds.  
+
+in order to use, you need to require the ```AtomicEventLoop``` module in your ```main.js```
+
+```require('AtomicEventLoop');```
+
+This mirrors the eventloop available in
+the web browser and provides the following global functions.
+
+### setTimeout ###
+```timer_id = setTimeout(func, delay, [params...])```
+This will allow you to schedule a function to execute ```delay``` number of milliseconds
+in the future.
+
+By providing the optional ```params```, you can specify what parameters to pass to the function
+when it is called.
+
+timer_id will be passed back to allow you to cancel the timer before it executes.
+
+### clearTimeout ###
+```clearTimeout(timer_id)```
+Cancels a previously scheduled timeout.
+
+### setInterval ###
+```timer_id = setInterval(func, delay, [params...])```
+This will allow you to schedule a function to execute every ```delay``` number of milliseconds
+in the future.
+
+By providing the optional ```params```, you can specify what parameters to pass to the function
+when it is called.
+
+timer_id will be passed back to allow you to cancel the timer before it executes.
+
+### clearInterval ###
+```clearInterval(timer_id)```
+Cancels a previously scheduled interval.
+
+### setImmediate ###
+```timer_id = setImmediate(func,[params...])```
+This will allow you to schedule a function to execute immediately after the current
+update loop.
+
+By providing the optional ```params```, you can specify what parameters to pass to the function
+when it is called.
+
+timer_id will be passed back to allow you to cancel the function before it executes.
+
+### clearImmediate ###
+```clearImmediate(timer_id)```
+Cancels a previously scheduled setImmediate.

+ 5 - 0
EventLoop/Resources.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "a059f5288351d7bb16744349e2ff3ee0",
+	"FolderImporter": {}
+}

+ 5 - 0
EventLoop/Resources/Components.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "d6e10959bcccccbbb7c5572b5ae62dd2",
+	"FolderImporter": {}
+}

+ 23 - 0
EventLoop/Resources/Components/Star.js

@@ -0,0 +1,23 @@
+'atomic component';
+
+module.exports.component = function (self) {
+
+    // Inspector fields will show up in the Atomic Editor scene view to allow editing
+    var inspectorFields = {
+        speed: 100,
+    };
+
+    var node = self.node;
+
+    // Start will be called when component is instantiated
+    self.start = function () {
+        var sprite2D = node.createComponent("StaticSprite2D");
+        sprite2D.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/star.png");
+        sprite2D.blendMode = Atomic.BLEND_ALPHA;
+    };
+
+    // Update will be called every cycle
+    self.update = function (timeStep) {
+        node.roll(timeStep * self.speed);
+    };
+};

+ 7 - 0
EventLoop/Resources/Components/Star.js.asset

@@ -0,0 +1,7 @@
+{
+	"version": 1,
+	"guid": "85a0d16feb30d877edde285b1d45d121",
+	"JavascriptImporter": {
+		"IsComponentFile": true
+	}
+}

+ 5 - 0
EventLoop/Resources/Scripts.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "608e204f195f83312d2be4ba86ea347f",
+	"FolderImporter": {}
+}

+ 78 - 0
EventLoop/Resources/Scripts/main.js

@@ -0,0 +1,78 @@
+// This script is the main entry point of the game
+// Require in the event loop handler
+// This is necessary if you want to take advantage of setTimeout/clearTimeout, setInterval/clearInterval, setImmediate/clearImmediate
+require('AtomicEventLoop');
+
+// it appears that scene needs to be stored in a global so it's not GC'd
+var scene;
+
+function createScene() {
+    // create a 2D scene
+    var scene = new Atomic.Scene();
+    scene.createComponent("Octree");
+
+    var cameraNode = scene.createChild("Camera");
+    cameraNode.position = [0.0, 0.0, -10.0];
+
+    var camera = cameraNode.createComponent("Camera");
+    camera.orthographic = true;
+    camera.orthoSize = Atomic.graphics.height * Atomic.PIXEL_SIZE;
+
+    var viewport = null;
+
+    viewport = new Atomic.Viewport(scene, camera);
+    Atomic.renderer.setViewport(0, viewport);
+
+    return scene;
+}
+
+
+// Set up the scene, create the star, and set some scheduled events via setTimeout and setInterval
+function main() {
+    // create a 2D scene
+    scene = createScene();
+
+    // create the star node.
+    var starNode = scene.createChild('Star');
+    var star = starNode.createJSComponent('Components/Star.js');
+    starNode.position2D = [0, 0];
+
+    // reverse direction after 2 seconds
+    setTimeout(function () {
+        star.speed = -100;
+    }, 2000);
+
+    // start moving the star after 3 seconds
+    setTimeout(function () {
+        var currentX = 0,
+            currentY = 0;
+        starNode.position2D = [currentX, currentY];
+
+        // every 5ms second move the star a little bit more in a diagonal
+        // NOTE, you are not going to want to do animations this way,...this is just an example.  Doing it this way ends up introducing a stutter
+        var movementId = setInterval(function () {
+            currentX += 0.05;
+            currentY += 0.05;
+            starNode.position2D = [currentX, currentY];
+            // stop moving when we get in position
+            if (currentX > 2.5 || currentY > 2.5) {
+                clearInterval(movementId);
+                // handle at the end of this update cycle
+                setImmediate(function() {
+                    star.speed = 100;
+                });
+                
+                // set up something that we are going to immediately cancel so it doesn't happen
+                var wonthappen  = setImmediate(function() {
+                    star.speed = 1000;
+                });
+                
+                clearImmediate(wonthappen);
+            }
+        }, 5);
+    }, 3000);
+}
+main();
+
+// we don't need an update handler here, but if we don't set one up, then main gets GC'd
+module.exports.update = function(timeStep) {};

+ 7 - 0
EventLoop/Resources/Scripts/main.js.asset

@@ -0,0 +1,7 @@
+{
+	"version": 1,
+	"guid": "c6b53081a4572271133c8d65e8f1f27e",
+	"JavascriptImporter": {
+		"IsComponentFile": false
+	}
+}

+ 5 - 0
EventLoop/Resources/Sprites.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "8dfbd7d752d9946307899dc86fd9f8d1",
+	"FolderImporter": {}
+}

BIN
EventLoop/Resources/Sprites/star.png


+ 5 - 0
EventLoop/Resources/Sprites/star.png.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "4152a9d4073c61e20d0f1bc221df96f7",
+	"TextureImporter": {}
+}