Advertisement
Guest User

BaeLevelBlockRunner.js

a guest
Apr 24th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.39 KB | None | 0 0
  1. var baseLayer = cc.Layer.extend
  2. ({
  3. mapWidth:0,
  4. map00:null,
  5. space:null,
  6. hudLayer:null,
  7. currLevel:0,
  8. pauseBtn:null,
  9. pauseMenu:null,
  10. collDead:false,
  11. screenSize:null,
  12. particle:null,
  13. coll:null,
  14. gameLayer:null,
  15. time:0,
  16. mPlayer:null,
  17. HudLayer:null,
  18. doOnce:false,
  19. outline:null,
  20. tiltLbl:null,
  21. followAction:null,
  22. setFollow:null,
  23. collEnt:null,
  24. collLayer:null,
  25. startTimerLabel:null,
  26. stopHud:false,
  27. gradient:null,
  28.  
  29. init:function(level)
  30. {
  31. this.stopMusic();
  32. this.screenSize = cc.director.getWinSize();
  33. GameVars.touchRestart = false;
  34. GameVars.currLevel = level;
  35. GameVars.levelTime = 0;
  36. this.gradient = cc.LayerGradient.create(cc.color(86,184,245,255),cc.color(150,238,250,255));
  37.  
  38. this.startTimerLabel = new cc.LabelBMFont("3", res.menuFont_ttf);
  39. this.startTimerLabel.setScale(3 * GameVars.scaleFactor,3 * GameVars.scaleFactor)
  40. this.startTimerLabel.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.5));
  41. this.startTimerLabel.color = cc.color.RED;
  42. this.startTimerLabel.visible = false;
  43. this.startTimerLabel.setOpacity(200);
  44. },
  45.  
  46. // init space of chipmunk
  47. initPhysics:function()
  48. {
  49. //1. new space object
  50. this.space = new cp.Space();
  51. //2. setup the Gravity
  52. this.space.gravity = cp.v(0, -(3150 * GameVars.scaleFactor));
  53.  
  54. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.floor, this.collisionFloorBegin.bind(this),this.collisionFloorBegin.bind(this), null, this.collisionFloorEnd.bind(this));
  55. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.goal, this.collisionGoal.bind(this),null, null, null);
  56. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.saw, this.collisionSaw.bind(this),null, null, null);
  57. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.boost, this.collisionBoost.bind(this),null,null,null);
  58. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.bomb, this.collisionBomb.bind(this),null,null,null);
  59. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.gravOn, this.collisionGravOn.bind(this),null,null,null);
  60. this.space.addCollisionHandler(SpriteTag.player, SpriteTag.gravOff, this.collisionGravOff.bind(this),null,null,null);
  61. },
  62.  
  63. //To be Overwritten
  64. collisionBomb:function()
  65. {
  66. },
  67. collisionGravOn:function()
  68. {
  69. },
  70. collisionGravOff:function()
  71. {
  72. },
  73. collisionBoost:function()
  74. {
  75. },
  76.  
  77. unlockLevel:function()
  78. {
  79. GameVars.levelLocks[GameVars.currLevel].level = 1;
  80. },
  81.  
  82. CreateMap:function()
  83. {
  84. //Load Correct map for current level
  85. switch (GameVars.currLevel)
  86. {
  87. case 2:
  88. this.map00 = new cc.TMXTiledMap(res.level2_tmx);
  89. break;
  90. case 3:
  91. this.map00 = new cc.TMXTiledMap(res.level3_tmx);
  92. break;
  93. case 4:
  94. this.map00 = new cc.TMXTiledMap(res.level4_tmx);
  95. break;
  96. case 5:
  97. this.map00 = new cc.TMXTiledMap(res.level5_tmx);
  98. break;
  99.  
  100. break;
  101. }
  102. },
  103.  
  104. createPauseMenu:function(player)
  105. {
  106. this.pauseMenu = new PauseMenu(player);
  107. this.addChild(this.pauseMenu);
  108. this.pauseMenu.visible = false;
  109.  
  110. this.pauseBtn = new ccui.Button("PauseBtn.png","PauseBtnDwn.png","",ccui.Widget.PLIST_TEXTURE);
  111. this.pauseBtn.x = this.screenSize.width * 0.05;
  112. this.pauseBtn.y = this.screenSize.height * 0.92;
  113. this.pauseBtn.setScale(GameVars.scaleFactor);
  114. this.pauseBtn.addTouchEventListener(this.pauseGame, this);
  115. this.addChild(this.pauseBtn);
  116. },
  117.  
  118. gradientSize:function(width,height)
  119. {
  120. this.gradient.setContentSize(width,height);
  121. },
  122.  
  123. createParticleSys:function()
  124. {
  125. //create particle system
  126. this.particle = cc.ParticleSystem.create(res.starsParticles_plist);
  127. this.particle.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5);
  128. this.particle.setScale(GameVars.scaleFactor);
  129. this.addChild(this.particle);
  130.  
  131. },
  132.  
  133. setUpdate:function()
  134. {
  135. //this.scheduleUpdate();
  136. },
  137.  
  138. collisionGoal:function (arbiter,space)
  139. {
  140. //Create Particle System
  141. this.createParticleSys();
  142.  
  143. this.mPlayer.stopMoving();
  144. cc.audioEngine.playEffect(res.sparkleSound_mp3);
  145.  
  146. //Stop HUD and set the time
  147. this.stopHud = true;
  148. GameVars.levelTime = this.time / 100;
  149. GameVars.canMove = false;
  150. var offset = 100;
  151. cc.log("Cur Level" + GameVars.currLevel)
  152. if(GameVars.currLevel == 2)
  153. {
  154. offset = -100;
  155. }
  156. var sprite_action = cc.moveBy(1, offset, 0);
  157. var endLev = cc.callFunc(this.endLevel,this);
  158. this.mPlayer.runAction(cc.sequence(cc.delayTime(1),sprite_action,endLev));
  159. },
  160.  
  161.  
  162. update:function(dt)
  163. {
  164. //Check if game is paused
  165. this.checkUnPause();
  166.  
  167. //check collision then run the explosion effect
  168. if(this.coll == true && this.doOnce == false)
  169. {
  170. this.createExplosion();
  171. this.doOnce = true;
  172. }
  173.  
  174. this.mPlayer.update();
  175.  
  176. //Game is not paused
  177. if(this.stopHud == false)
  178. {
  179. this.time++;
  180. this.HudLayer.updateTime(this.time / 100);
  181. }
  182.  
  183. //World Step for Chipmunk
  184. this.space.step(dt);
  185.  
  186. },
  187.  
  188. onExit:function()
  189. {
  190. this.unscheduleUpdate();
  191. this.stopAllActions();
  192. this._super();
  193. },
  194.  
  195. createBakcgroundGradient:function()
  196. {
  197. this.gradientSize(this.map00.getMapSize().width * GameVars.TileSize, this.map00.getMapSize().height * GameVars.TileSize);
  198. },
  199.  
  200. checkExplosion:function()
  201. {
  202. //check collision then run the explosion effect
  203. if(this.coll == true && this.doOnce == false)
  204. {
  205. this.createExplosion();
  206. this.doOnce = true;
  207. }
  208. },
  209.  
  210. checkRestart:function()
  211. {
  212. if(GameVars.touchRestart == true)
  213. {
  214. this.mPlayer.moveForCollision();
  215. GameVars.touchRestart = false;
  216. }
  217. },
  218.  
  219.  
  220. stopMusic:function()
  221. {
  222. cc.audioEngine.stopMusic();
  223. },
  224.  
  225. startMusic:function()
  226. {
  227. cc.audioEngine.playMusic(res.levelSnd_wav,true);
  228. },
  229.  
  230. getMapWidth:function()
  231. {
  232. return this.map00.getContentSize().width;
  233. },
  234.  
  235. setFollowAction:function()
  236. {
  237. //reset the player and "Camera"
  238. this.coll = false;
  239. this.doOnce = false;
  240. this.mPlayer.stopMoving();
  241. this.mPlayer.resetPos();
  242. this.removeColl();
  243. this.gameLayer.stopAllActions();
  244. this.followAction = cc.Follow.create(this.mPlayer.shape.image,cc.rect(0,0, GameVars.mapWidth, GameVars.mapHeight));
  245. this.gameLayer.runAction(this.followAction);
  246. },
  247.  
  248. //Loading of walls,floors and start
  249. loadBaseObjects:function(map,loadGoal)
  250. {
  251. var platforms = map.getObjectGroup("floor");
  252. cc.assert(platforms != [],"Platforms Is null");
  253. var platArray = platforms.getObjects();
  254. for (var i = 0; i < platArray.length; i++)
  255. {
  256. createStaticBody(this.space,cc.p(platArray[i]["x"] + platArray[i]["width"] / 2,platArray[i]["y"] + platArray[i]["height"] / 2),
  257. platArray[i]["width"],platArray[i]["height"],false,SpriteTag.floor,0.7);
  258. }
  259.  
  260. var walls = map.getObjectGroup("wall");
  261. var wallArray = walls.getObjects();
  262. for (var i = 0; i < wallArray.length; i++)
  263. {
  264. createStaticBody(this.space,cc.p(wallArray[i]["x"] + wallArray[i]["width"] / 2,wallArray[i]["y"] + wallArray[i]["height"] / 2),
  265. wallArray[i]["width"],wallArray[i]["height"],false,SpriteTag.wall,0)
  266. }
  267.  
  268. var start = map.getObjectGroup("start");
  269. var startArray = start.getObjects();
  270. GameVars.startPosx = startArray[0]["x"] + startArray[0]["width"] / 2;
  271. GameVars.startPosy = startArray[0]["y"] + startArray[0]["height"] / 2;
  272.  
  273. if(loadGoal)
  274. {
  275. var goals = this.map00.getObjectGroup("Goal");
  276. var goalArray = goals.getObjects();
  277. createStaticBody(this.space,cc.p(goalArray[0]["x"] + goalArray[0]["width"] / 2,goalArray[0]["y"] + goalArray[0]["height"] / 2),
  278. goalArray[0]["width"],goalArray[0]["height"],true,SpriteTag.goal);
  279. }
  280. },
  281.  
  282. startTimer:function()
  283. {
  284. this.timerLabel.visible = true;
  285. var end = cc.callFunc(this.sendTime,this);
  286. var time4 = cc.callFunc(this.setTime,this);
  287. this.mPlayer.runAction(cc.sequence(cc.delayTime(1),time4,cc.delayTime(1),time4,cc.delayTime(1),time4,
  288. cc.delayTime(1),time4,cc.delayTime(1),time4,end));
  289. },
  290.  
  291. setTime:function()
  292. {
  293. this.timeCount--;
  294. this.timerLabel.setString(this.timeCount);
  295. },
  296.  
  297. removeColl:function()
  298. {
  299. //loop delet the bodies and delete them from the array
  300. this.collEnt.length = 0;
  301. this.gameLayer.removeChild(this.collLayer);
  302. },
  303.  
  304. createExplosion:function()
  305. {
  306. //stop the action and player cant move
  307. cc.audioEngine.playEffect(res.bangSnd_mp3);
  308. this.gameLayer.stopAllActions();
  309.  
  310. this.collLayer = new cc.Layer();
  311.  
  312. //create particle system
  313. var bloodParticle = cc.ParticleSystem.create(res.bloodParticle_plist);
  314. bloodParticle.setPosition(this.mPlayer.body.p.x, this.mPlayer.body.p.y);
  315. this.gameLayer.addChild(bloodParticle);
  316.  
  317. //create new bodyPieces and add them to a new layer
  318. for(var i = 0; i < 10; i++)
  319. {
  320. var piece = new BodyPiece(this.space,this.mPlayer.body.p,30,30);
  321. this.collEnt.push(piece);
  322. this.collLayer.addChild(piece);
  323. }
  324. //add the layer move the player
  325. this.gameLayer.addChild(this.collLayer);
  326. this.collDead = true;
  327. this.mPlayer.moveForCollision();
  328. //reset the player and "Camera"
  329. this.setFollow = cc.CallFunc(this.setFollowAction,this);
  330. this.runAction(cc.sequence(cc.delayTime(1),this.setFollow));
  331. },
  332.  
  333.  
  334. startGameSeq:function(scale,xPos,yPos)
  335. {
  336. this.scheduleUpdate();
  337. //Set position for entry effect
  338. this.setScale(scale, scale);
  339. this.setPosition(-(xPos * GameVars.scaleFactor), -yPos);
  340. },
  341.  
  342. startGameSeqOdd:function(scaleX,scaleY,xPos,yPos)
  343. {
  344. //Set position for entry effect
  345. this.setScale(scaleX, scaleY);
  346. this.setPosition((xPos * GameVars.scaleFactor), yPos);
  347.  
  348. },
  349.  
  350. createNumbers:function()
  351. {
  352. //Create the numbers for start sequence
  353. if(GameVars.isAccelLvl == true)
  354. {
  355. this.tiltLbl = new cc.LabelBMFont("Tilt", res.menuFont_ttf);
  356. this.tiltLbl.setScale(1 * GameVars.scaleFactor,1 * GameVars.scaleFactor)
  357. this.tiltLbl.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.8));
  358. this.tiltLbl.color = cc.color.RED;
  359. this.addChild(this.tiltLbl);
  360.  
  361. this.outline = new cc.Sprite("#iPad.png");
  362. this.outline.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5);
  363. this.outline.setVisible(true);
  364. this.outline.setScale(2);
  365. this.outline.setRotation(90);
  366. this.addChild(this.outline);
  367. }
  368.  
  369. },
  370.  
  371. startSequnce:function(xPos,yPos)
  372. {
  373. //Start sequence actions
  374. var scaleAction = cc.scaleTo(3, 1, 1);
  375. var moveAction = cc.moveBy(3, xPos * GameVars.scaleFactor, yPos);
  376. var setHud = cc.callFunc(this.setHudStart,this);
  377. var setThree = cc.callFunc(this.setNum,this);
  378. var setTwo = cc.CallFunc(this.setNum2,this);
  379. var setOne = cc.CallFunc(this.setNum3,this);
  380. var setGo = cc.CallFunc(this.setGo,this);
  381. var startLevel = cc.CallFunc(this.startLevel,this);
  382.  
  383.  
  384. var rotateFunc = cc.callFunc(this.rotateFunc, this);
  385.  
  386. //Run the actions for the start sequence
  387. this.runAction(cc.sequence(cc.delayTime(3),scaleAction));
  388.  
  389. if(GameVars.isAccelLvl == true)
  390. {
  391. this.runAction(cc.sequence(cc.delayTime(3),moveAction,rotateFunc,cc.delayTime(2.5),setThree,cc.DelayTime(1),setTwo,cc.delayTime(1),setOne,
  392. cc.delayTime(1),setGo,cc.delayTime(0.5),startLevel));
  393. }
  394. else
  395. {
  396. this.runAction(cc.sequence(cc.delayTime(3),moveAction,setThree,cc.DelayTime(1),setTwo,cc.delayTime(1),setOne,
  397. cc.delayTime(1),setGo,cc.delayTime(0.5),startLevel));
  398. }
  399. },
  400.  
  401. rotateFunc:function()
  402. {
  403. var rotate = cc.rotateBy(1, 30, 30);
  404. var rotateBack = cc.rotateBy(1,-60,-60);
  405. var fade = cc.callFunc(this.deleteTilt,this);
  406. this.outline.runAction(cc.sequence(rotate,rotateBack,fade));
  407. },
  408.  
  409. deleteTilt:function()
  410. {
  411. this.outline.runAction(cc.fadeTo(1, 0));
  412. this.tiltLbl.runAction(cc.fadeTo(1, 0));
  413. },
  414.  
  415. collisionSaw:function (arbiter, space)
  416. {
  417. //Set collision to true for particle
  418. this.coll = true;
  419. },
  420.  
  421. startLevel:function()
  422. {
  423. //Set go vis and show the hud
  424. this.startTimerLabel.visible = false;
  425. this.setHudStart();
  426. },
  427.  
  428. setGo:function()
  429. {
  430. //set visible
  431. this.playTickAudio();
  432. this.startTimerLabel.setString("GO");
  433. },
  434.  
  435. setNum:function()
  436. {
  437. //set visible
  438. this.playTickAudio();
  439. this.startTimerLabel.visible = true;
  440. },
  441.  
  442. setNum2:function()
  443. {
  444. //set visible
  445. this.playTickAudio();
  446. this.startTimerLabel.setString("2");
  447. },
  448.  
  449. setNum3:function()
  450. {
  451. //set visible
  452. this.playTickAudio();
  453. this.startTimerLabel.setString("1");
  454. },
  455.  
  456. playTickAudio:function()
  457. {
  458. cc.audioEngine.playEffect(res.tick_wav, 0);
  459. },
  460.  
  461. setHudStart:function()
  462. {
  463. //remove start numbers and set hud visible
  464. this.deleteNumberTextures();
  465. this.HudLayer.setVisible(true);
  466. if(!GameVars.isMulti)
  467. this.pauseBtn.visible = true;
  468.  
  469. this.stopHud = false;
  470. GameVars.canMove = true;
  471. },
  472.  
  473. deleteNumberTextures:function()
  474. {
  475. this.removeChild(this.startTimerLabel);
  476. },
  477.  
  478. pauseGame:function(sender,type)
  479. {
  480. switch (type)
  481. {
  482. case ccui.Widget.TOUCH_ENDED:
  483. cc.audioEngine.playEffect(res.buttonClick_mp3);
  484. this.pausedPressed();
  485. break;
  486.  
  487. break;
  488. }
  489. },
  490.  
  491. pausedPressed:function()
  492. {
  493. this.pauseMenu.visible = true;
  494. this.pauseBtn.visible = false;
  495. cc.director.pause();
  496. },
  497.  
  498. checkUnPause:function()
  499. {
  500. if(this.pauseMenu.isResumed)
  501. {
  502. this.pauseBtn.visible = true;
  503. }
  504. },
  505.  
  506. endLevel:function()
  507. {
  508. cc.director.runScene(new cc.TransitionFade(1.5, new EndScene()));
  509. },
  510.  
  511. collisionFloorBegin:function (arbiter, space)
  512. {
  513. GameVars.canJump = true;
  514. },
  515. collisionFloorEnd:function(arbiter,space)
  516. {
  517. GameVars.canJump = false;
  518. },
  519. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement