Wealth.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import { WealthData } from "../../datas/WealthData";
  2. import Main from "../../Main";
  3. import AdM from "../../manager/AdM";
  4. import GameM, { AUDIO_TYPE } from "../../manager/GameM";
  5. import UiM from "../../manager/UiM";
  6. import WealthMng, { WealthState } from "../../manager/WealthMng";
  7. import MyExtends from "../../tools/MyExtends";
  8. import Time from "../../tools/Time";
  9. import EffectNode from "../../ui/EffectNode";
  10. import UIMng, { PanelType } from "../../uiFrames/UIMng";
  11. import Sciencen_M from "../../utils/Sciencen_M";
  12. const { ccclass, property } = cc._decorator;
  13. export enum WealthFightState {
  14. normal, //正常
  15. shielding, //金钟罩使用中
  16. damaging, //受伤中
  17. skill1ing, //使用技能 眩晕技能中
  18. }
  19. @ccclass
  20. export default class Wealth extends cc.Component {
  21. @property(cc.Node)
  22. cloud: cc.Node = null;
  23. @property(sp.Skeleton)
  24. wealthSpine: sp.Skeleton = null;
  25. @property(sp.Skeleton)
  26. shield: sp.Skeleton = null;
  27. @property(cc.Node)
  28. sliderNode: cc.Node = null;
  29. @property(cc.Sprite)
  30. slider: cc.Sprite = null;
  31. @property(cc.Label)
  32. txt: cc.Label = null;
  33. @property(cc.Label)
  34. txtTimer: cc.Label = null;
  35. @property(cc.Node)
  36. bulletShield: cc.Node = null;
  37. //@property(cc.Collider)
  38. wealthCollider: cc.PhysicsCollider = null;
  39. private fightState: WealthFightState = WealthFightState.normal;
  40. private isShielding: boolean = false;
  41. private isCanAttack: boolean = false;
  42. private playerAttackTimer: number = 0;
  43. private isDamaging: boolean = false;
  44. private isFightEnd: boolean = true;
  45. onLoad() {
  46. //console.log("_______________");
  47. this.wealthCollider = this.node.getComponent(cc.PhysicsCollider);
  48. this.wealthCollider.enabled = false;
  49. this.shield.node.active = false;
  50. //console.log("Collider:", this.wealthCollider);
  51. }
  52. start() {
  53. //console.log("_______________2");
  54. }
  55. update(dt) {
  56. this.TimerShield(dt);
  57. this.TimerAttack(dt);
  58. this.TimerWealthDuration(dt);
  59. }
  60. TriggerWealth(pos: cc.Vec2, callback: Function) {
  61. this.isDamaging = false;
  62. this.playerAttackTimer = 0;
  63. this.fightState = WealthFightState.normal;
  64. this.sliderNode.active = false;
  65. //this.node.setPosition(610, 500);
  66. this.node.setPosition(MyExtends.convetOtherNodeSpaceAR(UiM.Instance.hallNode.getComponent(Main).wealthBtn, this.node.parent));
  67. this.cloud.parent = this.node;
  68. this.cloud.setSiblingIndex(0);
  69. this.cloud.setPosition(0, -172);
  70. this.cloud.active = true;
  71. //this.UnUseShield();
  72. this.node.scale = 0.4;
  73. this.node.active = true;
  74. this.wealthSpine.setAnimation(0, "bainian", true);
  75. GameM.audioM.playEffect(AUDIO_TYPE.wealthlaugh, false);
  76. //let posBtn = MyExtends.convetOtherNodeSpaceAR(UiM.Instance.hallNode.getComponent(Main).wealthBtn, this.node.parent);
  77. cc.tween(this.node)
  78. .to(1, { position: cc.v2(-540, 310), scale: 0.75 })
  79. .to(1, { position: cc.v2(222.454, 160), scale: 1 })
  80. .call(() => {
  81. this.cloud.parent = this.node.parent;
  82. this.cloud.setPosition(this.node.x, this.node.y + this.cloud.y);
  83. this.cloud.setSiblingIndex(this.node.getSiblingIndex());
  84. //this.node.getSiblingIndex
  85. cc.tween(this.cloud).to(0.5, { position: cc.v2(this.cloud.x + 500, this.cloud.y) }).call(() => { this.cloud.active = false; }).start();
  86. console.log("StartPos:(" + pos.x + "," + pos.y + ")");
  87. let a1 = cc.jumpTo(0.7, pos, 300, 1);
  88. let a2 = cc.callFunc(() => {
  89. this.isFightEnd = false;
  90. this.isCanAttack = true;
  91. callback();
  92. this.wealthSpine.setAnimation(0, "idle", true);
  93. MyExtends.ScreenShock(UiM.Instance.hallNode, 0.3);
  94. });
  95. this.node.runAction(cc.sequence(a1, a2));
  96. })
  97. .start();
  98. }
  99. /**切换为战斗状态*/
  100. SwitchFightingState(pos: cc.Vec2, callback: Function) {
  101. this.isDamaging = false;
  102. this.playerAttackTimer = 0;
  103. this.fightState = WealthFightState.normal;
  104. this.sliderNode.active = false;
  105. //this.node.setPosition(610, 500);
  106. this.node.setPosition(MyExtends.convetOtherNodeSpaceAR(UiM.Instance.hallNode.getComponent(Main).wealthBtn, this.node.parent));
  107. this.cloud.parent = this.node;
  108. this.cloud.setSiblingIndex(0);
  109. this.cloud.setPosition(0, -172);
  110. this.cloud.active = true;
  111. //this.UnUseShield();
  112. this.node.scale = 0.4;
  113. this.node.active = true;
  114. this.wealthSpine.setAnimation(0, "bainian", true);
  115. GameM.audioM.playEffect(AUDIO_TYPE.wealthlaugh, false);
  116. //let posBtn = MyExtends.convetOtherNodeSpaceAR(UiM.Instance.hallNode.getComponent(Main).wealthBtn, this.node.parent);
  117. cc.tween(this.node)
  118. .to(1, { position: cc.v2(-540, 310), scale: 0.75 })
  119. .to(1, { position: cc.v2(222.454, 160), scale: 1 })
  120. .call(() => {
  121. this.cloud.parent = this.node.parent;
  122. this.cloud.setPosition(this.node.x, this.node.y + this.cloud.y);
  123. this.cloud.setSiblingIndex(this.node.getSiblingIndex());
  124. //this.node.getSiblingIndex
  125. cc.tween(this.cloud).to(0.5, { position: cc.v2(this.cloud.x + 500, this.cloud.y) }).call(() => { this.cloud.active = false; }).start();
  126. //console.log("StartPos:(" + pos.x + "," + pos.y + ")");
  127. let a1 = cc.jumpTo(0.7, pos, 300, 1);
  128. let a2 = cc.callFunc(() => {
  129. this.isFightEnd = false;
  130. this.isCanAttack = true;
  131. callback();
  132. this.wealthSpine.setAnimation(0, "idle", true);
  133. //console.log("=++++++++++:" + WealthData.Ins.shieldTimer);
  134. if (WealthData.Ins.shieldTimer > 0) {
  135. this.fightState = WealthFightState.shielding;
  136. this.wealthCollider.enabled = true;
  137. this.wealthCollider.enabled = false;
  138. this.wealthCollider.enabled = true;
  139. this.shield.node.active = true;
  140. this.shield.setAnimation(0, "animation", true);
  141. //WealthData.Ins.shieldTimer = WealthData.Ins.shieldTime;
  142. this.isShielding = true;
  143. //let a: cc.RigidBody = this.getComponent(cc.RigidBody);
  144. //a.allowSleep = false;
  145. this.wealthSpine.setAnimation(0, "jinzhongzhao", true);
  146. } else {
  147. this.wealthSpine.setAnimation(0, "idle", true);
  148. }
  149. MyExtends.ScreenShock(UiM.Instance.hallNode, 0.3);
  150. });
  151. this.node.runAction(cc.sequence(a1, a2));
  152. })
  153. .start();
  154. /*
  155. this.isDamaging = false;
  156. this.playerAttackTimer = 0;
  157. this.fightState = WealthFightState.normal;
  158. this.node.setPosition(pos);
  159. this.node.active = true;
  160. this.isFightEnd = false;
  161. this.isCanAttack = true;
  162. callback();
  163. if (WealthData.Ins.shieldTimer > 0) {
  164. console.log("----------0");
  165. this.fightState = WealthFightState.shielding;
  166. this.wealthCollider.enabled = true;
  167. this.wealthCollider.enabled = false;
  168. this.wealthCollider.enabled = true;
  169. this.shield.active = true;
  170. //WealthData.Ins.shieldTimer = WealthData.Ins.shieldTime;
  171. this.isShielding = true;
  172. //let a: cc.RigidBody = this.getComponent(cc.RigidBody);
  173. //a.allowSleep = false;
  174. this.wealthSpine.setAnimation(0, "jinzhongzhao", true);
  175. } else {
  176. this.wealthSpine.setAnimation(0, "idle", true);
  177. }
  178. */
  179. }
  180. UpdateTxt(curblood: string, totalblood: string) {
  181. if (!this.sliderNode.active) this.sliderNode.active = true;
  182. //console.log("AAA:" + curblood + " BBB:" + totalblood);
  183. this.slider.fillRange = 1 - Number(Sciencen_M.instance.accDiv(curblood, totalblood));
  184. //this.txt.string = Sciencen_M.instance.format(Sciencen_M.instance.subtraction(totalblood, curblood)) + "/" + Sciencen_M.instance.format(totalblood);
  185. //this.txt.string = Math.floor(((1 - Number(Sciencen_M.instance.accDiv(curblood, totalblood))) * 100)) + "%";
  186. this.txt.string = ((1 - Number(Sciencen_M.instance.accDiv(curblood, totalblood))) * 100).toFixed(2) + "%";
  187. }
  188. async GetDamage() {
  189. if (!this.isDamaging) {
  190. if (this.fightState != WealthFightState.skill1ing) {
  191. this.isDamaging = true;
  192. this.fightState = WealthFightState.damaging;
  193. this.wealthSpine.setAnimation(0, "shouji", false);
  194. await Time.WaitForSeconds(this.wealthSpine.findAnimation("shouji").duration);
  195. this.isDamaging = false;
  196. this.fightState = WealthFightState.normal;
  197. if (!this.isFightEnd) {
  198. this.wealthSpine.setAnimation(0, "idle", true);
  199. }
  200. }
  201. }
  202. }
  203. /**使用眩晕技能*/
  204. async UseSkill1(targetpos: cc.Vec2, callback: Function) {
  205. if (this.isFightEnd) return;
  206. this.fightState = WealthFightState.skill1ing;
  207. this.wealthSpine.setAnimation(0, "gongjiyubei", false);
  208. await Time.WaitForSeconds(this.wealthSpine.findAnimation("gongjiyubei").duration);
  209. this.fightState = WealthFightState.normal;
  210. this.wealthSpine.setAnimation(0, "idle", true);
  211. //let a1 = cc.jumpTo(0.3, this.node.getPosition(), 80, 1);
  212. let a1 = cc.jumpTo(0.3, targetpos, 80, 1);
  213. let a2 = cc.callFunc(callback);
  214. this.node.runAction(cc.sequence(a1, a2));
  215. }
  216. UseShield() {
  217. if (!this.isShielding) {
  218. this.fightState = WealthFightState.shielding;
  219. this.wealthCollider.enabled = true;
  220. this.wealthCollider.enabled = false;
  221. this.wealthCollider.enabled = true;
  222. //this.wealthCollider.enabled = false;
  223. this.shield.node.active = true;
  224. this.shield.setAnimation(0, "animation", true);
  225. WealthData.Ins.shieldTimer = WealthData.Ins.shieldTime;
  226. this.isShielding = true;
  227. //let a: cc.RigidBody = this.getComponent(cc.RigidBody);
  228. //a.allowSleep = false;
  229. this.wealthSpine.setAnimation(0, "jinzhongzhao", true);
  230. AdM.onSendEvent('caishen_skill_open', '财神金钟罩技能触发', 'caishen');
  231. WealthData.Ins.UpdateWealthState();
  232. }
  233. }
  234. async UnUseShield() {
  235. this.isShielding = false;
  236. WealthData.Ins.shieldTimer = 0;
  237. //console.log("Collider2:", this.node.getComponent(cc.Collider));
  238. this.shield.setAnimation(0, "animation2", false);
  239. await Time.WaitForSeconds(this.shield.findAnimation("animation2").duration);
  240. this.wealthCollider.enabled = false;
  241. this.shield.node.active = false;
  242. //let a: cc.RigidBody = this.getComponent(cc.RigidBody);
  243. //a.allowSleep = true;
  244. this.wealthSpine.setAnimation(0, "idle", true);
  245. WealthData.Ins.UpdateWealthState();
  246. }
  247. /**财神被击败*/
  248. WealthEnd(isSuccess: boolean) {
  249. this.isFightEnd = true;
  250. this.isShielding = false;
  251. WealthData.Ins.shieldTimer = WealthData.Ins.shieldTime;
  252. this.fightState = WealthFightState.normal;
  253. this.shield.node.active = false;
  254. this.sliderNode.active = false;
  255. if (!isSuccess) {
  256. this.cloud.parent = this.node;
  257. this.cloud.setSiblingIndex(0);
  258. this.cloud.setPosition(530, -172);
  259. //this.cloud.setPosition(0, -172);
  260. this.cloud.active = true;
  261. this.wealthSpine.setAnimation(0, "bainian", true);
  262. cc.tween(this.cloud)
  263. .to(0.5, { position: cc.v2(0, this.cloud.y) })
  264. .call(() => {
  265. cc.tween(this.node)
  266. .to(1, { position: cc.v2(-540, 100), scale: 0.85 })
  267. .call(() => {
  268. this.node.setPosition(-540, 520);
  269. let pos = MyExtends.convetOtherNodeSpaceAR(UiM.Instance.hallNode.getComponent(Main).wealthBtn, this.node.parent);
  270. cc.tween(this.node)
  271. .to(1, { position: pos, scale: 0.4 })
  272. .call(() => {
  273. this.node.active = false;
  274. //let wealthbtn = UiM.Instance.hallNode.getComponent(Main).wealthBtn;
  275. //wealthbtn.active = true;
  276. //wealthbtn.runAction(cc.fadeIn(0.3));
  277. })
  278. .start();
  279. })
  280. .start();
  281. })
  282. .start();
  283. }
  284. else {
  285. let pos = WealthMng.Ins.GetUiWealthPos();
  286. console.log("Pos::" + "(" + pos.x + "," + pos.y + ")");
  287. let a1 = cc.spawn(cc.jumpTo(1, pos, 120, 1), cc.scaleTo(1, 1.3));
  288. let a2 = cc.callFunc(() => {
  289. this.node.active = false;
  290. let wealthbtn = UiM.Instance.hallNode.getComponent(Main).wealthBtn;
  291. wealthbtn.active = true;
  292. wealthbtn.runAction(cc.fadeIn(0.3));
  293. if (UIMng.Ins.IsExistPanel(PanelType.WealthPanel)) {
  294. UIMng.Ins.GetPanel(PanelType.WealthPanel).OnExit();
  295. }
  296. console.log("-------4");
  297. UIMng.Ins.AsyncGetPanel(PanelType.WealthPanel, (panel) => {
  298. panel.OnEnter(4);
  299. });
  300. //UIMng.Ins.GetPanel(PanelType.WealthPanel).OnEnter(4);
  301. });
  302. this.node.runAction(cc.sequence(a1, a2));
  303. //cc.tween(this.node)
  304. // .to(1, { position: pos, scale: 1.3 })
  305. // .call(() => {
  306. // this.node.active = false;
  307. // let wealthbtn = UiM.Instance.hallNode.getComponent(Main).wealthBtn;
  308. // wealthbtn.active = true;
  309. // wealthbtn.runAction(cc.fadeIn(0.3));
  310. // UIMng.Ins.GetPanel(PanelType.WealthPanel).OnEnter(4);
  311. // })
  312. // .start();
  313. }
  314. }
  315. FightEnd() {
  316. //this.UnUseShield();
  317. }
  318. TimerShield(dt) {
  319. if (this.isShielding) {
  320. if (WealthMng.Ins.wealthState == WealthState.Fighting) {
  321. WealthData.Ins.shieldTimer -= dt;
  322. if (WealthData.Ins.shieldTimer <= 0) {
  323. this.UnUseShield();
  324. }
  325. } else {
  326. this.UnUseShield();
  327. }
  328. //if(WealthMng.Instance.wealthState==WealthState.)
  329. }
  330. }
  331. TimerAttack(dt) {
  332. if (!this.isFightEnd) {
  333. if (WealthMng.Ins.wealthState == WealthState.Fighting) {
  334. if (!this.isCanAttack) {
  335. this.playerAttackTimer += dt;
  336. if (this.playerAttackTimer > 0.1) {
  337. this.isCanAttack = true;
  338. }
  339. }
  340. }
  341. //if(WealthMng.Instance.wealthState==WealthState.)
  342. }
  343. }
  344. TimerWealthDuration(dt) {
  345. if (this.sliderNode.active) {
  346. this.txtTimer.string = MyExtends.TimeToFormat(WealthData.Ins.lifeTimer);
  347. }
  348. }
  349. async CreateBulletEft(info: cc.WorldManifold) {
  350. let eft = cc.instantiate(this.bulletShield);
  351. eft.active = true;
  352. eft.parent = this.node;
  353. eft.angle = cc.v2(0, 1).signAngle(info.normal) * 180 / Math.PI - 90;
  354. let pos = MyExtends.worldConvertLocalPointAR2D(this.node, info.points[0]);
  355. //console.log("Pos:",pos);
  356. eft.setPosition(pos);
  357. await Time.WaitForSeconds(0.5);
  358. eft.destroy();
  359. }
  360. //onCollisionEnter(other, self) {
  361. // //console.log("collider self:", self);
  362. // other.node.destroy();
  363. //}
  364. onBeginContact(contact, selfCollider, otherCollider) {
  365. //console.log("contact self:", contact.getWorldManifold());
  366. if (this.wealthCollider.enabled) {
  367. this.CreateBulletEft(contact.getWorldManifold());
  368. otherCollider.node.destroy();
  369. }
  370. }
  371. Click_WealthBtn() {
  372. if (!this.isFightEnd) {
  373. if (!this.isShielding) {
  374. if (this.isCanAttack) {
  375. this.isCanAttack = false;
  376. //console.log("click the wealth");
  377. WealthMng.Ins.GetDamage(2);
  378. if (WealthData.Ins.CheckShield()) {
  379. this.UseShield();
  380. } else {
  381. WealthMng.Ins.CheckUseSkill1();
  382. }
  383. }
  384. } else {
  385. console.log("-------3");
  386. GameM.audioM.playEffect(AUDIO_TYPE.wealthshield, false);
  387. UIMng.Ins.AsyncGetPanel(PanelType.WealthPanel, (panel) => {
  388. panel.OnEnter(3);
  389. });
  390. //UIMng.Ins.GetPanel(PanelType.WealthPanel).OnEnter(3);
  391. }
  392. AdM.onSendEvent('caishen_dianji', '财神点击次数');
  393. }
  394. }
  395. Click_WealthBowl() {
  396. console.log("-------7");
  397. UIMng.Ins.AsyncGetPanel(PanelType.WealthPanel, (panel) => {
  398. panel.OnEnter(7);
  399. });
  400. //UIMng.Ins.GetPanel(PanelType.WealthPanel).OnEnter(7);
  401. }
  402. }