Level.js 4.1 KB

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