Level.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * The Level
  3. */
  4. var glmatrix = require("gl-matrix");
  5. var vec2 = glmatrix.vec2;
  6. TheLevel = self;
  7. var node = self.node;
  8. var tmxFile = cache.getResource("TmxFile2D", "Levels/Level1.tmx");
  9. var tileMapNode = scene.createChild("TileMap");
  10. tileMapNode.setPosition([0.0, 0.0, 0.0]);
  11. var tileMap = tileMapNode.createComponent("TileMap2D");
  12. tileMap.setTmxFile(tmxFile);
  13. PlayerSpawnPoint = [0, 0];
  14. var platforms = {};
  15. self.coinNodes = [];
  16. self.batNodes = [];
  17. // vec2
  18. self.batWaypoints = [];
  19. // parsed coins
  20. var coins = [];
  21. var bats = [];
  22. var vines = [];
  23. var beginContactCallbacks = {};
  24. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  25. if (beginContactCallbacks.hasOwnProperty(nodeA)) {
  26. beginContactCallbacks[nodeA](world, bodyA, bodyB, nodeA, nodeB);
  27. }
  28. }
  29. // create a platform based on start and stop TileMapObject2D
  30. function createPlatform(start, stop) {
  31. var platformNode = scene.createChild("Platform");
  32. platform = platformNode.createComponent("JSComponent");
  33. // setting the classname calls start, we need a way to provide
  34. // component values before this, as anything is the component function
  35. // will override the values set before className is set
  36. // for now, this works
  37. platform.startPos = start.position;
  38. platform.stopPos = stop.position;
  39. platform.className = "Platform";
  40. }
  41. function createCoin(obj) {
  42. var coinNode = scene.createChild("Coin");
  43. coinNode.position2D = obj.position;
  44. coinNode.createJSComponent("Coin");
  45. self.coinNodes.push(coinNode);
  46. }
  47. function createBat(obj) {
  48. var batNode = scene.createChild("Bat");
  49. batNode.position2D = obj.position;
  50. batNode.createJSComponent("Bat");
  51. self.batNodes.push(batNode);
  52. }
  53. function createVine(obj) {
  54. var vineNode = scene.createChild("Vine");
  55. vineNode.position2D = obj.position;
  56. vineNode.createJSComponent("Vine");
  57. }
  58. function parseEntities() {
  59. entityLayer = tileMap.getLayerByName("Entities");
  60. if (entityLayer) {
  61. for (var i = 0; i < entityLayer.numObjects; i++) {
  62. var o = entityLayer.getObject(i);
  63. var onode = entityLayer.getObjectNode(i);
  64. if (o.type == "PlayerSpawn")
  65. PlayerSpawnPoint = onode.position2D;
  66. else if (o.type == "PlatformStart") {
  67. var pnum = Number(o.getProperty("Platform"));
  68. if (!platforms.hasOwnProperty(pnum))
  69. platforms[pnum] = [null, null];
  70. platforms[pnum][0] = o;
  71. } else if (o.type == "PlatformStop") {
  72. var pnum = Number(o.getProperty("Platform"));
  73. if (!platforms.hasOwnProperty(pnum))
  74. platforms[pnum] = [null, null];
  75. platforms[pnum][1] = o;
  76. } else if (o.type == "Coin") {
  77. coins.push(o);
  78. } else if (o.type == "Bat") {
  79. bats.push(o);
  80. } else if (o.type == "BatWaypoint") {
  81. self.batWaypoints.push(o.position);
  82. } else if (o.type == "Vine") {
  83. vines.push(o);
  84. }
  85. }
  86. }
  87. for (var pnum in platforms) {
  88. createPlatform(platforms[pnum][0], platforms[pnum][1]);
  89. }
  90. for (var i in coins) {
  91. createCoin(coins[i]);
  92. }
  93. for (var i in bats) {
  94. createBat(bats[i]);
  95. }
  96. for (var i in vines) {
  97. createVine(vines[i]);
  98. }
  99. }
  100. function parsePhysics() {
  101. physicsLayer = tileMap.getLayerByName("Physics");
  102. if (physicsLayer) {
  103. for (var i = 0; i < physicsLayer.numObjects; i++) {
  104. var o = physicsLayer.getObject(i);
  105. var onode = physicsLayer.getObjectNode(i);
  106. var group = tmxFile.getTileObjectGroup(o.tileGid);
  107. var obody = null;
  108. if (group) {
  109. for (var j = 0; j < group.numObjects; j++) {
  110. var go = group.getObject(j);
  111. if (go.validCollisionShape()) {
  112. if (!obody) {
  113. obody = onode.createComponent("RigidBody2D");
  114. obody.bodyType = Atomic.BT_DYNAMIC;
  115. obody.awake = false;
  116. }
  117. var shape = go.createCollisionShape(onode);
  118. shape.density = 1.0;
  119. shape.friction = 1.0;
  120. shape.restitution = .1;
  121. if (o.type == "Crate") {
  122. /*
  123. var soundSource = onode.createComponent("SoundSource");
  124. soundSource.soundType = Atomic.SOUND_EFFECT;
  125. soundSource.gain = .25;
  126. var crateSound = cache.getResource("Sound", "Sounds/CrateHit.ogg");
  127. beginContactCallbacks[onode] = function(world, bodyA, bodyB, nodeA, nodeB) {
  128. var vel = obody.linearVelocity;
  129. if (vec2.length(vel) > 2) {
  130. soundSource.play(crateSound);
  131. }
  132. }
  133. */
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. function start() {
  142. self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
  143. parsePhysics();
  144. parseEntities();
  145. }