Browse Source

Day/Night time for the PhysicsPlatformer!

rsredsq 10 years ago
parent
commit
362afbd20f

+ 2 - 15
Light2DExample/Resources/Scenes/Scene.scene

@@ -5,8 +5,8 @@
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Elapsed Time" value="0" />
 	<attribute name="Elapsed Time" value="0" />
-	<attribute name="Next Replicated Node ID" value="391" />
-	<attribute name="Next Replicated Component ID" value="2018" />
+	<attribute name="Next Replicated Node ID" value="392" />
+	<attribute name="Next Replicated Component ID" value="2019" />
 	<attribute name="Next Local Node ID" value="16778496" />
 	<attribute name="Next Local Node ID" value="16778496" />
 	<attribute name="Next Local Component ID" value="16777216" />
 	<attribute name="Next Local Component ID" value="16777216" />
 	<attribute name="Variables" />
 	<attribute name="Variables" />
@@ -17,19 +17,6 @@
 	<component type="Zone" id="2017">
 	<component type="Zone" id="2017">
 		<attribute name="Ambient Color" value="0.3 0.3 0.3 1" />
 		<attribute name="Ambient Color" value="0.3 0.3 0.3 1" />
 	</component>
 	</component>
-	<node id="2">
-		<attribute name="Is Enabled" value="true" />
-		<attribute name="Name" value="Zone" />
-		<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="Zone" id="4">
-			<attribute name="Bounding Box Min" value="-10000 -10000 -10000" />
-			<attribute name="Bounding Box Max" value="10000 10000 10000" />
-			<attribute name="Ambient Color" value="0.4 0.4 0.4 1" />
-		</component>
-	</node>
 	<node id="361">
 	<node id="361">
 		<attribute name="Is Enabled" value="true" />
 		<attribute name="Is Enabled" value="true" />
 		<attribute name="Name" value="Camera" />
 		<attribute name="Name" value="Camera" />

+ 10 - 0
PhysicsPlatformer/Resources/Components/Bat.js

