Josh Engebretson 11 years ago
parent
commit
8343682018

+ 45 - 0
PhysicsPlatformer/Resources/Components/Bat.js

@@ -0,0 +1,45 @@
+// 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/Bat/Bat.scml");
+var sprite = node.createComponent("AnimatedSprite2D");
+sprite.setAnimation(animationSet, "Fly");
+sprite.setLayer(100);
+node.scale2D = [.5, .5];
+
+var cwaypoint = -1;
+
+function update(timestep) {
+
+    var waypoints = TheLevel.batWaypoints;
+    var pos = node.position2D;
+    
+    if (cwaypoint == -1 || vec2.distance(pos, waypoints[cwaypoint]) < .5) {
+        cwaypoint = Math.round(Math.random() * (waypoints.length - 1));
+        return;
+    }
+
+
+    var dir = vec2.create();
+    var goal = waypoints[cwaypoint];
+
+    vec2.subtract(dir, goal, pos);
+    vec2.normalize(dir, dir);
+    vec2.scale(dir, dir, timestep * 2);
+
+    if (dir[0] < 0)
+        sprite.flipX = true;
+    else
+        sprite.flipX = false;
+
+    vec2.add(pos, pos, dir);
+    node.position2D = pos;
+
+
+}

+ 31 - 2
PhysicsPlatformer/Resources/Components/Level.js

@@ -15,16 +15,22 @@ PlayerSpawnPoint = [0, 0];
 var platforms = {};
 var platforms = {};
 
 
 self.coinNodes = [];
 self.coinNodes = [];
+self.batNodes = [];
+
+// vec2
+self.batWaypoints = [];
+
 
 
 // parsed coins
 // parsed coins
 var coins = [];
 var coins = [];
+var bats = [];
 
 
 camera.setZoom(.9);
 camera.setZoom(.9);
 
 
 
 
 self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
 self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
 
 
-    if (nodeA == ThePlayer.node && self.coinNodes.indexOf(nodeB) != -1) {    
+    if (nodeA == ThePlayer.node && self.coinNodes.indexOf(nodeB) != -1) {
         nodeB.coin.onPlayerHit();
         nodeB.coin.onPlayerHit();
     }
     }
 
 
@@ -52,10 +58,19 @@ function createCoin(obj) {
 
 
     var coinNode = scene.createChild("Coin");
     var coinNode = scene.createChild("Coin");
     coinNode.position2D = obj.position;
     coinNode.position2D = obj.position;
-    coin = coinNode.createJSComponent("Coin");
+    coinNode.createJSComponent("Coin");
     self.coinNodes.push(coinNode);
     self.coinNodes.push(coinNode);
 }
 }
 
 
+function createBat(obj) {
+
+    var batNode = scene.createChild("Bat");
+    batNode.position2D = obj.position;
+    batNode.createJSComponent("Bat");
+    self.batNodes.push(batNode);
+}
+
+
 function parseEntities() {
 function parseEntities() {
 
 
     entityLayer = tileMap.getLayerByName("Entities");
     entityLayer = tileMap.getLayerByName("Entities");
@@ -87,6 +102,14 @@ function parseEntities() {
 
 
                 coins.push(o);
                 coins.push(o);
 
 
+            } else if (o.type == "Bat") {
+
+                bats.push(o);
+
+            } else if (o.type == "BatWaypoint") {
+
+                self.batWaypoints.push(o.position);
+
             }
             }
 
 
         }
         }
@@ -104,6 +127,12 @@ function parseEntities() {
 
 
     }
     }
 
 
