weapon.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { cocosz } from "../Framework/CocosZ";
  2. import Constant from "../Framework/Constant";
  3. import Msg from "../Framework/Msg";
  4. import GameDate from "./gameDate";
  5. import { gameMgr } from "./gameMgr";
  6. import Person from "./person";
  7. // @ts-ignore
  8. const i18n = require('LanguageData');
  9. const { ccclass, property } = cc._decorator;
  10. export enum WeaponType {
  11. weapon_melee,
  12. weapon_range,
  13. weapon_rangeAd
  14. }
  15. @ccclass
  16. export default class Weapon extends cc.Component {
  17. public static readonly WeaponName = ["ak", "cfq", "dao", "gj", "jgb", "sd", "hdl", "sq", "ju", "ld", "nnp", "gtst", "tb", "mb", "mq", "szg", "rsq", "cjj", "jtl", "sq2", "tj", "fs"]
  18. public static readonly meleeWaapon = [3, 4, 5, 13, 14];
  19. public static readonly rangeWeapon = [1, 2, 6, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 20, 21, 22];
  20. @property({ tooltip: "是否播放开火升级效果" })
  21. can_effect_hit: boolean = false;
  22. @property(cc.Node)
  23. hand_effect: cc.Node = null;
  24. @property(cc.Node)
  25. hand_2: cc.Node = null;
  26. @property
  27. weaponNum: number = 1;
  28. @property(cc.Prefab)
  29. bullet: cc.Prefab = null;
  30. @property(cc.Color)
  31. bulletCollor: cc.Color = cc.Color.WHITE;
  32. @property(cc.Prefab)
  33. shellCall: cc.Prefab = null;
  34. @property(cc.Prefab)
  35. atkEffect: cc.Prefab = null;
  36. @property
  37. atkNum: number = 10;
  38. @property
  39. atkRangeNum: number = 1000;
  40. @property
  41. flySpeed: number = 2000;
  42. @property({ tooltip: "开火时间" })
  43. @property
  44. atkSpeed: number = 0.4;
  45. @property({ tooltip: "弹夹弹药数量" })
  46. bulletNum: number = 5;
  47. @property({ tooltip: "总弹药数量" })
  48. bulletTotal: number = 5;
  49. @property({ tooltip: "装弹时间" })
  50. reload: number = 3;
  51. @property({ tooltip: "装弹音效" })
  52. audioName: string = "read";
  53. weaponLevel: number = 0;
  54. weaponType: WeaponType = WeaponType.weapon_melee;
  55. // 武器拥有者
  56. public person: Person = null;
  57. // 是否是远程武器
  58. public get isRangeWeapon(): boolean {
  59. return Weapon.rangeWeapon.includes(this.weaponNum);
  60. }
  61. // 子弹
  62. private _curBullet: number = 0;
  63. public get curBullet(): number {
  64. return this._curBullet;
  65. }
  66. public set curBullet(v: number) {
  67. if (v > this.bulletNum) { v = this.bulletNum; }
  68. else if (v < 0) { v = 0; }
  69. // 远程武器
  70. if (this.isRangeWeapon) {
  71. this._curBullet = v;
  72. // 装弹
  73. if (this._curBullet <= 0) {
  74. this.reloadBullet();
  75. }
  76. // 设置子弹UI
  77. this.setBulletUI();
  78. }
  79. }
  80. protected onLoad(): void {
  81. // 等级
  82. if (!this.person || this.person.isPlayer) {
  83. this.weaponLevel = cocosz.dataMgr.getGunInfo(this.weaponNum - 1).Level;
  84. } else if ([1, 2, 3, 4, 5].includes(cocosz.gameMode)) {
  85. this.weaponLevel = Math.floor(Math.random() * 4);
  86. }
  87. this.atkNum = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].atk[this.weaponLevel];
  88. this.atkSpeed = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].atkSpeed[this.weaponLevel];
  89. this.bulletNum = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].bulletNum;
  90. this.bulletTotal = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].bulletTotal[this.weaponLevel];
  91. this.reload = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].reload;
  92. this.atkRangeNum = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].atkRange;
  93. if (this.person) this.bulletTotal = Math.ceil(this.bulletTotal * this.person.bulletRate);
  94. // 装弹
  95. this._curBullet = 0;
  96. this.addBullet();
  97. this.setSD();
  98. }
  99. protected start(): void { }
  100. setBulletUI() {
  101. // UI更新子弹数
  102. if (this.person && this.person.isPlayer) {
  103. if (this.weaponType == WeaponType.weapon_range) {
  104. if (gameMgr.ammo && gameMgr.ammo.isValid)
  105. gameMgr.ammo.string = this._curBullet + "/" + ([6, 7].includes(cocosz.gameMode) ? "♾" : this.bulletTotal);
  106. } else if (this.weaponType == WeaponType.weapon_rangeAd) {
  107. if (gameMgr.ammoAd && gameMgr.ammoAd.isValid)
  108. gameMgr.ammoAd.string = this._curBullet + "/" + ([6, 7].includes(cocosz.gameMode) ? "♾" : this.bulletTotal);
  109. }
  110. }
  111. }
  112. _arrName = ["", "y", "p", "r"]
  113. protected setSD(): void {
  114. // 闪电颜色
  115. if (this.person) {
  116. this.node.walk((child) => {
  117. if (child.name == "sd") {
  118. if (this.weaponLevel > 0) {
  119. child.active = true;
  120. let spAni = child.getComponent(sp.Skeleton);
  121. spAni && spAni.setSkin(this._arrName[this.weaponLevel]);
  122. } else {
  123. child.active = false;
  124. }
  125. }
  126. }, null);
  127. }
  128. }
  129. // 重置子弹
  130. reset() {
  131. this.bulletTotal = GameDate.Weapon[Weapon.WeaponName[this.weaponNum - 1]].bulletTotal[this.weaponLevel];
  132. if (this.person) {
  133. this.bulletTotal = Math.ceil(this.bulletTotal * this.person.bulletRate);
  134. }
  135. this._curBullet = 0;
  136. this.addBullet();
  137. }
  138. // 装弹
  139. addBullet() {
  140. if (this.isRangeWeapon) {
  141. if (this.person && this.person.isPlayer) {
  142. // 子弹小于弹夹数量, 且还有备弹
  143. if (this.curBullet < this.bulletNum && this.bulletTotal > 0) {
  144. if ([6, 7].includes(cocosz.gameMode)) {
  145. this.curBullet = this.bulletNum;
  146. cc.game.emit(Constant.E_GAME_LOGIC, { type: Constant.E_Bullet_Reload })
  147. } else {
  148. let n = this.bulletNum - this.curBullet;
  149. if (this.bulletTotal >= n) {
  150. // 子弹足够
  151. this.bulletTotal -= n;
  152. this.curBullet += n;
  153. } else {
  154. // 子弹不够
  155. this.bulletTotal = 0;
  156. this.curBullet += this.bulletTotal;
  157. }
  158. }
  159. }
  160. } else {
  161. this.curBullet = this.bulletNum;
  162. }
  163. }
  164. }
  165. // 装弹效果
  166. _isReload: boolean = false;
  167. reloadBullet() {
  168. if (this.isRangeWeapon && this._isReload == false) {
  169. this._isReload = true;
  170. if (this.person && this.person.isPlayer) {
  171. if (this.bulletTotal > 0) {
  172. // 音效
  173. cocosz.audioMgr.playEffect("reload");
  174. // 动作
  175. let y_back = 0;
  176. if (this.hand_effect) {
  177. y_back = this.hand_effect.y;
  178. cc.tween(this.hand_effect)
  179. .by(0.5, { y: 50 })
  180. .by(0.5, { y: -50 })
  181. .union()
  182. .repeat(this.reload)
  183. .start();
  184. }
  185. // 闪红
  186. cc.tween(gameMgr.rangedWeaponMess.children[0])
  187. .to(0.2, { color: cc.Color.RED })
  188. .to(0.2, { color: cc.Color.WHITE })
  189. .start();
  190. cocosz.scheduleOnce(() => {
  191. if (this && this.isValid) {
  192. this._isReload = false;
  193. this.addBullet();
  194. if (this.hand_effect) {
  195. this.hand_effect.stopAllActions();
  196. this.hand_effect.y = y_back;// 还原手的位置
  197. }
  198. // 闪绿
  199. cc.tween(gameMgr.rangedWeaponMess.children[0])
  200. .to(0.2, { color: cc.Color.GREEN })
  201. .to(0.2, { color: cc.Color.WHITE })
  202. .start();
  203. }
  204. }, this.reload * this.person.reloadRate);
  205. // 装弹按钮效果
  206. if (gameMgr.BtnBullet.active && gameMgr.BtnBullet.children[3] && gameMgr.BtnBullet.children[3].active) {
  207. // 装弹
  208. gameMgr.BtnBullet.children[3].active = false;
  209. gameMgr.BtnBullet.children[0].active = true;
  210. // 进度填充
  211. let t = gameMgr.playerTs.curWeapon.reload * gameMgr.playerTs.reloadRate;
  212. gameMgr.BtnBullet.children[1].active = true;
  213. let pro = gameMgr.BtnBullet.children[1].getComponent(cc.Sprite);
  214. cc.tween(pro)
  215. .set({ fillRange: 0 })
  216. .to(t, { fillRange: -1 })
  217. .call(() => {
  218. gameMgr.BtnBullet.children[0].active = false;
  219. gameMgr.BtnBullet.children[1].active = false;
  220. gameMgr.BtnBullet.children[2].active = false;
  221. gameMgr.BtnBullet.children[3].active = true;
  222. })
  223. .start();
  224. gameMgr.BtnBullet.children[2].active = true;
  225. let label = gameMgr.BtnBullet.children[2].getComponent(cc.Label);
  226. cc.tween(label)
  227. .call(() => { label.string = (t * (1 + pro.fillRange)).toFixed(1); })
  228. .delay(0.1)
  229. .union()
  230. .repeat(t / 0.1)
  231. .start()
  232. }
  233. } else {
  234. //没有备弹,提醒
  235. this._isReload = false;
  236. Msg.Show(i18n.t("msg.myzd"));//没有子弹
  237. cocosz.audioMgr.playEffect("bag", false, 1);
  238. }
  239. } else {
  240. cocosz.scheduleOnce(() => {
  241. if (this && this.isValid) {
  242. this._isReload = false;
  243. this.addBullet();
  244. }
  245. }, this.reload);
  246. }
  247. }
  248. }
  249. }