Browse Source

Light flickers

Josh Engebretson 11 years ago
parent
commit
8e75889395

+ 4 - 2
PhysicsPlatformer/Resources/Components/Avatar.js

@@ -16,11 +16,13 @@ sprite.setLayer(100);
 
 var light = node.createComponent("PointLight2D");
 light.color = [1, 1, 1, 1];
-light.softShadowLength = 4;
-light.radius = 4;
+light.softShadowLength = 20;
+light.radius = 20;
 
 lightGroup.addLight(light);
 
+node.createJSComponent("LightFlicker");
+
 camera.zoom = .9;
 
 node.setPosition(PlayerSpawnPoint);

+ 4 - 2
PhysicsPlatformer/Resources/Components/Bat.js

@@ -17,11 +17,13 @@ var cwaypoint = -1;
 
 var light = node.createComponent("PointLight2D");
 light.color = [1, 1, 1, 1];
-light.softShadowLength = 4;
-light.radius = 4;
+light.softShadowLength = 8;
+light.radius = 8;
 
 lightGroup.addLight(light);
 
+node.createJSComponent("LightFlicker");
+
 
 function update(timestep) {
 

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

@@ -0,0 +1,42 @@
+
+// a flickering light component
+
+node = self.node;
+self.light = node.getComponent("PointLight2D");
+var baseRange = self.light.radius;
+var targetValue = baseRange;
+
+
+flicker = "mmmaaaammmaaaabcdefgabcdefg";
+var index = Math.random() * (flicker.length - 1);
+
+// make sure first update catches
+time = 100;
+
+self.light.radius = baseRange * flicker.charCodeAt(index)/255;
+
+function update(timestep) {
+
+	time += timestep;
+	if (time > .025)
+	{
+		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 * 2;
+
+	if (self.light.radius > targetValue)
+		self.light.radius -= timestep * 2;
+		
+    self.light.softShadowLength = self.light.radius;
+		
+
+}
+