LevelParser.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. }
  22. if (o.type == "Vine") {
  23. entity.type = "Vine";
  24. entity.position = onode.position2D;
  25. } else if (o.type == "PlatformStart") {
  26. var pnum = Number(o.getProperty("Platform"));
  27. if (!platforms.hasOwnProperty(pnum))
  28. platforms[pnum] = [null, null];
  29. platforms[pnum][0] = o;
  30. } else if (o.type == "PlatformStop") {
  31. var pnum = Number(o.getProperty("Platform"));
  32. if (!platforms.hasOwnProperty(pnum))
  33. platforms[pnum] = [null, null];
  34. platforms[pnum][1] = o;
  35. }
  36. if (entity.type)
  37. this.entities.push(entity);
  38. }
  39. for (var pnum in platforms) {
  40. var entity = {
  41. type: "MovingPlatform",
  42. start: platforms[pnum][0].position,
  43. stop: platforms[pnum][1].position
  44. }
  45. this.entities.push(entity);
  46. }
  47. }
  48. },
  49. getEntity: function(type) {
  50. for (var i = 0; i < this.entities.length; i++)
  51. if (this.entities[i].type == type)
  52. return this.entities[i];
  53. return null;
  54. },
  55. getEntities: function(type) {
  56. var entities = [];
  57. for (var i = 0; i < this.entities.length; i++)
  58. if (this.entities[i].type == type)
  59. entities.push(this.entities[i]);
  60. return entities;
  61. },
  62. getSpawnpoint: function(type) {
  63. var pos = [0, 0];
  64. var entity = this.getEntity("PlayerSpawn");
  65. return entity ? entity.position : pos;
  66. },
  67. createPhysics: function(tileMap, tmxFile) {
  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. };