Sfoglia il codice sorgente

Commented, cleaned up code, physics fix, replaced sound

rsredsq 10 anni fa
parent
commit
1ab641c175

+ 13 - 5
Breakout/Resources/Components/Background.js

@@ -1,23 +1,31 @@
 "atomic component";
 
+//random function returns a random float number from min to max
 function random(min, max) {
     return (Math.random() * (max - min + 1)) + min;
 }
 
+//A Background component
 exports.component = function(self) {
     self.start = function() {
+        //get default zone
         var zone = Atomic.renderer.getDefaultZone();
+        //fog color with ortho camera is a clear clear
+        //so we set a clear color
         zone.setFogColor([0.282, 0.361, 0.557]);
-        for(var i = 0; i < 100; i++) {
+        //add stars on a background
+        for (var i = 0; i < 100; i++) {
+            //create a star node
             var starNode = self.node.createChild("Star");
+            //set its position to a random number
             starNode.position2D = [random(-Atomic.graphics.width/2, Atomic.graphics.width/2)*Atomic.PIXEL_SIZE, random(-Atomic.graphics.height/2, Atomic.graphics.height/2)*Atomic.PIXEL_SIZE];
+            //add static sprite component to display a 2d sprite
             var star = starNode.createComponent("StaticSprite2D");
+            //set layer of a star to -100
+            //it means that we move star node to 'background' layer
             star.layer = -100;
+            //load a sprite2D
             star.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/star.png");
         }
     }
-
-    self.update = function() {
-
-    }
 }

+ 33 - 14
Breakout/Resources/Components/Ball.js

@@ -1,36 +1,55 @@
 "atomic component";
 
-//A ball component
+var MAX_SPEED = 4;
+
+//A Ball component
 exports.component = function(self) {
+
+    //get a sound source from the scene
     var brickDestroySound = self.scene.getChild("BrickSound").getComponent("SoundSource");
+    //function to play the brick destroy sound
     function playBrickDestroySound() {
         brickDestroySound.play(brickDestroySound.sound);
     }
+
     self.start = function() {
         //define node name
         self.node.name = "Ball";
         self.rigidBody = self.getComponent("RigidBody2D");
         self.subscribeToEvent("PhysicsBeginContact2D", function(data){
-            //get a brick
-            var brick = (data.nodeA == self.node) ? data.nodeB : data.nodeA;
-            //in that case we are on 100% sure, that we could have only ball collided with brick, so we don't really need any checks
-            if (brick.name.indexOf("Brick") >= 0) {
+            //get an collidable object
+            var other = (data.nodeA == self.node) ? data.nodeB : data.nodeA;
+            //check collision for a brick
+            if (other.name.indexOf("Brick") > -1) {
+                //play brick destroy sound
                 playBrickDestroySound();
-                brick.remove();
+                //remove brick
+                other.remove();
             }
         });
     }
 
     self.update = function(delta) {
+        if (!self.started) return;
+
+        //if x || y velocity of the ball is around zero,
+        //add 1 velocity to prevent bound up / down or left / right for ever
+        if (Math.abs(self.rigidBody.linearVelocity[0]) <= 0.5)
+            self.rigidBody.linearVelocity = [self.rigidBody.linearVelocity[0]+1, self.rigidBody.linearVelocity[1]];
+
+        if (Math.abs(self.rigidBody.linearVelocity[1]) <= 0.5)
+            self.rigidBody.linearVelocity = [self.rigidBody.linearVelocity[0], self.rigidBody.linearVelocity[1]+1];
+
+        //normalize a ball speed
+        if (self.rigidBody.linearVelocity[0] > MAX_SPEED)
+            self.rigidBody.linearVelocity = [MAX_SPEED, self.rigidBody.linearVelocity[1]];
+
+        if (self.rigidBody.linearVelocity[1] > MAX_SPEED)
+            self.rigidBody.linearVelocity = [self.rigidBody.linearVelocity[0], MAX_SPEED];
+
+        //check if a ball fell down
         if (self.node.position2D[1] <= -4) {
-            self.rigidBody.remove();
-            self.gameOver = true;
-        }
-        if (self.gameOver) {
-            if(Atomic.input.getMouseButtonPress(Atomic.MOUSEB_LEFT)) {
-                self.gameOver = false;
-                Atomic.player.loadScene("Scenes/Scene.scene");
-            }
+            self.remove();
         }
     }
 }

+ 7 - 0
Breakout/Resources/Components/GameManager.js

@@ -0,0 +1,7 @@
+"atomic component";
+
+exports.component = function(self) {
+    self.start = function() {
+        
+    }
+}

+ 7 - 0
Breakout/Resources/Components/GameManager.js.asset

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

+ 16 - 3
Breakout/Resources/Components/Paddle.js

@@ -1,23 +1,36 @@
 "atomic component";
 
-//A paddle component
+//A Paddle component
 exports.component = function(self) {
     self.start = function() {
+        //define node name
         self.node.name = "Paddle";
+        //create startBall prefab
         self.startBall = self.scene.createChildPrefab("Ball", "Prefabs/Ball.prefab");
+        //also get a Ball component
+        self.startBallComponent = self.startBall.getJSComponent("Ball");
     }
 
     self.update = function(delta) {
+        //if we haven't ball started
         if(!self.started) {
+            //check mouse button click
             if(Atomic.input.getMouseButtonPress(Atomic.MOUSEB_LEFT)) {
-                self.started = true;
+                //get rigidBody component of ours ball
                 var body = self.startBall.getComponent("RigidBody2D");
+                //let's run our ball!
                 body.applyForceToCenter([20 + Math.random(), 20 + Math.random()], true);
+                self.startBallComponent.started = true;
+                self.started = true;
             }
+            //if we haven't started yet, move our ball with the paddle
             self.startBall.position2D = [self.node.position2D[0], self.node.position2D[1]+64*Atomic.PIXEL_SIZE];
         }
+        //get current mouse position, and project screen coordinates to the world coordinates
         var pos = Atomic.renderer.getViewport(0).screenToWorldPoint(Atomic.input.getMousePosition()[0], 0, 0);
-        pos[1] = -3;
+        //set y value to -3
+        pos[1] = -Atomic.graphics.height/2.5*Atomic.PIXEL_SIZE;
+        //set paddle position to the calculated one
         self.node.position2D = pos;
     }
 }

+ 13 - 17
Breakout/Resources/Components/PhysicsDebug.js

@@ -1,18 +1,14 @@
 "atomic component";
-var __extends = (this && this.__extends) || function (d, b) {for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];function __() { this.constructor = d; };__.prototype = b.prototype;d.prototype = new __();};
-function $bind(n,u){if(null==u)return null;var e;return null==e&&(e=function(){return e.method.apply(e.scope,arguments)},e.scope=n,e.method=u),e};
-var PhysicsDebug = (function(_super) {
-__extends(PhysicsDebug, _super);
-function PhysicsDebug () {
-	Atomic.JSComponent.call(this);
-};
-PhysicsDebug.prototype.start = function() {
-	var debug = this.scene.getComponent("DebugRenderer");
-	var world = this.scene.getComponent("PhysicsWorld2D");
-	this.subscribeToEvent("PostRenderUpdate",function(_) {
-		world.drawDebugGeometry();
-	});
-};
-return PhysicsDebug;
-})(Atomic.JSComponent);
-module.exports = PhysicsDebug;
+
+//A PhysicsDebug component
+exports.component = function(self) {
+    self.start = function() {
+        //we get a debugRenderer from the scene, if scene doesn't have one, it won't work
+        var debug = self.scene.getComponent("DebugRenderer");
+        var world = self.scene.getComponent("PhysicsWorld2D");
+        //we excecute drawDebugGeometry function to render world debug geometry
+        self.subscribeToEvent("PostRenderUpdate",function(_) {
+            world.drawDebugGeometry();
+        });
+    }
+}

+ 6 - 1
Breakout/Resources/Components/Wall.js

@@ -1,19 +1,24 @@
 "atomic component";
 
-//A paddle component
+//A Wall component
 exports.component = function(self) {
     self.start = function() {
+        //get rigid body component
         self.rigidBody = self.node.getComponent("RigidBody2D");
 
+        //create a new chain to define a wall
         var chain = new Atomic.CollisionChain2D();
         chain.loop = false;
+        //set vertex cound to 4
         chain.setVertexCount(4);
         var halfWidth = Atomic.graphics.width / 2 * Atomic.PIXEL_SIZE;
         var halfHeight = Atomic.graphics.height / 2 * Atomic.PIXEL_SIZE;
+        //define vertexes
         chain.setVertex(0, [-halfWidth, -halfHeight]);
         chain.setVertex(1, [-halfWidth, halfHeight]);
         chain.setVertex(2, [halfWidth, halfHeight]);
         chain.setVertex(3, [halfWidth, -halfHeight]);
+        //add a CollisionChain2D component
         self.node.addComponent(chain, 0, Atomic.LOCAL);
     }
 }

+ 0 - 5
Breakout/Resources/Materials/Star.material.asset

@@ -1,5 +0,0 @@
-{
-	"version": 1,
-	"guid": "c78e54c2d9b2c6b7e84857690da88523",
-	"MaterialImporter": {}
-}

+ 3 - 3
Breakout/Resources/Scenes/Scene.scene

@@ -5,8 +5,8 @@
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Elapsed Time" value="0" />
-	<attribute name="Next Replicated Node ID" value="470" />
-	<attribute name="Next Replicated Component ID" value="4350" />
+	<attribute name="Next Replicated Node ID" value="471" />
+	<attribute name="Next Replicated Component ID" value="4534" />
 	<attribute name="Next Local Node ID" value="16778496" />
 	<attribute name="Next Local Component ID" value="16777216" />
 	<attribute name="Variables" />
@@ -814,7 +814,7 @@
 		<attribute name="Scale" value="1 1 1" />
 		<attribute name="Variables" />
 		<component type="SoundSource" id="4347">
-			<attribute name="Sound" value="Sound;Sounds/brick.wav" />
+			<attribute name="Sound" value="Sound;Sounds/brickDeath.wav" />
 			<attribute name="Type" value="Brick" />
 		</component>
 	</node>

BIN
Breakout/Resources/Sounds/brick.wav


+ 0 - 5
Breakout/Resources/Sounds/brick.wav.asset

@@ -1,5 +0,0 @@
-{
-	"version": 1,
-	"guid": "5bcaab717f50bb4a9b2523045cdb065a",
-	"AudioImporter": {}
-}

BIN
Breakout/Resources/Sounds/brickDeath.wav


+ 5 - 0
Breakout/Resources/Sounds/brickDeath.wav.asset

@@ -0,0 +1,5 @@
+{
+	"version": 1,
+	"guid": "4affe181875af2f41bc08b7359748003",
+	"AudioImporter": {}
+}