dog_obj.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. import Utils from "../../../framework/utils/utils";
  8. import { ROLE_STATE } from "../../utrl/gameEnum";
  9. import GameObject from "../gameObject";
  10. import PlayerObject from "./playerObject";
  11. const { ccclass, property } = cc._decorator;
  12. enum state {
  13. standy = 0,
  14. walk = 1,
  15. hit = 2,
  16. die = 3,
  17. }
  18. @ccclass
  19. export default class dog_obj extends GameObject {
  20. spine: any;
  21. followNode: any;
  22. curState: state;
  23. // LIFE-CYCLE CALLBACKS:
  24. // onLoad () {}
  25. start() {
  26. this.spine = this.node.$spine.getComponent(sp.Skeleton);
  27. Utils.loadSpineBunble(this.spine, 'dog/dog', () => {
  28. this.spine.setAnimation(0, 'standy1', true);
  29. this.curState = state.standy;
  30. this.setSkin('dog');
  31. });
  32. }
  33. setFollowTarget(node) {
  34. this.followNode = node;
  35. }
  36. setSkin(name) {
  37. this.spine.setSkin(name);
  38. }
  39. follow(target) {
  40. if (!target || !cc.isValid(target) || target.getComponent(PlayerObject).isDie) {
  41. if (this.node && cc.isValid(this.node)) {
  42. this.node.destroy();
  43. }
  44. return;
  45. }
  46. this.node.scaleX = target.getComponent(PlayerObject).player_view.node.scaleX;
  47. let len = target.position.sub(this.node.position).mag();
  48. let curState = this.curState;
  49. if (curState == state.standy && len > 80) {
  50. this.moveToTarget(target);
  51. }
  52. else if (curState == state.walk && len > 50) {
  53. this.moveToTarget(target);
  54. }
  55. else {
  56. this.curState = state.standy;
  57. if (this.spine.animation !== 'standy1') {
  58. this.node.getComponent(cc.RigidBody).linearVelocity = cc.v2();
  59. this.spine.setAnimation(0, 'standy1', true)
  60. }
  61. }
  62. }
  63. /**向目标移动
  64. * @param target 目标
  65. */
  66. private moveToTarget(target: cc.Node) {
  67. this.curState = state.walk;
  68. let v_sub = target.position.sub(this.node.position).normalize();
  69. let rad = Math.acos(v_sub.x);
  70. if (v_sub.y < 0) {
  71. rad = Math.PI * 2 - rad;
  72. }
  73. this.setLinearVelocity(rad);
  74. if (this.spine.animation !== 'walk1') {
  75. this.spine.setAnimation(0, 'walk1', true)
  76. }
  77. }
  78. /**设置刚体线性速度
  79. * @param rad 弧度角
  80. */
  81. private setLinearVelocity(rad: number) {
  82. this.node.getComponent(cc.RigidBody).linearVelocity = cc.v2(Math.cos(rad), Math.sin(rad)).mul(280);
  83. }
  84. update(dt) {
  85. this.follow(this.followNode)
  86. }
  87. }