GameModeView.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
  2. import { Utils } from '../../Common/Utils';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('GameModeView')
  5. export class GameModeView extends Component {
  6. private otherAvatar: Node;
  7. private labelDistanceToEnd: Label;
  8. protected start(): void {
  9. //加速按钮
  10. this.node.on(Node.EventType.TOUCH_START, () => {
  11. window.gm.mode.getCharactor(window.gm.state.myPlayerIndex).inputAcc();
  12. });
  13. //他人去向
  14. this.otherAvatar = this.node.getChildByName("OtherAvatar");
  15. let playerInfo = window.gm.state.matchPlayerInfos[(window.gm.state.myPlayerIndex + 1) % 2];
  16. let avatarSprite = this.otherAvatar.getChildByPath("Avatar/Sprite").getComponent(Sprite);
  17. if (playerInfo) {
  18. Utils.LoadSpriteFrame(playerInfo.avatarUrl,
  19. (spriteFrame: SpriteFrame) => avatarSprite.spriteFrame = spriteFrame);
  20. }
  21. this.otherAvatar.active = false;
  22. //终点距离
  23. this.labelDistanceToEnd = this.node.getChildByName("LabelDistanceToEnd").getComponent(Label);
  24. }
  25. protected update(dt: number): void {
  26. //更新显示他人去向
  27. let ps = window.gm.state.gameSystemState.playerStates.find(v => v.index === window.gm.state.myPlayerIndex);
  28. let ops = window.gm.state.gameSystemState.playerStates.find(v => v.index !== window.gm.state.myPlayerIndex);
  29. if (ps && ops) {
  30. let dist = ops.moveX - ps.moveX;
  31. let absDist = Math.abs(dist);
  32. if (absDist > 380) {
  33. this.otherAvatar.position.set(
  34. Math.abs(this.otherAvatar.position.x) * (dist > 0 ? 1 : -1),
  35. this.otherAvatar.position.y
  36. );
  37. this.otherAvatar.setPosition(this.otherAvatar.position);
  38. this.otherAvatar.getChildByName("ArrowA").active = dist < 0;
  39. this.otherAvatar.getChildByName("ArrowB").active = dist > 0;
  40. this.otherAvatar.getChildByName("LabelDistance").getComponent(Label).string = (absDist / window.gm.config.gameLenDivRealLen).toFixed(2) + "米"
  41. this.otherAvatar.active = true;
  42. } else {
  43. this.otherAvatar.active = false;
  44. }
  45. }
  46. //更新终点距离
  47. if (ps) {
  48. let dist = window.gm.config.endPointDistance - ps.moveX
  49. if (dist < 0) dist = 0;
  50. this.labelDistanceToEnd.string = "距离终点" + (dist / window.gm.config.gameLenDivRealLen).toFixed(2) + "米"
  51. }
  52. }
  53. }