NoticeManager.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { instantiate, LabelComponent, Node, Prefab, Size, UITransformComponent, Vec2, Vec3, view } from "cc";
  2. import { NoticeViewScript } from "./NoticeViewScript";
  3. export class NoticeManager
  4. {
  5. private static startPos:Vec3[]=[];
  6. private static root:Node;
  7. private static ViewMap:Map<string,Prefab>;
  8. /**
  9. * 池
  10. */
  11. private static Pool:Map<string,NoticeViewScript[]>;
  12. public static Init(root:Node,viewMap:Map<string,Prefab>):void{
  13. this.root=root;
  14. this.ViewMap=viewMap;
  15. this.Pool=new Map<string,NoticeViewScript[]>();
  16. let pos:Vec3;
  17. let viewSize:Size=view.getVisibleSize();
  18. let w:number=viewSize.width/3;
  19. let helfW:number=w/2;
  20. let h:number=viewSize.height/3;
  21. let helfH:number=h/2;
  22. for (let yIndex = 0; yIndex < 3; yIndex++) {
  23. for (let xIndex = 0; xIndex < 3; xIndex++) {
  24. pos=new Vec3(xIndex*w+helfW,yIndex*h,0);
  25. this.startPos.push(pos);
  26. }
  27. }
  28. }
  29. /**
  30. * 显示提示
  31. * @param data 提示内容
  32. * @param startPos 开始位置
  33. * @param type 类型
  34. * @param moveType 移动类型
  35. * @param time 时间
  36. * @param dis 距离
  37. */
  38. public static ShowPromptByPos(data:any,startPos:Vec3,type:string="Text",moveType:number=1,time:number=1000,dis:number=100):void{
  39. let viewScript:NoticeViewScript=this.Create(type);
  40. viewScript.type=type;
  41. viewScript.data=data;
  42. viewScript.moveType=moveType;
  43. viewScript.startPos=startPos;
  44. viewScript.time=time;
  45. viewScript.dis=dis;
  46. viewScript.StartMove(data);
  47. viewScript.node.setPosition(viewScript.startPos);
  48. this.root.addChild(viewScript.node);
  49. }
  50. /**
  51. * 显示提示
  52. * @param data 提示内容
  53. * @param posType 位置类型678,345,012
  54. * @param type 类型
  55. * @param moveType 移动类型
  56. * @param time 持续时间
  57. * @param dis 移动距离
  58. */
  59. public static ShowPrompt(data:any,posType:number=7,type:string="Text",moveType:number=1,time:number=1000,dis:number=100):void{
  60. let startPos:Vec3=this.startPos[posType];
  61. this.ShowPromptByPos(data,startPos,type,moveType,time,dis);
  62. }
  63. public static Create(type:string):NoticeViewScript{
  64. let list:NoticeViewScript[];
  65. if(this.Pool.get(type)!=null){
  66. list=this.Pool.get(type);
  67. if(list.length>0){
  68. return list.shift();
  69. }
  70. }
  71. if(this.ViewMap.has(type)==false){
  72. throw new Error("找不到对应的Notice Prefab"+type);
  73. }
  74. let prefab:Prefab=this.ViewMap.get(type);
  75. let node:Node=instantiate(prefab);
  76. let result:NoticeViewScript=node.getComponent(NoticeViewScript);
  77. if(result==null){
  78. throw new Error("节点上找不到NoticeViewScript脚本!");
  79. }
  80. return result;
  81. }
  82. public static recycle(value:NoticeViewScript):void{
  83. this.root.removeChild(value.node);
  84. let list:NoticeViewScript[];
  85. if(this.Pool.get(value.type)!=null){
  86. list=this.Pool.get(value.type);
  87. }else{
  88. list=[];
  89. this.Pool.set(value.type,list);
  90. }
  91. if(list.indexOf(value)>=0){
  92. throw new Error("重复回收!");
  93. }
  94. list.push(value);
  95. }
  96. }