UI_Notify.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Label, Prefab, instantiate ,Node, math} from "cc";
  2. import { Layout_UI_Notify } from "./Layout_UI_Notify";
  3. import { aa } from "db://assets/scripts/aa";
  4. import { GameUILayers } from "db://assets/scripts/GameUILayers";
  5. class NotifyInfo {
  6. private static readonly time:number=2;
  7. private static readonly init_y:number=120;
  8. private static readonly up_y:number=80;//上升一格
  9. private static readonly move_speed:number=880;//向上移动速度
  10. private static readonly sc_state0:number=0.8;//变小大小
  11. private static readonly sc_state0_time:number=0.22;
  12. private static readonly sc_state1:number=1.2;//变大大小
  13. private static readonly sc_state1_time:number=0.41;
  14. private static readonly sc_state2:number=1;//正常大小
  15. private time:number;
  16. private node:Node;
  17. private state:number;
  18. private _state_time:number=0;
  19. public constructor(node:Node,parent:Node,txt:string){
  20. this.time=NotifyInfo.time;
  21. this.node=node;
  22. this.state=0;
  23. this._state_time=0;
  24. let label= node.getComponentInChildren(Label);
  25. label.string=txt;
  26. node.parent=parent;
  27. node.setPosition(0,NotifyInfo.init_y,0);
  28. node.setScale(NotifyInfo.sc_state0,NotifyInfo.sc_state0,NotifyInfo.sc_state0);
  29. node.active=true;
  30. }
  31. public up():void{
  32. this.node.setPosition(this.node.position.x, this.node.position.y+NotifyInfo.up_y, this.node.position.z);
  33. }
  34. public run(dt:number):boolean{
  35. this.time-=dt;
  36. if(this.time<=0){
  37. this.node.destroy();
  38. this.node=null;
  39. return true;
  40. }else if(this.time<0.5){
  41. this.node.setPosition(this.node.position.x, this.node.position.y+NotifyInfo.move_speed*dt, this.node.position.z);
  42. }else {
  43. if(this.state===0){
  44. this._state_time+=dt;
  45. let ratio=this._state_time/NotifyInfo.sc_state0_time;
  46. if(ratio>=1){
  47. this.state=1;
  48. this._state_time=0;
  49. this.node.setScale(NotifyInfo.sc_state1,NotifyInfo.sc_state1,NotifyInfo.sc_state1);
  50. }else{
  51. let sc=math.lerp(NotifyInfo.sc_state0,NotifyInfo.sc_state1,ratio);
  52. this.node.setScale(sc,sc,sc);
  53. }
  54. }else if(this.state===1){
  55. this._state_time+=dt;
  56. let ratio=this._state_time/NotifyInfo.sc_state1_time;
  57. if(ratio>=1){
  58. this.state=2;
  59. this._state_time=0;
  60. this.node.setScale(NotifyInfo.sc_state2,NotifyInfo.sc_state2,NotifyInfo.sc_state2);
  61. }else{
  62. let sc=math.lerp(NotifyInfo.sc_state1,NotifyInfo.sc_state2,ratio);
  63. this.node.setScale(sc,sc,sc);
  64. }
  65. }
  66. }
  67. return false;
  68. }
  69. }
  70. export class UI_Notify extends aa.UIcontroller {
  71. constructor() {
  72. super('ui/ui_notify/UI_Notify', GameUILayers.NOTICE, Layout_UI_Notify);
  73. }
  74. private _info_prefab:Prefab;
  75. private notifys:NotifyInfo[]=[];
  76. protected onCreated(): void {
  77. let layout = this.layout as Layout_UI_Notify;
  78. this._info_prefab=layout.info_prefab;
  79. }
  80. public newNotify(txt:string){
  81. let info = new NotifyInfo(instantiate(this._info_prefab),this.node,txt);
  82. for(var i=this.notifys.length-1;i>=0;i--)this.notifys[i].up();
  83. this.notifys.push(info);
  84. }
  85. protected onUpdate(dt?: number): void {
  86. for(var i=this.notifys.length-1;i>=0;i--)if(this.notifys[i].run(dt))this.notifys.splice(i,1);
  87. }
  88. dispose(): void {
  89. this._info_prefab=null;
  90. }
  91. }