ChildItem.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { _decorator, Component, Sprite, Label, Animation, Vec3, v3, ImageAsset, assetManager, SpriteFrame, Texture2D, EventHandler } from 'cc';
  2. import { Http } from '../../core/net/Http';
  3. import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
  4. import { BitmapFont } from '../../core/ui/BitmapFont';
  5. import { UITween } from '../../core/ui/tween/UITween';
  6. import { Utils } from '../../core/utils/Utils';
  7. import { g } from '../../Data/g';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('ChildItem')
  10. export class ChildItem extends Component {
  11. @property({ type: Http, tooltip: "http组件" }) http: Http;
  12. @property({ type: Sprite, tooltip: "头像" }) head: Sprite;
  13. @property({ type: BitmapFont, tooltip: "贡献" }) moneyLabel: Label;
  14. @property({ type: Animation, tooltip: "动画组件" }) anim: Animation;
  15. @property({ type: EventHandler, tooltip: "tween结束回调窗口" }) handler: EventHandler;
  16. private startPos: Vec3;
  17. private userID = -1;
  18. start() {
  19. this.anim.on(Animation.EventType.FINISHED, () => {
  20. this.deleyAnimation();
  21. }, this);
  22. this.deleyAnimation();
  23. this.startPos = this.node.position.clone();
  24. this.node.active = false;
  25. }
  26. public async setData(data: any) {
  27. this.node.active = true;
  28. this.userID = data.userID;
  29. if (data.avatar) {
  30. let img = await ResourcesUtils.loadRemote<ImageAsset>(data.avatar, { ext: ".png" }, this);
  31. const spriteFrame = new SpriteFrame();
  32. const texture = new Texture2D();
  33. texture.image = img;
  34. spriteFrame.texture = texture;
  35. this.head.spriteFrame = spriteFrame;
  36. }
  37. this.moneyLabel.string = "+" + (!!data.revenueAmount ? data.revenueAmount + "" : "0");
  38. }
  39. public async onTouch() {
  40. if (this.userID != -1) {
  41. let result = await this.http.send("/api/fission/ReceiveFission", { receiveUserID: this.userID });
  42. if (result.code == 0) {
  43. g.userData.contribution += result.data.addTotalContribution;
  44. this.doTween();
  45. }
  46. }
  47. }
  48. public doTween() {
  49. return new Promise<any>((resolve: (data: any) => void) => {
  50. let self = this;
  51. self.getComponent(UITween).play();
  52. this.scheduleOnce(() => {
  53. resolve(null);
  54. }, 0.2);
  55. })
  56. }
  57. private deleyAnimation(): void {
  58. let self = this;
  59. this.scheduleOnce(() => {
  60. self.anim.play();
  61. }, Utils.random_both(0.5, 2.5));
  62. }
  63. public onTweenOver(): void {
  64. this.node.active = false;
  65. this.node.setPosition(this.startPos);
  66. this.node.setScale(v3(1, 1, 1));
  67. this.handler && this.handler.emit([]);
  68. }
  69. }