Browse Source

Added score system. Added background music. Small fixes

rsredsq 10 years ago
parent
commit
3ddb04233d

+ 6 - 4
Breakout/Resources/Components/Ball.js

@@ -34,11 +34,11 @@ exports.component = function(self) {
 
 
         //if x || y velocity of the ball is around zero,
         //if x || y velocity of the ball is around zero,
         //add 1 velocity to prevent bound up / down or left / right for ever
         //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[0]) <= 0.0001)
+            self.rigidBody.linearVelocity = [MAX_SPEED, 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];
+        if (Math.abs(self.rigidBody.linearVelocity[1]) <= 0.0001)
+            self.rigidBody.linearVelocity = [self.rigidBody.linearVelocity[0], MAX_SPEED];
 
 
         //normalize a ball speed
         //normalize a ball speed
         if (self.rigidBody.linearVelocity[0] > MAX_SPEED)
         if (self.rigidBody.linearVelocity[0] > MAX_SPEED)
@@ -52,5 +52,7 @@ exports.component = function(self) {
             self.remove();
             self.remove();
             self.sendEvent("CreateNewBall");
             self.sendEvent("CreateNewBall");
         }
         }
+
+        console.log(self.rigidBody.linearVelocity);
     }
     }
 }
 }

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

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

+ 2 - 0
Breakout/Resources/Components/Paddle.js

@@ -15,6 +15,8 @@ exports.component = function(self) {
     self.createStartBall = function() {
     self.createStartBall = function() {
         //create startBall prefab
         //create startBall prefab
         self.startBall = self.scene.createChildPrefab("Ball", "Prefabs/Ball.prefab");
         self.startBall = self.scene.createChildPrefab("Ball", "Prefabs/Ball.prefab");
+        //move the ball off screen, because we will change its position later
+        self.startBall.position2D = [-100, -100];
         //also get a Ball component
         //also get a Ball component
         self.startBallComponent = self.startBall.getJSComponent("Ball");
         self.startBallComponent = self.startBall.getJSComponent("Ball");
     }
     }

+ 48 - 0
Breakout/Resources/Components/ScoreManager.js

@@ -0,0 +1,48 @@
+"atomic component";
+
+exports.component = function(self) {
+    self.start = function() {
+        self.scores = 0;
+        self.subscribeToEvent("PhysicsBeginContact2D", function(data){
+            //check if a ball collide with brick
+            if ((data.nodeA.name == "Ball" && data.nodeB.name.indexOf("Brick")>-1) || (data.nodeB.name == "Ball" && data.nodeA.name.indexOf("Brick")>-1)) {
+                //add scores
+                self.scores += 10;
+                self.updateText();
+            }
+        });
+
+        var view = new Atomic.UIView();
+        // Create a layout, otherwise child widgets won't know how to size themselves
+        // and would manually need to be sized
+        var layout = new Atomic.UILayout();
+
+        // specify the layout region
+        layout.rect = view.rect;
+
+        view.addChild(layout);
+
+        // we're laying out on the X axis so "position" controls top and bottom alignment
+        layout.layoutPosition = Atomic.UI_LAYOUT_POSITION_LEFT_TOP;
+        // while "distribution" handles the Y axis
+        layout.layoutDistributionPosition = Atomic.UI_LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP;
+
+        var fd = new Atomic.UIFontDescription();
+        fd.id = "Vera";
+        fd.size = 18;
+
+        self.scoreText = new Atomic.UIEditField();
+        self.scoreText.fontDescription = fd;
+        self.scoreText.readOnly = true;
+        self.scoreText.multiline = true;
+        self.scoreText.adaptToContentSize = true;
+        self.scoreText.text = "Scores: ";
+        layout.addChild(self.scoreText);
+
+        self.updateText();
+    }
+
+    self.updateText = function() {
+        self.scoreText.text = "Scrores: " + self.scores;
+    }
+}

+ 1 - 1
Breakout/Resources/Components/GameManager.js.asset → Breakout/Resources/Components/ScoreManager.js.asset

@@ -1,6 +1,6 @@
 {
 {
 	"version": 1,
 	"version": 1,
-	"guid": "b03f3eae2c7b91b0b3e50134d473456e",
+	"guid": "686cfbac4109178868fab0b33586a867",
 	"JavascriptImporter": {
 	"JavascriptImporter": {
 		"IsComponentFile": true
 		"IsComponentFile": true
 	}
 	}

+ 26 - 2
Breakout/Resources/Scenes/Scene.scene

@@ -5,8 +5,8 @@
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Smoothing Constant" value="50" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Snap Threshold" value="5" />
 	<attribute name="Elapsed Time" value="0" />
 	<attribute name="Elapsed Time" value="0" />
-	<attribute name="Next Replicated Node ID" value="471" />
-	<attribute name="Next Replicated Component ID" value="4534" />
+	<attribute name="Next Replicated Node ID" value="474" />
+	<attribute name="Next Replicated Component ID" value="4720" />
 	<attribute name="Next Local Node ID" value="16778496" />
 	<attribute name="Next Local Node ID" value="16778496" />
 	<attribute name="Next Local Component ID" value="16777216" />
 	<attribute name="Next Local Component ID" value="16777216" />
 	<attribute name="Variables" />
 	<attribute name="Variables" />
@@ -829,4 +829,28 @@
 			<attribute name="ComponentFile" value="JSComponentFile;Components/Background.js" />
 			<attribute name="ComponentFile" value="JSComponentFile;Components/Background.js" />
 		</component>
 		</component>
 	</node>
 	</node>
+	<node id="472">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="ScoreManager" />
+		<attribute name="Position" value="0 0 0" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="JSComponent" id="4718">
+			<attribute name="ComponentFile" value="JSComponentFile;Components/ScoreManager.js" />
+		</component>
+	</node>
+	<node id="473">
+		<attribute name="Is Enabled" value="true" />
+		<attribute name="Name" value="BackgroundSound" />
+		<attribute name="Position" value="0 0 0" />
+		<attribute name="Rotation" value="1 0 0 0" />
+		<attribute name="Scale" value="1 1 1" />
+		<attribute name="Variables" />
+		<component type="SoundSource" id="4719">
+			<attribute name="Sound" value="Sound;Sounds/backgroundMusic.ogg" />
+			<attribute name="Frequency" value="44100" />
+			<attribute name="Autoplay" value="true" />
+		</component>
+	</node>
 </scene>
 </scene>

BIN
Breakout/Resources/Sounds/backgroundMusic.ogg


+ 5 - 0
Breakout/Resources/Sounds/backgroundMusic.ogg.asset

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