Level.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var node = self.node;
  2. var tmxFile = cache.getResource("TmxFile2D", "Levels/Level1.tmx");
  3. var tileMapNode = scene.createChild("TileMap");
  4. tileMapNode.setPosition([0.0, 0.0, 0.0]);
  5. var tileMap = tileMapNode.createComponent("TileMap2D");
  6. tileMap.setTmxFile(tmxFile);
  7. PlayerSpawnPoint = [0, 0];
  8. var platforms = {};
  9. camera.setZoom(.75);
  10. // create a platform based on start and stop TileMapObject2D
  11. function createPlatform(start, stop) {
  12. var platformNode = scene.createChild("Platform");
  13. platform = platformNode .createComponent("JSComponent");
  14. // setting the classname calls start, we need a way to provide
  15. // component values before this, as anything is the component function
  16. // will override the values set before className is set
  17. // for now, this works
  18. platform.startPos = start.position;
  19. platform.stopPos = stop.position;
  20. platform.className = "Platform";
  21. }
  22. function parseEntities() {
  23. entityLayer = tileMap.getLayerByName("Entities");
  24. if (entityLayer) {
  25. for (var i = 0; i < entityLayer.numObjects; i++) {
  26. var o = entityLayer.getObject(i);
  27. var onode = entityLayer.getObjectNode(i);
  28. if (o.type == "PlayerSpawn")
  29. PlayerSpawnPoint = onode.position2D;
  30. else if (o.type == "PlatformStart") {
  31. var pnum = Number(o.getProperty("Platform"));
  32. if (!platforms.hasOwnProperty(pnum))
  33. platforms[pnum] = [null, null];
  34. platforms[pnum][0] = o;
  35. } else if (o.type == "PlatformStop") {
  36. var pnum = Number(o.getProperty("Platform"));
  37. if (!platforms.hasOwnProperty(pnum))
  38. platforms[pnum] = [null, null];
  39. platforms[pnum][1] = o;
  40. }
  41. }
  42. }
  43. for (var pnum in platforms) {
  44. createPlatform(platforms[pnum][0], platforms[pnum][1]);
  45. }
  46. }
  47. function parsePhysics() {
  48. physicsLayer = tileMap.getLayerByName("Physics");
  49. if (physicsLayer) {
  50. for (var i = 0; i < physicsLayer.numObjects; i++) {
  51. var o = physicsLayer.getObject(i);
  52. var onode = physicsLayer.getObjectNode(i);
  53. var group = tmxFile.getTileObjectGroup(o.tileGid);
  54. var obody = null;
  55. if (group) {
  56. for (var j = 0; j < group.numObjects; j++) {
  57. var go = group.getObject(j);
  58. if (go.validCollisionShape()) {
  59. if (!obody) {
  60. obody = onode.createComponent("RigidBody2D");
  61. obody.bodyType = Atomic.BT_DYNAMIC;
  62. obody.awake = false;
  63. }
  64. var shape = go.createCollisionShape(onode);
  65. shape.density = 1.0;
  66. shape.friction = 1.0;
  67. shape.restitution = .1;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. parsePhysics();
  75. parseEntities();