FSprite.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. import FObject, { GroupType } from "./FObject";
  2. import CMath from "../../../util/CMath";
  3. import BObject from "../../fight/bullet/BObject";
  4. import FPanel from "./FPanel";
  5. import { FFAttr } from "../../data/FFCalAttr";
  6. import AIBase from "./AI/AIBase";
  7. import FPanelIcon from "./FPanelIcon";
  8. import Equip from "../../home/equip/Equip";
  9. import { __SkillData } from "../../data/sdata/SManage";
  10. const { ccclass, property } = cc._decorator;
  11. /**
  12. * 精灵状态
  13. */
  14. export const SpriteType = cc.Enum({
  15. NONE: 0,//站立
  16. MOVE: 1,//移动
  17. // ATK: 2, //攻击
  18. });
  19. /**
  20. * 动画类型
  21. */
  22. export const SpriteActionType = cc.Enum({
  23. stand: 'idle',//站立
  24. move: 'move',//移动
  25. run: 'run',//跑步
  26. atk: 'atk',//行走攻击
  27. atk1: 'atk1',//站立攻击
  28. dead: 'dead',//死亡
  29. yun: 'yun',//倒地晕
  30. yun2: 'yun2',//晕倒地上
  31. dang: 'dang',//惊吓
  32. chuizi: 'chuizi',//锤子
  33. shiqu: 'shiqu'//捡起
  34. });
  35. /**
  36. * 碰撞精灵
  37. */
  38. export interface ColliderSprite {
  39. sprite: FSprite,
  40. dis: number,
  41. }
  42. /**
  43. * 精灵
  44. */
  45. @ccclass
  46. export default class FSprite extends FObject {
  47. /**
  48. * 当前调用的动画
  49. */
  50. public spine: sp.Skeleton = null;
  51. // public hurtSpine: sp.Skeleton = null;
  52. // public hurtParticle: cc.ParticleSystem = null;
  53. mBullet: cc.Prefab = null;//攻击效果
  54. // @property(cc.Node)
  55. // mWeapon: cc.Node = null;//武器
  56. public mAtkSite: sp.spine.Bone = null;//子弹发射插槽
  57. public mWeapon1: sp.spine.Bone = null;//武器
  58. mPanels: Array<FPanel> = [];//显示面板
  59. public mRigidBody: cc.RigidBody;//刚体
  60. public status = SpriteType.NONE;//当前状态
  61. /**
  62. * 当前剩余血量
  63. */
  64. public hp: number = -1;
  65. /**
  66. * 属性
  67. */
  68. public attrData: FFAttr = null;
  69. /**
  70. * 是否被激活
  71. */
  72. public isActive = true;
  73. public SPEED_WALK = 300;//行走速度
  74. // public SPEED_RUN = 350;//跑步速度
  75. public SPEED_SHOOT = 100;//攻击时候的移动速度
  76. /**
  77. * 当前移动的方向
  78. */
  79. private dir = { x: 0, y: 0 };
  80. public moveV2: cc.Vec2 = cc.v2(1, 0);
  81. public isWalk = false;//是否行走
  82. public isRuning = false;//是否跑
  83. public isShooting = false;//是否按住攻击按钮
  84. public wAngle = 0;//当前操作武器旋转角度
  85. /**
  86. * 当前是否暂停状态
  87. */
  88. public gamePause: boolean = false;
  89. public hitCount = 0;
  90. private yanwu: cc.Node = null;
  91. /**
  92. * 当前是否有盾,有盾的时候减少50%伤害
  93. */
  94. public hasDun = false;
  95. /**
  96. * 当前是否处于无敌状态
  97. */
  98. public hasWudi = false;
  99. onLoad() {
  100. let spriteNode = this.node.getChildByName('juese01');
  101. let spineNode0 = spriteNode.getChildByName('spineRight');
  102. if (this.node.name == "s1") {
  103. this.yanwu = spriteNode.getChildByName("yanwu_gs");
  104. }
  105. // this.hurtSpine = spriteNode.getChildByName("hurt").getComponent(sp.Skeleton);
  106. // this.hurtParticle = spriteNode.getChildByName("particle").getComponent(cc.ParticleSystem);
  107. this.mRigidBody = this.node.getComponent(cc.RigidBody);
  108. this.mRigidBody.allowSleep = false;
  109. this.spine = spineNode0.getComponent(sp.Skeleton);
  110. this.mAtkSite = this.spine.findBone('zidan');
  111. this.mWeapon1 = this.spine.findBone('control');
  112. }
  113. start() {
  114. if (this.id > 0) {//载入预设怪物数据
  115. let main = this.map.ff.main;
  116. let attrData = main.sManage.getMonsterData(this.id);
  117. this.setAttrData(attrData);
  118. this.hp = this.attrData.hp;
  119. for (let i = 0; i < this.mPanels.length; i++) {
  120. const element = this.mPanels[i];
  121. element.updatePanel()
  122. }
  123. }
  124. this.setYanwu(false);
  125. this.updateSkin()
  126. }
  127. /**
  128. * 使用锤子
  129. */
  130. public useHammer() {
  131. Equip.chaneWeapon(this.spine, '1000')
  132. }
  133. public updateSkin() {
  134. // if (this.hp <= 0) {
  135. // return
  136. // }
  137. // if (this.attrData.skin) {
  138. // this.spine.setSkin(this.attrData.skin)
  139. // }
  140. // if (this.attrData.weapon) {
  141. // Equip.chaneWeapon(this.spine, this.attrData.weapon)
  142. // }
  143. }
  144. update(dt) {
  145. if (this.gamePause) {
  146. return;
  147. }
  148. this.updateMove(dt)
  149. }
  150. public setPause(pause: boolean) {
  151. this.gamePause = pause
  152. if (this.gamePause) {
  153. this.setDir({ x: 0, y: 0 });
  154. this.setWalk(false);
  155. }
  156. }
  157. /**
  158. * 设置坐标和方向
  159. */
  160. public setPosition(pos) {
  161. this.node.setPosition(pos);
  162. }
  163. /**
  164. * 设置子弹
  165. * @param mBullet
  166. */
  167. public setBullet(mBullet) {
  168. this.mBullet = mBullet;
  169. }
  170. /**
  171. * 设置属性
  172. * @param attrData
  173. */
  174. public setAttrData(attrData: FFAttr) {
  175. this.attrData = attrData;
  176. //mBullet
  177. if (this.mBullet) {
  178. let node = cc.instantiate(this.mBullet);
  179. node.destroy();
  180. } else {
  181. if (!attrData.bullet) {
  182. attrData.bullet = '1001'
  183. }
  184. attrData.bullet = '1101'
  185. cc.resources.load('prefab/bullet/' + attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
  186. if (err) {
  187. cc.error(err);
  188. } else {
  189. //加载结束
  190. this.mBullet = prefab;
  191. let node = cc.instantiate(this.mBullet);
  192. node.destroy();
  193. }
  194. });
  195. }
  196. let asset = 'prefab/common/hp_monter';
  197. if (this.node.group == 'B') {
  198. asset = 'prefab/common/hp_monter_red'
  199. }
  200. cc.resources.load(asset, cc.Prefab, (err, prefab: cc.Prefab) => {
  201. if (err) {
  202. cc.error(err);
  203. } else {
  204. //加载结束
  205. let node = cc.instantiate(prefab);
  206. let panel = node.getComponent(FPanel);
  207. panel.sprite = this;
  208. this.mPanels.push(panel);
  209. node.y = -10;
  210. node.parent = this.node;
  211. panel.updatePanel();
  212. }
  213. });
  214. }
  215. public flushAttrData(attrData: FFAttr) {
  216. this.attrData = attrData;
  217. this.updateSkin()
  218. if (!attrData.bullet) {
  219. attrData.bullet = '1001'
  220. }
  221. cc.resources.load('prefab/bullet/' + attrData.bullet, cc.Prefab, (err, prefab: cc.Prefab) => {
  222. if (err) {
  223. cc.error(err);
  224. } else {
  225. //加载结束
  226. this.mBullet = prefab;
  227. let node = cc.instantiate(this.mBullet);
  228. node.destroy();
  229. }
  230. });
  231. }
  232. /**
  233. * 添加面板显示
  234. */
  235. public addPanelHUD() {
  236. let res = this.ff.fres;
  237. let node = cc.instantiate(res.mHudPrefab)
  238. node.parent = res.mHudNode;
  239. let panelIcon = node.getComponent(FPanelIcon)
  240. panelIcon.sprite = this
  241. panelIcon.setIcon()
  242. this.mPanels.push(panelIcon)
  243. }
  244. protected setWeaponAngle(angle: number) {
  245. if (this.mWeapon1) {
  246. this.wAngle = angle * 180 / Math.PI;
  247. // console.log("=====wAngle===", this.wAngle);
  248. this.mWeapon1.rotation = this.wAngle + 180;//this.spine.node.scaleX > 0 ? this.wAngle - 90 : -(this.wAngle - 90);
  249. }
  250. }
  251. /**
  252. * 更新武器旋转角度
  253. */
  254. protected updateWeaponAngle(angle: number) {
  255. if (this.mWeapon1) {
  256. // this.mWeapon1.data.rotation = (angle - 90) * this.spine.node.scaleX + 90
  257. this.mWeapon1.rotation = (angle - 90) * this.spine.node.scaleX
  258. // console.log("======武器角度=====", this.mWeapon1.rotation)
  259. }
  260. }
  261. /**
  262. * 角色状态改变后更新角色动作
  263. */
  264. public updateAction() {
  265. if (this.isWalk) {
  266. if (this.isShooting) {
  267. this.createBullet();
  268. this.playAction(SpriteActionType.atk, false, () => {
  269. this.updateSpine();
  270. // this.tmpDir.x = this.dir.x;
  271. // this.tmpDir.y = this.dir.y;
  272. this.updateAction();
  273. });
  274. } else {
  275. this.playAction(SpriteActionType.move, true);
  276. }
  277. } else {
  278. if (this.isShooting) {
  279. this.createBullet();
  280. this.playAction(SpriteActionType.atk1, false, () => {
  281. this.updateSpine();
  282. // this.tmpDir.x = this.dir.x;
  283. // this.tmpDir.y = this.dir.y;
  284. this.updateAction();
  285. });
  286. } else {
  287. this.playAction(SpriteActionType.stand, true);
  288. }
  289. }
  290. }
  291. /**
  292. * 更新动画朝向
  293. */
  294. public updateSpine(): boolean {
  295. let csprite: FSprite = this.findEnemy(1300).sprite;
  296. let p1 = this.node.getPosition();
  297. if (csprite && csprite.isValid) {
  298. let p2 = csprite.node.getPosition();
  299. if (p1.x == p2.x) {
  300. } else if (p1.x - p2.x > 3) {
  301. this.setLR(-1);
  302. } else {
  303. this.setLR(1);
  304. }
  305. //计算弧度
  306. // let angle = Math.floor(CMath.getAngle(p1, p2) * (180 / Math.PI));
  307. //this.wAngle = angle
  308. // this.updateWeaponAngle(angle);
  309. } else {
  310. // this.updateSpineByDir();
  311. // this.updateWeaponAngle(this.wAngle);
  312. }
  313. return false;
  314. }
  315. private updateSpineByDir() {
  316. if (this.dir.x >= 0) {
  317. this.setLR(1);
  318. } else {
  319. this.setLR(-1);
  320. }
  321. }
  322. /**
  323. * 设置精灵方向
  324. */
  325. public setDir(dir): boolean {
  326. if (this.gamePause) {
  327. return;
  328. }
  329. if (dir.x == 0 && dir.y == 0) {
  330. this.setWalk(false);
  331. return false;
  332. }
  333. if (this.isWalk && this.dir.x == dir.x && this.dir.y == dir.y) {
  334. return true;
  335. }
  336. this.dir.x = dir.x;
  337. this.dir.y = dir.y;
  338. this.setWalk(true);
  339. return true;
  340. }
  341. /**
  342. * 设置是否移动
  343. * @param isWalk
  344. */
  345. public setWalk(isWalk: boolean) {
  346. this.isWalk = isWalk;
  347. // this.updateSpine()
  348. // this.updateAction();
  349. }
  350. /**
  351. * 设置是否跑
  352. * @param isRuning
  353. */
  354. // public setRuning(isRuning: boolean) {
  355. // this.isRuning = isRuning;
  356. // this.updateAction();
  357. // }
  358. /**
  359. * 攻击时候的方向
  360. */
  361. /**
  362. * 设置攻击
  363. * @param isShooting
  364. */
  365. public setShooting(isShooting: boolean) {
  366. this.isShooting = isShooting;
  367. if (isShooting && this.status == SpriteType.NONE) {
  368. // this.tmpDir.x = this.dir.x;
  369. // this.tmpDir.y = this.dir.y;
  370. this.updateAction();
  371. }
  372. }
  373. public updateMove(dt) {
  374. if (this.isWalk) {
  375. // this.mRigidBody.allowSleep = false;
  376. let speed = 0;
  377. if (this.isShooting) {
  378. speed = this.SPEED_SHOOT;
  379. } else {
  380. speed = this.SPEED_WALK;
  381. }
  382. this.node.x += this.dir.x * dt * speed;
  383. this.node.y += this.dir.y * dt * speed;
  384. this.setYanwu(true);
  385. } else {
  386. // this.mRigidBody.allowSleep = true;
  387. this.setYanwu(false);
  388. }
  389. }
  390. /**
  391. * 移动到某个点
  392. * @param x
  393. * @param y
  394. */
  395. public moveTo(pos: cc.Vec2, dt): boolean {
  396. // this.node.x = pos.x;
  397. // this.node.y = pos.y;
  398. let speed = 1200;
  399. let px = this.node.x > pos.x ? -speed : speed;
  400. let py = this.node.y > pos.y ? -speed : speed;
  401. this.node.x += dt * px;
  402. this.node.y += dt * py;
  403. if (px > 0) {
  404. if (this.node.x > pos.x) {
  405. this.node.x = pos.x;
  406. }
  407. } else {
  408. if (this.node.x < pos.x) {
  409. this.node.x = pos.x;
  410. }
  411. }
  412. if (py > 0) {
  413. if (this.node.y > pos.y) {
  414. this.node.y = pos.y;
  415. }
  416. } else {
  417. if (this.node.y < pos.y) {
  418. this.node.y = pos.y;
  419. }
  420. }
  421. return false;
  422. }
  423. // public playAction(nActionType, loop?: boolean, finishBack?: () => void) {
  424. // if (finishBack) {
  425. // this.spine.setCompleteListener(() => {
  426. // this.spine.setCompleteListener(null);
  427. // finishBack();
  428. // });
  429. // }
  430. // if (!this.isActive) {
  431. // return;
  432. // }
  433. // this.spine.setAnimation(0, nActionType, loop);
  434. // }
  435. public setLR(lr: number) {
  436. let scaleX = Math.abs(this.spine.node.scaleX);
  437. if (lr == -1) {
  438. this.spine.node.scaleX = -scaleX;
  439. } else {
  440. this.spine.node.scaleX = scaleX;
  441. }
  442. }
  443. public getLR(): number {
  444. if (this.spine.node.scaleX > 0) {
  445. return 1
  446. } else {
  447. return -1;
  448. }
  449. }
  450. /**
  451. * hit事件回调
  452. */
  453. public fireCallback: () => void;
  454. public setFireCallback(fireCallback: () => void) {
  455. this.fireCallback = fireCallback;
  456. }
  457. /**
  458. *
  459. * @param prefab 添加攻击特效
  460. */
  461. public addSendEffect(prefab: cc.Prefab, target: cc.Node) {
  462. if (prefab) {
  463. let node = cc.instantiate(prefab);
  464. let x = this.mAtkSite.worldX * this.spine.node.scaleX
  465. let y = this.mAtkSite.worldY
  466. let pos = cc.v2(x, y);
  467. node.setPosition(pos);
  468. node.parent = this.node;
  469. // if(this.mWeapon1){
  470. // node.angle = this.mWeapon1.data.rotation*this.spine.node.scaleX
  471. // node.scaleX = this.spine.node.scaleX
  472. // }
  473. if (target) {
  474. let p1 = this.node.getPosition();
  475. let p2 = target.getPosition();
  476. //计算弧度
  477. let angle = Math.floor(CMath.getAngle(p1, p2) * (180 / Math.PI));
  478. node.angle = angle
  479. } else {
  480. node.angle = this.wAngle
  481. }
  482. let spine = node.getComponent(sp.Skeleton)
  483. spine.setCompleteListener(() => {
  484. node.destroy()
  485. });
  486. spine.setAnimation(0, 'atk', false)
  487. }
  488. }
  489. createBullet() {
  490. if (this.isValid && !this.gamePause) {
  491. let node = cc.instantiate(this.mBullet);
  492. node.group = 'bullet';
  493. let x = this.node.x + this.mAtkSite.worldX * this.spine.node.scaleX
  494. let y = this.node.y + this.mAtkSite.worldY
  495. let pos = cc.v2(x, y);
  496. node.setPosition(pos);
  497. let bObject = node.getComponent(BObject);
  498. bObject.setSprite(this);
  499. let csprite: FSprite = this.findEnemy(2000).sprite;
  500. node.parent = this.map.mSprites;
  501. if (csprite && csprite.isValid) {
  502. bObject.fire(csprite.node);
  503. this.addSendEffect(bObject.mStartEffect, csprite.node);
  504. } else {//没有目标的时候,向朝向开火
  505. bObject.fireAngle1(this.wAngle);
  506. this.addSendEffect(bObject.mStartEffect, null);
  507. }
  508. if (this.fireCallback) {
  509. this.fireCallback();
  510. }
  511. if (this.node == this.ff.mainSprite.node) {
  512. if (this.hitCount > 4) {
  513. this.ff.shockMap();
  514. this.hitCount = 0;
  515. } else {
  516. this.hitCount++;
  517. }
  518. }
  519. this.ff.main.playerEffectByPath('music/magic_1001_fs');
  520. }
  521. }
  522. public tmpActionType = null;
  523. public playAction(nActionType, loop?: boolean, finishBack?: () => void): boolean {
  524. if (!this.isActive) {
  525. return false;
  526. }
  527. if (!this.spine) {
  528. return false;
  529. }
  530. // cc.log('nActionType : ',this.tmpActionType,nActionType);
  531. if (nActionType == this.tmpActionType) {
  532. return false;
  533. }
  534. if (finishBack) {
  535. this.spine.setCompleteListener(() => {
  536. this.spine.setCompleteListener(null);
  537. this.tmpActionType = null;
  538. finishBack();
  539. });
  540. }
  541. //this.spine.setEventListener(null);
  542. // cc.log('nActionType : ',this.tmpActionType,nActionType);
  543. this.tmpActionType = nActionType;
  544. this.spine.setAnimation(0, nActionType, loop);
  545. return true
  546. }
  547. public playAction2(nActionType, loop?: boolean, finishBack?: () => void) {
  548. this.status = SpriteType.NONE
  549. if (finishBack) {
  550. this.spine.setCompleteListener(() => {
  551. this.spine.setCompleteListener(null);
  552. this.tmpActionType = null;
  553. finishBack();
  554. });
  555. }
  556. this.spine.setEventListener(null);
  557. this.spine.setAnimation(0, nActionType, loop);
  558. }
  559. /**
  560. * 攻击对手进行结算
  561. * @param target
  562. */
  563. public atkjs(target: FSprite, _skillData?: __SkillData) {
  564. let reslut = this.calculate(this.attrData, target.attrData, _skillData);
  565. target.reduce(reslut);
  566. if (this == this.ff.mainSprite as FSprite) {
  567. this.ff.flushHP(target);
  568. } else {
  569. this.ff.updateHP(target);
  570. }
  571. // this.ff.main.playSound2('music/magic_1001_sj');
  572. //对方存在AI,设置仇恨
  573. if (target.hp > 0) {
  574. let baseAI = target.node.getComponent(AIBase);
  575. if (baseAI) {
  576. baseAI.setTarget(this);
  577. }
  578. // 播放受击动画和粒子特效
  579. // if (target.node.group == "B") {
  580. // target.hurtParticle.enabled = true;
  581. // target.hurtSpine.enabled = true;
  582. // target.hurtSpine.setAnimation(0, "hurt", false);
  583. // target.hurtSpine.setCompleteListener(() => {
  584. // target.hurtParticle.enabled = false;
  585. // })
  586. // }
  587. }
  588. }
  589. /**
  590. * 被攻击
  591. * @param attrData
  592. */
  593. public bAtkjs(attrData: FFAttr) {
  594. let reslut = this.calculate(attrData, this.attrData);
  595. this.reduce(reslut);
  596. }
  597. /**
  598. * 计算攻击
  599. * @param fd
  600. */
  601. private calculate(A: FFAttr, B: FFAttr, _skillData?: __SkillData): any {
  602. let xx = A.atk - B.def;
  603. if (_skillData) {
  604. // 1 水
  605. // 2 火
  606. // 3 土
  607. // 4 风
  608. // 5 雷
  609. // 6 光
  610. // 7 暗
  611. if (_skillData.value4 == 1) {
  612. xx += A.water
  613. } else if (_skillData.value4 == 2) {
  614. xx += A.fire
  615. } else if (_skillData.value4 == 3) {
  616. xx += A.earth
  617. } else if (_skillData.value4 == 4) {
  618. xx += A.wind
  619. } else if (_skillData.value4 == 5) {
  620. xx += A.thunder
  621. }
  622. }
  623. if (xx <= 0) {
  624. xx = 1;
  625. }
  626. let hp = xx * 500 / (B.def + 300);
  627. if (hp < 1) {
  628. hp = 1;
  629. }
  630. let bj = false
  631. let sk = false
  632. let rand = CMath.getRandom(1, 100)
  633. if (rand < 30) {
  634. bj = true
  635. hp += hp * 3 / 10
  636. }
  637. if (_skillData) {
  638. hp = hp * _skillData.value1 / 100
  639. sk = true
  640. }
  641. let xd = hp / 10;
  642. let xxd = CMath.getRandom(-xd, xd);
  643. hp += xxd;
  644. if (hp < 10) {
  645. hp = CMath.getRandom(1, 10);
  646. }
  647. hp = Math.floor(hp)
  648. return {
  649. hp: Math.floor(hp),
  650. bj: bj,
  651. sk: sk
  652. }
  653. }
  654. /**
  655. * 返回敌人的分组
  656. */
  657. public getEnemyGroup() {
  658. return this.node.group == 'A' ? 'B' : 'A';
  659. }
  660. /**
  661. * 查找周边可攻击对象
  662. */
  663. public findEnemy(radius: number): ColliderSprite {
  664. let mGroup = this.getEnemyGroup();
  665. let minDis = 0
  666. let targetSprite = null;
  667. if (this.map) {
  668. let nodes = this.map.getSprites();
  669. for (let i = 0; i < nodes.length; i++) {
  670. const node = nodes[i];
  671. let sobj = node.getComponent(FSprite);
  672. if (sobj && node.active && sobj.isActive && sobj.hp > 0 && sobj.node.group == mGroup) {
  673. let dis = cc.Vec2.distance(this.node.getPosition(), node.getPosition());
  674. if (dis < radius) {
  675. if (targetSprite == null) {
  676. targetSprite = sobj;
  677. minDis = dis;
  678. } else if (dis < minDis) {
  679. targetSprite = sobj;
  680. minDis = dis;
  681. }
  682. }
  683. }
  684. }
  685. }
  686. return {
  687. sprite: targetSprite,
  688. dis: minDis
  689. };
  690. }
  691. public wudi(time) {
  692. this.hasWudi = true
  693. cc.tween(this.node).sequence(
  694. cc.delayTime(time),
  695. cc.callFunc(() => {
  696. this.hasWudi = false
  697. })
  698. ).start()
  699. }
  700. public reduce(result: any) {
  701. let hp = result.hp
  702. if (hp <= 0) {
  703. return
  704. }
  705. if (this.hasWudi) {
  706. return
  707. }
  708. if (this.hp <= 0) {//已经死了
  709. return
  710. }
  711. if (!this.ff.lockCamera) {
  712. return
  713. }
  714. let bj = result.bj
  715. let sk = result.sk
  716. this.playHit();
  717. if (this.hasDun) {
  718. hp = Math.floor(hp / 2);
  719. }
  720. if (sk) {
  721. this.showNumber('/' + hp, this.ff.mSkillFont);
  722. } else if (bj) {
  723. this.showNumber('/' + hp, this.ff.mBjFont, null, true);
  724. } else {
  725. this.showNumber('/' + hp, this.ff.mRed);
  726. }
  727. this.hp -= hp;
  728. if (this.hp <= 0) {
  729. this.hp = 0;
  730. this.isActive = false;
  731. if (this.ff.clearCallback) {
  732. this.ff.clearCallback(this);
  733. }
  734. if (this.ff.removeCallback) {
  735. this.ff.removeCallback(this);
  736. }
  737. if (this == this.ff.mainSprite as FSprite) {
  738. // this.ff.mainSprite = null;
  739. } else {
  740. this.removeSelf();
  741. }
  742. }
  743. this.updatePanel();
  744. }
  745. public updatePanel() {
  746. for (let i = 0; i < this.mPanels.length; i++) {
  747. const element = this.mPanels[i];
  748. element.updatePanel();
  749. }
  750. }
  751. public removeSelf() {
  752. this.gamePause = true;
  753. this.playAction2(SpriteActionType.dead, false, () => {
  754. this.node.destroy();
  755. });
  756. }
  757. /**
  758. * 显示字符
  759. * ; +
  760. * : -
  761. * @param hp
  762. * @param font
  763. */
  764. private showNumber(str: string, font: cc.Font, color?: cc.Color, bs?: boolean) {
  765. let node: cc.Node = new cc.Node('hp');
  766. node.group = 'map';
  767. let lable: cc.Label = node.addComponent(cc.Label);
  768. lable.fontSize = 30;
  769. if (font) {
  770. lable.font = font;
  771. }
  772. if (color) {
  773. node.color = color;
  774. }
  775. lable.string = str;
  776. node.x = this.node.x;
  777. node.y = this.node.y + this.node.height * 3 / 4;
  778. this.map.node.addChild(node);
  779. let moveBy = cc.moveBy(2, cc.v2(0, 150)).easing(cc.easeSineOut());
  780. let fadeout = cc.fadeOut(2);
  781. let spawn = null
  782. if (bs) {
  783. let seq = cc.sequence(cc.scaleTo(0.05, 1.5), cc.scaleTo(0.2, 1.2));
  784. spawn = cc.spawn(moveBy, fadeout, seq);
  785. } else {
  786. spawn = cc.spawn(moveBy, fadeout);
  787. }
  788. let seq = cc.sequence(cc.delayTime(0.1), spawn, cc.callFunc(() => {
  789. node.removeFromParent(true);
  790. node.destroy();
  791. }));
  792. node.runAction(seq);
  793. }
  794. /**
  795. * 播放受击动作
  796. */
  797. public playHit() {
  798. let spriteNode = this.node.getChildByName('juese01');
  799. if (spriteNode) {
  800. cc.tween(spriteNode).sequence(
  801. cc.scaleTo(0.06, 1.1, 0.95),
  802. cc.scaleTo(0.06, 1, 1.1),
  803. cc.scaleTo(0.06, 1, 1),
  804. ).start();
  805. // cc.tween(spriteNode).sequence(
  806. // cc.moveTo(0.1,cc.v2(0,-9)),
  807. // cc.moveTo(0.16,cc.v2(0,6)),
  808. // cc.moveTo(0.23,cc.v2(0,-6)),
  809. // cc.moveTo(0.3,cc.v2(0,0)),
  810. // ).start();
  811. }
  812. }
  813. public addActiveIcon(): cc.Node {
  814. let node = new cc.Node('activeIcon');
  815. let sprite = node.addComponent(cc.Sprite);
  816. sprite.spriteFrame = this.ff.mActiveIcon;
  817. node.parent = this.node;
  818. node.y = this.node.height + 20
  819. return node;
  820. }
  821. public removeActiveIcon() {
  822. let node = this.node.getChildByName('activeIcon');
  823. if (node) {
  824. node.destroy();
  825. }
  826. }
  827. /**
  828. * 后面的
  829. */
  830. public nextSprite: FSprite;
  831. public setNext(sprite: FSprite) {
  832. this.nextSprite = sprite;
  833. }
  834. /**
  835. * 目标位置的切线方向移动
  836. * @param target
  837. */
  838. public tangentMove(target: cc.Node) {
  839. }
  840. public setYanwu(status: boolean) {
  841. if (this.yanwu) {
  842. if (this.yanwu.active == status) return
  843. this.yanwu.active = status;
  844. }
  845. }
  846. public dead(){
  847. }
  848. }