Browse Source

Added comments to the PhysicsPlatformer project

rsredsq 10 years ago
parent
commit
3e82c99f85

+ 9 - 3
PhysicsPlatformer/Resources/Components/Background.js

@@ -1,16 +1,22 @@
 "atomic component";
 
+//A Background
 var component = function(self) {
-
+	//getting a main camera of current scene
     var camera = self.node.scene.getMainCamera();
     var cameraNode = camera.node;
 
+	//This function will be called each frame after the update function
     self.postUpdate = function() {
-
+		//get position of our camera
+		//position2D returns an array, which has 3 elements, the first is x, second is y, third is z
         var pos = cameraNode.position2D;
-        pos[1] -= 4;
+		pos[1] -= 4;
+		//set position to the current node
         self.node.position2D = pos;
+		//get camera's zoom, and changing it to make a nice paralax effect
         var zoom = 4.0 - camera.zoom;
+		//scale2D is an array with two elements, the first is x, the second is y
         self.node.scale2D = [zoom , zoom];
 
     }

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

@@ -1,4 +1,5 @@
-
+//requiring gl-matrix module
+//https://github.com/toji/gl-matrix for more information
 var glmatrix = require("gl-matrix");
 var vec2 = glmatrix.vec2;
 
@@ -7,7 +8,8 @@ var vec2 = glmatrix.vec2;
 var component = function (self) {
 
   var node = self.node;
-
+  
+  //get a component from our current node
   var sprite = node.getComponent("AnimatedSprite2D")
   sprite.setAnimation("Fly");
 
@@ -20,6 +22,7 @@ var component = function (self) {
       time += timestep * 4;
 
       var waypoints = node.waypoints;
+	  //get node position, returns an array with two elements, the first is x, the second is y
       var pos = node.position2D;
 
       if (cwaypoint == -1 || vec2.distance(pos, waypoints[cwaypoint]) < .5) {
@@ -40,6 +43,7 @@ var component = function (self) {
           sprite.flipX = false;
 
       vec2.add(pos, pos, dir);
+	  //set position of our node
       node.position2D = pos;
 
   }

+ 27 - 17
PhysicsPlatformer/Resources/Components/Coin.js

@@ -1,5 +1,11 @@
 "atomic component";
 
+//requiring gl-matrix module
+var glmatrix = require("gl-matrix");
+var vec2 = glmatrix.vec2;
+
+//settings some fields to make them visible from editor
+//the first parameter is a type, the second is a default value
 var inspectorFields = {
 
   pickupSound: ["Sound", "Sounds/Pickup_Coin8.ogg"],
@@ -7,29 +13,24 @@ var inspectorFields = {
 
 }
 
-var glmatrix = require("gl-matrix");
-var vec2 = glmatrix.vec2;
-
+//A Coin
 var component = function(self) {
 
   var node = self.node;
+  //getting main camera from our current scene
   var camera = self.node.scene.getMainCamera();
   var cameraNode = camera.node;
 
   // Resources
-
+  //getting AnimatedSprite2D component
   var sprite = node.getComponent("AnimatedSprite2D")
   sprite.setAnimation("idle");
-
+  //getting SoundSource component
   var soundSource = node.getComponent("SoundSource");
 
   var activated = false;
   var body;
 
-  self.start = function() {
-
-  }
-
   self.update = function(timeStep) {
 
     if (activated)
@@ -38,26 +39,34 @@ var component = function(self) {
     if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
 
       activated = true;
-
+	  //setting rigid body
       body = node.createComponent("RigidBody2D");
-      body.setBodyType(Atomic.BT_DYNAMIC);
+      //our body is Dynamic
+	  body.setBodyType(Atomic.BT_DYNAMIC);
+	  //fix rotation
       body.fixedRotation = true;
+	  //don't make our body to cast shadows
       body.castShadows = false;
-
+	  
+	  //subscribing to PhysicsBeginContact2D event, and specifying a callback 
       self.subscribeToEvent("PhysicsBeginContact2D", function(ev) {
-
+		//checking if nodeB is our current node
         if (ev.nodeB == node) {
-
+		  //checking if nodeA(another node) is a Player
           if (ev.nodeA && ev.nodeA.name == "Player") {
-
+			//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) {
               soundSource.gain = 1.0;
+			  //playing pickupSound
               soundSource.play(self.pickupSound);
             }
-
+		  //if it's not a player, and we have bounceSound, then play it
           } else if (self.bounceSound) {
 
             var vel = body.linearVelocity;
@@ -68,7 +77,8 @@ var component = function(self) {
           }
         }
       });
-
+	  
+	  //adding circle colision shape
       var circle = node.createComponent("CollisionCircle2D");
 
       // Set radius

+ 20 - 19
PhysicsPlatformer/Resources/Components/Level.js

@@ -1,26 +1,30 @@
 
 "atomic component";
-
+//requiring LevelParser module
 var LevelParser = require("LevelParser");
 
+//A Level
 var component = function (self) {
-
+  //start function will be excecuted, after connecting our component to the node, after the constructor
   self.start = function() {
-
+	//get TileMap2D component from our current node
     var tileMap = self.node.getComponent("TileMap2D");
     var tmxFile = tileMap.tmxFile;
-
+	
+	//create LevelParser object
     var levelParser = new LevelParser(tileMap);
     levelParser.createPhysics(tileMap, tmxFile);
 
     var position = levelParser.getSpawnpoint();
     position[1] += 1.5;
-
+	
+	//create a child prefab
     var node = self.scene.createChildPrefab("Player", "Prefabs/Hero.prefab");
+	//set it position to the current node position
     node.position2D = position;
-
+	
+	//get all entities from our map which names MovingPlatform
     var platforms = levelParser.getEntities("MovingPlatform");
-
     for (var i = 0; i < platforms.length; i++) {
 
         var p = platforms[i];
@@ -30,40 +34,37 @@ var component = function (self) {
         node.startPos = p.start;
         node.stopPos = p.stop;
     }
-
+	
+	//get all entities from our map which names Coin
     var coins = levelParser.getEntities("Coin");
     for (var i = 0; i < coins.length; i++) {
         var node = self.scene.createChildPrefab("Coin", "Prefabs/Coin.prefab");
         node.position2D = coins[i].position;
     }
-
+	
+	//get all entities from our map which names BatWaypoint
     var waypoints = [];
     var batWaypoints = levelParser.getEntities("BatWaypoint");
     for (var i = 0; i < batWaypoints.length; i++) {
         waypoints.push(batWaypoints[i].position);
     }
-
+	
+	//get all entities from our map which names Bat
     var bats = levelParser.getEntities("Bat");
-
     for (var i = 0; i < bats.length; i++) {
         var node = self.scene.createChildPrefab("Bat", "Prefabs/Bat.prefab");
         node.position2D = bats[i].position;
         node.scale2D = [.5, .5];
         node.waypoints = waypoints;
     }
-
+	
+	//get all entities from our map which names Vine
     var vines = levelParser.getEntities("Vine");
     for (var i = 0; i < vines.length; i++) {
         var vnode  = self.scene.createChild("Vine");
         vnode.createJSComponent("Components/Vine.js", {startPosition : vines[i].position});
     }
-
-
-
-  }
-
-  self.update = function(timeStep) {
-
+	
   }
 
 }

+ 6 - 6
PhysicsPlatformer/Resources/Components/MovingPlatform.js

@@ -1,12 +1,11 @@
-
+//requiring gl-matrix module
 var glmatrix = require("gl-matrix");
 var vec2 = glmatrix.vec2;
 
 "atomic component";
 
-var component = function (self) {
-
 // A moving platform
+var component = function (self) {
 
 var node = self.node;
 
@@ -14,10 +13,11 @@ var MAX_VELOCITY = 2;
 
 var movingToStop = true;
 
+//Get RigidBody2D component
 var body = self.getComponent("RigidBody2D");
-
+//update function called each frame
 self.update = function(timeStep) {
-
+	//get node position
     var pos = node.position2D;
     var dir = vec2.create();
     var dist = 0.0;
@@ -53,7 +53,7 @@ self.update = function(timeStep) {
         vec2.normalize(dir, dir);
         vec2.scale(dir, dir, MAX_VELOCITY);
     }
-
+	//set velocity of our body(node) to the dir value
     body.setLinearVelocity(dir);
 
 }

+ 0 - 35
PhysicsPlatformer/Resources/Components/PhysicsTest.js

@@ -1,35 +0,0 @@
-
-"atomic component";
-
-var component = function (self) {
-
-    var node = self.node;
-    var body = node.createComponent("RigidBody2D");
-    body.setBodyType(Atomic.BT_DYNAMIC);
-    body.fixedRotation = true;
-    body.bullet = true;
-    body.castShadows = false;
-
-    var circle = node.createComponent("CollisionCircle2D");
-    // Set radius
-    circle.setRadius(.5);
-    // Set density
-    circle.setDensity(1.0);
-    // Set friction.
-    circle.friction = .2;
-    // Set restitution
-    circle.setRestitution(0.1);
-
-
-
-  self.start = function() {
-
-  }
-
-  self.update = function(timeStep) {
-
-  }
-
-}
-
-exports.component = component;

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

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

+ 39 - 26
PhysicsPlatformer/Resources/Components/Player.js

@@ -6,14 +6,17 @@ var inspectorFields = {
 
 }
 
-
+//A Player
 exports.component = function(self) {
 
     var node = self.node;
+	//get main camera of the current scene
     var camera = self.node.scene.getMainCamera();
     var cameraNode = camera.node;
+	//link to Atomic.Input
     var input = Atomic.input;
-    var soundSource = node.getComponent("SoundSource");
+    //get SoundSource component
+	var soundSource = node.getComponent("SoundSource");
 
     var MAX_VELOCITY = 3;
     var anim = "";
@@ -33,35 +36,39 @@ exports.component = function(self) {
     // physics
     var body;
     var circle;
-
+	
+	//function called after the component attached to the node
     self.start = function() {
-
+		
+		//get components
         sprite = node.getComponent("AnimatedSprite2D");
         body = node.getComponent("RigidBody2D");
         circle = node.getComponent("CollisionCircle2D");
 
         setAnimation("Idle");
         
+		//subscribe to PhysicsPostStep2D
         self.subscribeToEvent("PhysicsPostStep2D", function(event) {
-        
-            cameraNode.position = node.position;
-        
+            //set camera position to the current node position
+			cameraNode.position = node.position;
         });
-
+		//subscribe to PhysicsBeginContact2D
         self.subscribeToEvent("PhysicsBeginContact2D", function(event) {
+			//if bodyB is our body, so increment contactCount
             if (event.bodyB == body)
                 contactCount++;
         });
-
+		//subscribe to 
         self.subscribeToEvent("PhysicsEndContact2D", function(event) {
-            if (event.bodyB == body)
+            //if bodyB is our body, so decrement contactCount
+			if (event.bodyB == body)
                 contactCount--;
         });
 
     }
 
     self.update = function(timeStep) {
-
+		//handle our input and animation stuff
         handleInput(timeStep);
         handleAnimation(timeStep);
 
@@ -70,8 +77,7 @@ exports.component = function(self) {
             // setposition catching dirty
             node.scale2D = [0, 0];
             node.position2D = spawnPosition;
-            node.scale2D = [1, 1];
-
+           	node.scale2D = [1, 1];
         }
 
     }
@@ -82,7 +88,7 @@ exports.component = function(self) {
             return;
 
         lastAnimDelta = .25;
-
+		//sprite is an AnimatedSprite2D object, set its animation to the given animName
         sprite.setAnimation(animName);
         anim = animName;
 
@@ -93,32 +99,33 @@ exports.component = function(self) {
         lastAnimDelta -= timeStep;
 
         var vel = body.linearVelocity;
-
+		//if we have a contact with something
         if (contactCount) {
-
+			//if velocity by X less than zero
             if (vel[0] < -0 && control) {
                 if (!flipped) {
                     sprite.flipX = true;
                     flipped = true;
-
                 }
+				//set current animation to Run animation
                 setAnimation("Run");
+			//if velocity by X is greater than zero
             } else if (vel[0] > 0 && control) {
-
                 if (flipped) {
                     sprite.flipX = false;
                     flipped = false;
-
                 }
+				//set current animation to Run animation
                 setAnimation("Run");
             } else {
-
+				//if our velocity equals to zero, so set current animation to Idle animation
                 setAnimation("Idle");
             }
         } else {
-
+			//if velocity by Y greater than 1, so we are jumping
             if (vel[1] > 1.0) {
                 setAnimation("Jump");
+			//if velocity by Y is less than 1, so we are falling
             } else if (vel[1] < -1.0) {
                 setAnimation("Land");
             }
@@ -136,25 +143,27 @@ exports.component = function(self) {
 
         if (Math.abs(vel[0]) > MAX_VELOCITY) {
             vel[0] = (vel[0] ? vel[0] < 0 ? -1 : 1 : 0) * MAX_VELOCITY;
+			//set body linear velocity to the current velocity
             body.setLinearVelocity(vel);
         }
-
+		//input stuff
         var left = input.getKeyDown(Atomic.KEY_LEFT) || input.getKeyDown(Atomic.KEY_A);
         var right = input.getKeyDown(Atomic.KEY_RIGHT) || input.getKeyDown(Atomic.KEY_D);
 
         var jump = input.getKeyDown(Atomic.KEY_UP) || input.getKeyDown(Atomic.KEY_SPACE) || input.getKeyDown(Atomic.KEY_W);
 
         control = false;
-
+		//if left or right is pressed
         if (left || right)
             control = true;
-
+			
+		//applying linear impulses to the left and right
         if (left && vel[0] > -MAX_VELOCITY) {
             body.applyLinearImpulse([-2, 0], pos, true);
         } else if (right && vel[0] < MAX_VELOCITY) {
             body.applyLinearImpulse([2, 0], pos, true);
         }
-
+		//if we are not pressing any buttons, so enable friction
         if (!left && !right) {
             vel[0] *= 0.9;
             body.linearVelocity = vel;
@@ -165,17 +174,21 @@ exports.component = function(self) {
 
         if (!contactCount)
             circle.friction = 0.0;
-
+		
+		//if we are jumping and colliding with something
         if (jump && jumpDelta <= 0 && contactCount) {
 
             jumpDelta = .25;
+			//is sound exists
             if (self.jumpSound) {
                 soundSource.gain = 0.45;
+				//playing sound
                 soundSource.play(self.jumpSound);
             }
 
             vel[1] = 0;
             body.linearVelocity = vel;
+			//applying linear impuls to the Y
             body.applyLinearImpulse([0, 6], pos, true);
         }
 

+ 0 - 15
PhysicsPlatformer/Resources/Components/Star.js

@@ -1,15 +0,0 @@
-"atomic component";
-
-var inspectorFields = {
-  speed: 1.0
-}
-
-exports.component = function(self) {
-
-  self.update = function(timeStep) {
-
-    self.node.rotate2D(timeStep * 75 * self.speed);
-
-  }
-
-}

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

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

+ 26 - 13
PhysicsPlatformer/Resources/Components/Vine.js

@@ -1,12 +1,13 @@
-// Rope Vine
 "atomic component";
 
+// Rope Vine
 var component = function (self) {
 
 var NUM_OBJECTS = 10;
 
 var node = self.node;
 
+//set current node position to 0, 0, 0
 node.position = [0, 0, 0];
 
 self.start = function() {
@@ -16,20 +17,25 @@ self.start = function() {
 
         // create the node body
         var groundBody = node.createComponent("RigidBody2D");
+		//do not cast shadows
         groundBody.castShadows = false;
 
         var prevBody = groundBody;
 
         for (var i = 0; i < NUM_OBJECTS; i++) {
-
+			//create a new vine node
             var vnode = node.scene.createChild("RigidBody");
-
+			//add StaticSprite2D to the created node
             var sprite2D = vnode.createComponent("StaticSprite2D");
-            sprite2D.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/vine.png");
-
+            //set its sprite
+			sprite2D.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/vine.png");
+			
+			//create rigid body component
             var vbody = vnode.createComponent("RigidBody2D");
-            vbody.castShadows = false;
-            vbody.bodyType = Atomic.BT_DYNAMIC;
+            //do not cast shadows
+			vbody.castShadows = false;
+            //set body type to Dynamic
+			vbody.bodyType = Atomic.BT_DYNAMIC;
 
             // Create box
             var vbox = vnode.createComponent("CollisionBox2D");
@@ -37,25 +43,32 @@ self.start = function() {
             vbox.friction = .2;
             // Set mask bits.
             vbox.maskBits = 0xFFFF & ~0x0002;
-
+			//set vine node position
             vnode.position = [x + 0.5 + 1.0 * i, y, 0.0];
-            vbox.size = [1.0, 0.1];
+            //set box collision size
+			vbox.size = [1.0, 0.1];
+			//set density
             vbox.density = 5.0;
+			//set category bits
             vbox.categoryBits = 0x0001;
 
             if (i == NUM_OBJECTS - 1)
                 vbody.angularDamping = 0.4;
-
+				
+			//create joint component
             var joint = vnode.createComponent("ConstraintRevolute2D");
-            joint.otherBody = prevBody;
+            //join it to the previous body
+			joint.otherBody = prevBody;
+			//set ancor
             joint.anchor = [x + i, y];
             joint.collideConnected = false;
             prevBody = vbody;
 
         }
-
+		//create ConstraintRope2D component
         var constraintRope = node.createComponent("ConstraintRope2D");
-        constraintRope.otherBody = prevBody;
+        //set other body to the previous body
+		constraintRope.otherBody = prevBody;
         constraintRope.ownerBodyAnchor = [x, y];
         constraintRope.maxLength = (NUM_OBJECTS + 0.01);
 

+ 32 - 23
PhysicsPlatformer/Resources/Modules/LevelParser.js

@@ -1,5 +1,7 @@
 // Atomic Script
+//It's just a module, so it don't need atomic component string
 
+//Define a constructor
 LevelParser = function(tileMap) {
 
     this.tileMap = tileMap;
@@ -8,26 +10,28 @@ LevelParser = function(tileMap) {
     this.parseEntities();
 
 }
-
+//Define a prototype
 LevelParser.prototype = {
-
+	
+	//parsing our entities from tileMap object
     parseEntities: function() {
 
         entityLayer = this.tileMap.getLayerByName("Entities");
 
         var platforms = {};
-
+		//if layer Entities exists
         if (entityLayer) {
-
+			//iterating through each object
             for (var i = 0; i < entityLayer.numObjects; i++) {
-
+				//get object itself
                 var o = entityLayer.getObject(i);
-                var onode = entityLayer.getObjectNode(i);
+                //get node of object
+				var onode = entityLayer.getObjectNode(i);
 
                 var entity = {
                     type: null
                 };
-
+				//checks for type of object
                 if (o.type == "PlayerSpawn") {
 
                     entity.type = "PlayerSpawn";
@@ -72,7 +76,7 @@ LevelParser.prototype = {
 
                     platforms[pnum][1] = o;
                 }
-
+				//if type was found, then push an object to the entities array
                 if (entity.type)
                     this.entities.push(entity);
 
@@ -92,7 +96,8 @@ LevelParser.prototype = {
         }
 
     },
-
+	
+	//returns the first found entity of the given type
     getEntity: function(type) {
 
         for (var i = 0; i < this.entities.length; i++)
@@ -102,7 +107,8 @@ LevelParser.prototype = {
         return null;
 
     },
-
+	
+	//returns an array of entities of the given type
     getEntities: function(type) {
 
         var entities = [];
@@ -114,7 +120,8 @@ LevelParser.prototype = {
         return entities;
 
     },
-
+	
+	//returns spawnpoint
     getSpawnpoint: function(type) {
 
         var pos = [0, 0];
@@ -123,23 +130,25 @@ LevelParser.prototype = {
         return entity ? entity.position : pos;
 
     },
-
-    createPhysics: function(tileMap, tmxFile) {
-
+	
+	createPhysics: function(tileMap, tmxFile) {
+		//get Physics layer
         physicsLayer = tileMap.getLayerByName("Physics");
 
+		//if layer exists
         if (physicsLayer) {
-
+			//iterate through each object
             for (var i = 0; i < physicsLayer.numObjects; i++) {
-
+				//get object
                 var o = physicsLayer.getObject(i);
-
+				//get node
                 var onode = physicsLayer.getObjectNode(i);
-                var group = tmxFile.getTileObjectGroup(o.tileGid);
+                //returns object group
+				var group = tmxFile.getTileObjectGroup(o.tileGid);
                 var obody = null;
-
+				//if group exists
                 if (group) {
-
+					//iterate through each group object
                     for (var j = 0; j < group.numObjects; j++) {
 
                         var go = group.getObject(j);
@@ -147,12 +156,12 @@ LevelParser.prototype = {
                         if (go.validCollisionShape()) {
 
                             if (!obody) {
+								//create rigid body
                                 obody = onode.createComponent("RigidBody2D");
-                                obody.bodyType = Atomic.BT_DYNAMIC;
+								obody.bodyType = Atomic.BT_DYNAMIC;
                                 obody.awake = false;
-
                             }
-
+							//create a collision shape for our object
                             var shape = go.createCollisionShape(onode);
                             shape.density = 1.0;
                             shape.friction = 1.0;