bgmanager.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. info : {
  14. default: null,
  15. type: cc.Prefab
  16. },
  17. player: {
  18. default: null,
  19. type: cc.Prefab
  20. },
  21. playerAi: {
  22. default: null,
  23. type: cc.Prefab
  24. },
  25. actor1: {
  26. default: null,
  27. type: cc.Prefab
  28. },
  29. actor2: {
  30. default: null,
  31. type: cc.Prefab
  32. },
  33. actor3: {
  34. default: null,
  35. type: cc.Prefab
  36. },
  37. actor4: {
  38. default: null,
  39. type: cc.Prefab
  40. },
  41. bgs1: {
  42. default: null,
  43. type: cc.Prefab
  44. },
  45. bgs2: {
  46. default: null,
  47. type: cc.Prefab
  48. },
  49. bgs3: {
  50. default: null,
  51. type: cc.Prefab
  52. },
  53. bgsend: {
  54. default: null,
  55. type: cc.Prefab
  56. },
  57. killItem : {
  58. default: null,
  59. type: cc.Prefab
  60. }
  61. },
  62. // LIFE-CYCLE CALLBACKS:
  63. // onLoad () {},
  64. start() {
  65. this._init();
  66. },
  67. onLoad () {
  68. app.bg = this;
  69. //镜头最开始的位置保存 每次游戏开始的时候使用
  70. this.camerainitP = {x: app.camera.x, y: app.camera.y};
  71. let bgss = this.bgss = [];
  72. bgss.push(this.bgs1);
  73. bgss.push(this.bgs2);
  74. bgss.push(this.bgs3);
  75. // bgss.push(this.bgs4);
  76. },
  77. _init: function () {
  78. app.camera.x = this.camerainitP.x;
  79. app.camera.y = this.camerainitP.y;
  80. for (var i = 0; i < this.node.children.length; i++) {
  81. this.node.children[i].destroy();
  82. }
  83. let actorps =this.actorps= UtilsNode.startActor(function (bgArr,postions) {
  84. console.log("背景数组", bgArr);
  85. this.bgArr = bgArr;
  86. this.initBg(postions);
  87. }.bind(this));
  88. this.actors = [];
  89. this.actors.push(this.actor1);
  90. this.actors.push(this.actor2);
  91. this.actors.push(this.actor3);
  92. this.actorsNode = UtilsNode.actorBirth(this.node, actorps, this.actors, this.actors[0], this
  93. .actor4);
  94. console.log("要创建东西的位置", actorps);
  95. this.addInfo(actorps);
  96. let node = this.playerNode = cc.instantiate(this.player);
  97. this.node.addChild(node);
  98. node.zIndex = 9999999;
  99. this.playerNode.getComponent("player").setMap(this.actorsNode,0);
  100. this._initAi();
  101. this.callback();
  102. this.playerNode.getComponent("player").startGame(false);
  103. this.playerNodeAi.getComponent("player").startGame(false);
  104. if (app.readGo != null) {
  105. app.readGo.readgo(function () {
  106. this.playerNode.getComponent("player").startGame(true);
  107. this.playerNodeAi.getComponent("player").startGame(true);
  108. }.bind(this));
  109. }
  110. },
  111. // 初始化ai
  112. _initAi : function(){
  113. let nodeAi = this.playerNodeAi = cc.instantiate(this.playerAi);
  114. this.node.addChild(nodeAi);
  115. this.playerNodeAi.getComponent("player").setMap(this.actorsNode,1);
  116. this.unschedule(this.callbacks);
  117. this.callbacks = function () {
  118. // if (this.car.x<-165) {
  119. // // 在第六次执行回调时取消这个计时器
  120. // this.unschedule(this.callback);
  121. // }
  122. // let x = UtilsNode.getRandom(2,0,1);
  123. //
  124. // if (x == 0) {
  125. // this.playerNodeAi.getComponent("player").addOne();
  126. // }else if (x == 1) {
  127. // this.playerNodeAi.getComponent("player").addTwo();
  128. // }
  129. this.playerNodeAi.getComponent("player").addAuto();
  130. }
  131. this.schedule(this.callbacks, 0.8);
  132. },
  133. //两步提示
  134. addInfo: function (actorps) {
  135. this.info = cc.instantiate(this.info);
  136. this.info.active = true;
  137. this.node.addChild(this.info);
  138. this.info.zIndex = 999999;
  139. // console.log("提示在node 里面有没有",this.node.children);
  140. this.info.setPosition(actorps[7].x, actorps[7].y+100);
  141. let sequence = cc.sequence(cc.moveTo(0.2, cc.v2(actorps[7].x-20,actorps[7].y+100)),cc.moveTo(0.2, cc.v2(actorps[7].x+20,actorps[7].y+100)));
  142. // let sequence = cc.moveTo(1, cc.v2(actorps[6].x-10,actorps[6].y));
  143. var myAction = cc.repeatForever(sequence);
  144. this.info.runAction(myAction)
  145. },
  146. initBg: function (postions) {
  147. //创建 荷花啥的
  148. for (var i = 0; i < this.bgArr.length; i++) {
  149. if (i % 2 == 0) {
  150. //偶数
  151. let node = this.addRandomBG()
  152. node.setPosition(this.bgArr[i].x - 350, this.bgArr[i].y);
  153. UtilsNode.setMyRotation(1,node);
  154. // this.addRandomBG().setPosition(this.bgArr[i].x - 350, this.bgArr[i].y);
  155. // this.addRandomBG().setPosition(this.bgArr[i].x - 500, this.bgArr[i].y);
  156. } else {
  157. //奇数
  158. let node = this.addRandomBG()
  159. node.setPosition(this.bgArr[i].x + 350, this.bgArr[i].y);
  160. UtilsNode.setMyRotation(0,node);
  161. // this.addRandomBG().setPosition(this.bgArr[i].x + 350, this.bgArr[i].y);
  162. // this.addRandomBG().setPosition(this.bgArr[i].x + 500, this.bgArr[i].y);
  163. }
  164. }
  165. // this.addBG(2).setPosition(postions[postions.length-1].x - 200, postions[postions.length-1].y+200);
  166. // this.addBG(2).setPosition(postions[postions.length-1].x + 200, postions[postions.length-1].y+250);
  167. //
  168. // this.addBG(2).setPosition(postions[postions.length-1].x - 220, postions[postions.length-1].y+400);
  169. // this.addBG(2).setPosition(postions[postions.length-1].x + 180, postions[postions.length-1].y+450);
  170. //
  171. // this.addBG(2).setPosition(postions[postions.length-1].x - 300, postions[postions.length-1].y+500);
  172. // this.addBG(2).setPosition(postions[postions.length-1].x - 150, postions[postions.length-1].y+600);
  173. // // this.addBG(2).setPosition(postions[postions.length-1].x + 200, postions[postions.length-1].y+600);
  174. //
  175. // this.add1BG(6).setPosition(postions[postions.length-1].x+200, postions[postions.length-1].y+600);
  176. let bgsend = cc.instantiate(this.bgsend);
  177. bgsend.setPosition(postions[postions.length-1].x+200, postions[postions.length-1].y+500);
  178. this.node.addChild(bgsend);
  179. },
  180. addRandomBG : function(Scale){
  181. let index = UtilsNode.getRandom(0, 0, this.bgss.length);
  182. let node = this.playerNode = cc.instantiate(this.bgss[index]);
  183. if (Scale!=null) {
  184. node.setScale(Scale);
  185. }else{
  186. node.setScale(1);
  187. }
  188. this.node.addChild(node);
  189. return node;
  190. },
  191. add1BG : function(Scale){
  192. let node = this.playerNode = cc.instantiate(this.bgss[1]);
  193. node.setScale(6);
  194. this.node.addChild(node);
  195. return node;
  196. },
  197. addBG : function(Scale){
  198. let index = UtilsNode.getRandom(2, 1, 2);
  199. let node = this.playerNode = cc.instantiate(this.bgss[index]);
  200. if (Scale!=null) {
  201. node.setScale(Scale);
  202. }else{
  203. node.setScale(1);
  204. }
  205. this.node.addChild(node);
  206. return node;
  207. },
  208. callback: function () {
  209. this.playerNode.getComponent("player").setCallBackGameOver(function () {
  210. console.log("游戏结束了");
  211. app.titlelayout.hideButton(true);
  212. this.playerNodeAi.getComponent("player").winOrLose(false);
  213. app.bgm.playimwin();
  214. // app.bgm.stop();
  215. }.bind(this));
  216. this.playerNode.getComponent("player").setCallBackTrap(function () {
  217. // console.log("掉陷阱");
  218. app.bgm.playluoshui();
  219. app.bgm.playa();
  220. }.bind(this));
  221. this.playerNode.getComponent("player").setCallBackMove(function (index) {
  222. this.playerIndex = index;
  223. this.isDisparity();
  224. console.log("移动了了吗",index);
  225. app.titlelayout.setProgress(0,index,this.actorps.length-1);
  226. }.bind(this));
  227. this.playerNode.getComponent("player").setCallBackInfo(function () {
  228. console.log("info消失");
  229. this.info.active = false;
  230. }.bind(this));
  231. this.playerNode.getComponent("player").setCallBackJump(function (p) {
  232. // let node = cc.instantiate(this.killItem);
  233. // let node = UtilsNode.getNode("qi",this.playerNode);
  234. // node.getComponent("kill").setActive(true);
  235. // this.playerNode.getComponent("player").showsSmoke(true);
  236. // node.setPosition(p.x, p.y);
  237. }.bind(this));
  238. this.playerNode.getComponent("player").setCallBackCameraMove(function (node) {
  239. //镜头移动
  240. if (node.y>500) {
  241. if (node.y-500 > app.camera.y) {
  242. var myAction =cc.moveTo(0.2, cc.v2(app.camera.x,node.y-500))
  243. app.camera.runAction(myAction);
  244. }
  245. }
  246. }.bind(this));
  247. this.playerNodeAi.getComponent("player").setCallBackGameOver(function () {
  248. console.log("游戏结束了");
  249. app.titlelayout.hideButton(true);
  250. this.playerNode.getComponent("player").winOrLose(false);
  251. // app.bgm.playkuaidaozhongdian();
  252. app.bgm.playzhecibusuan();
  253. }.bind(this));
  254. this.playerNodeAi.getComponent("player").setCallBackTrap(function () {
  255. // console.log("掉陷阱");
  256. app.bgm.playluoshui();
  257. app.bgm.playa();
  258. }.bind(this));
  259. this.playerNodeAi.getComponent("player").setCallBackMove(function (index) {
  260. this.playerNodeAiIndex = index;
  261. app.titlelayout.setProgress(1,index,this.actorps.length-1);
  262. // console.log("移动了",index);
  263. this.isDisparity();
  264. }.bind(this));
  265. this.playerNodeAi.getComponent("player").setCallBackInfo(function () {
  266. // console.log("info消失");
  267. // this.info.active = false;
  268. }.bind(this));
  269. this.playerNodeAi.getComponent("player").setCallBackJump(function (p) {
  270. // let node = cc.instantiate(this.killItem);
  271. // let node = UtilsNode.getNode("qi",this.playerNodeAi);
  272. // //落地灰尘显示
  273. // node.getComponent("kill").setActive(true);
  274. // this.playerNodeAi.getComponent("player").showsSmoke(true);
  275. // node.setPosition(p.x, p.y);
  276. }.bind(this));
  277. //初始化相差步数
  278. this.playerNodeAiIndex = 0;
  279. this.playerIndex = 0;
  280. //如有对象 就默认隐藏
  281. if (app.titlelayout != null) {
  282. app.titlelayout._init();
  283. }
  284. },
  285. isDisparity : function(){
  286. // console.log("info消失");
  287. let index = 0;
  288. if (this.playerNodeAiIndex-this.playerIndex>0) {
  289. index =this.playerNodeAiIndex-this.playerIndex;
  290. this.showDisparity(1,index);
  291. }else if (this.playerIndex-this.playerNodeAiIndex>0) {
  292. index =this.playerIndex-this.playerNodeAiIndex;
  293. this.showDisparity(0,index);
  294. }
  295. },
  296. showDisparity : function(type , index){
  297. if (index>10) {
  298. app.titlelayout.showinfo(true,type,index);
  299. }else{
  300. app.titlelayout.showinfo(false,type,index);
  301. }
  302. // switch (type) {
  303. // case 0:
  304. // console.log("你领先",index);
  305. //
  306. // break;
  307. // case 1:
  308. // console.log("你落后",index);
  309. // break;
  310. // }
  311. },
  312. one: function () {
  313. this.playerNode.getComponent("player").addOne();
  314. },
  315. two: function () {
  316. this.playerNode.getComponent("player").addTwo();
  317. },
  318. reset: function () {
  319. this._init();
  320. }
  321. // update (dt) {},
  322. });