Skip to content

Commit

Permalink
10.7 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ironmonk88 committed Apr 12, 2023
1 parent 1918ba9 commit 4e629c0
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 59 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Version 10.7

Fixed issue with holding shift to snap to grid

Fixed issue with negative dpeth.

Fixed issue getting access to the terrain from the canvas scene

Added webbing as an obstacle

Fixed issue with tokens causing difficult terrain.

Swapped setting up terrain to initialization rather than on draw.

# Version 10.6

Fixed issue refreshing the terrain shape if the shape doesn't exist
Expand Down
3 changes: 2 additions & 1 deletion classes/terrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,14 +762,15 @@ export class Terrain extends PlaceableObject {
const now = Date.now();
temporary = (now - this._drawTime) < this.constructor.FREEHAND_SAMPLE_RATE;
}
const snap = !(isShift || isFreehand);
const snap = isShift && !isFreehand;
this._addPoint(position, { snap, temporary });
break;

// Other Shapes
default:
const shape = this.shape;
const minSize = canvas.dimensions.size * 0.5;
if (isShift) position = canvas.grid.getSnappedPosition(position.x, position.y, this.layer.gridPrecision);
let dx = position.x - origin.x;
let dy = position.y - origin.y;
if (Math.abs(dx) < minSize) dx = minSize * Math.sign(shape.width);
Expand Down
14 changes: 7 additions & 7 deletions classes/terraindocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ export class TerrainDocument extends CanvasDocumentMixin(BaseTerrain) {
}

get top() {
return this.elevation + this.depth;
return (this.depth < 0 ? this.elevation : this.elevation + this.depth);
}

get bottom() {
return this.elevation;
return (this.depth < 0 ? this.elevation + this.depth: this.elevation);
}

/* -------------------------------------------- */
Expand Down Expand Up @@ -239,7 +239,7 @@ export class TerrainDocument extends CanvasDocumentMixin(BaseTerrain) {
}

//add it to the terrain set
canvas["#scene"].terrain.set(document._id, document);
canvas.scene.terrain.set(document._id, document);

