LevelParser.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Atomic Script
  2. LevelParser = function(tileMap) {
  3. this.tileMap = tileMap;
  4. this.entities = [];
  5. this.parseEntities();
  6. }
  7. LevelParser.prototype = {
  8. parseEntities: function() {
  9. entityLayer = this.tileMap.getLayerByName("Entities");
  10. var platforms = {};
  11. if (entityLayer) {
  12. for (var i = 0; i < entityLayer.numObjects; i++) {
  13. var o = entityLayer.getObject(i);
  14. var onode = entityLayer.getObjectNode(i);
  15. var entity = {
  16. type: null
  17. };
  18. if (o.type == "PlayerSpawn") {
  19. entity.type = "PlayerSpawn";
  20. entity.position = onode.position2D;
  21. } else if (o.type == "Vine") {
  22. entity.type = "Vine";
  23. entity.position = onode.position2D;
  24. } else if (o.type == "Coin") {
  25. entity.type = "Coin";
  26. entity.position = onode.position2D;
  27. } else if (o.type == "Bat") {
  28. entity.type = "Bat";
  29. entity.position = onode.position2D;
  30. } else if (o.type == "BatWaypoint") {
  31. entity.type = "BatWaypoint";
  32. entity.position = onode.position2D;
  33. } else if (o.type == "PlatformStart") {
  34. var pnum = Number(o.getProperty("Platform"));
  35. if (!platforms.hasOwnProperty(pnum))
  36. platforms[pnum] = [null, null];
  37. platforms[pnum][0] = o;
  38. } else if (o.type == "PlatformStop") {
  39. var pnum = Number(o.getProperty("Platform"));
  40. if (!platforms.hasOwnProperty(pnum))
  41. platforms[pnum] = [null, null];
  42. platforms[pnum][1] = o;
  43. }
  44. if (entity.type)
  45. this.entities.push(entity);
  46. }
  47. for (var pnum in platforms) {
  48. var entity = {
  49. type: "MovingPlatform",
  50. start: platforms[pnum][0].position,
  51. stop: platforms[pnum][1].position
  52. }
  53. this.entities.push(entity);
  54. }
  55. }
  56. },
  57. getEntity: function(type) {
  58. for (var i = 0; i < this.entities.length; i++)
  59. if (this.entities[i].type == type)
  60. return this.entities[i];
  61. return null;
  62. },
  63. getEntities: function(type) {
  64. var entities = [];
  65. for (var i = 0; i < this.entities.length; i++)
  66. if (this.entities[i].type == type)
  67. entities.push(this.entities[i]);
  68. return entities;
  69. },
  70. getSpawnpoint: function(type) {
  71. var pos = [0, 0];
  72. var entity = this.getEntity("PlayerSpawn");
  73. return entity ? entity.position : pos;
  74. },
  75. createPhysics: function(tileMap, tmxFile) {
  76. physicsLayer = tileMap.getLayerByName("Physics");
  77. if (physicsLayer) {
  78. for (var i = 0; i < physicsLayer.numObjects; i++) {
  79. var o = physicsLayer.getObject(i);
  80. var onode = physicsLayer.getObjectNode(i);
  81. var group = tmxFile.getTileObjectGroup(o.tileGid);
  82. var obody = null;
  83. if (group) {
  84. for (var j = 0; j < group.numObjects; j++) {
  85. var go = group.getObject(j);
  86. if (go.validCollisionShape()) {
  87. if (!obody) {
  88. obody = onode.createComponent("RigidBody2D");
  89. obody.bodyType = Atomic.BT_DYNAMIC;
  90. obody.awake = false;
  91. }
  92. var shape = go.createCollisionShape(onode);
  93. shape.density = 1.0;
  94. shape.friction = 1.0;
  95. shape.restitution = .1;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. };