Level.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. //create LevelParser object
  12. var levelParser = new LevelParser(tileMap);
  13. levelParser.createPhysics(tileMap, tmxFile);
  14. var position = levelParser.getSpawnpoint();
  15. position[1] += 1.5;
  16. //create a child prefab
  17. var node = self.scene.createChildPrefab("Player", "Prefabs/Hero.prefab");
  18. //set it position to the current node position
  19. node.position2D = position;
  20. //get all entities from our map which names MovingPlatform
  21. var platforms = levelParser.getEntities("MovingPlatform");
  22. for (var i = 0; i < platforms.length; i++) {
  23. var p = platforms[i];
  24. var node = self.scene.createChildPrefab("MovingPlatform", "Prefabs/MovingPlatform.prefab");
  25. node.position2D = p.start;
  26. node.startPos = p.start;
  27. node.stopPos = p.stop;
  28. }
  29. //get all entities from our map which names Coin
  30. var coins = levelParser.getEntities("Coin");
  31. for (var i = 0; i < coins.length; i++) {
  32. var node = self.scene.createChildPrefab("Coin", "Prefabs/Coin.prefab");
  33. node.position2D = coins[i].position;
  34. }
  35. //get all entities from our map which names BatWaypoint
  36. var waypoints = [];
  37. var batWaypoints = levelParser.getEntities("BatWaypoint");
  38. for (var i = 0; i < batWaypoints.length; i++) {
  39. waypoints.push(batWaypoints[i].position);
  40. }
  41. //get all entities from our map which names Bat
  42. var bats = levelParser.getEntities("Bat");
  43. for (var i = 0; i < bats.length; i++) {
  44. var node = self.scene.createChildPrefab("Bat", "Prefabs/Bat.prefab");
  45. node.position2D = bats[i].position;
  46. node.scale2D = [.5, .5];
  47. node.waypoints = waypoints;
  48. }
  49. //get all entities from our map which names Vine
  50. var vines = levelParser.getEntities("Vine");
  51. for (var i = 0; i < vines.length; i++) {
  52. var vnode = self.scene.createChild("Vine");
  53. vnode.createJSComponent("Components/Vine.js", {startPosition : vines[i].position});
  54. }
  55. }
  56. }
  57. exports.component = component;