-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.js
174 lines (131 loc) · 3.31 KB
/
Map.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
goog.provide('de.marvas.engine.Map');
goog.require('de.marvas.engine.MapfieldStorage');
/**
* @constructor Map
* @param {Object=} params
*/
function Map(params) {
if (params) {
if (params.fieldHeight) {
this.fieldHeight = parseInt(params.fieldHeight, 10);
}
if (params.fieldWidth) {
this.fieldWidth = parseInt(params.fieldWidth, 10);
}
if (params.height) {
this.height = parseInt(params.height, 10);
}
if (params.width) {
this.width = parseInt(params.width, 10);
}
}
this.xfields = parseInt(this.width / this.fieldWidth, 10);
this.yfields = parseInt(this.height / this.fieldHeight, 10);
this.fields = new MapfieldStorage();
for (var x = 0; x < this.xfields; x++) {
// var temp_array = [];
for (var y = 0; y < this.yfields; y++) {
this.fields.add(new MapField(x, y, this.fieldHeight, this.fieldWidth));
}
// this.fields.push(temp_array);
}
}
/**
* @type {MapfieldStorage}
*/
Map.prototype.fields = new MapfieldStorage();
/**
* @type {jQuery|number}
*/
Map.prototype.height = $(window).height();
/**
* @type {jQuery|number}
*/
Map.prototype.width = $(window).width();
/**
* @type {number}
*/
Map.prototype.fieldHeight = 50;
/**
* @type {number}
*/
Map.prototype.fieldWidth = 50;
/**
* @type {number}
*/
Map.prototype.xfields = 0;
/**
* @type {number}
*/
Map.prototype.yfields = 0;
/**
* @type {Array|null}
*/
Map.prototype.borderFields = null;
/**
* @param {number} x
* @param {number} y
* @return {MapField}
*/
Map.prototype.getField = function(x, y) {
return this.fields.getField(x, y);
};
/**
* @description set random background-color for all fields
*/
Map.prototype.random = function() {
this.fields.simpleList.forEach(function(field) {
field.setColor(this.getRandomColor());
field.refreshCSS();
}.bind(this));
};
/**
* @const
* @type {Array}
*/
Map.prototype.COLORS = ["black", "white", "yellow", "green", "red", "blue", "violet", "gray", "brown", 'gold', 'lila', 'lightblue'];
/**
* @description get a random color of this.colors
* @return {string}
*/
Map.prototype.getRandomColor = function() {
return this.COLORS[Math.floor(Math.random() * this.COLORS.length)];
};
Map.prototype.getBorderFields = function() {
if (this.borderFields === null) {
this.borderFields = [];
this.fields.simpleList.forEach(function(field) {
if (this.isFieldBorder(field)) {
this.borderFields.push(field);
}
}.bind(this));
}
return this.borderFields;
};
/**
* @param {MapField} field
* @returns {boolean}
*/
Map.prototype.isFieldBorder = function(field) {
if (!field) {
return false;
}
return field.x === 0 || field.y === 0 || field.x === this.xfields - 1 || field.y === this.yfields - 1;
};
/**
* @returns {MapField}
*/
Map.prototype.getRandomField = function() {
return this.fields.gridList[Math.floor(Math.random() * this.xfields)][Math.floor(Math.random() * this.yfields)];
};
/**
* @returns {MapField}
*/
Map.prototype.getRandomInnerField = function() {
var randomField;
do {
randomField = this.getRandomField();
}
while (this.isFieldBorder(randomField));
return randomField;
};