-
Notifications
You must be signed in to change notification settings - Fork 0
/
scriptBag.ts
43 lines (37 loc) · 979 Bytes
/
scriptBag.ts
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
// Add your code here
class ScriptBag {
bag: Block[];
maxBagSize = 3;
held: Block;
constructor(public current: Block[]) {
this.bag = [];
}
public length() {
return this.current.length + this.maxBagSize;
}
public currentStartIndex() {
return this.maxBagSize;
}
public grab(ind: number) {
if (ind < this.maxBagSize) {
this.held = this.bag[ind];
this.bag[ind] = undefined;
} else {
ind -= this.maxBagSize;
this.held = this.current[ind];
this.current.splice(ind, 1);
}
}
public release(ind: number) {
const curr = this.held;
if (!curr) return;
if (ind < this.maxBagSize) {
this.held = this.bag[ind];
this.bag[ind] = curr;
} else {
ind -= this.maxBagSize;
this.held = undefined;
this.current.insertAt(ind, curr);
}
}
}