GamePlayerInfo.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { _decorator, Component, Node, Label, Sprite, director } from 'cc';
  2. import { UserMgr } from './UserMgr';
  3. import { RoomEvent, RoomMgr } from './RoomMgr';
  4. import { MsgUserDataChangedPush } from '../shared/protocols/public/room/MsgUserDataChangedPush';
  5. const { ccclass, property } = _decorator;
  6. const aiLevelStr = ['初级', '中级', '高级'];
  7. @ccclass('GamePlayerInfo')
  8. export class GamePlayerInfo extends Component {
  9. @property(Label)
  10. userId: Label;
  11. @property(Sprite)
  12. icon: Sprite
  13. @property(Node)
  14. readyState: Node;
  15. private _defaultIcon;
  16. private _userId: string;
  17. onLoad() {
  18. this._defaultIcon = this.icon.spriteFrame;
  19. }
  20. protected start(): void {
  21. director.on(RoomEvent.USER_DATA_CHANGED, this.onUserDataChanged, this);
  22. }
  23. protected onDestroy(): void {
  24. director.off(RoomEvent.USER_DATA_CHANGED, this.onUserDataChanged, this);
  25. }
  26. onUserDataChanged(msg: MsgUserDataChangedPush) {
  27. if (msg.uid != this._userId) {
  28. return;
  29. }
  30. if (!msg.isOnline) {
  31. this.readyState.active = true;
  32. this.readyState.getComponent(Label).string = '离线';
  33. }
  34. else {
  35. this.readyState.active = (!RoomMgr.inst.isPlaying && RoomMgr.inst.getUser(this._userId).ready);
  36. }
  37. }
  38. setAILevel(level: number) {
  39. this.userId.string = '电脑-' + (aiLevelStr[level] || aiLevelStr[0]);
  40. this.icon.spriteFrame = this._defaultIcon;
  41. this.readyState.active = false;
  42. }
  43. setUserId(userId: string) {
  44. this._userId = userId;
  45. if (!userId) {
  46. this.userId.string = '等待加入...';
  47. this.icon.spriteFrame = this._defaultIcon;
  48. this.readyState.active = false;
  49. if (RoomMgr.inst.isWatcher) {
  50. this.readyState.active = true;
  51. this.readyState.getComponent(Label).string = '加入';
  52. }
  53. }
  54. else {
  55. UserMgr.inst.setUserIconAndName(userId, this.icon, this.userId);
  56. this.readyState.active = false;
  57. if (RoomMgr.inst.isOnline) {
  58. this.readyState.active = (!RoomMgr.inst.isPlaying && RoomMgr.inst.getUser(userId).ready);
  59. if (this.readyState.active) {
  60. this.readyState.getComponent(Label).string = '准备';
  61. }
  62. }
  63. }
  64. }
  65. update(deltaTime: number) {
  66. }
  67. }