MatchPanel.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { _decorator, Component, SpriteFrame, AudioClip, Animation, Sprite, Label, Node, color, AudioSource, tween, UIOpacity, assetManager, Texture2D, ImageAsset, resources } from 'cc';
  2. const {ccclass, property} = _decorator;
  3. @ccclass('MatchPanel')
  4. export default class MatchPanel extends Component {
  5. @property({type: SpriteFrame})
  6. private frameBoy: SpriteFrame | null = null;
  7. @property({type: SpriteFrame})
  8. private frameGirl: SpriteFrame | null = null;
  9. @property({type: SpriteFrame})
  10. private genderBoy: SpriteFrame | null = null;
  11. @property({type: SpriteFrame})
  12. private genderGirl: SpriteFrame | null = null;
  13. @property({type: AudioClip})
  14. private matchSuccess: AudioClip | null = null;
  15. @property({type: Animation})
  16. private meteorLine: Animation | null = null;
  17. @property({type: Sprite})
  18. private frameSelf: Sprite | null = null;
  19. @property({type: Sprite})
  20. private avatarSelf: Sprite | null = null;
  21. @property({type: Sprite})
  22. private genderSelf: Sprite | null = null;
  23. @property({type: Label})
  24. private nameSelf: Label | null = null;
  25. @property({type: Sprite})
  26. private frameOther: Sprite | null = null;
  27. @property({type: Sprite})
  28. private avatarOther: Sprite | null = null;
  29. @property({type: Sprite})
  30. private genderOther: Sprite | null = null;
  31. @property({type: Label})
  32. private nameOther: Label | null = null;
  33. @property({type: Node})
  34. private backGround: Node | null = null;
  35. @property({type: Node})
  36. private content: Node | null = null;
  37. @property({type: Label})
  38. private labelMatching: Label | null = null;
  39. @property({type: Label})
  40. private labelTime: Label | null = null;
  41. @property({type: Label})
  42. private labelWaitingTime: Label | null = null;
  43. @property({type: Node})
  44. private points: Node | null = null;
  45. private waitingTime: number = 0;
  46. private countingTime: boolean = true;
  47. public static GENDER_BOY = "GENDER_BOY";
  48. public static GENDER_GIRL = "GENDER_GIRL";
  49. public static Instance: MatchPanel = null;
  50. protected onLoad(): void {
  51. MatchPanel.Instance = this;
  52. }
  53. protected onDestroy(): void {
  54. if (MatchPanel.Instance === this) MatchPanel.Instance = null;
  55. }
  56. protected update(dt: number): void {
  57. if (this.countingTime) {
  58. this.waitingTime += dt;
  59. this.labelTime.string = this._timeFormatMS(this.waitingTime*1000);
  60. }
  61. }
  62. public renderMyInfo(nickName: string, gender: string, avatar: SpriteFrame | string) {
  63. //render self info
  64. this.nameSelf.string = nickName;
  65. if (avatar instanceof SpriteFrame) {
  66. this.avatarSelf.spriteFrame = avatar;
  67. } else {
  68. this._loadSpriteFrame(avatar, (spriteFrame: SpriteFrame) => {
  69. this.avatarSelf.spriteFrame = spriteFrame;
  70. });
  71. }
  72. if (gender === MatchPanel.GENDER_GIRL) {
  73. this.frameSelf.spriteFrame = this.frameGirl;
  74. this.genderSelf.spriteFrame = this.genderGirl;
  75. } else {
  76. this.frameSelf.spriteFrame = this.frameBoy;
  77. this.genderSelf.spriteFrame = this.genderBoy;
  78. }
  79. //rendere other style according to self style
  80. if (gender === MatchPanel.GENDER_GIRL) {
  81. let pointColor = color(30, 144, 255, 255);
  82. this.frameOther.spriteFrame = this.frameBoy;
  83. this.genderOther.spriteFrame = this.genderBoy;
  84. this.points.children.forEach((child: Node) => {
  85. child.getComponent(Sprite).color = pointColor;
  86. });
  87. } else {
  88. let pointColor = color(255, 105, 180, 255);
  89. this.frameOther.spriteFrame = this.frameGirl;
  90. this.genderOther.spriteFrame = this.genderGirl;
  91. this.points.children.forEach((child: Node) => {
  92. child.getComponent(Sprite).color = pointColor;
  93. });
  94. }
  95. }
  96. public renderOtherInfo(nickName: string, gender: string, avatar: SpriteFrame | string) {
  97. //render other info
  98. this.nameOther.string = nickName;
  99. if (avatar instanceof SpriteFrame) {
  100. this.avatarOther.spriteFrame = avatar;
  101. } else {
  102. this._loadSpriteFrame(avatar, (spriteFrame: SpriteFrame) => {
  103. this.avatarOther.spriteFrame = spriteFrame;
  104. });
  105. }
  106. if (gender === MatchPanel.GENDER_GIRL) {
  107. this.frameOther.spriteFrame = this.frameGirl;
  108. this.genderOther.spriteFrame = this.genderGirl;
  109. } else {
  110. this.frameOther.spriteFrame = this.frameBoy;
  111. this.genderOther.spriteFrame = this.genderBoy;
  112. }
  113. this.countingTime = false;
  114. this.points.destroy();
  115. this.labelMatching.string = "匹配完成";
  116. this.meteorLine.play("MeteorLine");
  117. this.labelTime.node.destroy();
  118. this.labelWaitingTime.node.destroy();
  119. this._playOneShot(this.matchSuccess);
  120. let tweenAfter = tween(this.backGround.getComponent(UIOpacity))
  121. .to(0.5, {opacity: 55});
  122. tween(this.content.getComponent(UIOpacity))
  123. .delay(2)
  124. .to(1, {opacity: 0})
  125. .call(() => tweenAfter.start())
  126. .start()
  127. }
  128. //=====================private=====================
  129. private _timeFormatMS(millis:number) {
  130. let minute = Math.floor(millis / 1000 / 60);
  131. let second = Math.floor(millis / 1000 % 60);
  132. return (minute < 10 ? '0' : '') + minute + ':' + (second < 10 ? '0' : '') + second;
  133. }
  134. private _playOneShot(audioClip: AudioClip) {
  135. let as = this.node.getComponent(AudioSource);
  136. if (!as) as = this.node.addComponent(AudioSource);
  137. as.playOneShot(audioClip);
  138. }
  139. private _loadSpriteFrame(url: string, onSuccess: (spriteFrame: SpriteFrame) => void) {
  140. if (url.startsWith("data:image/")) {
  141. const img = new Image();
  142. img.src = url;
  143. const tex = new Texture2D();
  144. img.onload = () => {
  145. tex.reset({
  146. width: img.width,
  147. height: img.height,
  148. });
  149. tex.uploadData(img, 0, 0);
  150. const sp = new SpriteFrame();
  151. sp.texture = tex;
  152. onSuccess(sp);
  153. };
  154. } else if (url.startsWith("http")) {
  155. assetManager.loadRemote<ImageAsset>(url, {ext: ".jpg"}, (err, imageAsset) => {
  156. if (err) return;
  157. const spriteFrame = new SpriteFrame();
  158. const texture = new Texture2D();
  159. texture.image = imageAsset;
  160. spriteFrame.texture = texture;
  161. onSuccess(spriteFrame);
  162. });
  163. } else {
  164. resources.load(url + "/spriteFrame", SpriteFrame, (err, spriteFrame) => {
  165. if (err) return;
  166. onSuccess(spriteFrame);
  167. });
  168. }
  169. }
  170. }