Level.js 3.7 KB

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