+    for (var i in bats) {
+
+        createBat(bats[i]);
+
+    }
+
 }
 }
 
 
 function parsePhysics() {
 function parsePhysics() {

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

@@ -2185,5 +2185,30 @@
   <object name="Coin" type="Coin" x="2604.99" y="1036.46" width="65.1303" height="70.1403">
   <object name="Coin" type="Coin" x="2604.99" y="1036.46" width="65.1303" height="70.1403">
    <ellipse/>
    <ellipse/>
   </object>
   </object>
+  <object name="Coin" type="Coin" x="768.331" y="1057.97" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="1004.44" y="1121.68" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="847.901" y="1004.99" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="927.601" y="1066.18" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Coin" type="Coin" x="690.651" y="1115.87" width="65.1303" height="70.1403">
+   <ellipse/>
+  </object>
+  <object name="Bat" type="Bat" x="1106.75" y="719.961" width="36.63" height="39.4477"/>
+  <object name="Bat" type="Bat" x="2102.74" y="1335.07" width="36.63" height="39.4477"/>
+  <object name="Bat" type="Bat" x="2704.69" y="718.036" width="36.63" height="39.4477"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="1770.62" y="1462.11" width="85.5131" height="65.3924"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="423.374" y="708.417" width="85.5131" height="65.3924"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="3139.67" y="1459.59" width="85.5131" height="65.3924"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="1387.49" y="782.193" width="85.5131" height="65.3924"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="768.779" y="1417.67" width="85.5131" height="65.3924"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="2021.29" y="587.693" width="85.5131" height="65.3924"/>
+  <object name="BatWaypoint" type="BatWaypoint" x="2297.95" y="1050.47" width="85.5131" height="65.3924"/>
  </objectgroup>
  </objectgroup>
 </map>
 </map>

+ 69 - 0
PhysicsPlatformer/Resources/Sprites/Bat/Bat.scml

@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<spriter_data scml_version="1.0" generator="BrashMonkey Spriter" generator_version="r2">
+    <folder id="0">
+        <file id="0" name="bat-frame-8.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="1" name="bat-frame-1.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="2" name="bat-frame-2.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="3" name="bat-frame-3.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="4" name="bat-frame-4.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="5" name="bat-frame-5.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="6" name="bat-frame-6.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+        <file id="7" name="bat-frame-7.png" width="128" height="128" pivot_x="0" pivot_y="1"/>
+    </folder>
+    <entity id="0" name="Bat">
+        <animation id="0" name="Fly" length="500" interval="100">
+            <mainline>
+                <key id="0">
+                    <object_ref id="0" timeline="0" key="0" z_index="0"/>
+                </key>
+                <key id="1" time="62">
+                    <object_ref id="0" timeline="0" key="1" z_index="0"/>
+                </key>
+                <key id="2" time="125">
+                    <object_ref id="0" timeline="0" key="2" z_index="0"/>
+                </key>
+                <key id="3" time="187">
+                    <object_ref id="0" timeline="0" key="3" z_index="0"/>
+                </key>
+                <key id="4" time="250">
+                    <object_ref id="0" timeline="0" key="4" z_index="0"/>
+                </key>
+                <key id="5" time="312">
+                    <object_ref id="0" timeline="0" key="5" z_index="0"/>
+                </key>
+                <key id="6" time="375">
+                    <object_ref id="0" timeline="0" key="6" z_index="0"/>
+                </key>
+                <key id="7" time="437">
+                    <object_ref id="0" timeline="0" key="7" z_index="0"/>
+                </key>
+            </mainline>
+            <timeline id="0" name="Fly">
+                <key id="0" spin="0">
+                    <object folder="0" file="1" x="-68" y="56"/>
+                </key>
+                <key id="1" time="62" spin="0">
+                    <object folder="0" file="2" x="-68" y="56"/>
+                </key>
+                <key id="2" time="125" spin="0">
+                    <object folder="0" file="3" x="-68" y="56"/>
+                </key>
+                <key id="3" time="187" spin="0">
+                    <object folder="0" file="4" x="-68" y="56"/>
+                </key>
+                <key id="4" time="250" spin="0">
+                    <object folder="0" file="5" x="-68" y="56"/>
+                </key>
+                <key id="5" time="312" spin="0">
+                    <object folder="0" file="6" x="-68" y="56"/>
+                </key>
+                <key id="6" time="375" spin="0">
+                    <object folder="0" file="7" x="-68" y="56"/>
+                </key>
+                <key id="7" time="437" spin="0">
+                    <object folder="0" file="0" x="-68" y="56"/>
+                </key>
+            </timeline>
+        </animation>
+    </entity>
+</spriter_data>

BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-1.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-2.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-3.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-4.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-5.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-6.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-7.png


BIN
PhysicsPlatformer/Resources/Sprites/Bat/bat-frame-8.png