jingyan.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { VibrateType } from "../../common-plugin/Scripts/YZ_Constant";
  2. import { cocosz } from "../Framework/CocosZ";
  3. import Constant from "../Framework/Constant";
  4. import { gameMgr } from "./gameMgr";
  5. import { upgradeMgr } from "./UpgradeMgr";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export default class Jingyan extends cc.Component {
  9. isRun: boolean = false;
  10. // 成功距离
  11. moveSpeed: number = 1000;
  12. onLoad() { }
  13. start(): void { }
  14. init() {
  15. this.isRun = false;
  16. cc.game.on(Constant.E_Skill_Citie, (e) => { this.isRun = true; }, this);
  17. }
  18. finish() {
  19. upgradeMgr.curJingyan += 1;
  20. cc.game.emit(Constant.E_GAME_LOGIC, { type: Constant.E_Jingyan_Finish });
  21. if (!cocosz.isPause) {
  22. gameMgr.playEffect("jingyan");
  23. gameMgr.shakeEffect(0, 0, true, VibrateType.Long);
  24. }
  25. // 回收
  26. cc.game.targetOff(this);
  27. gameMgr && gameMgr.isValid && gameMgr.nodePut("jingyan", this.node);
  28. }
  29. private _time: number = -1;
  30. protected update(dt: number): void {
  31. if (this.isRun == false) {
  32. if (++this._time % 10 == 0) {
  33. this.activating();
  34. }
  35. }
  36. else {
  37. this.followTo(dt);
  38. }
  39. }
  40. activating() {
  41. if (upgradeMgr && upgradeMgr.isValid && gameMgr && gameMgr.playerTs && gameMgr.playerTs.isValid) {
  42. let from: cc.Vec2 = this.node.getPosition();
  43. let to: cc.Vec2 = gameMgr.playerTs.node.getPosition();
  44. if (from.sub(to).mag() < upgradeMgr.jingyanRange) {
  45. this.isRun = true;
  46. }
  47. }
  48. }
  49. followTo(dt) {
  50. if (upgradeMgr && upgradeMgr.isValid && gameMgr && gameMgr.playerTs && gameMgr.playerTs.isValid) {
  51. let from = this.node.getPosition();
  52. let to = gameMgr.playerTs.node.getPosition();
  53. let div = to.subSelf(from);
  54. let moveDis = this.moveSpeed * dt;
  55. // 成功到达
  56. if (div.mag() < moveDis) {
  57. this.finish();
  58. } else {
  59. this.node.setPosition(from.addSelf(div.normalizeSelf().mulSelf(moveDis)));
  60. }
  61. }
  62. }
  63. }