Level.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "atomic component";
  2. //requiring LevelParser module
  3. var LevelParser = require("LevelParser");
  4. //A Level
  5. var component = function (self) {
  6. //start function will be excecuted, after connecting our component to the node, after the constructor
  7. self.start = function() {
  8. //get TileMap2D component from our current node
  9. var tileMap = self.node.getComponent("TileMap2D");
  10. var tmxFile = tileMap.tmxFile;
  11. //looping main game song
  12. var music = Atomic.cache.getResource("Sound", "Sounds/JumpingBat.ogg");
  13. music.looped = true;
  14. //create LevelParser object
  15. var levelParser = new LevelParser(tileMap);
  16. levelParser.createPhysics(tileMap, tmxFile);
  17. var position = levelParser.getSpawnpoint();
  18. position[1] += 1.5;
  19. //create a child prefab
  20. var node = self.scene.createChildPrefab("Player", "Prefabs/Hero.prefab");
  21. //set it position to the current node position
  22. node.position2D = position;
  23. //get all entities from our map which names MovingPlatform
  24. var platforms = levelParser.getEntities("MovingPlatform");
  25. for (var i = 0; i < platforms.length; i++) {
  26. var p = platforms[i];
  27. var node = self.scene.createChildPrefab("MovingPlatform", "Prefabs/MovingPlatform.prefab");
  28. node.position2D = p.start;
  29. node.startPos = p.start;
  30. node.stopPos = p.stop;
  31. }
  32. //get all entities from our map which names Coin
  33. var coins = levelParser.getEntities("Coin");
  34. for (var i = 0; i < coins.length; i++) {
  35. var node = self.scene.createChildPrefab("Coin", "Prefabs/Coin.prefab");
  36. node.position2D = coins[i].position;
  37. }
  38. //get all entities from our map which names BatWaypoint
  39. var waypoints = [];
  40. var batWaypoints = levelParser.getEntities("BatWaypoint");
  41. for (var i = 0; i < batWaypoints.length; i++) {
  42. waypoints.push(batWaypoints[i].position);
  43. }
  44. //get all entities from our map which names Bat
  45. var bats = levelParser.getEntities("Bat");
  46. for (var i = 0; i < bats.length; i++) {
  47. var node = self.scene.createChildPrefab("Bat", "Prefabs/Bat.prefab");
  48. node.position2D = bats[i].position;
  49. node.scale2D = [.5, .5];
  50. node.waypoints = waypoints;
  51. }
  52. //get all entities from our map which names Vine
  53. var vines = levelParser.getEntities("Vine");
  54. for (var i = 0; i < vines.length; i++) {
  55. var vnode = self.scene.createChild("Vine");
  56. vnode.createJSComponent("Components/Vine.js", {startPosition : vines[i].position});
  57. }
  58. //reduce num rays on mobile/web platforms for better performance
  59. if(Atomic.platform == "Android" || Atomic.platform == "iOS" || Atomic.platform == "WebGL") {
  60. self.scene.getChild("TheSun").getComponent("DirectionalLight2D").numRays = 512;
  61. }
  62. };
  63. };
  64. exports.component = component;