Browse Source

Light2D example

Josh Engebretson 11 years ago
parent
commit
bd4f921dbb

+ 0 - 0
Light2DExample/Light2DExample.atomic


BIN
Light2DExample/Resources/Backgrounds/background.jpg


+ 26 - 0
Light2DExample/Resources/Components/Background.js

@@ -0,0 +1,26 @@
+var node = self.node;
+var game = Atomic.game;
+var cameraNode = game.cameraNode;
+var camera = game.camera;
+
+function start() {
+
+    var sprite2D = node.createComponent("StaticSprite2D");
+    sprite2D.sprite = game.cache.getResource("Sprite2D", "Backgrounds/background.jpg");
+    sprite2D.layer = -100;
+    
+    var width = sprite2D.texture.width;
+    var height = sprite2D.texture.height;
+    
+    var viewWidth = game.viewport.width;
+    var viewHeight = game.viewport.height;       
+    
+    node.scale2D = [viewWidth/width, viewHeight/height];
+
+}
+
+
+// fixme must have an update
+function update(timeStep) {
+
+}

+ 45 - 0
Light2DExample/Resources/Components/Light.js

@@ -0,0 +1,45 @@
+var game = Atomic.game;
+var node = self.node;
+
+var light = node.createComponent("PointLight2D");
+light.color = [.1 + Math.random() * .9, .1 + Math.random() * .9, .1 + Math.random() * .9, 1];
+light.radius = 4;
+light.castShadows = true;
+light.softShadows = true;
+light.numRays = 512;
+
+
+Light2DExample.lightGroup.addLight(light);
+
+var x = -Light2DExample.halfWidth + (Light2DExample.halfWidth * 2) * Math.random();
+var y = -Light2DExample.halfHeight + (Light2DExample.halfHeight * 2) * Math.random();
+
+node.position2D = [x, y];
+
+var movex = -2 + (Math.random() * 4);
+var movey = -2 + (Math.random() * 4);
+
+function start() {
+
+
+}
+
+function update(timeStep) {
+
+    var prev = node.position2D;
+
+    node.translate2D([movex * timeStep, movey * timeStep]);
+
+    var p = node.position2D;
+
+    if (p[0] < -Light2DExample.halfWidth || p[0] > Light2DExample.halfWidth) {
+        node.position2D = prev;
+        movex = -movex;
+    }
+    
+    if (p[1] < -Light2DExample.halfHeight || p[1] > Light2DExample.halfHeight) {
+        node.position2D = prev;
+        movey = -movey;
+    }
+
+}

+ 14 - 0
Light2DExample/Resources/Components/Physics.js

@@ -0,0 +1,14 @@
+var game = Atomic.game;
+var scene = game.scene;
+
+
+
+function start() {
+
+	
+}
+
+function update(timeStep) {	
+
+
+}

+ 24 - 0
Light2DExample/Resources/Components/ShadowCaster.js

@@ -0,0 +1,24 @@
+var game = Atomic.game;
+var node = self.node;
+
+var body = node.createComponent("RigidBody2D");
+body.bodyType = Atomic.BT_STATIC;
+body.castShadows = true;
+
+var circle = node.createComponent("CollisionCircle2D");
+circle.radius = .25;
+
+var x = -Light2DExample.halfWidth + (Light2DExample.halfWidth * 2) * Math.random();
+var y = -Light2DExample.halfHeight + (Light2DExample.halfHeight * 2) * Math.random();
+
+node.position2D = [x, y];
+
+function start() {
+
+	
+}
+
+function update(timeStep) {	
+
+
+}

+ 57 - 0
Light2DExample/Resources/Scripts/main.js

@@ -0,0 +1,57 @@
+
+// This script is the main entry point of the game
+
+Light2DExample = {};
+
+// called at the start of play
+function Start() {
+
+	var game = Atomic.game;
+	
+	Light2DExample.halfWidth = game.graphics.width * Atomic.PIXEL_SIZE * 0.5;
+    Light2DExample.halfHeight = game.graphics.height * Atomic.PIXEL_SIZE * 0.5;
+
+	// create a 2D scene
+	game.createScene2D();
+		
+	var scene = game.scene;
+	
+    var physicsWorld = scene.createComponent("PhysicsWorld2D");        
+    physicsWorld.continuousPhysics = false;
+    physicsWorld.subStepping = false;
+    
+    var lightGroupNode = scene.createChild("LightGroup");
+    
+    var lightGroup = lightGroupNode.createComponent("Light2DGroup");
+    lightGroup.setPhysicsWorld(physicsWorld);
+    lightGroup.ambientColor = [.8, .8, .8, .5];
+    
+    Light2DExample.lightGroup = lightGroup;
+    
+	var node = game.scene.createChild();
+	node.createJSComponent("Background");
+	node.createJSComponent("Physics");
+	
+	for (var i = 0; i < 4; i++) {
+	
+	    var lightNode = scene.createChild("LightNode");
+        lightNode.createJSComponent("Light");
+
+	}
+	
+	for (var i = 0; i < 16; i++) {
+	
+	    var shadowCasterNode = scene.createChild("ShadowCaster");
+        shadowCasterNode.createJSComponent("ShadowCaster");
+
+	}
+	
+
+}
+
+// called per frame
+function Update(timeStep) {
+
+    physicsWorld.drawDebugGeometry();
+
+}