leave holes in the wall

This commit is contained in:
2020-03-22 19:18:32 +01:00
parent c6ec64f702
commit 32fa3b39ec
2 changed files with 196 additions and 148 deletions

View File

@@ -1,153 +1,184 @@
Hooks.on("getSceneDirectoryEntryContext", (html, list) => { Hooks.on("getSceneDirectoryEntryContext", (html, list) => {
console.log(html, list); console.log(html, list);
list.push({ list.push({
name: "DungeonDraft Import", name: "DungeonDraft Import",
icon: "<i class='fas fa-file-import'></i>", icon: "<i class='fas fa-file-import'></i>",
callback: scene => { callback: scene => {
DDImporter.Prompt(game.scenes.get(scene.attr("data-entity-id"))) DDImporter.Prompt(game.scenes.get(scene.attr("data-entity-id")))
} }
}) })
}) })
class DDImporter { class DDImporter {
static Prompt(scene) static Prompt(scene)
{ {
if (canvas.scene._id != scene.data._id) if (canvas.scene._id != scene.data._id)
{ {
ui.notifications.error("Move to this scene to import"); ui.notifications.error("Move to this scene to import");
return return
} }
let html = let html =
`<textarea class = "dd-import-text" name= "importData"></textarea>` `<textarea class = "dd-import-text" name= "importData"></textarea>`
new Dialog({ new Dialog({
title: "Dungeon Draft Import", title: "Dungeon Draft Import",
content : html, content : html,
buttons : { buttons : {
confirm : { confirm : {
label: "Confirm", label: "Confirm",
callback : (html) => this.DDImport(scene, JSON.parse(html.find('[name="importData').val())) callback : (html) => this.DDImport(scene, JSON.parse(html.find('[name="importData').val()))
},
cancel : {
label: "Cancel"
}
}, },
default: "confirm" cancel : {
label: "Cancel"
}
},
default: "confirm"
}).render(true); }).render(true);
} }
static async DDImport(scene, file) static async DDImport(scene, file)
{ {
let level = 0; let level = 0;
if (file.world.levels[1]) if (file.world.levels[1])
{ {
let levelOptions = ""; let levelOptions = "";
for (let level in file.world.levels) for (let level in file.world.levels)
levelOptions = levelOptions.concat(`<option value="${level}">${file.world.levels[level].label}</option>`) levelOptions = levelOptions.concat(`<option value="${level}">${file.world.levels[level].label}</option>`)
let html = let html =
`<p>Which level do you want to import?</p> `<p>Which level do you want to import?</p>
<select name = "level-selector"> <select name = "level-selector">
${levelOptions} ${levelOptions}
</select> </select>
` `
await new Dialog({ await new Dialog({
title: "Dungeon Draft Level Selector", title: "Dungeon Draft Level Selector",
content : html, content : html,
buttons : { buttons : {
confirm : { confirm : {
label: "Select", label: "Select",
callback : (html) => { callback : (html) => {
level = JSON.parse(html.find('[name="level-selector').val()) level = JSON.parse(html.find('[name="level-selector').val())
scene.update({"walls" : this.GetWalls(file, level)}); scene.update({"walls" : this.GetWalls(file, level)});
scene.update({"lights" : this.GetLights(file, level)}); scene.update({"lights" : this.GetLights(file, level)});
return; return;
} }
}, },
cancel : { cancel : {
label: "Cancel", label: "Cancel",
callback : html => {return} callback : html => {return}
} }
}, },
default: "confirm" default: "confirm"
}).render(true); }).render(true);
} }
else else
{ {
scene.update({"walls" : this.GetWalls(file, level)}); scene.update({"walls" : this.GetWalls(file, level)});
scene.update({"lights" : this.GetLights(file, level)}); scene.update({"lights" : this.GetLights(file, level)});
} }
}
static GetWalls(file, level)
{
}
static GetWalls(file, level)
{
let walls = []; let walls = [];
let ddPortalList = []; let ddPortalList = [];
for (let index in file.world.levels[level].walls) for (let index in file.world.levels[level].walls)
{ {
if (!isNaN(index)) if (!isNaN(index))
{
let ddWalls = file.world.levels[level].walls[index]
let wallSet = []
let ddPointString = ddWalls.points;
let points = ddPointString.substring(18, ddPointString.length-2).split(", ").map(a => Number(a))
let offsetX = canvas.dimensions.paddingX;
let offsetY = canvas.dimensions.paddingY;
let ddScale = canvas.grid.size/256
if (ddWalls.portals)
{ {
let ddWalls = file.world.levels[level].walls[index] for(let i = 0; i < points.length-3; i+=2)
let wallSet = [] {
let ddPointString = ddWalls.points; for (let portal of ddWalls.portals){
let points = ddPointString.substring(18, ddPointString.length-2).split(", ").map(a => Number(a)) console.log(portal);
let offsetX = canvas.dimensions.paddingX; let portalCenterPoint = portal.position.substring(8, portal.position.length-2).split(", ").map(a => Number(a))
let offsetY = canvas.dimensions.paddingY; let portalDirection = portal.direction.substring(8, portal.direction.length-2).split(", ").map(a => Number(a))
let ddScale = canvas.grid.size/256 let portalPoint1 = [portalCenterPoint[0] + portal.radius*portalDirection[0], portalCenterPoint[1] + portal.radius*portalDirection[1]]
let portalPoint2 = [portalCenterPoint[0] - portal.radius*portalDirection[0], portalCenterPoint[1] - portal.radius*portalDirection[1]]
/* if (ddWalls.portals) console.log(portalCenterPoint)
{ console.log(portalPoint1)
for(let i = 0; i < points.length-3; i+=2) console.log(portalPoint2)
{ let wall = new Wall({
let point1 = [(points[i]*ddScale)+offsetX, (points[i+1]*ddScale)+offsetY] c : [point1[0], point1[1], point2[0], point2[1]],
let point2 = [(points[i+2]*ddScale)+offsetX, (points[i+3]*ddScale)+offsetY] });
wall.data.door = CONST.WALL_DOOR_TYPES.DOOR
let portal = ddWalls.portals[0]; walls.push(wall.data)
let portalPoints = this.findDoorPoints(portal); let point1 = [(points[i]*ddScale)+offsetX, (points[i+1]*ddScale)+offsetY]
if (this.pointIsOnLine([portalPoints[0], portalPoints[1]], point1.concat(point2))) let point2 = [(points[i+2]*ddScale)+offsetX, (points[i+3]*ddScale)+offsetY]
{ let line = [point1,point2]
if (this.pointIsOnLine(portalCenterPoint,line))
} {
else if (this.pointIsOnLine([portalPoints[2], portalPoints[3]], point1.concat(point2))) console.log('yes');
{ console.log(this.getNearerPoint(point1, [portalPoint1, portalPoint2]));
console.log(this.getNearerPoint(point2, [portalPoint1, portalPoint2]));
} topoint1 = this.getNearerPoint(point1, [portalPoint1, portalPoint2]);
topoint2 = this.getNearerPoint(point2, [portalPoint1, portalPoint2]);
let wall = new Wall({ let wall = new Wall({
c : [(points[i]*ddScale)+offsetX, (points[i+1]*ddScale)+offsetY, (points[i+2]*ddScale)+offsetX, (points[i+3]*ddScale)+offsetY], c : [
}); point1[0],
wallSet.push(wall.data) point1[1],
} topoint1[0],
topoint1[1],
if (wallSet.length % 2 != 0) });
wallSet.push(new Wall({ wallSet.push(wall.data)
c: [wallSet[0].c[0], wallSet[0].c[1], wallSet[wallSet.length-1].c[2], wallSet[wallSet.length-1].c[3]] let wall = new Wall({
}).data) c : [
point2[0],
walls = walls.concat(wallSet); point2[1],
}*/ topoint2[0],
for(let i = 0; i < points.length-3; i+=2) topoint2[1],
{ });
wallSet.push(wall.data)
let wall = new Wall({ }
c : [(points[i]*ddScale)+offsetX, (points[i+1]*ddScale)+offsetY, (points[i+2]*ddScale)+offsetX, (points[i+3]*ddScale)+offsetY], else{
}); let wall = new Wall({
wallSet.push(wall.data) c : [
} (points[i]*ddScale)+offsetX,
(points[i+1]*ddScale)+offsetY,
if (wallSet.length % 2 != 0) (points[i+2]*ddScale)+offsetX,
wallSet.push(new Wall({ (points[i+3]*ddScale)+offsetY],
c: [wallSet[0].c[0], wallSet[0].c[1], wallSet[wallSet.length-1].c[2], wallSet[wallSet.length-1].c[3]] });
}).data) wallSet.push(wall.data)
}
walls = walls.concat(wallSet); }
}
} }
else
{
for(let i = 0; i < points.length-3; i+=2)
{
let wall = new Wall({
c : [
(points[i]*ddScale)+offsetX,
(points[i+1]*ddScale)+offsetY,
(points[i+2]*ddScale)+offsetX,
(points[i+3]*ddScale)+offsetY],
});
wallSet.push(wall.data)
}
if (wallSet.length % 2 != 0)
wallSet.push(new Wall({
c: [wallSet[0].c[0], wallSet[0].c[1], wallSet[wallSet.length-1].c[2], wallSet[wallSet.length-1].c[3]],
}).data)
}
walls = walls.concat(wallSet);
}
} }
@@ -170,18 +201,28 @@ static GetWalls(file, level)
return walls return walls
} }
static pointIsOnLine(point, line) static pointIsOnLine(point, line)
{ {
slope = (line[1] - line[3])/(line[0] - line[2]) var slope = (line[0][1] - line[1][1])/(line[0][0] - line[1][0])
intercept = line[3] - slope * line[2] var intercept = line[1][1] - slope * line[1][0]
return (point[1] == (slope * point[0] + intercept)) return (point[1] == (slope * point[0] + intercept))
} }
static findDoorPoints(ddPortal) static pointsDistance(point1, point2){
{ return Math.sqrt(((point2[0] - point1[0])**2) + (point2[1] - point1[1])**2);
}
static getNearerPoint(from, points){
if (this.pointsDistance(from, points[0]) < this.pointsDistance(from, points[1]))
return points[0]
return points[1]
}
static findDoorPoints(ddPortal)
{
let ddPortalString = ddPortals.position; let ddPortalString = ddPortals.position;
let offsetX = canvas.dimensions.paddingX; let offsetX = canvas.dimensions.paddingX;
let offsetY = canvas.dimensions.paddingY; let offsetY = canvas.dimensions.paddingY;
@@ -206,15 +247,15 @@ static findDoorPoints(ddPortal)
point2[1]+=adjustY; point2[1]+=adjustY;
return point1, point2 return point1, point2
} }
static GetLights(file, level) static GetLights(file, level)
{ {
let lights = []; let lights = [];
for (let index in file.world.levels[level].lights) for (let index in file.world.levels[level].lights)
{ {
if (!isNaN(index)) if (!isNaN(index))
{ {
let ddPointString = file.world.levels[level].lights[index].position let ddPointString = file.world.levels[level].lights[index].position
let ddLight = file.world.levels[level].lights[index] let ddLight = file.world.levels[level].lights[index]
let points = ddPointString.substring(9, ddPointString.length-2).split(", ").map(a => Number(a)) let points = ddPointString.substring(9, ddPointString.length-2).split(", ").map(a => Number(a))
@@ -223,23 +264,23 @@ static GetLights(file, level)
let ddScale = canvas.grid.size/256 let ddScale = canvas.grid.size/256
let light = new AmbientLight({ let light = new AmbientLight({
t: "l", t: "l",
x: (points[0]*ddScale)+offsetX, x: (points[0]*ddScale)+offsetX,
y: (points[1]*ddScale)+offsetY, y: (points[1]*ddScale)+offsetY,
rotation: 0, rotation: 0,
dim: ddLight.range*2, dim: ddLight.range*2,
bright: ddLight.range, bright: ddLight.range,
angle: 360, angle: 360,
tintColor: "#" + ddLight.color.substring(2), tintColor: "#" + ddLight.color.substring(2),
tintAlpha: 0.05 tintAlpha: 0.05
}) })
lights.push(light.data); lights.push(light.data);
} }
} }
return lights; return lights;
} }
} }
Hooks.on("ready", ev => { Hooks.on("ready", ev => {
canvas.stage.on('mousedown', (ev) => console.log(ev.data.destination.x - 768, ev.data.destination.y -768)); canvas.stage.on('mousedown', (ev) => console.log(ev.data.destination.x - 768, ev.data.destination.y -768));
}) })

View File

@@ -2,12 +2,19 @@
"name": "dd-import", "name": "dd-import",
"title": "DungeonDraft Importer", "title": "DungeonDraft Importer",
"description": "Imports scene elements from Dungeon Draft map files", "description": "Imports scene elements from Dungeon Draft map files",
"version": 0.3, "version": "0.4.1",
"author": "Moo Man", "authors": [
{
"name": "Moo Man"
},
{
"name": "m42e"
}
],
"scripts": ["./ddimport.js"], "scripts": ["./ddimport.js"],
"styles": ["./styles.css"], "styles": ["./styles.css"],
"packs": [], "packs": [],
"minimumCoreVersion": "0.5.1", "minimumCoreVersion": "0.5.1",
"manifest": "https://raw.githubusercontent.com/moo-man/FVTT-DD-Import/master/module.json", "manifest": "https://raw.githubusercontent.com/m42e/FVTT-DD-Import/master/module.json",
"download": "https://github.com/moo-man/FVTT-DD-Import/archive/master.zip" "download": "https://github.com/m42e/FVTT-DD-Import/archive/master.zip"
} }