Josh Engebretson 11 years ago
parent
commit
144a80992f

+ 1 - 0
PhysicsPlatformer/Resources/Components/Avatar.js

@@ -1,6 +1,7 @@
 // CONSTANTS
 // CONSTANTS
 var MAX_VELOCITY = 3;
 var MAX_VELOCITY = 3;
 
 
+ThePlayer = self;
 
 
 var node = self.node;
 var node = self.node;
 
 

+ 55 - 0
PhysicsPlatformer/Resources/Components/Coin.js

@@ -0,0 +1,55 @@
+// FIXME
+keepAlive = typeof(keepAlive) == "undefined" ? [] : keepAlive;
+keepAlive.push(self);
+
+
+var glmatrix = require("gl-matrix");
+var vec2 = glmatrix.vec2;
+
+var node = self.node;
+
+var animationSet = cache.getResource("AnimationSet2D", "Sprites/GoldIcon.scml");
+
+var sprite = node.createComponent("AnimatedSprite2D");
+sprite.setAnimation(animationSet, "idle");
+sprite.setLayer(100);
+
+var activated = false;
+
+var body;
+
+self.onPlayerHit = function() {
+    
+    // sprite enabled is not removing the sprite
+    node.scale2D = [0, 0];
+    sprite.enabled = false;
+    body.enabled = false;
+
+}
+
+function update(timeStep) {
+
+    if (activated)
+        return false;
+
+    if (vec2.distance(cameraNode.position2D, node.position2D) < 3.0) {
+        activated = true;
+
+        body = node.createComponent("RigidBody2D");
+        body.setBodyType(Atomic.BT_DYNAMIC);
+        body.fixedRotation = true;
+
+        var circle = node.createComponent("CollisionCircle2D");
+        // Set radius
+        circle.setRadius(.3);
+        // Set density
+        circle.setDensity(1.0);
+        // Set friction.
+        circle.friction = .2;
+        // Set restitution
+        circle.setRestitution(1.1);
+
+
+    }
+
+}

+ 43 - 3
PhysicsPlatformer/Resources/Components/Level.js

@@ -1,3 +1,5 @@
+TheLevel = self;
+
 var node = self.node;
 var node = self.node;
 
 
 var tmxFile = cache.getResource("TmxFile2D", "Levels/Level1.tmx");
 var tmxFile = cache.getResource("TmxFile2D", "Levels/Level1.tmx");
@@ -12,14 +14,29 @@ PlayerSpawnPoint = [0, 0];
 
 
 var platforms = {};
 var platforms = {};
 
 
+self.coinNodes = [];
+
+// parsed coins
+var coins = [];
+
 camera.setZoom(.75);
 camera.setZoom(.75);
 
 
+
+self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
+
+    if (nodeA == ThePlayer.node && self.coinNodes.indexOf(nodeB) != -1) {    
+        nodeB.coin.onPlayerHit();
+    }
+
+}
+
+
 // create a platform based on start and stop TileMapObject2D
 // create a platform based on start and stop TileMapObject2D
 
 
 function createPlatform(start, stop) {
 function createPlatform(start, stop) {
 
 
     var platformNode = scene.createChild("Platform");
     var platformNode = scene.createChild("Platform");
-    platform = platformNode .createComponent("JSComponent");
+    platform = platformNode.createComponent("JSComponent");
 
 
     // setting the classname calls start, we need a way to provide
     // setting the classname calls start, we need a way to provide
     // component values before this, as anything is the component function
     // component values before this, as anything is the component function
@@ -31,6 +48,14 @@ function createPlatform(start, stop) {
 
 
 }
 }
 
 
+function createCoin(obj) {
+
+    var coinNode = scene.createChild("Coin");
+    coinNode.position2D = obj.position;
+    coin = coinNode.createJSComponent("Coin");
+    self.coinNodes.push(coinNode);
+}
+
 function parseEntities() {
 function parseEntities() {
 
 
     entityLayer = tileMap.getLayerByName("Entities");
     entityLayer = tileMap.getLayerByName("Entities");
@@ -58,6 +83,10 @@ function parseEntities() {
                     platforms[pnum] = [null, null];
                     platforms[pnum] = [null, null];
 
 
                 platforms[pnum][1] = o;
                 platforms[pnum][1] = o;
+            } else if (o.type == "Coin") {
+
+                coins.push(o);
+
             }
             }
 
 
         }
         }
@@ -68,6 +97,13 @@ function parseEntities() {
         createPlatform(platforms[pnum][0], platforms[pnum][1]);
         createPlatform(platforms[pnum][0], platforms[pnum][1]);
 
 
     }
     }
+
+    for (var i in coins) {
+
+        createCoin(coins[i]);
+
+    }
+
 }
 }
 
 
 function parsePhysics() {
 function parsePhysics() {
@@ -111,5 +147,9 @@ function parsePhysics() {
     }
     }
 }
 }
 
 
