/
PatchNine.js
131 lines (110 loc) · 3.07 KB
/
PatchNine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class PatchNine{
constructor(uSize=1){
this.unitSize = uSize;
this.recycleHandler = null; //How to handle items being recycled
this.areaX = 0; //Current Center Location
this.areaY = 0;
this.areas = [ //Grid of the 9 Locations, Center being the main area
{x:-1, y:-1, idx:null},
{x: 0, y:-1, idx:null},
{x: 1, y:-1, idx:null},
{x:-1, y: 0, idx:null},
{x: 0, y: 0, idx:null},
{x: 1, y: 0, idx:null},
{x:-1, y: 1, idx:null},
{x: 0, y: 1, idx:null},
{x: 1, y: 1, idx:null}
];
this.pool = [ //List of items that will act as areas
{x:-1, y:-1, item:null},
{x: 0, y:-1, item:null},
{x: 1, y:-1, item:null},
{x:-1, y: 0, item:null},
{x: 0, y: 0, item:null},
{x: 1, y: 0, item:null},
{x:-1, y: 1, item:null},
{x: 0, y: 1, item:null},
{x: 1, y: 1, item:null}
];
}
//=============================================================
// Add Transforms Items that will act as our areas
addTransform(t){
for(var i of this.pool){
if(!i.item){
i.item = t;
t.setPosition( i.x * this.unitSize, 0.01, i.y * this.unitSize );
if(this.recycleHandler) this.recycleHandler(i.x, i.y, t);
break;
}
}
return this;
}
//=============================================================
// Controlling the center of the area grid
move(x, y){ this.center( this.areaX + x, this.areaY + y ); return this; }
center(x,y){
this.areaX = x;
this.areaY = y;
//Create XY positions as -1, 0, 1
var itm, i=0;
for(itm of this.areas){
itm.x = x + (i % 3 - 1);
itm.y = y + (Math.floor(i / 3) - 1);
itm.idx = null;
i++;
}
return this;
}
//=============================================================
//
updateAreas(){
let i, a, p,
stack = [],
xRangeA = this.areaX - 1,
xRangeB = this.areaX + 1,
yRangeA = this.areaY - 1,
yRangeB = this.areaY + 1;
//......................................
// Find which items needs to be recycled
// and update the areas with which item is assigned to it.
for(i=0; i < this.pool.length; i++){
p = this.pool[ i ];
if( p.x > xRangeB || p.x < xRangeA ||
p.y > yRangeB || p.y < yRangeA ) stack.push( i ); //Recycle Item
else{
for(a of this.areas){
if(a.x == p.x && a.y == p.y){ a.idx = i; break; }
}
}
}
//......................................
//Reuse old items for new areas, update them if available.
while(stack.length > 0){
i = stack.pop();
p = this.pool[ i ];
for(a of this.areas){
if(a.idx == null){
a.idx = i;
p.x = a.x;
p.y = a.y;
p.item.setPosition(p.x * this.unitSize, 0.01, p.y * this.unitSize);
if(this.recycleHandler) this.recycleHandler(p.x, p.y, p.item);
break;
}
}
}
}
//=============================================================
// Helper Functions
debug(){
var txt = "";
for(var i=0; i < 9; i++){
txt += this.areas[i].x + ":" + this.areas[i].y + " \t ";
if( ((i+1) % 3) == 0 ) txt += "\n";
}
console.log(txt);
return this;
}
}
export default PatchNine;