LevelParser.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Atomic Script
  2. //It's just a module, so it don't need atomic component string
  3. //Define a constructor
  4. var LevelParser = function(tileMap) {
  5. this.tileMap = tileMap;
  6. this.entities = [];
  7. this.parseEntities();
  8. };
  9. //Define a prototype
  10. LevelParser.prototype = {
  11. //parsing our entities from tileMap object
  12. parseEntities: function() {
  13. var entityLayer = this.tileMap.getLayerByName("Entities");
  14. var platforms = {};
  15. //if layer Entities exists
  16. if (entityLayer) {
  17. //iterating through each object
  18. for (var i = 0; i < entityLayer.numObjects; i++) {
  19. //get object itself
  20. var o = entityLayer.getObject(i);
  21. //get node of object
  22. var onode = entityLayer.getObjectNode(i);
  23. var entity = {
  24. type: null
  25. };
  26. //checks for type of object
  27. if (o.type == "PlayerSpawn") {
  28. entity.type = "PlayerSpawn";
  29. entity.position = onode.position2D;
  30. } else if (o.type == "Vine") {
  31. entity.type = "Vine";
  32. entity.position = onode.position2D;
  33. } else if (o.type == "Coin") {
  34. entity.type = "Coin";
  35. entity.position = onode.position2D;
  36. } else if (o.type == "Bat") {
  37. entity.type = "Bat";
  38. entity.position = onode.position2D;
  39. } else if (o.type == "BatWaypoint") {
  40. entity.type = "BatWaypoint";
  41. entity.position = onode.position2D;
  42. } else if (o.type == "PlatformStart") {
  43. var pnum = Number(o.getProperty("Platform"));
  44. if (!platforms.hasOwnProperty(pnum))
  45. platforms[pnum] = [null, null];
  46. platforms[pnum][0] = o;
  47. } else if (o.type == "PlatformStop") {
  48. var pnum = Number(o.getProperty("Platform"));
  49. if (!platforms.hasOwnProperty(pnum))
  50. platforms[pnum] = [null, null];
  51. platforms[pnum][1] = o;
  52. }
  53. //if type was found, then push an object to the entities array
  54. if (entity.type)
  55. this.entities.push(entity);
  56. }
  57. for (var pnum in platforms) {
  58. var entity = {
  59. type: "MovingPlatform",
  60. start: platforms[pnum][0].position,
  61. stop: platforms[pnum][1].position
  62. };
  63. this.entities.push(entity);
  64. }
  65. }
  66. },
  67. //returns the first found entity of the given type
  68. getEntity: function(type) {
  69. for (var i = 0; i < this.entities.length; i++)
  70. if (this.entities[i].type == type)
  71. return this.entities[i];
  72. return null;
  73. },
  74. //returns an array of entities of the given type
  75. getEntities: function(type) {
  76. var entities = [];
  77. for (var i = 0; i < this.entities.length; i++)
  78. if (this.entities[i].type == type)
  79. entities.push(this.entities[i]);
  80. return entities;
  81. },
  82. //returns spawnpoint
  83. getSpawnpoint: function(type) {
  84. var pos = [0, 0];
  85. var entity = this.getEntity("PlayerSpawn");
  86. return entity ? entity.position : pos;
  87. },
  88. createPhysics: function(tileMap, tmxFile) {
  89. //get Physics layer
  90. var physicsLayer = tileMap.getLayerByName("Physics");
  91. //if layer exists
  92. if (physicsLayer) {
  93. //iterate through each object
  94. for (var i = 0; i < physicsLayer.numObjects; i++) {
  95. //get object
  96. var o = physicsLayer.getObject(i);
  97. //get node
  98. var onode = physicsLayer.getObjectNode(i);
  99. //returns object group
  100. var group = tmxFile.getTileObjectGroup(o.tileGid);
  101. var obody = null;
  102. //if group exists
  103. if (group) {
  104. //iterate through each group object
  105. for (var j = 0; j < group.numObjects; j++) {
  106. var go = group.getObject(j);
  107. if (go.validCollisionShape()) {
  108. if (!obody) {
  109. //create rigid body
  110. obody = onode.createComponent("RigidBody2D");
  111. obody.bodyType = Atomic.BT_DYNAMIC;
  112. obody.awake = false;
  113. }
  114. //create a collision shape for our object
  115. var shape = go.createCollisionShape(onode);
  116. shape.density = 1.0;
  117. shape.friction = 1.0;
  118. shape.restitution = .1;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. };
  126. module.exports = LevelParser;