Level.js 3.0 KB

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