-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapfieldStorage.js
117 lines (101 loc) · 2.16 KB
/
MapfieldStorage.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
goog.require('de.marvas.engine.Mapfield');
goog.provide('de.marvas.engine.MapfieldStorage');
/**
* @constructor
*/
function MapfieldStorage() {
// not static
this.simpleList = [];
this.gridList = {};
}
/**
* @type {Array}
*/
MapfieldStorage.prototype.simpleList = [];
/**
* @type {Object}
*/
MapfieldStorage.prototype.gridList = {};
/**
* @param {MapField} field
*/
MapfieldStorage.prototype.add = function(field) {
this.simpleList.push(field);
this.addToGrid(field);
};
/**
* @param {MapField} field
*/
MapfieldStorage.prototype.push = function(field) {
this.add(field);
};
/**
* @private
* @param {MapField} field
*/
MapfieldStorage.prototype.addToGrid = function(field) {
if (!this.gridList[field.x]) {
this.gridList[field.x] = {};
}
this.gridList[field.x][field.y] = field;
};
/**
* @private
* @param {MapField} field
*/
MapfieldStorage.prototype.removeFromGrid = function(field) {
if (Object.getOwnPropertyNames(this.gridList[field.x]).length === 1) {
// clean up before object is empty
delete this.gridList[field.x];
} else {
delete this.gridList[field.x][field.y];
}
};
/**
* @returns {number}
*/
MapfieldStorage.prototype.getLength = function() {
return this.simpleList.length;
};
/**
* @param {MapField} field
*/
MapfieldStorage.prototype.unshift = function(field) {
this.simpleList.unshift(field);
this.addToGrid(field);
};
/**
* @returns {?|MapField}
*/
MapfieldStorage.prototype.getFirst = function() {
return this.simpleList[0];
};
MapfieldStorage.prototype.pop = function() {
this.removeFromGrid(this.getLast());
this.simpleList.pop();
};
/**
* Synonym for pop
*/
MapfieldStorage.prototype.removeLast = function() {
this.pop();
};
/**
* @returns {MapField}
*/
MapfieldStorage.prototype.getLast = function() {
return this.simpleList[this.getLength() - 1];
};
/**
* @param {number} x
* @param {number} y
* @return {MapField|null}
*/
MapfieldStorage.prototype.getField = function(x, y) {
if (!this.gridList[x]) {
return null;
} else if (!this.gridList[x][y]) {
return null;
}
return this.gridList[x][y];
};