-parsePhysics();
-parseEntities();
+function start() {
+    self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
+    parsePhysics();
+    parseEntities();
+
+}

+ 45 - 0
PhysicsPlatformer/Resources/Levels/Level1.tmx

@@ -2140,5 +2140,50 @@
     <property name="Platform" value="2"/>
     <property name="Platform" value="2"/>
    </properties>
    </properties>
   </object>
   </object>
+  <object name="Coin" type="Coin" x="1238.74" y="1002.63" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1316.42" y="944.731" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1395.99" y="891.748" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1475.69" y="952.941" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1552.53" y="1008.44" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2202.8" y="1262.41" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1889.01" y="1256.6" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2125.96" y="1206.91" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1966.69" y="1198.7" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2046.26" y="1145.72" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2761.53" y="1153.15" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2447.74" y="1147.34" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2684.69" y="1097.65" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2525.42" y="1089.44" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="2604.99" y="1036.46" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
  </objectgroup>
  </objectgroup>
 </map>
 </map>

+ 1 - 2
PhysicsPlatformer/Resources/PhysicsPlatformer.js

@@ -24,7 +24,7 @@ function Start() {
 
 
 function Update() {
 function Update() {
     
     
-    //physicsWorld.drawDebugGeometry();
+   // physicsWorld.drawDebugGeometry();
 
 
 }
 }
 
 
@@ -40,7 +40,6 @@ function CreateScene() {
     physicsWorld.continuousPhysics = false;
     physicsWorld.continuousPhysics = false;
     physicsWorld.subStepping = false;
     physicsWorld.subStepping = false;
         
         
-
     cameraNode = scene.createChild("Camera");
     cameraNode = scene.createChild("Camera");
     cameraNode.position = [0.0, 0.0, -10.0];
     cameraNode.position = [0.0, 0.0, -10.0];
     camera = cameraNode.createComponent("Camera");
     camera = cameraNode.createComponent("Camera");

+ 48 - 0
PhysicsPlatformer/Resources/Sprites/GoldIcon.scml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<spriter_data scml_version="1.0" generator="BrashMonkey Spriter" generator_version="b8_1">
+    <folder id="0" name="GoldIcon">
+        <file id="0" name="GoldIcon/2.png" width="64" height="64" pivot_x="0.5" pivot_y="0.5"/>
+        <file id="1" name="GoldIcon/1.png" width="64" height="64" pivot_x="0.5" pivot_y="0.5"/>
+        <file id="2" name="GoldIcon/3.png" width="17" height="60" pivot_x="0.5" pivot_y="0.5"/>
+        <file id="3" name="GoldIcon/4.png" width="64" height="64" pivot_x="0.5" pivot_y="0.5"/>
+        <file id="4" name="GoldIcon/5.png" width="64" height="64" pivot_x="0.5" pivot_y="0.5"/>
+    </folder>
+    <entity id="0" name="entity_000">
+        <animation id="0" name="idle" length="500">
+            <mainline>
+                <key id="0">
+                    <object_ref id="0" timeline="0" key="0" z_index="0"/>
+                </key>
+                <key id="1" time="100">
+                    <object_ref id="0" timeline="0" key="1" z_index="0"/>
+                </key>
+                <key id="2" time="200">
+                    <object_ref id="0" timeline="0" key="2" z_index="0"/>
+                </key>
+                <key id="3" time="300">
+                    <object_ref id="0" timeline="0" key="3" z_index="0"/>
+                </key>
+                <key id="4" time="400">
+                    <object_ref id="0" timeline="0" key="4" z_index="0"/>
+                </key>
+            </mainline>
+            <timeline id="0" name="1">
+                <key id="0" spin="0">
+                    <object folder="0" file="1" angle="0"/>
+                </key>
+                <key id="1" time="100" spin="0">
+                    <object folder="0" file="0" angle="0"/>
+                </key>
+                <key id="2" time="200" spin="0">
+                    <object folder="0" file="2" angle="0"/>
+                </key>
+                <key id="3" time="300" spin="0">
+                    <object folder="0" file="3" angle="0"/>
+                </key>
+                <key id="4" time="400" spin="0">
+                    <object folder="0" file="4" angle="0"/>
+                </key>
+            </timeline>
+        </animation>
+    </entity>
+</spriter_data>

BIN
PhysicsPlatformer/Resources/Sprites/GoldIcon/1.png


BIN
PhysicsPlatformer/Resources/Sprites/GoldIcon/2.png


BIN
PhysicsPlatformer/Resources/Sprites/GoldIcon/3.png


BIN
PhysicsPlatformer/Resources/Sprites/GoldIcon/4.png


BIN
PhysicsPlatformer/Resources/Sprites/GoldIcon/5.png