Level.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. TheLevel = self;
  2. var node = self.node;
  3. var tmxFile = cache.getResource("TmxFile2D", "Levels/Level1.tmx");
  4. var tileMapNode = scene.createChild("TileMap");
  5. tileMapNode.setPosition([0.0, 0.0, 0.0]);
  6. var tileMap = tileMapNode.createComponent("TileMap2D");
  7. tileMap.setTmxFile(tmxFile);
  8. PlayerSpawnPoint = [0, 0];
  9. var platforms = {};
  10. self.coinNodes = [];
  11. self.batNodes = [];
  12. // vec2
  13. self.batWaypoints = [];
  14. // parsed coins
  15. var coins = [];
  16. var bats = [];
  17. camera.setZoom(.9);
  18. self.onPhysicsBeginContact2D = function(world, bodyA, bodyB, nodeA, nodeB) {
  19. if (nodeA == ThePlayer.node && self.coinNodes.indexOf(nodeB) != -1) {
  20. nodeB.coin.onPlayerHit();
  21. }
  22. }
  23. // create a platform based on start and stop TileMapObject2D
  24. function createPlatform(start, stop) {
  25. var platformNode = scene.createChild("Platform");
  26. platform = platformNode.createComponent("JSComponent");
  27. // setting the classname calls start, we need a way to provide
  28. // component values before this, as anything is the component function
  29. // will override the values set before className is set
  30. // for now, this works
  31. platform.startPos = start.position;
  32. platform.stopPos = stop.position;
  33. platform.className = "Platform";
  34. }
  35. function createCoin(obj) {
  36. var coinNode = scene.createChild("Coin");
  37. coinNode.position2D = obj.position;
  38. coinNode.createJSComponent("Coin");
  39. self.coinNodes.push(coinNode);
  40. }
  41. function createBat(obj) {
  42. var batNode = scene.createChild("Bat");
  43. batNode.position2D = obj.position;
  44. batNode.createJSComponent("Bat");
  45. self.batNodes.push(batNode);
  46. }
  47. function parseEntities() {
  48. entityLayer = tileMap.getLayerByName("Entities");
  49. if (entityLayer) {
  50. for (var i = 0; i < entityLayer.numObjects; i++) {
  51. var o = entityLayer.getObject(i);
  52. var onode = entityLayer.getObjectNode(i);
  53. if (o.type == "PlayerSpawn")
  54. PlayerSpawnPoint = onode.position2D;
  55. else if (o.type == "PlatformStart") {
  56. var pnum = Number(o.getProperty("Platform"));
  57. if (!platforms.hasOwnProperty(pnum))
  58. platforms[pnum] = [null, null];
  59. platforms[pnum][0] = o;
  60. } else if (o.type == "PlatformStop") {
  61. var pnum = Number(o.getProperty("Platform"));
  62. if (!platforms.hasOwnProperty(pnum))
  63. platforms[pnum] = [null, null];
  64. platforms[pnum][1] = o;
  65. } else if (o.type == "Coin") {
  66. coins.push(o);
  67. } else if (o.type == "Bat") {
  68. bats.push(o);
  69. } else if (o.type == "BatWaypoint") {
  70. self.batWaypoints.push(o.position);
  71. }
  72. }
  73. }
  74. for (var pnum in platforms) {
  75. createPlatform(platforms[pnum][0], platforms[pnum][1]);
  76. }
  77. for (var i in coins) {
  78. createCoin(coins[i]);
  79. }
  80. for (var i in bats) {
  81. createBat(bats[i]);
  82. }
  83. }
  84. function parsePhysics() {
  85. physicsLayer = tileMap.getLayerByName("Physics");
  86. if (physicsLayer) {
  87. for (var i = 0; i < physicsLayer.numObjects; i++) {
  88. var o = physicsLayer.getObject(i);
  89. var onode = physicsLayer.getObjectNode(i);
  90. var group = tmxFile.getTileObjectGroup(o.tileGid);
  91. var obody = null;
  92. if (group) {
  93. for (var j = 0; j < group.numObjects; j++) {
  94. var go = group.getObject(j);
  95. if (go.validCollisionShape()) {
  96. if (!obody) {
  97. obody = onode.createComponent("RigidBody2D");
  98. obody.bodyType = Atomic.BT_DYNAMIC;
  99. obody.awake = false;
  100. }
  101. var shape = go.createCollisionShape(onode);
  102. shape.density = 1.0;
  103. shape.friction = 1.0;
  104. shape.restitution = .1;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. function start() {
  112. self.listenToEvent(null, "PhysicsBeginContact2D", self.onPhysicsBeginContact2D);
  113. parsePhysics();
  114. parseEntities();
  115. }