Josh Engebretson 11 years ago
parent
commit
b49f77a7ef

+ 40 - 36
PhysicsPlatformer/Resources/Components/Avatar.js

@@ -7,7 +7,7 @@ var node = self.node;
 
 // Resources
 var animationSet = cache.getResource("AnimationSet2D", "Sprites/Hero/Hero.scml");
-var jumpSound = cache.getResource("Sound","Sounds/Jump13.ogg");
+var jumpSound = cache.getResource("Sound", "Sounds/Jump13.ogg");
 
 var sprite = node.createComponent("AnimatedSprite2D");
 sprite.setAnimation(animationSet, "Idle");
@@ -64,54 +64,58 @@ function start() {
     self.listenToEvent(null, "PhysicsEndContact2D", self.onPhysicsEndContact2D);
 }
 
-function handleAnimation() {
+
+var lastAnimDelta = 0;
+
+function setAnimation(animName) {
+
+    if (anim == animName || lastAnimDelta > 0)
+        return;
+
+    lastAnimDelta = .25;
+
+    sprite.setAnimation(animationSet, animName);
+    anim = animName;
+
+}
+
+function handleAnimation(timeStep) {
+
+    lastAnimDelta -= timeStep;
 
     var vel = body.linearVelocity;
-    
-    if (contactCount ) {
+
+    if (contactCount) {
 
         if (vel[0] < -0.75) {
             if (!flipped) {
                 sprite.flipX = true;
                 flipped = true;
-    
-            }
-            if (anim != "Run") {
-                sprite.setAnimation(animationSet, "Run");
-                anim = "Run";
+
             }
+            setAnimation("Run");
         } else if (vel[0] > 0.75) {
-    
+
             if (flipped) {
                 sprite.flipX = false;
                 flipped = false;
-    
-            }
-            if (anim != "Run") {
-                sprite.setAnimation(animationSet, "Run");
-                anim = "Run";
+
             }
+            setAnimation("Run");
         } else {
-            if (anim != "Idle") {
-                sprite.setAnimation(animationSet, "Idle");
-                anim = "Idle";
-            }
+
+            setAnimation("Idle");
         }
     } else {
-        
-        if ( vel[1] > 0 && anim != "Jump")
-            {
-                sprite.setAnimation(animationSet, "Jump");
-                anim = "Jump";
-            }
-        else if ( vel[1] < 0 && anim != "Land")
-            {
-                sprite.setAnimation(animationSet, "Land");
-                anim = "Land";
-            }
-        
+
+        if (vel[1] > 0) {
+            setAnimation("Jump");
+        } else if (vel[1] < 0) {
+            setAnimation("Land");
+        }
+
     }
-    
+
 
 
 }
@@ -151,7 +155,7 @@ function handleInput(timeStep) {
 
     if (zoomIn)
         camera.zoom += timeStep;
-    
+
     if (zoomOut)
         camera.zoom -= timeStep;
 
@@ -179,8 +183,8 @@ function handleInput(timeStep) {
         circle.friction = 0.0;
 
     if (jump && contactCount) {
-            //soundSource.gain = 0.75;
-        self.soundSource.play(jumpSound );
+        //soundSource.gain = 0.75;
+        self.soundSource.play(jumpSound);
 
         vel[1] = 0;
         body.linearVelocity = vel;
@@ -198,7 +202,7 @@ function postUpdate() {
 function update(timeStep) {
 
     handleInput(timeStep);
-    handleAnimation();
+    handleAnimation(timeStep);
 
     if (node.position[1] < 14) {
         //TODO: FIX, I have to set scale to 0 and the back to 1 to force 

+ 28 - 4
PhysicsPlatformer/Resources/Components/Level.js

@@ -1,3 +1,10 @@
+/*
+ * The Level
+ */
+
+var glmatrix = require("gl-matrix");
+var vec2 = glmatrix.vec2;
+
 TheLevel = self;
 
 var node = self.node;
@@ -20,16 +27,18 @@ self.batNodes = [];
 // vec2
 self.batWaypoints = [];
 
-
 // parsed coins
 var coins = [];
 var bats = [];
 
+var beginContactCallbacks = {};
+
+
 self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
 
-    //if (nodeA == ThePlayer.node && self.coinNodes.indexOf(nodeB) != -1) {
-    //    nodeB.coin.onPlayerHit();
-   // }
+    if (beginContactCallbacks.hasOwnProperty(nodeA)) {
+        beginContactCallbacks[nodeA](world, bodyA, bodyB, nodeA, nodeB);
+    }
 
 }
 
@@ -166,6 +175,21 @@ function parsePhysics() {
                         shape.friction = 1.0;
                         shape.restitution = .1;
 
+                        if (o.type == "Crate") {
+                            /*
+                            var soundSource = onode.createComponent("SoundSource");
+                            soundSource.soundType = Atomic.SOUND_EFFECT;
+                            soundSource.gain = .25;
+                            var crateSound = cache.getResource("Sound", "Sounds/CrateHit.ogg");
+                            beginContactCallbacks[onode] = function(world, bodyA, bodyB, nodeA, nodeB) {
+
+                                var vel = obody.linearVelocity;
+                                if (vec2.length(vel) > 2) {
+                                    soundSource.play(crateSound);
+                                }
+                            }
+                            */
+                        }
                     }
                 }
             }

+ 37 - 37
PhysicsPlatformer/Resources/Levels/Level1.tmx

@@ -2070,43 +2070,43 @@
   </data>
  </layer>
  <objectgroup name="Physics">
-  <object gid="32" x="2090.16" y="1795.08"/>
-  <object gid="32" x="2090.56" y="1731.47"/>
-  <object gid="32" x="2090.96" y="1604.67"/>
-  <object gid="32" x="2090.56" y="1668.27"/>
-  <object gid="32" x="2154.16" y="1731.08"/>
-  <object gid="32" x="2153.76" y="1794.68"/>
-  <object gid="32" x="2216.97" y="1794.68"/>
-  <object gid="32" x="2155.74" y="1668.27"/>
-  <object gid="32" x="2216.97" y="1731.47"/>
-  <object gid="32" x="2278.2" y="1794.68"/>
-  <object gid="32" x="2686.65" y="1508.68"/>
-  <object gid="32" x="2747.88" y="1571.89"/>
-  <object gid="32" x="2686.65" y="1571.89"/>
-  <object gid="32" x="2560.24" y="1508.68"/>
-  <object gid="31" x="2729" y="1570.46"/>
-  <object gid="32" x="2625.42" y="1445.48"/>
-  <object gid="32" x="2623.84" y="1508.29"/>
-  <object gid="32" x="2560.24" y="1445.48"/>
-  <object gid="32" x="2559.84" y="1572.29"/>
-  <object gid="32" x="2623.44" y="1571.89"/>
-  <object gid="30" x="2665.01" y="1570.62"/>
-  <object gid="32" x="2560.64" y="1381.88"/>
-  <object gid="32" x="1557.98" y="1496.53"/>
-  <object gid="32" x="1494.55" y="1496.53"/>
-  <object gid="32" x="1494.55" y="1431.12"/>
-  <object gid="32" x="977.201" y="1668.98"/>
-  <object gid="32" x="1008.92" y="1732.41"/>
-  <object gid="32" x="945.491" y="1732.41"/>
-  <object gid="32" x="1557.98" y="1431.12"/>
-  <object gid="32" x="1495.04" y="1304.1"/>
-  <object gid="32" x="1558.47" y="1369.51"/>
-  <object gid="32" x="1558.47" y="1304.1"/>
-  <object gid="32" x="1495.04" y="1369.51"/>
-  <object gid="32" x="1494.1" y="1177.63"/>
-  <object gid="32" x="1557.53" y="1243.04"/>
-  <object gid="32" x="1557.53" y="1177.63"/>
-  <object gid="32" x="1494.1" y="1243.04"/>
+  <object name="Crate" type="Crate" gid="32" x="2090.16" y="1795.08"/>
+  <object name="Crate" type="Crate" gid="32" x="2090.56" y="1731.47"/>
+  <object name="Crate" type="Crate" gid="32" x="2090.96" y="1604.67"/>
+  <object name="Crate" type="Crate" gid="32" x="2090.56" y="1668.27"/>
+  <object name="Crate" type="Crate" gid="32" x="2154.16" y="1731.08"/>
+  <object name="Crate" type="Crate" gid="32" x="2153.76" y="1794.68"/>
+  <object name="Crate" type="Crate" gid="32" x="2216.97" y="1794.68"/>
+  <object name="Crate" type="Crate" gid="32" x="2155.74" y="1668.27"/>
+  <object name="Crate" type="Crate" gid="32" x="2216.97" y="1731.47"/>
+  <object name="Crate" type="Crate" gid="32" x="2278.2" y="1794.68"/>
+  <object name="Crate" type="Crate" gid="32" x="2686.65" y="1508.68"/>
+  <object name="Crate" type="Crate" gid="32" x="2747.88" y="1571.89"/>
+  <object name="Crate" type="Crate" gid="32" x="2686.65" y="1571.89"/>
+  <object name="Crate" type="Crate" gid="32" x="2560.24" y="1508.68"/>
+  <object name="Crate" type="Crate" gid="31" x="2729" y="1570.46"/>
+  <object name="Crate" type="Crate" gid="32" x="2625.42" y="1445.48"/>
+  <object name="Crate" type="Crate" gid="32" x="2623.84" y="1508.29"/>
+  <object name="Crate" type="Crate" gid="32" x="2560.24" y="1445.48"/>
+  <object name="Crate" type="Crate" gid="32" x="2559.84" y="1572.29"/>
+  <object name="Crate" type="Crate" gid="32" x="2623.44" y="1571.89"/>
+  <object name="Crate" type="Crate" gid="30" x="2665.01" y="1570.62"/>
+  <object name="Crate" type="Crate" gid="32" x="2560.64" y="1381.88"/>
+  <object name="Crate" type="Crate" gid="32" x="1557.98" y="1496.53"/>
+  <object name="Crate" type="Crate" gid="32" x="1494.55" y="1496.53"/>
+  <object name="Crate" type="Crate" gid="32" x="1494.55" y="1431.12"/>
+  <object name="Crate" type="Crate" gid="32" x="977.201" y="1668.98"/>
+  <object name="Crate" type="Crate" gid="32" x="1008.92" y="1732.41"/>
+  <object name="Crate" type="Crate" gid="32" x="945.491" y="1732.41"/>
+  <object name="Crate" type="Crate" gid="32" x="1557.98" y="1431.12"/>
+  <object name="Crate" type="Crate" gid="32" x="1495.04" y="1304.1"/>
+  <object name="Crate" type="Crate" gid="32" x="1558.47" y="1369.51"/>
+  <object name="Crate" type="Crate" gid="32" x="1558.47" y="1304.1"/>
+  <object name="Crate" type="Crate" gid="32" x="1495.04" y="1369.51"/>
+  <object name="Crate" type="Crate" gid="32" x="1494.1" y="1177.63"/>
+  <object name="Crate" type="Crate" gid="32" x="1557.53" y="1243.04"/>
+  <object name="Crate" type="Crate" gid="32" x="1557.53" y="1177.63"/>
+  <object name="Crate" type="Crate" gid="32" x="1494.1" y="1243.04"/>
  </objectgroup>
  <objectgroup name="Entities">
   <object name="PlayerSpawn" type="PlayerSpawn" x="377.192" y="1151.12" width="89.9005" height="118.592"/>

BIN
PhysicsPlatformer/Resources/Sounds/CrateHit.ogg