-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapGridOverlay.js
More file actions
203 lines (186 loc) · 7 KB
/
MapGridOverlay.js
File metadata and controls
203 lines (186 loc) · 7 KB
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
202
203
/*:
* @target MZ
* @plugindesc Customizable grid overlay for RPG Maker MZ. Shows a grid on the map with configurable options such as color, opacity, coordinates display, etc
* @author Undermax Games | Maxii1996
* @url https://undermax.itch.io/
*
* @param Grid Width
* @text Grid Cell Width
* @desc Width of each grid cell in pixels.
* @default 48
* @type number
*
* @param Grid Height
* @text Grid Cell Height
* @desc Height of each grid cell in pixels.
* @default 48
* @type number
*
* @param Grid Opacity
* @text Grid Opacity
* @desc Opacity of the grid lines (0-255).
* @default 128
* @type number
*
* @param Grid Color
* @text Grid Line Color
* @desc Color of the grid lines (CSS color format).
* @default #ffffff
* @type string
*
* @param Fill Grid
* @text Fill Grid
* @desc Whether to fill the grid cells with a background color.
* @default false
* @type boolean
*
* @param Fill Color
* @text Fill Color
* @desc Background color for the grid cells (CSS color format).
* @default #000000
* @type string
*
* @param Fill Opacity
* @text Fill Opacity
* @desc Opacity of the background fill (0-255).
* @default 64
* @type number
*
* @param Show Coordinates
* @text Show Coordinates
* @desc Whether to display X:Y coordinates in the center of each cell.
* @default false
* @type boolean
*
* @param Coordinates Color
* @text Coordinates Color
* @desc Color of the coordinates text (CSS color format).
* @default #ff0000
* @type string
*
* @param Coordinates Size
* @text Coordinates Size
* @desc Font size of the coordinates text.
* @default 12
* @type number
*
* @param Highlight Impassable
* @text Highlight Impassable Tiles
* @desc Whether to highlight impassable tiles with a different color.
* @default false
* @type boolean
*
* @param Impassable Color
* @text Impassable Tile Color
* @desc Color used to highlight impassable tiles (CSS color format).
* @default #ff0000
* @type string
*
* @param Impassable Opacity
* @text Impassable Tile Opacity
* @desc Opacity of the impassable tile highlight (0-255).
* @default 128
* @type number
*
* @command ToggleGrid
* @text Toggle Grid
* @desc Toggles the visibility of the grid overlay on the map.
*
* @help
* This plugin displays a customizable grid overlay on the map during gameplay.
*
*/
(() => {
const pluginName = "MapGridOverlay";
const parameters = PluginManager.parameters(pluginName);
const gridWidth = Number(parameters['Grid Width'] || 48);
const gridHeight = Number(parameters['Grid Height'] || 48);
const gridOpacity = Number(parameters['Grid Opacity'] || 128);
const gridColor = String(parameters['Grid Color'] || '#ffffff');
const fillGrid = parameters['Fill Grid'] === 'true';
const fillColor = String(parameters['Fill Color'] || '#000000');
const fillOpacity = Number(parameters['Fill Opacity'] || 64);
const showCoordinates = parameters['Show Coordinates'] === 'true';
const coordinatesColor = String(parameters['Coordinates Color'] || '#ff0000');
const coordinatesSize = Number(parameters['Coordinates Size'] || 12);
const highlightImpassable = parameters['Highlight Impassable'] === 'true';
const impassableColor = String(parameters['Impassable Color'] || '#ff0000');
const impassableOpacity = Number(parameters['Impassable Opacity'] || 128);
let gridVisible = false;
PluginManager.registerCommand(pluginName, "ToggleGrid", () => {
gridVisible = !gridVisible;
});
const _Spriteset_Map_createLowerLayer = Spriteset_Map.prototype.createLowerLayer;
Spriteset_Map.prototype.createLowerLayer = function () {
_Spriteset_Map_createLowerLayer.call(this);
this.createGridOverlay();
};
Spriteset_Map.prototype.createGridOverlay = function () {
this._gridSprite = new Sprite(new Bitmap(Graphics.width, Graphics.height));
this._gridSprite.opacity = gridOpacity;
this._gridSprite.visible = gridVisible;
this.addChild(this._gridSprite);
this.refreshGrid();
};
Spriteset_Map.prototype.isTileImpassable = function (tileX, tileY) {
const x = tileX;
const y = tileY;
if (!$gameMap.isPassable(x, y, 2) &&
!$gameMap.isPassable(x, y, 4) &&
!$gameMap.isPassable(x, y, 6) &&
!$gameMap.isPassable(x, y, 8)) {
return true;
}
const events = $gameMap.eventsXy(x, y);
for (const event of events) {
if (event._priorityType === 1 && !event.isThrough()) {
return true;
}
}
return false;
};
Spriteset_Map.prototype.refreshGrid = function () {
const bitmap = this._gridSprite.bitmap;
bitmap.clear();
if (gridVisible) {
const width = Graphics.width;
const height = Graphics.height;
const dx = $gameMap.displayX() * $gameMap.tileWidth();
const dy = $gameMap.displayY() * $gameMap.tileHeight();
const startX = -Math.floor(dx % gridWidth);
const startY = -Math.floor(dy % gridHeight);
for (let x = startX; x < width; x += gridWidth) {
for (let y = startY; y < height; y += gridHeight) {
const tileX = Math.floor((x + dx) / gridWidth);
const tileY = Math.floor((y + dy) / gridHeight);
if (highlightImpassable && this.isTileImpassable(tileX, tileY)) {
bitmap.paintOpacity = impassableOpacity;
bitmap.fillRect(x, y, gridWidth, gridHeight, impassableColor);
} else if (fillGrid) {
bitmap.paintOpacity = fillOpacity;
bitmap.fillRect(x, y, gridWidth, gridHeight, fillColor);
}
bitmap.paintOpacity = gridOpacity;
bitmap.fillRect(x, y, 1, gridHeight, gridColor);
bitmap.fillRect(x, y, gridWidth, 1, gridColor);
if (showCoordinates) {
bitmap.fontSize = coordinatesSize;
bitmap.textColor = coordinatesColor;
const text = `${tileX}:${tileY}`;
bitmap.drawText(text, x, y, gridWidth, gridHeight, 'center');
}
}
}
bitmap.fillRect(startX + Math.floor(width / gridWidth) * gridWidth, startY, 1, height, gridColor);
bitmap.fillRect(startX, startY + Math.floor(height / gridHeight) * gridHeight, width, 1, gridColor);
}
};
const _Spriteset_Map_update = Spriteset_Map.prototype.update;
Spriteset_Map.prototype.update = function () {
_Spriteset_Map_update.call(this);
if (this._gridSprite) {
this._gridSprite.visible = gridVisible;
this.refreshGrid();
}
};
})();