Enemy.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. import { _decorator, Node, Tween, Vec3, ProgressBar, UIOpacity, tween, Label, SkeletalAnimation, Animation, SphereCollider, CylinderCollider, MeshCollider, Collider, PhysicsRayResult} from 'cc';
  2. import { Game } from './Game';
  3. import { Player } from './Player';
  4. import { Utils } from '../utils/Utils';
  5. import { ResUtil } from '../utils/ResUtil';
  6. import MsgHints from '../utils/MsgHints';
  7. import { userIns } from '../data/UserData';
  8. import { autoBind } from '../extend/AutoBind';
  9. import { BaseExp } from '../core/base/BaseExp';
  10. import { GunBase } from '../items/base/GunBase';
  11. import { PoolManager } from '../core/manager/PoolManager';
  12. import { audioMgr } from '../core/manager/AudioManager';
  13. import { Constants } from '../data/Constants';
  14. const { ccclass, property } = _decorator;
  15. //部位类型
  16. export enum EPartType {
  17. head = "headCollider", //头部的碰撞体
  18. body = "bodyCollider", //身体的碰撞体
  19. tank = "tankCollider", //坦克的碰撞体
  20. shield = "shieldCollider", //盾的碰撞体
  21. sundries = "sundriesCollider", //杂物的碰撞体
  22. }
  23. //敌人动作类型
  24. export enum EAnimType {
  25. idle = "idle", //idle 待机状态
  26. die = "die", //die 死亡状态
  27. shoot = "shoot", //shoot 射击状态
  28. walk = "walk", //shoot 行走状态
  29. }
  30. //敌人类型枚举
  31. export enum EnemyType {
  32. SoldierPistol = 10001, // 大兵 - 普通士兵,使用手枪
  33. SniperSoldier = 10002, // 狙击兵 - 远程攻击单位,使用狙击枪
  34. ShieldSoldier = 10003, // 盾牌兵 - 高生命值防御单位,使用手枪和盾牌
  35. SnipeCaptain = 20001, // 狙击队长 - BOSS单位,使用强力狙击枪
  36. ScatterCaptain = 20002, // 机枪队长 - BOSS单位,使用高射速机关枪
  37. Tank = 20003, // 坦克 - BOSS单位,高生命值重型单位
  38. GeneralPistol = 20004 // 将军 - BOSS单位,综合型强力敌人
  39. }
  40. //武器类型枚举
  41. export enum WeaponType {
  42. Pistol = 1001, // 大兵手枪 将军手枪
  43. Sniper = 1002, // 狙击兵狙击枪 狙击队长狙击枪
  44. Shield = 1003, // 盾牌兵盾牌
  45. Scatter = 1005, // 机枪队长机关枪
  46. Tank_Pao = 1006, // 坦克
  47. }
  48. @ccclass('Enemy')
  49. export class Enemy extends BaseExp {
  50. @autoBind({type: ProgressBar,tooltip: "敌人血量"})
  51. public hpBar: ProgressBar = null!;
  52. @autoBind({type: Node,tooltip: "敌人受伤节点"})
  53. public hurt_num: Node = null!;
  54. @autoBind({type: Collider,tooltip: "敌人头部"})
  55. public headCollider: Collider = null!;
  56. @autoBind({type: Collider,tooltip: "敌人身体"})
  57. public bodyCollider: Collider = null!;
  58. @autoBind({type: Collider,tooltip: "坦克身体"})
  59. public tankCollider: Collider = null!;
  60. @autoBind({type: Node,tooltip: "坦克行走"})
  61. public tank_walk: Node = null!;
  62. @autoBind({type: Node,tooltip: "坦克射击"})
  63. public tank_shoot: Node = null!;
  64. //浓烟节点 只有军车的时候血量低于30%才会有
  65. public heavySmoke: Node = null;
  66. //敌人动画节点
  67. private skeletalAnim: SkeletalAnimation = null!;
  68. //根据敌人类型动态获取
  69. public enemyNode: Node = null!;
  70. public defaultSpeed: number = 0;
  71. public speed: number = 0;
  72. public isDead: boolean = false;
  73. //拥有的枪
  74. public gun: GunBase = null;
  75. //盾的节点
  76. public shieldNode: Node = null!;
  77. //拥有的盾可以使用的血量
  78. private _shieldHp: number = 0;
  79. public set shieldHp(value: number) {
  80. this._shieldHp = value;
  81. if(this._shieldHp <= 0){
  82. this.removeShield();
  83. }
  84. }
  85. public get shieldHp(): number {
  86. return this._shieldHp;
  87. }
  88. //当前金币的值
  89. public goldCount : number = 0;
  90. //玩家
  91. public player: Player = null!;
  92. //运动方向
  93. public moveDir: Vec3 = new Vec3();
  94. //设置的血量
  95. public curHP: number = 0;
  96. //敌人的原始血量
  97. public originHP: number = 0;
  98. //是否是没打死逃逸
  99. public escape: boolean = false;
  100. //敌人数据
  101. public data: any = null;
  102. //是否能锁定攻击对象了
  103. public isCanLock: boolean = false;
  104. //是否是在开火
  105. private isFire: boolean = false;
  106. //减血的时候是否是打到头
  107. public isShotHead: boolean = false;
  108. //敌人行走的路径
  109. public pathList: Array<Vec3> = [];
  110. //当前行走的位置
  111. public curMoveIndex: number = 0;
  112. //枪旋转角度
  113. public angle: number = 0;
  114. //新增警戒状态属性
  115. public isAlert: boolean = false;
  116. //子弹击中的的位置
  117. public raycastResults:PhysicsRayResult = null;
  118. start() {
  119. this.hurt_num.active = false;
  120. }
  121. /**
  122. * 初始化数据
  123. */
  124. public async init(data: any){
  125. this.data = data;
  126. this.player = Game.I.player.getComponent(Player);
  127. const isTank:boolean = this.isTank();
  128. this.enemyNode = this.node.getChildByName(data.prb_name);
  129. if(isTank){
  130. this.tankCollider["args"] = [EPartType.tank,this];
  131. }else{
  132. this.headCollider["args"] = [EPartType.head,this];
  133. this.bodyCollider["args"] = [EPartType.body,this];
  134. this.skeletalAnim = this.enemyNode.getComponent(SkeletalAnimation);
  135. if(this.skeletalAnim){
  136. this.skeletalAnim?.play(EAnimType.idle);
  137. }
  138. }
  139. if(!this.enemyNode){
  140. MsgHints.show(`不存在:${JSON.stringify(data)}`)
  141. return;
  142. }
  143. this.isShotHead = false;
  144. this.isAlert = false;
  145. this.data = this.enemyData(data);
  146. this.isDead = false;
  147. this.escape = false;
  148. //设置血量
  149. this.curHP = this.originHP = data.hp;
  150. //敌人速度
  151. const s: number = data.speed * 3;
  152. this.speed = s;
  153. this.defaultSpeed = s;
  154. //设置枪的数据
  155. await this.createGun();
  156. //恢复初始雪条
  157. this.hpBar.progress = 1;
  158. this.hpBar.node.active = false;
  159. this.endFire();
  160. }
  161. /**
  162. * 把武器的参数带到敌人身上
  163. */
  164. public enemyData(data:any){
  165. if(!data)return;
  166. //主武器id
  167. const mainWeaponID:number = data.weapon_id_1;
  168. let hp: number = Utils.clone(data).hp;
  169. let gData:any = userIns.enemyWeaponTable.find(e=>e.gun_id == mainWeaponID);
  170. //副武器 盾上的血量
  171. const secondWeaponID:number = data.weapon_id_2;
  172. if(secondWeaponID != 0){
  173. let sData:any = userIns.enemyWeaponTable.find(e=>e.gun_id == secondWeaponID);
  174. if(sData && sData.hp > 0){
  175. data.hp += hp + sData.shield_hp;
  176. }
  177. }
  178. return Object.assign(data,gData);
  179. }
  180. /**
  181. * 创建英雄所拥有的枪
  182. */
  183. public async createGun(){
  184. this.removeGun();
  185. //敌人主武器
  186. let gunPos:Node = this.enemyNode.getChildByName("gun_pos");
  187. const mainWeaponID:number = this.data.weapon_id_1;
  188. let mData:any = userIns.enemyWeaponTable.find(e=>e.gun_id == mainWeaponID);
  189. let gunNode:Node = await ResUtil.loadRes(`enemy/gun/${mData.gun_prb_name}`,this.enemyNode) as Node;
  190. this.gun = gunNode.getComponent(GunBase);
  191. gunNode.active = true;
  192. gunNode.parent = gunPos.parent;
  193. gunNode.worldPosition = gunPos.worldPosition.clone();
  194. gunNode.eulerAngles = gunPos.eulerAngles.clone();
  195. this.gun.init(this.data,this);
  196. //敌人附武器
  197. const secondWeaponID:number = this.data.weapon_id_2;
  198. if(secondWeaponID != 0){
  199. let sData:any = userIns.enemyWeaponTable.find(e=>e.gun_id == secondWeaponID);
  200. let shieldNode:Node = await ResUtil.loadRes(`enemy/gun/${sData.gun_prb_name}`,this.enemyNode) as Node;
  201. let shieldPos:Node = this.enemyNode.getChildByName("shield_pos");
  202. shieldNode.active = true;
  203. shieldNode.parent = shieldPos.parent;
  204. shieldNode.worldPosition = shieldPos.worldPosition.clone();
  205. shieldNode.eulerAngles = shieldPos.eulerAngles.clone();
  206. shieldNode.children[0].getComponent(Collider)["args"] = [EPartType.shield,this];
  207. this.shieldNode = shieldNode;
  208. this.shieldHp = sData.shield_hp;
  209. }
  210. }
  211. /**
  212. * 移除枪回收武器的时候调用
  213. */
  214. public removeGun(){
  215. if(this.gun){
  216. this.gun.endFire();
  217. PoolManager.putNode(this.gun.node);
  218. this.gun = null;
  219. }
  220. this.isFire = false;
  221. this.removeShield();
  222. }
  223. /**
  224. * 移除盾
  225. */
  226. public removeShield(){
  227. if(this.shieldNode){
  228. PoolManager.putNode(this.shieldNode);
  229. this.shieldNode = null;
  230. }
  231. this._shieldHp = 0;
  232. }
  233. /**
  234. * 坦克冒浓烟
  235. */
  236. public tankHeavySmoke(){
  237. if(!this.isTank())return;
  238. if(this.heavySmoke)return;
  239. //血量过低30%冒浓烟
  240. if(this.curHP < this.data.hp * 0.3){
  241. ResUtil.playParticle(
  242. `effects/Prefabs/HeavySmoke`,
  243. 0,
  244. new Vec3(0.3,0.3,0.3),
  245. (heavySmoke) => {
  246. heavySmoke.parent = this.node.parent;
  247. heavySmoke.worldPosition = this.node.worldPosition.clone();
  248. heavySmoke.active = true;
  249. this.heavySmoke = heavySmoke;
  250. }
  251. );
  252. }
  253. }
  254. /**
  255. * 扣掉血
  256. * @param hp 血
  257. * @param pData 英雄数据
  258. * @param isHeadShot 是否是爆头
  259. */
  260. public subHP(hp: number, pData:any){
  261. if(Game.I.isPause
  262. ||this.isDead
  263. ||hp == null
  264. ||this.curHP <= 0){
  265. return;
  266. }
  267. this.hpBar.node.active = true;
  268. this.scheduleOnce(() => {
  269. if(this.hpBar){this.hpBar.node.active = false;}
  270. }, 0.8);
  271. this.curHP -= hp;
  272. this.tankHeavySmoke();
  273. //这种是伤害超级高直接死亡了
  274. if(hp > this.originHP){
  275. this.escape = false;
  276. this.showHurt(Utils.numberToString(hp));
  277. this.recycle()
  278. return;
  279. }
  280. //敌人死亡
  281. if(this.curHP <= 0 && !this.isDead){
  282. this.escape = false;
  283. this.recycle();
  284. }else{//进度条和单独扣血
  285. this.hpBar.progress = this.curHP / this.originHP;
  286. this.showHurt(Utils.numberToString(hp));
  287. }
  288. }
  289. /**
  290. * 开始敌人移动,面向玩家行走
  291. */
  292. public walk(points:Vec3[]) {
  293. if(points.length <= 0)return;
  294. this.curMoveIndex = 0;
  295. this.pathList = points;
  296. const time: number = 1 / this.data.speed;
  297. if(this.isTank()){
  298. this.tank_walk.active = true;
  299. this.tank_shoot.active = false;
  300. }else{
  301. ResUtil.playSkeletalAnim(this.skeletalAnim,EAnimType.walk,time);
  302. }
  303. this.updateDir(points[0].clone());
  304. }
  305. /**
  306. * 开始攻击
  307. */
  308. public beginFire(){
  309. if(this.player
  310. && !this.player.isDead
  311. && this.gun){
  312. this.isFire = true;
  313. this.gun.fire();
  314. }
  315. }
  316. /**
  317. * 结束攻击
  318. */
  319. public endFire(){
  320. this.gun.endFire();
  321. this.isFire = false;
  322. }
  323. /**
  324. * 回收敌人节点
  325. * @param f 是否是游戏进行中的正常回收 程序主动回收不参数个数统计和加分这些
  326. * @returns
  327. */
  328. public recycle(f: boolean = true){
  329. if(!this.node
  330. ||this.isDead){
  331. return;
  332. };
  333. if(f){//爆头击杀播放音效
  334. audioMgr.playOneShot(this.isShotHead ? Constants.audios.head_shot : Constants.audios.enemy_die);
  335. }
  336. this.removeGun();
  337. this.curHP = 0;
  338. //动画和回收时间
  339. let recycleTime: number = 2;
  340. if(!this.isTank()){
  341. ResUtil.playSkeletalAnim(this.skeletalAnim,EAnimType.die);
  342. }else{
  343. //坦克爆炸后生成爆炸特效
  344. ResUtil.playParticle(
  345. `effects/Prefabs/TankBoom`,
  346. 3,
  347. new Vec3(0.1,0.1,0.1),
  348. (particle) => {
  349. particle.parent = this.node.parent;
  350. particle.worldPosition = this.node.worldPosition.clone();
  351. particle.active = true;
  352. },
  353. () => {//爆炸完成后 回收浓烟
  354. if(this.heavySmoke && this.heavySmoke.parent){
  355. PoolManager.putNode(this.heavySmoke);
  356. this.heavySmoke = null;
  357. }
  358. }
  359. );
  360. recycleTime = 3;
  361. }
  362. this.isDead = true;
  363. this.hpBar.node.active = false;
  364. let death: Function = function(inite: boolean){
  365. this.node.getComponent(UIOpacity).opacity = 255;
  366. PoolManager.putNode(this.node);
  367. if(inite){
  368. Game.I.buildEnemys.subtractEnemy(this);
  369. }
  370. }.bind(this,f);
  371. this.unschedule(death)
  372. this.scheduleOnce(death,recycleTime);
  373. }
  374. /**
  375. * 展示敌人受到的伤害
  376. * @param hpStr
  377. */
  378. public showHurt(hpStr: string) {
  379. if(Game.I.isGameOver || this.isDead) return;
  380. //敌人流血特效
  381. if(this.raycastResults){
  382. ResUtil.playParticle(
  383. `effects/Prefabs/blood`,
  384. 1,
  385. new Vec3(0.2,0.2,0.2),
  386. (blood) => {
  387. blood.active = true;
  388. blood.setParent(this.raycastResults.collider.node.parent);
  389. blood.worldPosition = this.raycastResults.hitPoint.add(this.raycastResults.hitNormal.multiplyScalar(0.01));
  390. blood.forward = this.raycastResults.hitNormal.multiplyScalar(-1);
  391. /*blood.active = true;
  392. blood.parent = this.enemyNode.parent;
  393. const targetPos: Vec3 = this.enemyNode.worldPosition.clone();
  394. blood.worldPosition = new Vec3(targetPos.x,targetPos.y - 0.2,targetPos.z);*/
  395. }
  396. );
  397. }
  398. //创建3D伤害数字
  399. const n = PoolManager.getNode(this.hurt_num, this.hurt_num.parent);
  400. let label:Label = n.getComponent(Label);
  401. label.color = Utils.hexColor(this.isShotHead ? "#F51414" : "#FFFFFF");
  402. label.string = hpStr;
  403. const oPos: Vec3 = this.hpBar.node.position.clone();
  404. n.position = oPos;
  405. const oScale: Vec3 = this.hurt_num.scale.clone();
  406. n.scale = oScale;
  407. n.getComponent(UIOpacity).opacity = 255;
  408. let bezier: Vec3[] = [];
  409. let num: number = 100;
  410. let forward = Utils.getRandomFloat(0, 1) > 0.5 ;
  411. if (forward) {
  412. bezier = [new Vec3(-(10/num)+oPos.x, (30/num)+oPos.y), new Vec3((-40/num)+oPos.x, (40/num)+oPos.y), new Vec3((-60/num)+oPos.x, oPos.y)];
  413. } else {
  414. bezier = [new Vec3((10/num)+oPos.x, (30/num)+oPos.y), new Vec3((40/num)+oPos.x, (40/num)+oPos.y), new Vec3((60/num)+oPos.x, oPos.y)];
  415. }
  416. n.position = new Vec3(bezier[2].x,bezier[2].y - (80/num),oPos.z);
  417. //贝塞尔曲线坐标函数
  418. let twoBezier = (t: number, p1: Vec3, cp: Vec3, p2: Vec3) => {
  419. let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
  420. let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
  421. return new Vec3(x, y, oPos.z);
  422. };
  423. let time: number = 0.65;
  424. tween(n)
  425. .parallel(
  426. tween()
  427. .to(time,{ scale: new Vec3(oScale.x * 0.4,oScale.y * 0.4,oScale.z * 0.4)}),
  428. tween()
  429. .to(time, bezier[2], {
  430. onUpdate: (target: Vec3, ratio: number) => {
  431. if(n.parent){
  432. const pos: Vec3 = twoBezier(ratio, bezier[0], bezier[1], bezier[2]);
  433. n.position = new Vec3(pos.x,pos.y - (80/num),oPos.z)
  434. }
  435. }
  436. })
  437. ).call(function(){
  438. tween(this.getComponent(UIOpacity))
  439. .to(0.15,{opacity: 0})
  440. .call(()=>{
  441. PoolManager.putNode(this);
  442. })
  443. .start();
  444. }.bind(n)).start();
  445. }
  446. /**
  447. * 计算是否命中玩家
  448. * @returns 返回命中状态
  449. */
  450. public calculateIsHit():boolean{
  451. let isGuaranteedHit: boolean = false;
  452. //获取射击的精准度
  453. const data:any = this.getDifficultyData();
  454. //生成0-1随机数用于命中判定 根据precision范围确定命中逻辑
  455. const random = Math.random();
  456. if(data.precision < 0){//负值100%不命中
  457. isGuaranteedHit = false;
  458. }else if(data.precision > 1) {//超过1 100%命中
  459. isGuaranteedHit = true;
  460. }else {//正常范围:随机判定
  461. isGuaranteedHit = random <= data.precision;
  462. }
  463. return isGuaranteedHit;
  464. }
  465. /**
  466. * 轻松杀敌增加游戏难度 只有单其他附加因素 例如:连杀等叠加状态 给敌人增加血
  467. */
  468. public addDiffHP(){
  469. if(!this.node
  470. ||this.isDead
  471. ||Game.I.isGameOver
  472. ||this.isDead){
  473. return;
  474. };
  475. const data:any = this.getDifficultyData();
  476. if(data.hp_ratio > 0){
  477. this.curHP += this.originHP * data.hp_ratio;
  478. }
  479. }
  480. /**
  481. * 敌人的射击的精准度 获得难度数据
  482. */
  483. public getDifficultyData(): any{
  484. //获取基础精准度(玩家预设值)
  485. const precision: number = userIns.getCurLevelData().precision;
  486. let basePrecision: number = precision;
  487. let diff:any = {
  488. precision: precision,//增加或减少的精准度 0-100%
  489. hp_ratio: 0,//增加或减少的血量 0-100%
  490. reaction_time: 0,//增加或减少的反应时间 0-100%
  491. }
  492. const each: number = Math.floor(Game.I.buildEnemys.getCurKillNum() / 3);
  493. //连杀3个敌人的次数 AI精准度增加1%
  494. diff.precision += each * 0.01;
  495. //连续通关3次以上才有的难度
  496. const lx_num: number = 3;
  497. if(userIns.passNum > lx_num){//1、连续通关 AI生命值增加15% 精准度增加5%
  498. diff.precision += userIns.passNum * 0.05;
  499. diff.precision = Math.min(basePrecision, 0.9);
  500. diff.hp_ratio += userIns.passNum * 0.15;
  501. diff.hp_ratio = Math.min(diff.hp_ratio, 1);
  502. }else{//2、通关失败 下一局 AI精准度下降5% AI反应时间增加0.5s
  503. diff.precision -= userIns.passNum * 0.05;
  504. diff.precision = Math.max(diff.precision, 0.1);
  505. diff.reaction_time += 0.5;
  506. }
  507. return diff;
  508. }
  509. /**
  510. * 更新敌人行走和变化方向
  511. */
  512. protected update(dt: number): void {
  513. if(Game.I.isGameOver
  514. || Game.I.isPause
  515. || !this.data
  516. || this.isDead) return;
  517. const targetPos = this.pathList[this.curMoveIndex];
  518. if(!targetPos)return;
  519. //保持速度调节逻辑
  520. //dt = dt / Game.I.map.multiplySpeed();
  521. const currentPos = this.node.worldPosition.clone();
  522. const toTarget = targetPos.clone().subtract(currentPos);
  523. //敌人移动
  524. const moveDir = new Vec3();
  525. Vec3.subtract(moveDir, targetPos, currentPos);
  526. let distance:number = moveDir.length();
  527. //计算实际应移动距离
  528. const moveDistance = this.speed * dt;
  529. if(moveDistance > 0) {
  530. //使用标准化方向向量 + 实际移动距离
  531. const newPos = currentPos.add(toTarget.normalize().multiplyScalar(moveDistance));
  532. this.node.worldPosition = newPos;
  533. //添加移动平滑过渡 平滑系数 10-15比较好 值越大越平滑
  534. /*const smoothFactor = 10;
  535. this.node.worldPosition = Vec3.lerp(
  536. new Vec3(),
  537. currentPos,
  538. newPos,
  539. Math.min(1, dt * smoothFactor)
  540. );*/
  541. //更新方向
  542. const isLastPathPoint = this.curMoveIndex == this.pathList.length - 1;
  543. const dirTarget = isLastPathPoint
  544. ? Game.I.player.node.worldPosition
  545. : this.pathList[this.curMoveIndex + 1];
  546. this.updateDir(dirTarget);
  547. }
  548. //到达判断增加缓冲范围
  549. if(moveDistance > distance) {
  550. //从出生点走到第二个点就是外门第一个点就开门 并且门没有被打开
  551. if(this.curMoveIndex == 1
  552. && !Game.I.buildEnemys.isOpenDoor){
  553. Game.I.map.openDoor();
  554. Game.I.buildEnemys.isOpenDoor = true;
  555. }
  556. this.curMoveIndex++;
  557. if (this.curMoveIndex >= this.pathList.length) {
  558. this.beginFire();
  559. if(this.isTank()){
  560. this.tank_walk.active = false;
  561. this.tank_shoot.active = true;
  562. } else {
  563. ResUtil.playSkeletalAnim(this.skeletalAnim, EAnimType.shoot, this.data.atk_speed);
  564. }
  565. this.updateDir(Game.I.player.node.worldPosition);
  566. }
  567. }
  568. }
  569. /**
  570. * 更新敌人方向(根据目标坐标转向)
  571. * @param targetPos 目标坐标(路径点或玩家坐标)
  572. */
  573. public updateDir(targetPos: Vec3){
  574. const curPos = this.node.worldPosition.clone();
  575. //从目标位置指向当前位置
  576. const targetDir = Vec3.subtract(new Vec3(), curPos, targetPos);
  577. targetDir.normalize();
  578. //平滑插值转向 避免瞬间转向
  579. const currentDir = this.node.forward.clone();
  580. Vec3.slerp(currentDir, currentDir, targetDir, 0.15);
  581. this.node.forward = currentDir;
  582. }
  583. /**
  584. * 是否是坦克
  585. */
  586. public isTank():boolean{
  587. return this.data.id == EnemyType.Tank;
  588. }
  589. }