Level.js 5.1 KB

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