-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.pde
201 lines (160 loc) · 5.93 KB
/
Map.pde
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class Map {
// Properties
Table tileIdTable;
PImage tilesetImage;
JSONObject tilesetProperties;
int tileIdTableColumns;
int tileIdTableRows;
int tileId;
int previousTileId;
int tileX, tileY;
PImage img;
int tileHeight;
int tileWidth;
int imageRows;
int imageColumns;
int imageHeight;
int imageWidth;
Object playMapTiles[];
int playMapTilesNumber;
// Constructor
Map () {
tileId = 1;
previousTileId = 1000;
tileHeight = json.getInt("tileheight");
tileWidth = json.getInt("tilewidth");
imageHeight = json.getInt("imageheight");
imageRows = imageHeight / tileHeight;
imageWidth = json.getInt("imagewidth");
imageColumns = imageWidth / tileWidth;
tileIdTableRows = table.getRowCount();
tileIdTableColumns = table.getColumnCount();
playMapTilesNumber = tileIdTableColumns * tileIdTableRows;
playMapTiles = new Object[playMapTilesNumber];
for (int i = 0; i < playMapTilesNumber; ++i) {
playMapTiles[i] = new Object();
}
}
void update() {
for (int row = 0; row < tileIdTableRows; ++row) {
for (int column = 0; column < tileIdTableColumns; ++column) {
tileId = tileIdTable.getInt(row, column);
if (tileId == -1)
continue;
tileX = tileId % imageColumns;
tileY = tileId / imageColumns;
img = tilesetImage.get(tileX*tileWidth, tileY*tileHeight, tileWidth, tileHeight);
imageMode(CORNER);
image(img, cameraOffsetX + column*tileWidth, cameraOffsetY + row*tileHeight, tileWidth, tileHeight);
}
}
}
void updateWithObjects () {
Object mapTile;
for (int row = 0; row < tileIdTableRows; ++row) {
for (int column = 0; column < tileIdTableColumns; ++column) {
tileId = tileIdTable.getInt(row, column);
mapTile = playMapTiles[row*tileIdTableColumns+column];
mapTile.id = tileId;
if (tileId == -1)
continue;
mapTile.beginX = cameraOffsetX + column*tileWidth;
mapTile.beginY = cameraOffsetY + row*tileHeight;
mapTile.endX = cameraOffsetX + column*tileWidth + tileWidth;
mapTile.endY = cameraOffsetY + row*tileHeight + tileHeight;
mapTile.idColumn = column;
mapTile.idRow = row;
tileX = tileId % imageColumns;
tileY = tileId / imageColumns;
img = tilesetImage.get(tileX*tileWidth, tileY*tileHeight, tileWidth, tileHeight);
imageMode(CORNER);
image(img, cameraOffsetX + column*tileWidth, cameraOffsetY + row*tileHeight, tileWidth, tileHeight);
}
}
}
void displayInEditMap () {
noFill();
strokeWeight(0.5);
stroke(191, 191, 191);
int x, y;
int tWidth = tileWidth/2;
int tHeight = tileHeight/2;
// Display of the level map
for (int row = 0; row < editedMapRows; ++row) {
for (int i = beginEditedMapColumn, column = 0; i < endEditedMapColumn; ++i, ++column) {
x = editedMapX + column*tWidth;
y = editedMapY + row*tHeight;
tileId = tileIdTable.getInt(row, i);
if (tileId == -1) {
rectMode(CORNER);
rect(x, y, tWidth, tHeight);
editedMapTiles[row*editedMapColumns+column].idColumn = i;
editedMapTiles[row*editedMapColumns+column].idRow = row;
continue;
}
editedMapTiles[row*editedMapColumns+column].idColumn = i;
editedMapTiles[row*editedMapColumns+column].idRow = row;
tileX = tileId % imageColumns;
tileY = tileId / imageColumns;
img = tilesetImage.get(tileX*tileWidth, tileY*tileHeight, tileWidth, tileHeight);
imageMode(CORNER);
image(img, x, y, tWidth, tHeight);
rectMode(CORNER);
rect(x, y, tWidth, tHeight);
}
}
}
void displayTileMap () {
noFill();
strokeWeight(1);
stroke(191, 191, 191);
// display of the tilemap
for (int row = 0; row < imageRows; ++row) {
for (int column = 0; column < imageColumns; ++column) {
img = tilesetImage.get(column*tileWidth, row*tileHeight, tileWidth, tileHeight);
imageMode(CORNER);
image(img, tilemapX + column*tileWidth, tilemapY + row*tileHeight, tileWidth, tileHeight);
// lines between tiles
rectMode(CORNER);
rect(tilemapX + column*tileWidth, tilemapY + row*tileHeight, tileWidth, tileHeight);
}
}
}
void createTiles () {
int index;
for (int row = 0; row < imageRows; ++row) {
for (int column = 0; column < imageColumns; ++column) {
index = row*imageColumns+column;
tilemapTiles[index].beginX = tilemapX + column*tileWidth;
tilemapTiles[index].beginY = tilemapY + row*tileHeight;
tilemapTiles[index].endX = tilemapX + column*tileWidth + tileWidth;
tilemapTiles[index].endY = tilemapY + row*tileHeight + tileHeight;
tilemapTiles[index].idColumn = column;
tilemapTiles[index].idRow = row;
}
}
}
void createEditedMapTiles () {
int index;
int tWidth = tileWidth/2;
int tHeight = tileHeight/2;
for (int row = 0; row < editedMapRows; ++row) {
for (int column = 0; column < editedMapColumns; ++column) {
index = row*editedMapColumns+column;
editedMapTiles[index].beginX = editedMapX + column*tWidth;
editedMapTiles[index].beginY = editedMapY + row*tHeight;
editedMapTiles[index].endX = editedMapX + column*tWidth + tWidth;
editedMapTiles[index].endY = editedMapY + row*tHeight + tHeight;
}
}
}
void addTile (int index) {
tileIdTable.setInt(editedMapTiles[index].idRow, editedMapTiles[index].idColumn, currentTileId);
}
void deleteTile (int index) {
tileIdTable.setInt(editedMapTiles[index].idRow, editedMapTiles[index].idColumn, -1);
}
void deleteTile (Object tile) {
tileIdTable.setInt(tile.idRow, tile.idColumn, -1);
}
}