Browse Source

Platform updates

Josh Engebretson 11 years ago
parent
commit
def47e7c15

+ 27 - 2
PhysicsPlatformerNew/Resources/Components/Level.js

@@ -1,14 +1,19 @@
 var game = Atomic.game;
-var node = self.node;
 var cache = game.cache;
 var scene = game.scene;
+var node = self.node;
 
 self.init = function(level) {
-
+        
+    node.createJSComponent("Lighting");
     self.tmxFile = cache.getResource("TmxFile2D", "Levels/" + level);
     self.tileMap = node.createComponent("TileMap2D");
     self.tileMap.setTmxFile(self.tmxFile);
     self.levelParser = new LevelParser(self.tileMap);
+    
+    var backgroundNode = scene.createChild("Background");
+    backgroundNode.createJSComponent("Background");
+
 }
 
 function spawnPlayer() {
@@ -21,11 +26,31 @@ function spawnPlayer() {
 
 }
 
+function createEntities() {
+
+    var platforms = self.levelParser.getEntities("MovingPlatform");
+    for (var i = 0; i < platforms.length; i++) {
+        var p = platforms[i];
+        var pnode = scene.createChild("PlatformNode");
+        var platform = pnode.createJSComponent("MovingPlatform");
+        platform.init(p.start, p.stop);
+    }
+    
+    var vines = self.levelParser.getEntities("Vine");
+    for (var i = 0; i < vines.length; i++) {
+        var vnode  = scene.createChild("Vine");
+        var vine = vnode.createJSComponent("Vine");
+        vine.init(vines[i].position);        
+    }
+}
+
 function start() {
 
     // create the physics
     self.levelParser.createPhysics(self.tileMap, self.tmxFile);
 
+    createEntities();
+
     // spawn the player
     spawnPlayer();
 

+ 45 - 0
PhysicsPlatformerNew/Resources/Components/LightFlicker.js

@@ -0,0 +1,45 @@
+
+// a flickering light component
+
+var 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 start() {
+}
+
+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;
+		
+
+}
+

+ 4 - 4
PhysicsPlatformerNew/Resources/Components/Lighting.js

@@ -5,19 +5,19 @@ var game = Atomic.game;
 var scene = game.scene;
 var node = self.node;
 
-var daytime = true;
+var daytime = Platformer.daytime;
 
 function start() {
 
     // currently lightgroup must be created after viewport 0 is set
-    var lightGroup = node.createComponent("Light2DGroup");
+    var lightGroup = node.createComponent("Light2DGroup");    
     lightGroup.setPhysicsWorld(Platformer.physicsWorld);
+    Platformer.lightGroup = lightGroup;
     
     if (daytime)
         lightGroup.ambientColor = [1, 1, 1, 1];
     else
-        lightGroup.ambientColor = [.8, .8, .8, .25];
-    
+        lightGroup.ambientColor = [.8, .8, .8, .25];    
     
     if (daytime)
     {

+ 0 - 1
PhysicsPlatformerNew/Resources/Components/MovingPlatform.js

@@ -13,7 +13,6 @@ var body;
 
 self.init = function(start, stop) {
 
-    print(start);
     self.startPos = start;
     self.stopPos = stop;
 

+ 11 - 7
PhysicsPlatformerNew/Resources/Components/Platformer.js

@@ -4,13 +4,17 @@ var node = self.node;
 
 Platformer = self;
 
-var physicsWorld = scene.createComponent("PhysicsWorld2D");
-physicsWorld.continuousPhysics = false;
-physicsWorld.subStepping = false;
-
-
-self.level = node.createJSComponent("Level");
-self.level.init("Level1.tmx");
+self.init = function(daytime) {
+
+    Platformer.daytime = daytime;
+    
+    var physicsWorld = self.physicsWorld = scene.createComponent("PhysicsWorld2D");
+    physicsWorld.continuousPhysics = false;
+    physicsWorld.subStepping = false;
+    
+    self.level = node.createJSComponent("Level");
+    self.level.init("Level1.tmx");
+}
 
 function start() {
 	

+ 20 - 0
PhysicsPlatformerNew/Resources/Components/Player.js

@@ -174,6 +174,26 @@ self.onPhysicsEndContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
 
 function start() {
 
+    if (!Platformer.daytime) {
+    
+        var light = node.createComponent("PointLight2D");
+        
+        light.color = [1, 1, 1, .75];
+        light.softShadowLength = 20;
+        light.radius = 20;
+        light.castShadows = true;
+        light.softShadows = true;
+        light.numRays = 256;
+        light.backtrace = true;
+        self.light = light;
+        
+        Platformer.lightGroup.addLight(light);
+        
+        node.createJSComponent("LightFlicker");
+    
+    }
+    
+
     // TODO: only listen to collisions for our node
     self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
     self.listenToEvent(null, "PhysicsEndContact2D", self.onPhysicsEndContact2D);

+ 115 - 0
PhysicsPlatformerNew/Resources/Components/UI.js

@@ -0,0 +1,115 @@
+
+var game = Atomic.game;
+var ui = game.ui;
+var root = ui.getRoot();
+
+var uiStyle = game.cache.getResource("XMLFile", "UI/DefaultStyle.xml");
+root.defaultStyle = uiStyle;
+
+var window = new Atomic.Window();
+root.addChild(window);
+
+window.setMinSize(384, 192);
+
+window.setAlignment(Atomic.HA_CENTER, Atomic.VA_CENTER);
+
+window.setLayout(Atomic.LM_VERTICAL, 6, [6, 6, 6, 6]);
+window.setName("Window");
+
+var titleBar = new Atomic.UIElement();
+titleBar.setMinSize(0, 24);
+titleBar.setVerticalAlignment(Atomic.VA_TOP);
+titleBar.setLayoutMode(Atomic.LM_HORIZONTAL);
+
+// Create the Window title Text
+var windowTitle = new Atomic.Text();
+windowTitle.setName("WindowTitle");
+windowTitle.setText("Please select Daytime of Nighttime");
+titleBar.addChild(windowTitle);
+
+window.addChild(titleBar);
+
+// Daytime button
+var button = new Atomic.Button();
+button.setName ("Daytime");
+button.setMinHeight(48);
+
+var buttonText = new Atomic.Text();
+
+buttonText.text = "Daytime";
+var font = game.cache.getResource("Font", "Fonts/Anonymous Pro.ttf");
+
+buttonText.setFont(font, 12);
+buttonText.color = [1, 1, 0, 1];
+
+buttonText.horizontalAlignment = Atomic.HA_CENTER;
+buttonText.verticalAlignment = Atomic.VA_CENTER;
+button.addChild(buttonText);
+
+window.addChild(button);
+button.setStyleAuto();
+
+// Nighttime button
+button = new Atomic.Button();
+button.setName ("Nighttime");
+button.setMinHeight(48);
+
+buttonText = new Atomic.Text();
+
+buttonText.text = "Nighttime";
+
+buttonText.setFont(font, 12);
+buttonText.color = [0, 1, 1, 1];
+
+buttonText.horizontalAlignment = Atomic.HA_CENTER;
+buttonText.verticalAlignment = Atomic.VA_CENTER;
+button.addChild(buttonText);
+
+window.addChild(button);
+button.setStyleAuto();
+
+window.movable = true;
+window.resizeable = true;
+
+window.setStyleAuto();
+titleBar.setStyleAuto();
+windowTitle.setStyleAuto();
+
+
+self.onMouseClick = function(element) {
+
+    var go = 0;
+    
+    if (element.name == "Daytime") {
+        go = 1;
+    }
+    
+    if (element.name == "Nighttime") {
+        go = 2;    
+    }
+   
+    if (go) {
+    
+        root.removeChild(window);
+    
+        var platformerNode = game.scene.createChild("Platformer");
+        var platformer = platformerNode.createJSComponent("Platformer");
+        
+        platformer.init(go == 1);
+    
+    }
+    
+}
+
+function start() {
+
+    self.listenToEvent(null, "UIMouseClick", self.onMouseClick );
+
+}
+
+function update(timeStep) {
+
+    
+    
+}
+

+ 53 - 1
PhysicsPlatformerNew/Resources/Scripts/LevelParser.js

@@ -15,6 +15,8 @@ LevelParser.prototype = {
 
         entityLayer = this.tileMap.getLayerByName("Entities");
 
+        var platforms = {};
+
         if (entityLayer) {
 
             for (var i = 0; i < entityLayer.numObjects; i++) {
@@ -22,12 +24,39 @@ LevelParser.prototype = {
                 var o = entityLayer.getObject(i);
                 var onode = entityLayer.getObjectNode(i);
 
-                entity = {};
+                var entity = {
+                    type: null
+                };
 
                 if (o.type == "PlayerSpawn") {
 
                     entity.type = "PlayerSpawn";
                     entity.position = onode.position2D;
+
+                }
+                if (o.type == "Vine") {
+
+                    entity.type = "Vine";
+                    entity.position = onode.position2D;
+
+                } else if (o.type == "PlatformStart") {
+
+
+                    var pnum = Number(o.getProperty("Platform"));
+
+                    if (!platforms.hasOwnProperty(pnum))
+                        platforms[pnum] = [null, null];
+
+                    platforms[pnum][0] = o;
+
+                } else if (o.type == "PlatformStop") {
+
+                    var pnum = Number(o.getProperty("Platform"));
+
+                    if (!platforms.hasOwnProperty(pnum))
+                        platforms[pnum] = [null, null];
+
+                    platforms[pnum][1] = o;
                 }
 
                 if (entity.type)
@@ -35,6 +64,17 @@ LevelParser.prototype = {
 
             }
 
+            for (var pnum in platforms) {
+
+                var entity = {
+                    type: "MovingPlatform",
+                    start: platforms[pnum][0].position,
+                    stop: platforms[pnum][1].position
+                }
+
+                this.entities.push(entity);
+            }
+
         }
 
     },
@@ -49,6 +89,18 @@ LevelParser.prototype = {
 
     },
 
+    getEntities: function(type) {
+
+        var entities = [];
+
+        for (var i = 0; i < this.entities.length; i++)
+            if (this.entities[i].type == type)
+                entities.push(this.entities[i]);
+
+        return entities;
+
+    },
+
     getSpawnpoint: function(type) {
 
         var pos = [0, 0];

+ 3 - 3
PhysicsPlatformerNew/Resources/Scripts/main.js

@@ -16,9 +16,9 @@ function start() {
 	// create a 2D scene
 	game.createScene2D();
 	
-    var platformerNode = game.scene.createChild("Platformer");
-    platformerNode.createJSComponent("Platformer");
-	
+	var uiNode = game.scene.createChild("UI");
+	uiNode.createJSComponent("UI");
+		
 }
 
 // called per frame