person.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. import PlatUtils from "../../common-plugin/Scripts/PlatUtils";
  2. import { cocosz } from "../Framework/CocosZ";
  3. import { PanelName, ZindexLayer } from "../Framework/Constant";
  4. import GameDate from "./gameDate";
  5. import { gameMgr } from "./gameMgr";
  6. import Weapon, { WeaponType } from "./weapon";
  7. // @ts-ignore
  8. const i18n = require('LanguageData');
  9. const { ccclass, property } = cc._decorator;
  10. @ccclass
  11. export default class Person extends cc.Component {
  12. // 光环特效
  13. ghAniNode: cc.Node = null;
  14. // 烟尘动画节点
  15. ycAniNode: cc.Node = null;
  16. personAtk: number = 0;
  17. playerName: string = "";
  18. personLevel: number = 0;
  19. atkNum: number = 40;
  20. atkRange: number = 800;
  21. atkSpeed: number = 4;
  22. reloadSpeed: number = 2;
  23. totleHp: number = 300;
  24. curHp: number = 300;
  25. atkRate: number = 1;
  26. speedRate: number = 1;
  27. atkSpeedRate: number = 1;
  28. reloadRate: number = 1;
  29. bulletRate: number = 1;
  30. meleeAtkRate: number = 1;
  31. damageReduction: number = 0;
  32. recoverItemNum: number = 0;
  33. killNum: number = 0;
  34. curKillNum: number = 0;
  35. MoveSpeed: number = 600;
  36. moveDir: cc.Vec2 = cc.v2(0, 0);
  37. atkDir: cc.Vec2 = cc.v2(0, 0);
  38. footNum: number = 0;
  39. lastPos: cc.Vec2 = cc.v2(0, 0);
  40. // 开关
  41. public canMove: boolean = true;//能否移动
  42. public canMoveDir: boolean = true;//能否改变移动方向
  43. // 检测范围
  44. detectRange: number = 1000;
  45. @property(cc.Material)
  46. mat_common: cc.Material = null;
  47. @property(cc.Material)
  48. mat_attacked: cc.Material = null;
  49. // onLoad () {}
  50. @property(cc.Node)
  51. body: cc.Node = null;
  52. @property(cc.Node)
  53. leg: cc.Node = null;
  54. @property(cc.Node)
  55. leg_back: cc.Node = null;
  56. @property(cc.Node)
  57. head: cc.Node = null;
  58. @property(cc.ProgressBar)
  59. hpBar: cc.ProgressBar = null;
  60. @property(cc.Node)
  61. hpSpr: cc.Node = null;
  62. @property(cc.ProgressBar)
  63. shieldBar: cc.ProgressBar = null;
  64. @property(cc.ProgressBar)
  65. atkBar: cc.ProgressBar = null;
  66. head_atk: cc.Node = null;
  67. head_hart: cc.Node = null;
  68. head_death: cc.Node = null;
  69. playerMess: cc.Node = null;
  70. ani: cc.Animation = null;
  71. id: number = 0;//1玩家
  72. isPlayer: boolean = false;
  73. atkTarget: cc.Node = null;
  74. weapon: cc.Node = null;
  75. weapon_dao: cc.Node = null;
  76. hpNumNode: cc.Node = null;
  77. get HP() {
  78. return this.curHp;
  79. }
  80. set HP(num) {
  81. num = Math.floor(num);
  82. if (num < 0) num = 0;
  83. else if (num > this.totleHp) num = this.totleHp;
  84. this.curHp = num;
  85. // 血条
  86. if (this.hpBar && this.hpBar.isValid) {
  87. this.hpBar.progress = this.curHp / this.totleHp;
  88. }
  89. // 主动使用道具
  90. if (this.isPlayer) {
  91. if (cocosz.gameMode == 6) {
  92. gameMgr.update_model6_xuedi();
  93. }
  94. }
  95. }
  96. _shield: number = 0;
  97. maxShield: number = 100;
  98. get Shiled() {
  99. return this._shield;
  100. }
  101. set Shiled(num) {
  102. cc.Tween.stopAllByTarget(this.shieldBar);
  103. this._shield = num;
  104. if (this._shield < 0) {
  105. this._shield = 0;
  106. this.shieldBar.node.opacity = 0;
  107. this.body.getChildByName("body").children[0].active = false;
  108. }
  109. else {
  110. this.shieldBar.node.opacity = 255;
  111. }
  112. cc.tween(this.shieldBar).to(0.3, { progress: this._shield / this.maxShield }).start();
  113. }
  114. rig: cc.RigidBody = null;
  115. protected onLoad(): void { }
  116. protected start(): void {
  117. // 人物信息
  118. if (this.playerMess && this.playerMess.isValid) {
  119. this._playerMessY = this.playerMess.y;
  120. this.playerMess.setParent(this.node.parent);
  121. this.playerMess.zIndex = ZindexLayer.zindex_hp;
  122. this.updateMess();
  123. } else if (this.hpBar && this.hpBar.isValid) {
  124. this._playerMessY = this.hpBar.node.y;
  125. this.hpBar.node.setParent(this.node.parent);
  126. this.hpBar.node.zIndex = ZindexLayer.zindex_hp;
  127. this.updateMess();
  128. }
  129. }
  130. curTime: number = 0;
  131. update(dt) { }
  132. lateUpdate(dt) { }
  133. protected _playerMessY: number = 200;
  134. /** 人物信息 */
  135. protected updateMess() {
  136. if (this.playerMess && this.playerMess.isValid) {
  137. if (this.node && this.node.isValid && this.node.active && this.node.opacity && this.HP) {
  138. this.playerMess.active = true;
  139. this.playerMess.setPosition(this.node.x, this.node.y + this._playerMessY);
  140. } else {
  141. this.playerMess.active = false;
  142. }
  143. } else if (this.hpBar && this.hpBar.isValid && this.hpBar.node && this.hpBar.node.isValid) {
  144. if (this.node && this.node.isValid && this.node.active && this.node.opacity && this.HP) {
  145. this.hpBar.node.active = true;
  146. this.hpBar.node.setPosition(this.node.x, this.node.y + this._playerMessY);
  147. } else {
  148. this.hpBar.node.active = false;
  149. }
  150. }
  151. }
  152. /** 光环 */
  153. protected updateGhAni() {
  154. if (this.ghAniNode && this.ghAniNode.isValid) {
  155. if (this.node && this.node.isValid && this.node.active && this.node.opacity && this.HP) {
  156. this.ghAniNode.active = true;
  157. this.ghAniNode.angle = this.node.angle;
  158. this.ghAniNode.setPosition(this.node.x, this.node.y - 90);
  159. } else {
  160. this.ghAniNode.active = false;
  161. }
  162. }
  163. }
  164. /** 烟尘 */
  165. protected updateYcAni() {
  166. if (this.ycAniNode && this.ycAniNode.isValid) {
  167. if (this.node && this.node.isValid && this.node.active && this.node.opacity && this.HP && !this.moveDir.equals(cc.Vec2.ZERO)) {
  168. this.ycAniNode.active = true;
  169. this.ycAniNode.setPosition(this.node.x, this.node.y - 50);
  170. this.ycAniNode.scaleX = this.moveDir.x > 0 ? 1 : -1;
  171. } else {
  172. this.ycAniNode.active = false;
  173. }
  174. }
  175. }
  176. isReady: boolean = false;
  177. curSkin: number = 0;
  178. setProperty() {
  179. this.ani = this.body.getComponent(cc.Animation);
  180. this.node.getComponent(cc.CircleCollider).radius = this.detectRange;
  181. if (this.isPlayer) this.atkRange = 1000;
  182. this.personLevel = Math.floor(Math.random() * 6);
  183. let num1 = Weapon.meleeWaapon[Math.floor(Math.random() * Weapon.meleeWaapon.length)];
  184. let num2 = Weapon.rangeWeapon[Math.floor(Math.random() * 13/* Weapon.rangedWeapon.length */)];
  185. if (this.isPlayer) {
  186. num1 = cocosz.dataMgr.CurMelee + 1;
  187. num2 = cocosz.dataMgr.CurRange + 1;
  188. if (cocosz.gameMgr.gameCtr.curUseSkinId < 0) {
  189. this.curSkin = cocosz.dataMgr.CurSkinId;
  190. } else {
  191. this.curSkin = cocosz.gameMgr.gameCtr.curUseSkinId;
  192. }
  193. this.head.children[0].children[this.curSkin].active = true;
  194. let head1 = this.head.children[0].children[this.curSkin];
  195. this.head.children[1].children[this.curSkin].active = true;
  196. let head2 = this.head.children[1].children[this.curSkin];
  197. for (let i = this.head.children[0].childrenCount - 1; i >= 0; i--) {
  198. if (this.head.children[0].children[i].uuid != head1.uuid) {
  199. this.head.children[0].children[i].destroy();
  200. }
  201. }
  202. for (let i = this.head.children[1].childrenCount - 1; i >= 0; i--) {
  203. if (this.head.children[1].children[i].uuid != head2.uuid) {
  204. this.head.children[1].children[i].destroy();
  205. }
  206. }
  207. // 表情(父节点设置为正脸)
  208. this.head_atk = this.head.children[2].children[this.curSkin];
  209. this.head_atk.active = true;
  210. this.head_atk.opacity = 0;
  211. this.head_hart = this.head.children[3].children[this.curSkin];
  212. this.head_hart.active = true;
  213. this.head_hart.opacity = 0;
  214. this.head_death = this.head.children[4].children[this.curSkin];
  215. this.head_death.active = true;
  216. this.head_death.opacity = 0;
  217. this.head_atk.setParent(this.head.children[0]);
  218. this.head_hart.setParent(this.head.children[0]);
  219. this.head_death.setParent(this.head.children[0]);
  220. this.head_atk.zIndex = 1;
  221. this.head_hart.zIndex = 2;
  222. this.head_death.zIndex = 3;
  223. // 删除多余表情
  224. this.head.children[2].destroy();
  225. this.head.children[3].destroy();
  226. this.head.children[4].destroy();
  227. this.personLevel = cocosz.dataMgr.getSkinInfo(this.curSkin).Level;
  228. this.personAtk = GameDate.SkinMess[`${this.curSkin + 1}`].atk[this.personLevel];
  229. if (cocosz.gameMode == 6) {
  230. this.totleHp = GameDate.SkinMess[`${this.curSkin + 1}`].xuedi;
  231. } else {
  232. this.totleHp = GameDate.SkinMess[`${this.curSkin + 1}`].hp[this.personLevel];
  233. }
  234. // 玩家血量增加
  235. if ([1, 2, 4].includes(cocosz.gameMode)) {
  236. this.totleHp *= 4;
  237. }
  238. else if (3 == cocosz.gameMode) {
  239. if (cocosz.curLevel >= 15) {
  240. this.totleHp *= 9;
  241. } else if (cocosz.curLevel >= 10) {
  242. this.totleHp *= 8;
  243. } else if (cocosz.curLevel >= 5) {
  244. this.totleHp *= 6;
  245. } else {
  246. this.totleHp *= 4;
  247. }
  248. }
  249. else if (5 == cocosz.gameMode) {
  250. this.totleHp *= 6;
  251. }
  252. else if (7 == cocosz.gameMode) {
  253. this.totleHp *= 3;
  254. }
  255. this.HP = this.totleHp;
  256. }
  257. else { }
  258. // 初始武器
  259. if (cocosz.gameMode == 4) {
  260. if (this.isPlayer) {
  261. this.setNewWeapon(14 - 1);// 玩家固定木棒
  262. } else {
  263. this.setNewWeapon(num1 - 1);// 敌人随机近战武器
  264. }
  265. this.changeCurWeapon(this.meleeWeapon, false);
  266. } else {
  267. if (cocosz.gameMode === 7) {
  268. this.setNewWeapon(num2 - 1);
  269. }
  270. else {
  271. this.setNewWeapon(num1 - 1);
  272. this.setNewWeapon(num2 - 1);
  273. }
  274. this.changeCurWeapon(this.rangedWeapon, false);
  275. // 广告远程武器
  276. if (this.isPlayer /* && cocosz.isShowAd */) {
  277. cc.tween(gameMgr.rangedWeaponAdMess)
  278. .call(() => {
  279. this.refreshWeaponAd();
  280. })
  281. .delay(10)
  282. .union()
  283. .repeatForever()
  284. .start();
  285. }
  286. }
  287. // 血条
  288. if (cocosz.gameMode == 6) {
  289. this.hpBar.node.active = false;
  290. this.atkBar.node.active = false;
  291. }
  292. // 角色速度
  293. this.MoveSpeed = 200 + GameDate.SkinMess[`${this.curSkin + 1}`].speed[this.personLevel];
  294. if (PlatUtils.IsOPPO) { this.MoveSpeed /= 2; }
  295. // 光环
  296. if (this.personLevel > 0) {
  297. this.ghAniNode.color = cc.Color.WHITE;
  298. let arr = ["", "y", "p", "r"];
  299. let ghAni = this.ghAniNode.getComponent(sp.Skeleton);
  300. if (ghAni) {
  301. ghAni.setSkin(arr[Math.ceil(this.personLevel / 2)]);
  302. }
  303. } else {
  304. this.ghAniNode.color = cc.Color.BLACK;
  305. let ghAni = this.ghAniNode.getComponent(sp.Skeleton);
  306. ghAni.setSkin("r");
  307. ghAni.setAnimation(0, "animation", true);
  308. }
  309. if (!this.playerName) {
  310. this.playerName = i18n.t("game.player") + this.id;
  311. }
  312. let name = this.playerMess.getChildByName("nameLabel");
  313. if (name) name.active = false;
  314. this.lastPos = this.node.getPosition();
  315. this.isReady = true;
  316. }
  317. /** 视频武器 */
  318. weaponAdArr = [9, 10, 14, 17, 18];
  319. weaponAdIndex: number = Math.floor(Math.random() * this.weaponAdArr.length);
  320. refreshWeaponAd() {
  321. if (cocosz.isPause || !this.isPlayer) { return; }
  322. if (++this.weaponAdIndex >= this.weaponAdArr.length) {
  323. this.weaponAdIndex = 0;
  324. }
  325. let weaponNum = this.weaponAdArr[this.weaponAdIndex];
  326. let str = Weapon.WeaponName[weaponNum];
  327. let prefab = cocosz.resMgr.getRes("weapon_" + str, cc.Prefab);
  328. let new_weapon = cc.instantiate(prefab);
  329. let new_weaponTs: Weapon = new_weapon.getComponent(Weapon);
  330. // 初始化
  331. new_weaponTs.person = this;
  332. this.body.addChild(new_weapon);
  333. new_weapon.active = false;
  334. new_weaponTs.weaponType = WeaponType.weapon_rangeAd;
  335. this.rangedWeaponAd && this.rangedWeaponAd.destroy();
  336. this.rangedWeaponAd = new_weapon;
  337. this.rangedWeaponAdNum = new_weaponTs.weaponNum;
  338. let str2 = "w_" + Weapon.WeaponName[this.rangedWeaponAdNum - 1];
  339. let spr2 = cocosz.resMgr.getRes(str2, cc.SpriteFrame);
  340. gameMgr.rangedWeaponAdMess.children[3].getComponent(cc.Sprite).spriteFrame = spr2;
  341. gameMgr.rangedWeaponAdMess.children[4].getComponent(cc.Sprite).spriteFrame = cocosz.resMgr.getRes("w_" + (weaponNum + 1), cc.SpriteFrame);
  342. gameMgr.rangedWeaponAdMess.children[5].getComponent(cc.Label).string = new_weaponTs.atkNum.toString();
  343. new_weaponTs.setBulletUI();
  344. }
  345. meleeWeaponNum: number = 3;
  346. rangedWeaponNum: number = 1;
  347. rangedWeaponAdNum: number = 1;
  348. meleeWeapon: cc.Node = null;
  349. rangedWeapon: cc.Node = null;
  350. rangedWeaponAd: cc.Node = null;
  351. curWeapon: Weapon = null;
  352. startPosNode: cc.Node = null;
  353. setNewWeapon(weaponNum: number = this.newWeapon) {
  354. if (weaponNum < 0) return;
  355. let str = Weapon.WeaponName[weaponNum];
  356. let prefab = cocosz.resMgr.getRes("weapon_" + str, cc.Prefab);
  357. let new_weapon = cc.instantiate(prefab);
  358. let new_weaponTs: Weapon = new_weapon.getComponent(Weapon);
  359. new_weaponTs.person = this;
  360. this.body.addChild(new_weapon);
  361. new_weapon.active = false;
  362. if (new_weaponTs.isRangeWeapon) {
  363. if (this.curWeapon && this.curWeapon.weaponType == WeaponType.weapon_rangeAd) {
  364. // 设置广告远程
  365. new_weaponTs.weaponType = WeaponType.weapon_rangeAd;
  366. this.rangedWeaponAd && this.rangedWeaponAd.destroy();
  367. this.rangedWeaponAd = new_weapon;
  368. this.rangedWeaponAdNum = new_weaponTs.weaponNum;
  369. if (this.isPlayer) {
  370. let str2 = "w_" + Weapon.WeaponName[this.rangedWeaponAdNum - 1];
  371. let spr2 = cocosz.resMgr.getRes(str2, cc.SpriteFrame);
  372. gameMgr.rangedWeaponAdMess.children[3].getComponent(cc.Sprite).spriteFrame = spr2;
  373. gameMgr.rangedWeaponAdMess.children[4].getComponent(cc.Sprite).spriteFrame = cocosz.resMgr.getRes("w_" + (weaponNum + 1), cc.SpriteFrame);
  374. gameMgr.rangedWeaponAdMess.children[5].getComponent(cc.Label).string = new_weaponTs.atkNum.toString();
  375. new_weaponTs.setBulletUI();
  376. }
  377. } else {
  378. // 设置远程
  379. new_weaponTs.weaponType = WeaponType.weapon_range;
  380. this.rangedWeapon && this.rangedWeapon.destroy();
  381. this.rangedWeapon = new_weapon;
  382. this.rangedWeaponNum = new_weaponTs.weaponNum;
  383. if (this.isPlayer) {
  384. let str2 = "w_" + Weapon.WeaponName[this.rangedWeaponNum - 1];
  385. let spr2 = cocosz.resMgr.getRes(str2, cc.SpriteFrame);
  386. gameMgr.rangedWeaponMess.children[3].getComponent(cc.Sprite).spriteFrame = spr2;
  387. gameMgr.rangedWeaponMess.children[4].getComponent(cc.Sprite).spriteFrame = cocosz.resMgr.getRes("w_" + (weaponNum + 1), cc.SpriteFrame);
  388. gameMgr.rangedWeaponMess.children[5].getComponent(cc.Label).string = new_weaponTs.atkNum.toString();
  389. new_weaponTs.setBulletUI();
  390. }
  391. }
  392. }
  393. else {
  394. // 设置近战
  395. new_weaponTs.weaponType = WeaponType.weapon_melee;
  396. this.meleeWeapon && this.meleeWeapon.destroy();
  397. this.meleeWeapon = new_weapon;
  398. this.meleeWeaponNum = new_weaponTs.weaponNum;
  399. }
  400. // 替换当前武器
  401. if (this.curWeapon && this.curWeapon.isRangeWeapon === new_weaponTs.isRangeWeapon) {
  402. this.changeCurWeapon(new_weapon, false);
  403. }
  404. // 销毁武器道具
  405. this.newWeapon = -1;
  406. if (this.newWeaponItem && this.newWeaponItem.isValid) this.newWeaponItem.destroy();
  407. this.newWeaponItem = null;
  408. }
  409. lastTime: number = 0;
  410. changeCurWeapon(newWeaponNode: cc.Node, isCheck: boolean = true) {
  411. if (!newWeaponNode || !newWeaponNode.isValid) return;
  412. if (this.curWeapon && this.curWeapon.isValid && newWeaponNode == this.curWeapon.node) return;
  413. let curTime = Number(new Date());
  414. if (isCheck && (curTime - this.lastTime < 1000)) return;
  415. this.lastTime = curTime;
  416. // 切换武器显示
  417. if (this.curWeapon && cc.isValid(this.curWeapon)) this.curWeapon.node.active = false;
  418. newWeaponNode.active = true;
  419. // 切换当前武器
  420. let newWeapinTs = newWeaponNode.getComponent(Weapon);
  421. if (newWeapinTs) {
  422. // UI效果
  423. if (this.isPlayer) {
  424. let newWeapinMess: cc.Node = null;
  425. if (newWeapinTs.weaponType == WeaponType.weapon_melee) {
  426. gameMgr.BtnBullet.active = false;
  427. } else if (newWeapinTs.weaponType == WeaponType.weapon_range) {
  428. newWeapinMess = gameMgr.rangedWeaponMess;
  429. } else if (newWeapinTs.weaponType == WeaponType.weapon_rangeAd) {
  430. newWeapinMess = gameMgr.rangedWeaponAdMess;
  431. }
  432. newWeapinMess.children[0].active = true;
  433. if (this.curWeapon && this.curWeapon.isValid) {
  434. cocosz.audioMgr.playEffect("changeWeapon");
  435. let oldWeapinMess: cc.Node = null;
  436. if (this.curWeapon.weaponType == WeaponType.weapon_melee) {
  437. } else if (this.curWeapon.weaponType == WeaponType.weapon_range) {
  438. oldWeapinMess = gameMgr.rangedWeaponMess;
  439. } else if (this.curWeapon.weaponType == WeaponType.weapon_rangeAd) {
  440. oldWeapinMess = gameMgr.rangedWeaponAdMess;
  441. }
  442. if (oldWeapinMess !== newWeapinMess) {
  443. oldWeapinMess.children[0].active = false;
  444. oldWeapinMess.children[1].active = true;
  445. cc.tween(oldWeapinMess.children[1].getComponent(cc.Sprite))
  446. .set({ fillRange: -1 })
  447. .to(1, { fillRange: 0 })
  448. .call(() => {
  449. if (oldWeapinMess.isValid) {
  450. oldWeapinMess.children[1].active = false;
  451. }
  452. })
  453. .start();
  454. }
  455. }
  456. }
  457. // 开火点
  458. if (newWeapinTs.isRangeWeapon) {
  459. this.startPosNode = newWeaponNode.getChildByName("startPos");
  460. }
  461. this.curWeapon = newWeapinTs;
  462. this.atkNum = this.curWeapon.atkNum * (this.curWeapon.isRangeWeapon ? 1 : this.meleeAtkRate);
  463. this.atkSpeed = this.curWeapon.atkSpeed;
  464. this.reloadSpeed = this.curWeapon.reload;
  465. }
  466. }
  467. atkStart() { }
  468. atkEnemy() { }
  469. atkComplete() { }
  470. setWeaponAngle(dir: cc.Vec2) {
  471. if (this.curWeapon && this.curWeapon.isValid && this.curWeapon.isRangeWeapon) {
  472. if (dir.equals(cc.Vec2.ZERO)) {
  473. this.curWeapon.node.angle = 0;
  474. } else {
  475. if (dir.x < 0) {
  476. this.curWeapon.node.angle = cc.v2(dir).signAngle(cc.v2(-1, 0)) / Math.PI * 180;
  477. } else {
  478. this.curWeapon.node.angle = -cc.v2(dir).signAngle(cc.v2(1, 0)) / Math.PI * 180;
  479. }
  480. if (dir.y > 0) {
  481. if (this.curWeapon.node) this.rangedWeapon.zIndex = -1;
  482. } else {
  483. if (this.curWeapon.node) this.rangedWeapon.zIndex = 1;
  484. }
  485. }
  486. }
  487. }
  488. _aniName: string = "";
  489. /**
  490. * 设置动画
  491. * @param name 动画名字
  492. * @param enforce 强制播放
  493. */
  494. updateAni(name?: string, enforce: boolean = false) {
  495. if (enforce) {
  496. this._playAni(name);
  497. }
  498. else if (gameMgr.isGameStart) {
  499. if (this.isDeath) {
  500. name = "die";
  501. } else if (this.moveDir.equals(cc.Vec2.ZERO)) {
  502. name = "daiji_body";
  503. } else {
  504. name = "run_body";
  505. }
  506. this._playAni(name);
  507. }
  508. }
  509. _playAni(name: string) {
  510. if (name && name != this._aniName) {
  511. this.ani.play(name);
  512. this._aniName = name;
  513. }
  514. }
  515. getCurSpeed() {
  516. let speed: number = this.MoveSpeed * this.speedRate * (this.curWeapon && this.curWeapon.isRangeWeapon ? 1 : 1.2);
  517. if (speed > 1000) speed = 1000;
  518. return speed;
  519. }
  520. isAtk: boolean = false;
  521. udpateRBody(dir: cc.Vec2) {
  522. if (this.rig.type == cc.RigidBodyType.Dynamic) {
  523. if (this.canMove) {
  524. if (!dir.equals(cc.Vec2.ZERO)) {
  525. this.rig.linearVelocity = dir.mul(this.getCurSpeed());
  526. } else {
  527. this.rig.linearVelocity = cc.Vec2.ZERO;
  528. }
  529. }
  530. }
  531. }
  532. updatePerson() {
  533. let dir = null;
  534. if (this.atkDir && !this.atkDir.equals(cc.Vec2.ZERO)) {
  535. dir = this.atkDir;
  536. } else if (this.moveDir && !this.moveDir.equals(cc.Vec2.ZERO)) {
  537. dir = this.moveDir;
  538. } else {
  539. return;
  540. }
  541. if (dir.x < 0) this.body.scaleX = -Math.abs(this.body.scaleX);
  542. else this.body.scaleX = Math.abs(this.body.scaleX);
  543. if (dir.y > 0) {
  544. this.leg_back.opacity = 255;
  545. this.leg.opacity = 0;
  546. this.head.children[1].opacity = 255;
  547. this.head.children[0].opacity = 0;
  548. }
  549. else {
  550. this.leg_back.opacity = 0;
  551. this.leg.opacity = 255;
  552. this.head.children[0].opacity = 255;
  553. this.head.children[1].opacity = 0;
  554. }
  555. // 远程武器
  556. this.setWeaponAngle(dir);
  557. }
  558. updateAtk() {
  559. if (this.atkList.length > 0) {
  560. let dt = 1000;
  561. let n = -1;
  562. for (let i = 0; i < this.atkList.length; i++) {
  563. let ts = this.atkList[i].getComponent(Person);
  564. let dt2 = this.atkList[i].getPosition().sub(this.node.getPosition()).mag();
  565. if (dt2 < dt) {
  566. dt = dt2;
  567. n = i;
  568. }
  569. }
  570. if (n >= 0) {
  571. this.atkDir = cc.v2(this.atkList[n].getPosition().sub(this.node.getPosition()).normalize());
  572. this.atkTarget = this.atkList[n];
  573. }
  574. else {
  575. this.atkDir = this.moveDir.mag() == 0 ? this.atkDir : cc.v2(this.moveDir);
  576. }
  577. }
  578. else {
  579. this.atkDir = this.moveDir.mag() == 0 ? this.atkDir : cc.v2(this.moveDir);
  580. }
  581. }
  582. atkList: cc.Node[] = [];
  583. curItem: number = 0;
  584. curItemEffect: cc.Prefab = null;
  585. itemTarget: cc.Node = null;
  586. grassID: number = 0;
  587. houseID: number = 0;
  588. isInHouse: boolean = false;
  589. // 0:检测碰撞;1:检测子弹;2:检测在层级下,3:检测在层级上
  590. onCollisionEnter(other: cc.Collider, self: cc.Collider) { }
  591. newWeapon: number = -1;
  592. newWeaponItem: cc.Node = null;
  593. poisonCount: number = 0;
  594. onCollisionExit(other: cc.Collider, self: cc.Collider) { }
  595. isDeath: boolean = false;
  596. isAttackedEffect: boolean = false;
  597. maxNum: number = 0;
  598. /** 受伤 */
  599. hart(atkNum: number, from: cc.Node, dir: cc.Vec2 = null, isAudio: boolean = true, isEmit: boolean = true, labelColor?: cc.Color) { }
  600. isAvoidInjury: number = 0;
  601. /** 免伤 */
  602. avoidInjury(time: number) {
  603. this.isAvoidInjury++;
  604. this.scheduleOnce(() => {
  605. this.isAvoidInjury--;
  606. }, time);
  607. }
  608. killer: any = null;
  609. death() {
  610. this.isDeath = true;
  611. // 隐藏光环
  612. if (this.ghAniNode && this.ghAniNode.isValid) {
  613. this.ghAniNode.active = false;
  614. }
  615. if (this.playerMess && this.playerMess.isValid) {
  616. this.playerMess.active = false;
  617. }
  618. this.head_death.opacity = 255;
  619. // 死亡音效
  620. gameMgr.playEffect("death", this.node);
  621. this.unscheduleAllCallbacks();
  622. // this.ani.stop();
  623. // this.ani.play("die");
  624. this.updateAni("die");
  625. if (this.meleeWeapon) {
  626. let ani = this.meleeWeapon.getComponent(cc.Animation);
  627. ani && ani.stop();
  628. }
  629. this.leg.opacity = 255;
  630. this.leg_back.opacity = 0;
  631. this.head.children[0].opacity = 255;
  632. this.head.children[1].opacity = 0;
  633. if (this.curWeapon && this.curWeapon.isValid) this.curWeapon.node.active = false;
  634. if (this.rig) this.rig.active = false;
  635. this.node.getComponent(cc.BoxCollider).enabled = false;
  636. this.node.getComponent(cc.CircleCollider).enabled = false;
  637. gameMgr.deathNum++;
  638. if (this.isPlayer) {
  639. let rate = 0.3;
  640. cc.director.getScheduler().setTimeScale(rate);
  641. setTimeout(() => {
  642. cc.director.getScheduler().setTimeScale(1);
  643. }, rate * 3000);
  644. }
  645. if (cocosz.gameMode == 6) {
  646. if (this.isPlayer) {
  647. if (!gameMgr.isWin && !gameMgr.isFail) {
  648. this.revivePos = this.node.getPosition();
  649. this.scheduleOnce(() => {
  650. cocosz.uiMgr.openPanel(PanelName.UIRevivePanel);
  651. }, 2)
  652. }
  653. }
  654. }
  655. }
  656. revive() {
  657. if (gameMgr && !gameMgr.isWin && !gameMgr.isFail) {
  658. gameMgr.deathNum--;
  659. // 恢复光环
  660. if (this.ghAniNode && this.ghAniNode.isValid) {
  661. this.ghAniNode.active = true;
  662. }
  663. if (this.playerMess && this.playerMess.isValid) {
  664. this.playerMess.active = true;
  665. }
  666. if (this.rangedWeapon) this.rangedWeapon.getComponent(Weapon).reset();
  667. if (this.rangedWeaponAd) this.rangedWeaponAd.getComponent(Weapon).reset();
  668. // if (this.rangedWeapon) this.rangedWeapon.zIndex = 1;
  669. // if (this.rangedWeapon) this.rangedWeapon.angle = 0;
  670. if (this.meleeWeapon) {
  671. let ani = this.meleeWeapon.getComponent(cc.Animation);
  672. ani && ani.play();
  673. }
  674. if (this.curWeapon && this.curWeapon.isValid) this.curWeapon.node.active = true;
  675. this.head_death.opacity = 0;
  676. let pos = this.revivePos;
  677. this.node.setPosition(pos);
  678. this.HP = this.totleHp;
  679. this.body.setPosition(cc.v2(0, 0));
  680. this.body.angle = 0;
  681. this.leg.opacity = 255;
  682. this.leg_back.opacity = 0;
  683. this.head.children[0].opacity = 255;
  684. this.head.children[1].opacity = 0;
  685. if (this.node.scaleX < 0) this.node.scaleX *= -1;
  686. if (this.rig) this.rig.active = true;
  687. // this.node.getComponent(cc.RigidBody).enabled = true;
  688. this.node.getComponent(cc.BoxCollider).enabled = true;
  689. this.node.getComponent(cc.CircleCollider).enabled = true;
  690. this.isDeath = false;
  691. this.canMove = true;
  692. }
  693. }
  694. // findTime: number = 0;
  695. revivePos: cc.Vec2 = cc.v2(0, 0);
  696. creatItem() { }
  697. }