-
Notifications
You must be signed in to change notification settings - Fork 24
/
follow-warp.js
87 lines (75 loc) · 2.17 KB
/
follow-warp.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
/// <reference types="@mapeditor/tiled-api" />
/*
* follow-warp.js
*
* This extension adds a 'Follow Selected Warp' (Ctrl+F) action to the Map
* menu, which can be used to jump to the destination of a selected warp object
* (as used on maps made for Source of Tales - http://sourceoftales.org/).
*
* Warps are common in games and there are of course many ways to implement
* them, so this script is unlikely to work in your particular project. But
* with some adjustments it probably could!
*/
/**
* @param thing {TileMap | GroupLayer}
* @param name {string}
*/
function findObjectByName(thing, name) {
for (let i = thing.layerCount - 1; i >= 0; i--) {
const layer = thing.layerAt(i);
if (layer.isGroupLayer) {
const obj = findObjectByName(layer, name);
if (obj) {
return obj;
}
} else if (layer.isObjectLayer) {
for (const obj of layer.objects) {
if (obj.name == name) {
return obj;
}
}
}
}
return null;
}
let followWarp = tiled.registerAction("FollowWarp", function(/* action */) {
/** @type TileMap */
const map = tiled.activeAsset;
if (!map.isTileMap) {
tiled.alert("Not a tile map!");
return;
}
const selectedObject = map.selectedObjects[0];
if (!selectedObject) {
tiled.alert("No object selected!");
return;
}
const destMapProperty = selectedObject.property("DEST_MAP");
if (!destMapProperty) {
tiled.alert("No DEST_MAP property!");
return;
}
const mapsPath = map.fileName.substr(0, map.fileName.indexOf("maps/") + 5);
const destinationMapFile = mapsPath + destMapProperty + ".tmx";
const destinationName = selectedObject.property("DEST_NAME");
/** @type TileMap */
const destinationMap = tiled.open(destinationMapFile);
if (!destinationMap) {
return;
}
if (destinationName) {
const object = findObjectByName(destinationMap, destinationName);
if (!object) {
tiled.alert("Failed to find object named '" + destinationName + "'");
return;
}
tiled.mapEditor.currentMapView.centerOn(object.x, object.y);
destinationMap.selectedObjects = [object];
}
});
followWarp.text = "Follow Selected Warp";
followWarp.shortcut = "Ctrl+F";
tiled.extendMenu("Map", [
{ separator: true },
{ action: "FollowWarp" },
]);