Charactor.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { _decorator, CCInteger, Component, Label, sp, Vec3, Node } from 'cc';
  2. import { GameSystemInputType } from '../GameStruct';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('Charactor')
  5. export class Charactor extends Component {
  6. @property({type: CCInteger})
  7. playerIndex: number;
  8. basePosition: Vec3 = new Vec3();
  9. targetPosition: Vec3 = new Vec3();
  10. skeleton: sp.Skeleton;
  11. currentAnimationName: string;
  12. labelAccCount: Label = null;
  13. pointer: Node = null;
  14. start() {
  15. this.node.getPosition(this.basePosition);
  16. this.skeleton = this.getComponent(sp.Skeleton);
  17. //我方箭头显示并延时销毁
  18. if (window.gm.state.myPlayerIndex === this.playerIndex) {
  19. this.pointer = this.node.getChildByName("Pointer");
  20. this.pointer.active = true;
  21. }
  22. //计数标签
  23. this.labelAccCount = this.node.getChildByName("LabelAccCount").getComponent(Label);
  24. }
  25. playAnimation(name: string) {
  26. if (name === this.currentAnimationName) return;
  27. this.currentAnimationName = name;
  28. this.skeleton.setAnimation(0, name, true);
  29. }
  30. update(dt: number) {
  31. let ps = window.gm.state.gameSystemState.playerStates.find(v => v.index === this.playerIndex);
  32. if (ps) {
  33. this.targetPosition.set(this.basePosition).add3f(ps.moveX, 0, 0);
  34. this.node.setPosition(this.node.position.lerp(this.targetPosition, 20 * dt));
  35. if (ps.speedX / 100 >= 5) {
  36. this.playAnimation("Run3");
  37. } else if (ps.speedX / 100 >= 3) {
  38. this.playAnimation("Run2");
  39. } else if (ps.speedX > 0) {
  40. this.playAnimation("Run1");
  41. } else {
  42. this.playAnimation("idle");
  43. }
  44. this.labelAccCount.string = ps.accCount.toString();
  45. } else {
  46. this.playAnimation("idle");
  47. }
  48. //ReadyGo结束后要销毁箭头
  49. if (this.pointer && window.gm.state.gameSystemState.readyComplete) {
  50. this.pointer.destroy();
  51. this.pointer = null;
  52. }
  53. }
  54. inputAcc() {
  55. let accInput: PlayerAccInput = {
  56. type: GameSystemInputType.PlayerAcc,
  57. playerIndex: this.playerIndex,
  58. };
  59. window.gm.socketPlayer.cacheInputs(accInput);
  60. }
  61. }