UICrazyAwardView.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import auto_crazyAwardView from "../../../ui/uidata/Interface/auto_crazyAwardView";
  2. import UIBase from "../../../framework/ui/UIBase";
  3. import UIHelp from "../../../framework/ui/UIHelp";
  4. import PlayerView from "../../../gameLogic/gameObject/player/playerView";
  5. import PlayerObject from "../../../gameLogic/gameObject/player/playerObject";
  6. import Utils from "../../../framework/utils/utils";
  7. import ZomBieObject from "../../../gameLogic/gameObject/zombie/zombieObject";
  8. import BulletObject from "../../../gameLogic/gameObject/bullet/bulletObject";
  9. import gameData from "../../../gameLogic/utrl/gameData";
  10. import gameEventManager from "../../../gameLogic/utrl/gameEventManager";
  11. import {EVENT_TYPE, FALLITEM_TYPE, ITEM_TYPE} from "../../../gameLogic/utrl/gameEnum";
  12. import UIObtainView from "./UIObtainView";
  13. import UIMng from "../../../framework/ui/UIMng";
  14. import UIGameScene from "../scene/UIGameScene";
  15. import EventManager from "../../../framework/event/EventManager";
  16. import tween = cc.tween;
  17. import AccountModel from "../../../data/Account/AccountModel";
  18. const {ccclass, menu, property} = cc._decorator;
  19. @ccclass
  20. @menu("UI/Interface/UICrazyAwardView")
  21. export default class UICrazyAwardView extends UIBase {
  22. ui: auto_crazyAwardView = null;
  23. protected static prefabUrl = "Interface/crazyAwardView";
  24. protected static className = "UICrazyAwardView";
  25. @property(cc.Prefab)
  26. heroPrefab: cc.Prefab = null;
  27. @property(cc.Prefab)
  28. zombiePrefab: cc.Prefab = null;
  29. @property(cc.Prefab)
  30. bulletPrefab: cc.Prefab = null;
  31. playerObjects: PlayerObject [] = [];
  32. zomBieObjects: ZomBieObject [] = [];
  33. lastClickTime = -1;
  34. nowClickTIme = -1;
  35. curProgress = 100;
  36. isShowBanner = false;
  37. bannerClick = false;
  38. triggerCloseSecond = -1;
  39. bannerType = 0;
  40. temp = 0;
  41. isClosing = false;
  42. isADShowed = false;
  43. onUILoad() {
  44. this.ui = this.node.addComponent(auto_crazyAwardView);
  45. AccountModel.getInstance().show_crazy_in_level ++;
  46. UIMng.getInstance().setLock(true)
  47. }
  48. onShow() {
  49. tween(this.ui.kdzl)
  50. .repeatForever(
  51. cc.tween(this.ui.kdzl)
  52. .to(0.8,{scale:0.5,opacity:0})
  53. .to(0.8,{scale:1,opacity:255})
  54. )
  55. .start();
  56. tween(this.ui.fxtsjs_btn)
  57. .repeatForever(
  58. cc.tween(this.ui.fxtsjs_btn)
  59. .to(0.8,{scale:0.8})
  60. .to(0.8,{scale:1})
  61. )
  62. .start();
  63. tween(this.ui.point)
  64. .repeatForever(
  65. cc.tween(this.ui.point)
  66. .to(0.8,{opacity:0})
  67. .to(0.8,{opacity:255})
  68. )
  69. .start();
  70. this.bannerType = Utils.random(0, 2); // 随机一个banner的显示方案
  71. for (let i = 1; i <= 4; i++) {
  72. this.randomHero(i);
  73. }
  74. this.randomZombie(1);
  75. }
  76. randomZombie(index: number) {
  77. let id = 0;
  78. // let isBoss = false;
  79. // if (Utils.random(0, 1)) {
  80. id = Utils.random(53, 56);
  81. if (id == 54){
  82. id = 56
  83. }
  84. // }else {
  85. // id = Utils.random(22, 48);
  86. // }
  87. let nd = cc.instantiate(this.zombiePrefab);
  88. // if (isBoss){
  89. nd.scale = 0.8;
  90. // }
  91. let comp = nd.getComponent(ZomBieObject);
  92. this.zomBieObjects.push(comp);
  93. this.ui[`zombieSlot${index}`].addChild(nd)
  94. nd.group = 'default';
  95. comp.init(id, cc.v3(0, 0, 0), [], false, true);
  96. this.scheduleOnce(() => {
  97. nd.setPosition(cc.v3(0, 0, 0));
  98. comp.cur_hp_bar.parent.active = false;
  99. })
  100. }
  101. private randomHero(index: number) {
  102. let id = Utils.random(1, 9);
  103. let nd = cc.instantiate(this.heroPrefab);
  104. let playerViewCom = nd.getChildByName('view').getComponent(PlayerView);
  105. let playerObjCom = nd.getComponent(PlayerObject);
  106. this.playerObjects.push(playerObjCom);
  107. playerObjCom.init(id, []);
  108. nd.group = 'default';
  109. nd.scaleX *= -1;
  110. this.scheduleOnce(() => {
  111. nd.setPosition(cc.v3(0, 0, 0));
  112. const randomWeapon = 41;
  113. playerObjCom.player_prop.setWeapon(randomWeapon, false);
  114. playerObjCom.player_prop.cur_weapon = playerObjCom.player_prop.remote_weapon;
  115. playerViewCom.init(id);
  116. this.scheduleOnce(() => {
  117. playerViewCom.playIdle(randomWeapon)
  118. },0)
  119. }, 0);
  120. // playerObjCom.setLeader(false);
  121. this.ui[`heroSlot${index}`].addChild(nd)
  122. }
  123. onHide() {
  124. }
  125. onStart() {
  126. }
  127. update(dt) {
  128. let speed = 60 * dt;
  129. let subNum = this.curProgress + speed;
  130. this.curProgress = subNum < 100 ? subNum : 100;
  131. this.ui.nodeProgress.getComponent(cc.Sprite).fillRange = this.curProgress / 100;
  132. if (this.triggerCloseSecond !== -1 && this.triggerCloseSecond > 0) {
  133. this.triggerCloseSecond -= dt;
  134. if (this.triggerCloseSecond <= 0) {
  135. if (this.bannerClick) {
  136. if (this.isADShowed) {
  137. zjSdk?.hideBannerAd();
  138. this.toClose();
  139. }
  140. } else {
  141. this.triggerCloseSecond = -1
  142. this.isShowBanner = false;
  143. zjSdk?.hideBannerAd();
  144. this.toClose();
  145. }
  146. }
  147. }
  148. }
  149. toClose() {
  150. this.isClosing = true;
  151. this.schedule(() => {
  152. this.clickCrazy();
  153. }, 0.1)
  154. }
  155. clickCrazy() {
  156. this.heroAttack();
  157. // 存储上一次点击时间与当前点击时间
  158. const clickTime = new Date().getTime() / 1000;
  159. if (this.lastClickTime <= 0) {
  160. this.lastClickTime = clickTime;
  161. } else {
  162. this.lastClickTime = this.nowClickTIme;
  163. }
  164. this.nowClickTIme = clickTime;
  165. const addProgress = Utils.random(12, 15);
  166. this.curProgress -= addProgress;
  167. if (this.curProgress < 0) {
  168. this.curProgress = 0;
  169. }
  170. this.ui.nodeProgress.getComponent(cc.Sprite).fillRange = this.curProgress / 100;
  171. this.checkShow();
  172. }
  173. checkShow() {
  174. if (this.curProgress == 0) {
  175. this.reword();
  176. return;
  177. }
  178. if (this.isShowBanner) {
  179. return;
  180. }
  181. if (this.isClosing) {
  182. return;
  183. }
  184. // 每次点击均有50%的几率出现banner
  185. if (this.bannerType === 0) {
  186. if (Math.random() <= 0.5) {
  187. this.showBanner();
  188. }
  189. return;
  190. }
  191. // 进度条到达20%出现banner
  192. if (this.bannerType === 1) {
  193. if (this.curProgress < 20) {
  194. this.showBanner();
  195. }
  196. return;
  197. }
  198. // 阶段点击按钮时显示banner的概率
  199. if (this.bannerType === 2) {
  200. if (this.curProgress < 25 && Math.random() < 0.75) {
  201. this.showBanner();
  202. } else if (this.curProgress < 50 && Math.random() < 0.5) {
  203. this.showBanner();
  204. } else if (this.curProgress < 75 && Math.random() < 0.25) {
  205. this.showBanner();
  206. } else {
  207. this.showBanner();
  208. }
  209. return;
  210. }
  211. // 两次点击间隔小于0.3秒时出现banner
  212. if (this.bannerType === 3) {
  213. if (this.nowClickTIme !== this.lastClickTime && this.nowClickTIme - this.lastClickTime < 0.31) {
  214. this.showBanner();
  215. }
  216. return;
  217. }
  218. }
  219. private heroAttack() {
  220. for (let i = 0; i < 4; i++) {
  221. this.fire(this.playerObjects[i], i);
  222. }
  223. }
  224. private reword() {
  225. gameData.curPowerNum += 100;
  226. gameData.curBulletNum += 100;
  227. const randomId = Utils.random(1, 10);
  228. gameEventManager.emit(EVENT_TYPE.ADD_HERO_BY_ID, randomId);
  229. this.clickClose();
  230. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,true)
  231. EventManager.once(EVENT_TYPE.closeObtain, () => {
  232. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS,false)
  233. })
  234. UIHelp.ShowUI(UIObtainView, null, {
  235. ids: [FALLITEM_TYPE.qiaokeli, FALLITEM_TYPE.zidan1],
  236. nums: [100, 100],
  237. modelIds: [randomId]
  238. })
  239. }
  240. private fire(playerObject: PlayerObject, i) {
  241. let b = cc.instantiate(this.bulletPrefab);
  242. this.node.addChild(b);
  243. let com = b.getComponent(BulletObject);
  244. com.max_dis = 400;
  245. const pos = playerObject.node.position;
  246. pos.x += this.ui.heroContent.x;
  247. pos.y += this.ui.heroContent.y;
  248. pos.x += this.ui[`heroSlot${i + 1}`].x;
  249. pos.y += this.ui[`heroSlot${i + 1}`].y;
  250. let red = 0;
  251. if (i == 0) {
  252. red = -0.1;
  253. } else if (i == 3) {
  254. red = 0.1;
  255. }
  256. com.init(pos.add(cc.v3(0, 30, 0)), red, 50, playerObject);
  257. com.destroyCall = () => {
  258. const zombieView = this.zomBieObjects[0].zombie_view;
  259. if (zombieView) {
  260. zombieView.hitEff();
  261. }
  262. }
  263. }
  264. clickClose() {
  265. gameEventManager.emit(EVENT_TYPE.CHANGE_STATUS, false);
  266. this.onClose();
  267. }
  268. showBanner() {
  269. this.isShowBanner = true;
  270. zjSdk?.showBannerAd({
  271. adPlaceName: '大按钮狂点',
  272. success: () => {
  273. this.isADShowed = true;
  274. },
  275. fail: (err) => {
  276. this.isADShowed = true;
  277. },
  278. }, (isStateClick) => {
  279. this.bannerClick = isStateClick;
  280. });
  281. this.triggerCloseSecond = 2;
  282. }
  283. onClose() {
  284. zjSdk?.hideBannerAd();
  285. cc.Canvas.instance.scheduleOnce(() => {
  286. UIMng.getInstance().setLock(false)
  287. }, 1)
  288. UIHelp.CloseUI(UICrazyAwardView);
  289. }
  290. }