//if the multiple has changed then update the image
if (document._object != undefined)
Expand Down Expand Up @@ -272,7 +272,7 @@ export class TerrainDocument extends CanvasDocumentMixin(BaseTerrain) {
let originals = [];
let updated = [];
for (let update of updates) {
let document = canvas["#scene"].terrain.get(update._id);
let document = canvas.scene.terrain.get(update._id);

if (game.user.isGM) {
originals.push(document.toObject(false));
Expand Down Expand Up @@ -328,15 +328,15 @@ export class TerrainDocument extends CanvasDocumentMixin(BaseTerrain) {
let updates = [];
let originals = [];
let deleted = [];
const deleteIds = options.deleteAll ? canvas["#scene"].terrain.keys() : ids;
const deleteIds = options.deleteAll ? canvas.scene.terrain.keys() : ids;
for (let id of deleteIds) {
let terrain = canvas["#scene"].terrain.find(t => t.id == id);
let terrain = canvas.scene.terrain.find(t => t.id == id);

if (terrain == undefined)
continue;

//remove this object from the terrain list
canvas["#scene"].terrain.delete(id);
canvas.scene.terrain.delete(id);

if (game.user.isGM) {
let key = `flags.enhanced-terrain-layer.-=terrain${id}`;
Expand Down
56 changes: 13 additions & 43 deletions classes/terrainlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export class TerrainLayer extends PlaceablesLayer {
}

getDocuments() {
return canvas["#scene"].terrain || null;
return canvas.scene.terrain || null;
}

get(objectId) {
return canvas["#scene"].terrain?.get(objectId)?.object || undefined;
return canvas.scene.terrain?.get(objectId)?.object || undefined;
}

get gridPrecision() {
Expand Down Expand Up @@ -101,7 +101,8 @@ export class TerrainLayer extends PlaceablesLayer {
{ id: 'furniture', text: 'EnhancedTerrainLayer.obstacle.furniture', icon: 'modules/enhanced-terrain-layer/img/environment/furniture.png', obstacle: true },
{ id: 'magic', text: 'EnhancedTerrainLayer.obstacle.magic', icon: 'modules/enhanced-terrain-layer/img/environment/magic.png', obstacle: true },
{ id: 'plants', text: 'EnhancedTerrainLayer.obstacle.plants', icon: 'modules/enhanced-terrain-layer/img/environment/plants.png', obstacle: true },
{ id: 'rubble', text: 'EnhancedTerrainLayer.obstacle.rubble', icon: 'modules/enhanced-terrain-layer/img/environment/rubble.png', obstacle: true }
{ id: 'rubble', text: 'EnhancedTerrainLayer.obstacle.rubble', icon: 'modules/enhanced-terrain-layer/img/environment/rubble.png', obstacle: true },
{ id: 'webbing', text: 'EnhancedTerrainLayer.obstacle.webbing', icon: 'modules/enhanced-terrain-layer/img/environment/spiderweb.png', obstacle: true }
];

Hooks.call(`getTerrainEnvironments`, this, environments);
Expand Down Expand Up @@ -210,8 +211,14 @@ export class TerrainLayer extends PlaceablesLayer {
if (!terrainFlag)
continue;
const terraincost = terrainFlag.multiple ?? 1;
const terrainbottom = terrainFlag.elevation ?? Terrain.defaults.elevation;
const terraintop = terrainbottom + (terrainFlag.depth ?? Terrain.defaults.depth);
let terrainbottom = terrainFlag.elevation ?? Terrain.defaults.elevation;
let terraintop = terrainbottom + (terrainFlag.depth ?? Terrain.defaults.depth);
if (terraintop < terrainbottom) {
let temp = terrainbottom;
terrainbottom = terraintop;
terraintop = temp;
}

const environment = terrainFlag.environment || '';
const obstacle = terrainFlag.obstacle || '';
if (elevation < terrainbottom || elevation > terraintop)
Expand Down Expand Up @@ -247,7 +254,7 @@ export class TerrainLayer extends PlaceablesLayer {
continue;
if (token.hidden)
continue;
if (elevation != undefined && token.elevation != undefined && token.elevation != elevation)
if (elevation != undefined && token.document.elevation != undefined && token.document.elevation != elevation)
continue;
let dead = isDead(token);
let checkValue = dead ? tokenDead : tokenDifficult;
Expand Down Expand Up @@ -339,43 +346,6 @@ export class TerrainLayer extends PlaceablesLayer {
}

async draw() {
canvas["#scene"].terrain = new foundry.abstract.EmbeddedCollection(canvas.scene, [], Terrain);
let etl = canvas.scene.flags['enhanced-terrain-layer'];
if (etl) {
for (let [k, v] of Object.entries(etl)) {
if (k.startsWith('terrain')) {
if (k != 'terrainundefined' && v != undefined && v.x != undefined && v.y != undefined && v._id != undefined) {
//lets correct any changes
let change = false;
if (v.environment == '' && v.obstacle != '') {
v.environment = v.obstacle;
v.obstacle = '';
change = true;
}
if (v.elevation == undefined || v.depth == undefined) {
if (v.terrainheight != undefined && typeof v.terrainheight === 'string')
v.terrainheight = JSON.parse(v.terrainheight);
v.elevation = v.min || (v.terrainheight != undefined ? v.terrainheight.min : (v.terraintype == 'air' ? 5 : 0)) || 0;
let max = v.max || (v.terrainheight != undefined ? v.terrainheight.max : (v.terraintype == 'air' || v.terraintype == 'both' ? 100 : 0)) || 0;
v.depth = max - v.elevation;
change = true;
}

change = !!TerrainDocument.migrateData(v);

if (change)
await canvas.scene.setFlag('enhanced-terrain-layer', k, v);

//add this the the terrain collection
let document = new TerrainDocument(v, { parent: canvas.scene });
canvas["#scene"].terrain.set(v._id, document);
}
else
await canvas.scene.unsetFlag('enhanced-terrain-layer', k);
}
};
}

const d = canvas.dimensions;
this.width = d.width;
this.height = d.height;
Expand Down
Binary file added img/environment/spiderweb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified img/environment/underdark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@
"EnhancedTerrainLayer.obstacle.magic": "Magic",
"EnhancedTerrainLayer.obstacle.plants": "Plants",
"EnhancedTerrainLayer.obstacle.rubble": "Rubble",
"EnhancedTerrainLayer.obstacle.water": "Water"

"EnhancedTerrainLayer.obstacle.water": "Water",
"EnhancedTerrainLayer.obstacle.webbing": "Webbing"
}
11 changes: 7 additions & 4 deletions module.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"title": "Enhanced Terrain Layer",
"description": "A base module that adds a Terrain Layer to Foundry. Used as a library for Rulers and other modules",
"version": "10.6",
"version": "10.7",
"authors": [
{
"name": "IronMonk",
"discord": "ironmonk88#4075",
"patreon": "https://www.patreon.com/ironmonk",
"ko-fi": "https://ko-fi.com/ironmonk88"
"flags": {
"github": "https://github.com/ironmonk88",
"patreon": "https://www.patreon.com/ironmonk",
"ko-fi": "https://ko-fi.com/ironmonk88"
}
}
],
"socket": true,
Expand Down Expand Up @@ -47,7 +50,7 @@
"css/terrainlayer.css"
],
"url": "https://github.com/ironmonk88/enhanced-terrain-layer",
"download": "https://github.com/ironmonk88/enhanced-terrain-layer/archive/10.6.zip",
"download": "https://github.com/ironmonk88/enhanced-terrain-layer/archive/10.7.zip",
"manifest": "https://github.com/ironmonk88/enhanced-terrain-layer/releases/latest/download/module.json",
"bugs": "https://github.com/ironmonk88/enhanced-terrain-layer/issues",
"allowBugReporter": true,
Expand Down
71 changes: 69 additions & 2 deletions terrain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function registerLayer() {
objectClass: Terrain
};

canvas["#scene"] = {};
//canvas["#scene"] = {};

let createEmbeddedDocuments = async function (wrapped, ...args) {
let [embeddedName, updates = [], context = {}] = args;
Expand Down Expand Up @@ -147,11 +147,13 @@ function registerLayer() {
return oldDeleteEmbeddedDocuments.call(this, embeddedName, ids, context);
}*/

/*
Object.defineProperty(Scene.prototype, "terrain", {
get: function terrain() {
return this.data.terrain;
}
});
*/
}

/*
Expand Down Expand Up @@ -321,6 +323,55 @@ async function addControlsv9(app, dest, { full = false, insert = false } = {}) {
$('input[name="flags.enhanced-terrain-layer.drawcolor"]', dest).attr("placeholder", defaults.drawcolor);
}

function setupScene(scene) {
scene.terrain = new foundry.abstract.EmbeddedCollection(scene, [], Terrain);
let etl = scene.flags['enhanced-terrain-layer'];
if (etl) {
for (let [k, v] of Object.entries(etl)) {
if (k.startsWith('terrain')) {
if (k != 'terrainundefined' && v != undefined && v.x != undefined && v.y != undefined && v._id != undefined) {
//lets correct any changes
let change = false;
if (v.environment == '' && v.obstacle != '') {
v.environment = v.obstacle;
v.obstacle = '';
change = true;
}
if (v.elevation == undefined || v.depth == undefined) {
if (v.terrainheight != undefined && typeof v.terrainheight === 'string')
v.terrainheight = JSON.parse(v.terrainheight);
v.elevation = v.min || (v.terrainheight != undefined ? v.terrainheight.min : (v.terraintype == 'air' ? 5 : 0)) || 0;
let max = v.max || (v.terrainheight != undefined ? v.terrainheight.max : (v.terraintype == 'air' || v.terraintype == 'both' ? 100 : 0)) || 0;
v.depth = max - v.elevation;
change = true;
}

change = !!TerrainDocument.migrateData(v);

if (change) {
if (game.user.isGM)
scene.setFlag('enhanced-terrain-layer', k, v);
setProperty(scene, `flags.enhanced-terrain-layer.${k}`, v);
}

//add this the the terrain collection
try {
let document = new TerrainDocument(v, { parent: scene });
scene.terrain.set(v._id, document);
} catch (err) {
error(err);
}
}
else {
if (game.user.isGM)
scene.unsetFlag('enhanced-terrain-layer', k);
delete scene.flags["enhanced-terrain-layer"][k]
}
}
};
}
}

Hooks.on('canvasInit', () => {
canvas.hud.terrain = new TerrainHUD();
//Scene.constructor.config.embeddedEntities.Terrain = "terrain";
Expand Down Expand Up @@ -393,6 +444,22 @@ Hooks.on('init', async () => {
registerLayer();
registerKeybindings();

let initializeDocuments = async function (wrapped, ...args) {
wrapped(...args);
for (let scene of game.scenes) {
setupScene(scene);
}
}

if (game.modules.get("lib-wrapper")?.active) {
libWrapper.register("enhanced-terrain-layer", "Game.prototype.initializeDocuments", initializeDocuments, "WRAPPER");
} else {
const oldInitializeDocuments = Game.prototype.initializeDocuments;
Game.prototype.initializeDocuments = function (event) {
return initializeDocuments.call(this, oldInitializeDocuments.bind(this), ...arguments);
}
}

//remove old layer's controls
let getControlButtons = function (wrapped, ...args) {
let controls = wrapped.call(this, ...args);
Expand Down Expand Up @@ -623,7 +690,7 @@ Hooks.on("updateScene", (scene, data) => {
if (getProperty(data, "flags.enhanced-terrain-layer.opacity") || getProperty(data, "flags.enhanced-terrain-layer.drawcolor")) {
canvas.terrain.refresh(true); //refresh the terrain to respond to default terrain color
}
if (canvas.terrain.toolbar)
if (canvas.terrain?.toolbar)
canvas.terrain.toolbar.render(true);
});

Expand Down

0 comments on commit 4e629c0

Please sign in to comment.