dishu.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. touchFlag: false,
  5. _isEnemy: false,
  6. },
  7. init(parents, randomIndex) {
  8. this.父级 = parents;
  9. this.添加事件();
  10. this._name = (randomIndex - 1).toString();
  11. // console.log("我现在完全露头了 名字是 11111",this._type);
  12. if (randomIndex == 1)
  13. this._name = "";
  14. this.surplusCount = this.击打次数(randomIndex);
  15. this.clickCount = 0;
  16. this.reduceBloodCount = 0;
  17. this.目标父级 = this.父级;
  18. this.node.position = new cc.Vec2(0, -190);
  19. //是否允许被击打
  20. this.canClick = false;
  21. this.hit = false;
  22. this.playquan = 0;
  23. if (!this._isEnemy) {
  24. // utils.isMobile(function () {
  25. // manager_android_js.mydishu = this;
  26. // }.bind(this));
  27. }
  28. },
  29. //是不是我方
  30. isMy : function(){
  31. let my = false;
  32. if (!this._isEnemy) {
  33. my = true;
  34. }
  35. return my;
  36. },
  37. initData: function (Index,type,conf,Pool) {
  38. this._Index = Index;
  39. this._type = type;
  40. this.conf = conf;
  41. // console.log("地鼠出生", "设置特殊数字",Index, type);
  42. this.PlayDragonBones(this.animationDisplay, "jian" + this._name, this.默认状态, 1);
  43. // Pool[Index] =this;
  44. // Log.info("准备伸头 ",this.animationDisplay)
  45. if (this.isMy()) {
  46. app.myDishuPool[Index] = this;
  47. Statistics.dishu.num+=1
  48. }
  49. // console.log("现在是什么" ,"判断结果哪个是空的可以上地鼠的",Index ,"进来的地鼠盒", Pool,"全局地鼠盒",app.myDishuPool);
  50. this.animationDisplay.timeScale = this.conf.top.timeScale;
  51. this._isEnemy = false;
  52. if (this.父级.name == "敌方父级") {
  53. this._isEnemy = true;
  54. }
  55. },
  56. toclick: function (data) {
  57. // console.log("当前地鼠", this.canClick,"方向是",this._Index,"类型是",this._type);
  58. if (!this.canClick)
  59. return;
  60. ObjectPools.playSimpleAudioEngine(0);
  61. utils.isMobile(function () {
  62. if (manager_android_js.gamestatus == 3) {
  63. return;
  64. }
  65. }.bind(this));
  66. this.attackMode();
  67. // cc.log("点击地鼠", this._Index);
  68. },
  69. attackMode: function () {
  70. var pos = this.node.parent.parent.position;
  71. Games.拳套.position = pos;
  72. // if (this.hit) {
  73. // return;
  74. // }
  75. // this.hit = true;
  76. var randomInt = parseInt(Math.random() * 3, 10);
  77. this.node.stopAllActions();
  78. this.unscheduleAllCallbacks();
  79. if (!this._isEnemy) {
  80. if (this._type == 0) {
  81. Statistics.addScore(0,dishuManager.rewardScore);
  82. Statistics.addCount(0, this._Index);
  83. }else{
  84. Statistics.addScore(0,-dishuManager.rewardScore);
  85. Statistics.addCount(0, this._Index);
  86. Statistics.addmiss(0);
  87. }
  88. this. attacklose();
  89. }
  90. Games.拳套.active = true;
  91. switch (randomInt) {
  92. case 0:
  93. this.PlayDragonBones(Games.glovesDragon, "attack2", this.隐藏拳套, 1);
  94. this.击中();
  95. break;
  96. case 1:
  97. Games.拳套.scale = new cc.Vec2(1, 1);
  98. this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
  99. this.右击();
  100. break;
  101. case 2:
  102. Games.拳套.scale = new cc.Vec2(-1, 1);
  103. this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
  104. this.左击();
  105. break;
  106. }
  107. },
  108. //打错了 或者 该打的没有打 就下去了
  109. lose : function(){
  110. // console.log("现在被打的或者自己下去的 是什么类型的",this._type);
  111. Log.info("现在被打的或者自己下去的 是什么类型的",this._type);
  112. if (Games.gameTag) {
  113. if (this._type == 0) {
  114. Statistics.discontinuity(0);
  115. ObjectPools.playSimpleAudioEngine(8);
  116. }
  117. }
  118. },
  119. attacklose : function(){
  120. // console.log("现在被打的或者自己下去的 是什么类型的",this._type);
  121. if (Games.gameTag){
  122. if (this._type == 1) {
  123. Statistics.discontinuity(0);
  124. ObjectPools.playSimpleAudioEngine(8);
  125. }
  126. }
  127. },
  128. 添加事件() {
  129. this.animationDisplay = this.getComponent(dragonBones.ArmatureDisplay);
  130. if (this._isEnemy) {
  131. } else {
  132. this.node.on(cc.Node.EventType.TOUCH_START, function () {
  133. // this.toclick();
  134. }, this);
  135. // this.toclick();
  136. // Games.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  137. // if (!this.canClick)
  138. // return;
  139. // if (Games.checkTouch(this.node, event.getLocation())) {
  140. // if (this.touchFlag == false) {
  141. // this.lastPos = event.getLocation();
  142. // this.touchFlag = true;
  143. // }
  144. // } else {
  145. // if (this.touchFlag == true) {
  146. // this.currentPos = event.getLocation();
  147. // this.方向判断();
  148. // }
  149. // }
  150. // }, this);
  151. this.我方事件();
  152. }
  153. },
  154. 敌方事件() {
  155. this.randomClickTime = 0.5;
  156. this.clickCallback = function () {
  157. utils.isMobile(function () {
  158. if (manager_android_js.gamestatus == 3) {
  159. return;
  160. }
  161. }.bind(this));
  162. this.randomClickTime = (parseInt(Math.random() * 3, 10) + 3) * 0.1;
  163. var randomClickHole = parseInt(Math.random() * 100, 10);
  164. if (this._type == 0) {
  165. if (randomClickHole > 10) {
  166. var randomClickCount = parseInt(Math.random() * 9, 10) + 3;
  167. this.randomIntervalTime = 0.2;
  168. this.randomIntervalTime = (parseInt(Math.random() * 2, 10) + 1) * 0.1;
  169. var randomClcikType = parseInt(Math.random() * 3, 10); //0-3(击打类型0:左,1:中2:右)
  170. // console.log("随机打击?", randomClcikType);
  171. if (this._isEnemy) {
  172. Statistics.addCount(1, this._Index);
  173. if (this._type == 0) {
  174. Statistics.addScore(1,dishuManager.rewardScore);
  175. }else{
  176. Statistics.addScore(1,-dishuManager.rewardScore);
  177. }
  178. this. attacklose();
  179. }
  180. Games.拳套蓝.active = true;
  181. if (randomClcikType == 0) {
  182. var pos = this.node.parent.parent.position;
  183. Games.拳套蓝.position = pos;
  184. Games.拳套蓝.scale = new cc.Vec2(-0.5, 0.5);
  185. this.PlayDragonBones(Games.glovesDragonlan, "attack1", this.隐藏拳套蓝, 1);
  186. this.左击();
  187. } else if (randomClcikType == 1) {
  188. var pos = this.node.parent.parent.position;
  189. Games.拳套蓝.position = pos;
  190. Games.拳套蓝.scale = new cc.Vec2(0.5, 0.5);
  191. this.PlayDragonBones(Games.glovesDragonlan, "attack2", this.隐藏拳套蓝, 1);
  192. this.击中();
  193. } else {
  194. var pos = this.node.parent.parent.position;
  195. Games.拳套蓝.position = pos;
  196. Games.拳套蓝.scale = new cc.Vec2(0.5, 0.5);
  197. this.PlayDragonBones(Games.glovesDragonlan, "attack1", this.隐藏拳套蓝, 1);
  198. this.右击();
  199. }
  200. // this.callback = function () {
  201. //
  202. // // if (randomClickCount <= 0) {
  203. // // this.unschedule(this.callback);
  204. // // }
  205. //
  206. // this.randomIntervalTime = (parseInt(Math.random() * 2, 10) + 1) * 0.1;
  207. // var randomClcikType = parseInt(Math.random() * 2, 10); //0-3(击打类型0:左,1:中2:右)
  208. //
  209. //
  210. // if (randomClcikType == 0) {
  211. // this.左击();
  212. // } else if (randomClcikType == 1) {
  213. // this.击中();
  214. // } else {
  215. // this.右击();
  216. // }
  217. // randomClickCount--;
  218. // }
  219. // this.schedule(this.callback, this.randomIntervalTime);
  220. }
  221. }
  222. }.bind(this)
  223. setTimeout(this.clickCallback, this.randomClickTime);
  224. // this.schedule(this.clickCallback, this.randomClickTime);
  225. },
  226. 我方事件() {
  227. // this.node.on(cc.Node.EventType.TOUCH_START, function () {
  228. // if (!this.canClick)
  229. // return;
  230. // ObjectPools.playSimpleAudioEngine(0);
  231. // var pos = this.node.parent.parent.position;
  232. // Games.拳套.position = pos;
  233. // this.PlayDragonBones(Games.glovesDragon, "attack2", this.隐藏拳套, 1);
  234. // this.击中();
  235. // }, this);
  236. // Games.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
  237. // if (!this.canClick)
  238. // return;
  239. // if (Games.checkTouch(this.node, event.getLocation())) {
  240. // if (this.touchFlag == false) {
  241. // this.lastPos = event.getLocation();
  242. // this.touchFlag = true;
  243. // } else {
  244. // this.currentPos = event.getLocation();
  245. // this.方向判断();
  246. // }
  247. // }
  248. // }, this);
  249. // ObjectPools.playSimpleAudioEngine(3);
  250. },
  251. 方向判断() {
  252. if (!this.canClick)
  253. return;
  254. if (this.node.parent == null)
  255. return;
  256. var valuex = this.currentPos.x - this.lastPos.x;
  257. if (Math.abs(valuex) < 40)
  258. return;
  259. this.touchFlag = false;
  260. // 左右滑动
  261. if (valuex > 0) {
  262. ObjectPools.playSimpleAudioEngine(0);
  263. var pos = this.node.parent.parent.position;
  264. Games.拳套.position = pos;
  265. Games.拳套.scale = new cc.Vec2(1, 1);
  266. this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
  267. this.右击();
  268. } else {
  269. ObjectPools.playSimpleAudioEngine(0);
  270. var pos = this.node.parent.parent.position;
  271. Games.拳套.position = pos;
  272. Games.拳套.scale = new cc.Vec2(-1, 1);
  273. this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
  274. this.左击();
  275. }
  276. },
  277. 默认状态(event) {
  278. var scripts = this.node.getComponent("dishu");
  279. // scripts.PlayDragonBonesRemoveEvent(scripts.animationDisplay,"jian" + this._name);
  280. // 出现后 倒计时2秒缩头
  281. if (scripts.animationDisplay._animationName.indexOf('jian') != -1) {
  282. if (scripts._isEnemy) {
  283. scripts.敌方事件();
  284. }
  285. if (!scripts._isEnemy) {
  286. ObjectPools.playAudioEngineDirection(scripts._Index);
  287. // app.myDishuPool[scripts._Index] = scripts;
  288. // console.log("地鼠出生", "出生类型", this._name, "出生点是", Index,"当前地鼠池样子是",app.myDishuPool);
  289. // console.log("现在数字" + Index );
  290. // console.log("假定0" + "左" );
  291. // console.log("假定1" + "中" );
  292. // console.log("假定2" + "右" );
  293. }else{
  294. // app.hisDishuPool[scripts._Index] = scripts;
  295. }
  296. // scripts.缩头();
  297. // console.log("现在是有");
  298. scripts.animationDisplay.timeScale = scripts.conf.mid.timeScale;
  299. if (scripts._type == 1) {
  300. scripts.PlayDragonBonesNotEvent(scripts.animationDisplay, "cry" + scripts._name, null, 0);
  301. }else {
  302. scripts.PlayDragonBonesNotEvent(scripts.animationDisplay, "idle" + scripts._name, null, 0);
  303. }
  304. scripts.canClick = true;
  305. scripts.伸头();
  306. }else{
  307. // console.log("现在是没有");
  308. }
  309. // console.log("现在上来了",scripts._name,scripts._type,"现在动画名字是",scripts.animationDisplay._animationName);
  310. // scripts.PlayDragonBones(scripts.animationDisplay, "idle" + scripts._name, scripts.默认状态, 1);
  311. },
  312. // 伸头后倒计时 2秒后自动缩头
  313. 伸头() {
  314. var self = this;
  315. // console.log("我现在要缩头了",this._name,this._type);
  316. this.scheduleOnce(function () {
  317. if (this.canClick) {
  318. // console.log("ceshi");
  319. this.canClick = false;
  320. this.unscheduleAllCallbacks();
  321. if (!this._isEnemy) {
  322. // ObjectPools.playSimpleAudioEngine(5);
  323. }
  324. // if (!this._isEnemy) {
  325. // if (manager_android_js != null) {
  326. // manager_android_js.mydishu = null;
  327. // }
  328. // }
  329. self.回收(0);
  330. if (!this._isEnemy) {
  331. this.lose();
  332. }
  333. }
  334. }, this.conf.mid.time);
  335. },
  336. 击中() {
  337. this.node.stopAllActions();
  338. this.unscheduleAllCallbacks();
  339. var names;
  340. if (this._name == "") {
  341. names = "21";
  342. } else {
  343. names = 20 + parseInt(this._name) + 1;
  344. }
  345. this.PlayDragonBones(this.animationDisplay, "attack" + names, this.默认状态, 1);
  346. // this.PlayDragonBones(this.animationDisplay, "yun" + this._name, this.默认状态, 1);
  347. this.schedule(function () {
  348. this.连击();
  349. }.bind(this), 0.1)
  350. },
  351. 左击() {
  352. this.node.stopAllActions();
  353. this.unscheduleAllCallbacks();
  354. var names;
  355. if (this._name == "") {
  356. names = "11";
  357. } else {
  358. names = 10 + parseInt(this._name) + 1;
  359. }
  360. this.PlayDragonBones(this.animationDisplay, "attack" + names, this.默认状态, 1, -1);
  361. this.schedule(function () {
  362. this.连击();
  363. }.bind(this), 0.1)
  364. },
  365. 右击() {
  366. this.node.stopAllActions();
  367. this.unscheduleAllCallbacks();
  368. var names;
  369. if (this._name == "") {
  370. names = "11";
  371. } else {
  372. names = 10 + parseInt(this._name) + 1;
  373. }
  374. this.PlayDragonBonesNotEvent(this.animationDisplay, "attack" + names, this.默认状态, 1);
  375. this.schedule(function () {
  376. this.连击();
  377. }.bind(this), 0.1)
  378. },
  379. 连击() {
  380. this.surplusCount--;
  381. this.clickCount++;
  382. // console.log("我这边的打击", this._isEnemy);
  383. if (this.node.parent != null) {
  384. if (!this._isEnemy) {
  385. // console.log("播放连击动画中","进来了吗",Statistics.gethit(0));
  386. if (Statistics.gethit(0) >= 3) {
  387. if (this.playquan == 0) {
  388. this.playquan++;
  389. // ObjectPools.playSimpleAudioEngine(4);
  390. Games.连击播放(this.node.parent.parent.position, Games.连击动画, Statistics.gethit(0));
  391. // console.log("播放连击动画中", Statistics.gethit(0));
  392. }
  393. }
  394. }
  395. }
  396. this.挂掉();
  397. // if (this.surplusCount == 0) {
  398. // this.挂掉();
  399. // }
  400. },
  401. 隐藏拳套蓝(event) {
  402. Games.拳套蓝.position = cc.Vec2.ZERO;
  403. },
  404. 隐藏拳套(event) {
  405. Games.拳套.position = cc.Vec2.ZERO;
  406. },
  407. 挂掉() {
  408. if (!this.canClick)
  409. return;
  410. // ObjectPools.playSimpleAudioEngine(2);
  411. this.canClick = false;
  412. // manager_android_js.boom();
  413. var randomIndex = parseInt(Math.random() * 2, 10);
  414. // if (randomIndex == 0) {
  415. //
  416. // this.PlayDragonBones(this.animationDisplay, "yun" + this._name, this.temps, 0.2);
  417. // } else {
  418. // this.PlayDragonBones(this.animationDisplay, "cry" + this._name, this.temps, 0.2);
  419. // }
  420. this.PlayDragonBones(this.animationDisplay, "yun" + this._name, this.temps, 0.2);
  421. // Log.info("打死一个地鼠")
  422. this.回收(1);
  423. },
  424. temps : function(){
  425. },
  426. 回收(type) {
  427. // if (!this._isEnemy) {
  428. // if (manager_android_js != null) {
  429. // manager_android_js.mydishu = null;
  430. // }
  431. // }
  432. //0自然 死亡 1 被打死亡
  433. var scripts = this.node.getComponent("dishu");
  434. if (type == 0) {
  435. if (scripts._type == 0) {
  436. Games.掉血(scripts.目标父级, scripts.reduceBloodCount);
  437. }
  438. }else if (type == 1) {
  439. if (scripts._type == 1) {
  440. Games.掉血(scripts.目标父级, scripts.reduceBloodCount);
  441. }
  442. }
  443. if (testUI) {
  444. testUI.showDishu(this._Index,false);
  445. }
  446. var self = this;
  447. var finished = cc.callFunc(function () {
  448. self.node.stopAllActions();
  449. self.removeDishu();
  450. setTimeout(function () {
  451. ObjectPools.地鼠回收(self.node);
  452. // Games.随机生成(self.父级);
  453. }.bind(this), 1);
  454. });
  455. this.缩头动作 = cc.sequence(cc.moveBy(this.conf.lower.time, cc.v2(0, -150)), finished);
  456. this.node.runAction(this.缩头动作);
  457. },
  458. //删除下去的地鼠
  459. removeDishu : function(){
  460. if (this.isMy()) {
  461. app.myDishuPool[this._Index] = null;
  462. }else{
  463. app.hisDishuPool[this._Index] = null;
  464. }
  465. },
  466. PlayDragonBones(animationDisplays, newAnimation, completeCallback, playTimes, scaleX = 1) { //动态加载龙骨
  467. animationDisplays.playAnimation(newAnimation, playTimes);
  468. this.node.scale = new cc.Vec2(scaleX, 1);
  469. animationDisplays.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback, animationDisplays);//.apply(this));
  470. },
  471. PlayDragonBonesNotEvent(animationDisplays, newAnimation, completeCallback, playTimes, scaleX = 1) { //动态加载龙骨
  472. animationDisplays.playAnimation(newAnimation, playTimes);
  473. this.node.scale = new cc.Vec2(scaleX, 1);
  474. // animationDisplays.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback, animationDisplays);//.apply(this));
  475. },
  476. PlayDragonBonesRemoveEvent(animationDisplays,completeCallback, scaleX = 1) { //动态加载龙骨
  477. // animationDisplays.playAnimation(newAnimation, playTimes);
  478. this.node.scale = new cc.Vec2(scaleX, 1);
  479. animationDisplays.removeEventListener(dragonBones.EventObject.COMPLETE, completeCallback, animationDisplays);//.apply(this));
  480. },
  481. 击打次数(index) {
  482. var count;
  483. // idle:普通 idle1:安全帽 idel2:海盗帽 idle:南瓜帽
  484. switch (index) {
  485. case 1:
  486. count = 1;
  487. break;
  488. case 2:
  489. count = 1;
  490. break;
  491. case 3:
  492. count = 1;
  493. break;
  494. case 4:
  495. count = 1;
  496. break;
  497. }
  498. return count;
  499. },
  500. });