ExClass.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Node, Vec3 } from "cc";
  2. export class CircleArray<T>{
  3. private arr: Array<T> = null;
  4. private begin = 0;
  5. private len = 0;
  6. constructor(arr:Array<T>, len:number){
  7. this.arr = arr;
  8. this.len = len;
  9. }
  10. public get(index:number):T{
  11. if(index > -1 && index < this.len){
  12. let real_index = (this.begin + index) % this.len;
  13. return this.arr[real_index];
  14. }
  15. return null;
  16. }
  17. public getLast(offset:number):T{
  18. let index = this.begin - offset;
  19. if(index < 0)index += this.len;
  20. return this.arr[index];
  21. }
  22. public addOffset(add:number){
  23. this.begin = (this.begin + add) % this.len;
  24. if(this.begin < 0)this.begin += this.len;
  25. }
  26. public getLen(){
  27. return this.len;
  28. }
  29. public resetArrIndex(){
  30. if(this.begin == 0)return;
  31. let arr_temp = this.arr.slice();
  32. let map = new Array(this.len);
  33. for(let i = 0; i < this.len; i++){
  34. map[i] = (this.begin + i) % this.len;
  35. }
  36. for(let i = 0; i < this.len; i++){
  37. this.arr[i] = arr_temp[map[i]];
  38. }
  39. }
  40. }
  41. export class SplitInfo{
  42. public static getOffsetByNode(node:Node){
  43. let children = node.children;
  44. let arr = new Array(children.length);
  45. for(let i = 0 ; i < children.length ; i++){
  46. arr[i] = [children[i].position.x, children[i].position.y];
  47. }
  48. return arr;
  49. }
  50. public offset = 0;
  51. public parent: Node = null;
  52. public children: Array<Node> = null;
  53. private status: Array<boolean> = null;
  54. private state = false;
  55. constructor(parent:Node){
  56. this.parent = parent;
  57. this.children = parent.children.concat();
  58. let grand = parent.parent;
  59. this.status = new Array(this.children.length);
  60. for(let i = 0 ; i < this.children.length ; i++){
  61. this.children[i].setParent(grand);
  62. this.status[i] = this.children[i].active;
  63. }
  64. this.state = parent.active;
  65. }
  66. public setSlisByIndex(index:number){
  67. let len = this.children.length;
  68. for(let i = 0 ; i < len ; i++){
  69. this.children[i].setSiblingIndex((index + 1) * (i + 1) + index + this.offset);
  70. }
  71. }
  72. public setActive(state:boolean){
  73. if(this.state == state)return;
  74. this.state = state;
  75. if(state){
  76. for(let i = 0 , l = this.children.length ; i < l ; i++)this.children[i].active = this.status[i];
  77. }
  78. else{
  79. for(let i = 0 , l = this.children.length ; i < l ; i++){
  80. this.status[i] = this.children[i].active;
  81. this.children[i].active = false;
  82. }
  83. }
  84. }
  85. public moveByOffset(offset_arr:Array<Array<number>>){
  86. let base_x = this.parent.position.x;
  87. let base_y = this.parent.position.y;
  88. for(let i = 0 ; i < offset_arr.length ; i++){
  89. this.children[i].toXY(base_x + offset_arr[i][0], base_y + offset_arr[i][1]);
  90. }
  91. }
  92. }