@@ -17,6 +17,16 @@ var component = function (self) {
 
 
   var time = Math.random() * 10000;
   var time = Math.random() * 10000;
 
 
+  self.start = function() {
+    var dayTime = require("GlobalVariables").dayTime;
+    if(!dayTime) {
+      //ok, it's a night, then create a light
+      var light = node.createComponent("PointLight2D");
+      light.color = [1, 0.1, 0.8, 1];
+      light.radius = 1;
+    }
+  }
+
   self.update = function(timestep) {
   self.update = function(timestep) {
 
 
       time += timestep * 4;
       time += timestep * 4;

+ 16 - 9
PhysicsPlatformer/Resources/Components/Coin.js

@@ -1,5 +1,4 @@
 "atomic component";
 "atomic component";
-
 //requiring gl-matrix module
 //requiring gl-matrix module
 var glmatrix = require("gl-matrix");
 var glmatrix = require("gl-matrix");
 var vec2 = glmatrix.vec2;
 var vec2 = glmatrix.vec2;
@@ -31,13 +30,25 @@ var component = function(self) {
   var activated = false;
   var activated = false;
   var body;
   var body;
 
 
+  self.start = function() {
+    //get current dayTime by requiring GlobalVariables object
+    var dayTime = require("GlobalVariables").dayTime;
+    if(!dayTime) {
+      //ok, it's a night, then create a light
+      var light = node.createComponent("PointLight2D");
+      light.color = [1, 1, .56, .8];
+      light.radius = .85;
+
+      node.createJSComponent("Components/LightFlicker.js");
+    }
+  }
+
   self.update = function(timeStep) {
   self.update = function(timeStep) {
 
 
     if (activated)
     if (activated)
       return false;
       return false;
 
 
     if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
     if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
-
       activated = true;
       activated = true;
       //setting rigid body
       //setting rigid body
       body = node.createComponent("RigidBody2D");
       body = node.createComponent("RigidBody2D");
@@ -47,7 +58,7 @@ var component = function(self) {
       body.fixedRotation = true;
       body.fixedRotation = true;
       //don't make our body to cast shadows
       //don't make our body to cast shadows
       body.castShadows = false;
       body.castShadows = false;
-      
+
       //subscribing to PhysicsBeginContact2D event, and specifying a callback
       //subscribing to PhysicsBeginContact2D event, and specifying a callback
       self.subscribeToEvent("PhysicsBeginContact2D", function(ev) {
       self.subscribeToEvent("PhysicsBeginContact2D", function(ev) {
         //checking if nodeB is our current node
         //checking if nodeB is our current node
@@ -55,16 +66,12 @@ var component = function(self) {
           //checking if nodeA(another node) is a Player
           //checking if nodeA(another node) is a Player
           if (ev.nodeA && ev.nodeA.name == "Player") {
           if (ev.nodeA && ev.nodeA.name == "Player") {
             //picking up a coin
             //picking up a coin
-            //setting scale to 0, 0
-            node.scale2D = [0, 0];
-            //disable sprite
-            sprite.enabled = false;
-            //disable body
-            body.enabled = false;
             if (self.pickupSound) {
             if (self.pickupSound) {
               soundSource.gain = 1.0;
               soundSource.gain = 1.0;
               //playing pickupSound
               //playing pickupSound
               soundSource.play(self.pickupSound);
               soundSource.play(self.pickupSound);
+              //ok, remove itself from the parent node
+              node.remove();
             }
             }
           //if it's not a player, and we have bounceSound, then play it
           //if it's not a player, and we have bounceSound, then play it
           } else if (self.bounceSound) {
           } else if (self.bounceSound) {

+ 17 - 0
PhysicsPlatformer/Resources/Components/DayTime.js

@@ -0,0 +1,17 @@
+"atomic component";
+
+//DayTime component
+var component = function(self) {
+
+  var dayTime = require("GlobalVariables").dayTime;
+
+  self.start = function() {
+    if(!dayTime) {
+      //if it's night, make TheSun color darker
+      var sun = self.node.getChild("TheSun").getComponent("DirectionalLight2D");
+      sun.color = [0.05, 0.05, 0.07, 0.1];
+    }
+  }
+}
+
+exports.component = component;

+ 7 - 0
PhysicsPlatformer/Resources/Components/DayTime.js.asset

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

+ 37 - 0
PhysicsPlatformer/Resources/Components/LightFlicker.js

@@ -0,0 +1,37 @@
+"atomic component"
+
+// a flickering light component
+exports.component = function(self){
+  var node = self.node;
+  self.light = node.getComponent("PointLight2D");
+  var baseRange = 2;
+  var targetValue = baseRange;
+
+  //define a flicker pattern
+  var flicker = "mmmaaaammmaaaabcdefgabcdefg";
+  var index = Math.random() * (flicker.length - 1);
+
+  // make sure first update catches
+  var time = 100;
+
+  self.update = function(timestep) {
+
+    time += timestep;
+    if (time > .05)
+    {
+      index++;
+      time = 0.0;
+      if (index >= flicker.length)
+        index = 0;
+
+      targetValue = baseRange * (flicker.charCodeAt(index)/255);
+
+    }
+
+    if (self.light.radius < targetValue)
+      self.light.radius += timestep * 10;
+
+    if (self.light.radius > targetValue)
+      self.light.radius -= timestep * 10;
+  }
+}

+ 7 - 0
PhysicsPlatformer/Resources/Components/LightFlicker.js.asset

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

+ 11 - 0
PhysicsPlatformer/Resources/Components/Player.js

@@ -65,6 +65,17 @@ exports.component = function(self) {
                 contactCount--;
                 contactCount--;
         });
         });
 
 
+        //get current dayTime by requiring GlobalVariables object
+        var dayTime = require("GlobalVariables").dayTime;
+        if(!dayTime) {
+          //ok, it's a night, then create a light
+          var light = node.createComponent("PointLight2D");
+          light.color = [1, 0.5, 0.7, 0.8];
+          light.backtrace = true;
+          light.castShadows = true;
+          light.numRays = 256;
+        }
+
     }
     }
 
 
     self.update = function(timeStep) {
     self.update = function(timeStep) {

+ 7 - 0
PhysicsPlatformer/Resources/Modules/GlobalVariables.js

@@ -0,0 +1,7 @@
+//can be required
+//uses to pass dayTime as static global variable
+var GlobalVariables = {
+  DayTime: false
+};
+
+exports = GlobalVariables;

+ 7 - 0
PhysicsPlatformer/Resources/Modules/GlobalVariables.js.asset

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

+ 14 - 9
PhysicsPlatformer/Resources/Scenes/Scene.scene

@@ -5,23 +5,28 @@
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Elapsed Time" value="0" />
 	<attribute name="Elapsed Time" value="0" />
-	<attribute name="Next Replicated Node ID" value="26319" />
-	<attribute name="Next Replicated Component ID" value="33451" />
-	<attribute name="Next Local Node ID" value="16778764" />
-	<attribute name="Next Local Component ID" value="16777422" />
+	<attribute name="Next Replicated Node ID" value="32079" />
+	<attribute name="Next Replicated Component ID" value="40437" />
+	<attribute name="Next Local Node ID" value="16778812" />
+	<attribute name="Next Local Component ID" value="16777462" />
 	<attribute name="Variables" />
 	<attribute name="Variables" />
 	<attribute name="Variable Names" value="" />
 	<attribute name="Variable Names" value="" />
 	<component type="Octree" id="2" />
 	<component type="Octree" id="2" />
-	<component type="DebugRenderer" id="3" />
 	<component type="Renderer2D" id="1976" />
 	<component type="Renderer2D" id="1976" />
-	<component type="PhysicsWorld2D" id="1983">
+	<component type="PhysicsWorld2D" id="36942">
 		<attribute name="Draw Shape" value="true" />
 		<attribute name="Draw Shape" value="true" />
 		<attribute name="Allow Sleeping" value="true" />
 		<attribute name="Allow Sleeping" value="true" />
 		<attribute name="Warm Starting" value="true" />
 		<attribute name="Warm Starting" value="true" />
 		<attribute name="Auto Clear Forces" value="true" />
 		<attribute name="Auto Clear Forces" value="true" />
 	</component>
 	</component>
-	<component type="Zone" id="16857">
-		<attribute name="Ambient Color" value="1 1 1 1" />
+	<component type="DebugRenderer" id="37815" />
+	<component type="Zone" id="38691">
+		<attribute name="Bounding Box Min" value="-100 -100 -100" />
+		<attribute name="Bounding Box Max" value="100 100 100" />
+		<attribute name="Ambient Color" value="0.8 0.8 0.8 0.8" />
+	</component>
+	<component type="JSComponent" id="38692">
+		<attribute name="ComponentFile" value="JSComponentFile;Components/DayTime.js" />
 	</component>
 	</component>
 	<node id="361">
 	<node id="361">
 		<attribute name="Is Enabled" value="true" />
 		<attribute name="Is Enabled" value="true" />
@@ -74,7 +79,7 @@
 		<attribute name="Scale" value="1 1 1" />
 		<attribute name="Scale" value="1 1 1" />
 		<attribute name="Variables" />
 		<attribute name="Variables" />
 		<component type="DirectionalLight2D" id="18606">
 		<component type="DirectionalLight2D" id="18606">
-			<attribute name="Color" value="1 1 1 0.15" />
+			<attribute name="Color" value="1 1 1 0.4" />
 			<attribute name="Cast Shadows" value="true" />
 			<attribute name="Cast Shadows" value="true" />
 			<attribute name="Num Rays" value="512" />
 			<attribute name="Num Rays" value="512" />
 		</component>
 		</component>

+ 62 - 1
PhysicsPlatformer/Resources/Scripts/main.js

@@ -1,3 +1,64 @@
 // This script is the main entry point of the game
 // This script is the main entry point of the game
 
 
-Atomic.player.loadScene("Scenes/Scene.scene");
+// create the start ui programmatically, we could also
+// use a ui template
+//create  main view
+var view = new Atomic.UIView();
+//create a window
+var window = new Atomic.UIWindow();
+//disable tile bard and make it non resizeable
+window.settings = Atomic.UI_WINDOW_SETTINGS_TITLEBAR;
+window.text = "Physics Platformer";
+
+// Create a layout, otherwise child widgets won't know how to size themselves
+// and would manually need to be sized
+var layout = new Atomic.UILayout();
+layout.rect = view.rect;
+// give ourselves a little more spacing
+layout.spacing = 18;
+//axis to y
+layout.axis = Atomic.UI_AXIS_Y;
+//add ours layout to window
+window.addChild(layout);
+
+//create a text field
+var text = new Atomic.UITextField();
+text.text = "Please select the time of day:";
+layout.addChild(text);
+
+// Buttons layout
+var buttonLayout = new Atomic.UILayout();
+buttonLayout.axis = Atomic.UI_AXIS_X;
+layout.addChild(buttonLayout);
+var buttonDaytime = new Atomic.UIButton();
+buttonDaytime.text = "Play Daytime";
+buttonDaytime.onClick = function () {
+  run(true);
+  //we need to return value here, otherwise we will be GC'ed
+  return true;
+}
+buttonLayout.addChild(buttonDaytime);
+
+var buttonNightTime = new Atomic.UIButton();
+buttonNightTime.text = "Play Nighttime";
+buttonNightTime.onClick = function () {
+  run(false);
+  //we need to return value here, otherwise we will be GC'ed
+  return true;
+}
+buttonLayout.addChild(buttonNightTime);
+
+window.resizeToFitContent();
+
+// add to the root view and center
+view.addChild(window);
+window.center();
+var dayTime;
+function run(daytime) {
+  //ok, then remove ours window
+  view.removeChild(window);
+  //require GlobalVariables module, and set its dayTime value to the current daytime
+  require("GlobalVariables").dayTime = daytime;
+  //load main scene!
+  Atomic.player.loadScene("Scenes/Scene.scene");
+}