FSprite.ts 25 KB

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