-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgamemap.js
131 lines (120 loc) · 3.2 KB
/
gamemap.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
const FloodFill = require('./floodfill.js');
module.exports = function(mapSize, tileSize, innerTileSize, trailTileSize) {
this.map = [];
this.updateTiles = [];
this.mapSize = mapSize;
this.tileSize = tileSize;
this.innerTileSize = innerTileSize;
this.trailTileSize = trailTileSize;
this.setCellData = function(x, y, id, color, type) {
while (this.map == null) {
}
this.map[x][y] = {
x: x,
y: y,
id: id,
color: color,
type: type
};
this.updateTiles.push(this.map[x][y]);
};
this.clearPlayerLand = function(id) {
for (var i = 0; i < this.mapSize; i++) {
for (var j = 0; j < this.mapSize; j++) {
if (this.map[i][j].id == id) {
this.setCellData(i, j, 0, "#3a4f56", "land");
}
}
}
};
this.addPlayerLand = function(player) {
for (var i = -1; i < 2; i++) {
for (var j = -1; j < 2; j++) {
this.setCellData(player.x + i, player.y + j, player.id, player.color, "land");
}
}
};
this.floodFill = function(player) {
var tempArray = [];
var tempMinX = this.getMinMaxPlayerCoords(player.id).minX;
tempMinX -= 1;
var tempMaxX = this.getMinMaxPlayerCoords(player.id).maxX;
tempMaxX += 1;
var tempMinY = this.getMinMaxPlayerCoords(player.id).minY;
tempMinY -= 1;
var tempMaxY = this.getMinMaxPlayerCoords(player.id).maxY;
tempMaxY += 1;
for (var k = tempMinX; k <= tempMaxX; k++) {
for (var l = tempMinY; l <= tempMaxY; l++) {
if (k > tempMinX && k < tempMaxX && l > tempMinY && l < tempMaxY) {
tempArray.push(this.map[k][l]);
} else {
tempArray.push({ //add blank layer
x: k,
y: l,
id: 0,
color: "#3a4f56",
type: "land"
});
}
}
}
var tempFloodFill = new FloodFill(tempArray, tempMinX, tempMaxX, tempMinY, tempMaxY, player.id);
for (var j = 0; j < tempFloodFill.length; j++) {
if (tempFloodFill[j].x < 0 || tempFloodFill[j].x >= mapSize || tempFloodFill[j].y < 0 || tempFloodFill[j].y >= mapSize) {
continue;
}
if (tempFloodFill[j].id == player.id) {
this.setCellData(tempFloodFill[j].x, tempFloodFill[j].y, tempFloodFill[j].id, player.color, "land");
} else {
this.setCellData(tempFloodFill[j].x, tempFloodFill[j].y, tempFloodFill[j].id, tempFloodFill[j].color, "land");
}
}
};
this.getMinMaxPlayerCoords = function(id) {
var tempMinX = this.mapSize;
var tempMinY = this.mapSize;
var tempMaxX = 0;
var tempMaxY = 0;
for (var i = 0; i < this.mapSize; i++) {
for (var j = 0; j < this.mapSize; j++) {
if (this.map[i][j].id == id) {
if (this.map[i][j].x < tempMinX) {
tempMinX = this.map[i][j].x;
}
if (this.map[i][j].y < tempMinY) {
tempMinY = this.map[i][j].y;
}
if (this.map[i][j].x > tempMaxX) {
tempMaxX = this.map[i][j].x;
}
if (this.map[i][j].y > tempMaxY) {
tempMaxY = this.map[i][j].y;
}
}
}
}
var output = {
minX: tempMinX,
minY: tempMinY,
maxX: tempMaxX,
maxY: tempMaxY
};
return output;
};
this.clearMap = function() {
for (var i = 0; i < this.mapSize; i++) {
this.map[i] = [];
for (var j = 0; j < this.mapSize; j++) {
this.map[i][j] = {
x: i,
y: j,
id: 0,
color: "#3a4f56",
type: "land"
};
}
}
};
this.clearMap();
};