Skip to content

Commit

Permalink
1.0.4 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
IronMonk88 committed Mar 13, 2021
1 parent 4792459 commit ce76ea7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
12 changes: 1 addition & 11 deletions classes/terrain.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { log, setting } from "./terrainlayer.js";
import { log, setting, makeid } from "./terrainlayer.js";

export class Terrain extends PlaceableObject {
constructor(...args) {
Expand Down Expand Up @@ -86,16 +86,6 @@ export class Terrain extends PlaceableObject {
* @private
*/
_cleanData() {
let makeid = function () {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < 16; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

if (this.data._id == undefined)
this.data._id = makeid();

Expand Down
41 changes: 34 additions & 7 deletions classes/terrainlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export let setting = key => {
return game.settings.get("TerrainLayer", key);
};

export let makeid = () => {
return TerrainLayer.makeid();
}

export class TerrainLayer extends PlaceablesLayer {
constructor() {
super();
Expand All @@ -42,6 +46,16 @@ export class TerrainLayer extends PlaceablesLayer {
return [0.5, 2, 3, 4];
}

static makeid = function () {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < 16; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}

static multipleText(multiple) {
return (parseInt(multiple) == 1 || parseInt(multiple) == 0.5 ? '1/2' : multiple);
}
Expand Down Expand Up @@ -100,7 +114,12 @@ export class TerrainLayer extends PlaceablesLayer {
let grid = canvas.scene.getFlag('TerrainLayer', 'costGrid');
for (let y in grid) {
for (let x in grid[y]) {
this.createTerrain({ x: parseInt(x), y: parseInt(y), multiple: grid[y][x].multiple });
if (canvas.scene.data.terrain.find(t => { return t.x == x && t.y == y }) == undefined) {
let id = makeid();
let data = { _id: id, x: parseInt(x), y: parseInt(y), multiple: grid[y][x].multiple };
canvas.scene.data.terrain.push(data);
await canvas.scene.setFlag('TerrainLayer', 'terrain' + id, data);
}
}
}
//canvas.scene.unsetFlag('TerrainLayer', 'costGrid');
Expand Down Expand Up @@ -217,16 +236,19 @@ export class TerrainLayer extends PlaceablesLayer {
for (let id of ids) {
const object = this.get(id);
log('Removing terrain', object.data.x, object.data.y);
originals.push(object);
if(!options.isUndo)
originals.push(object.data);
this.objects.removeChild(object);
delete this._controlled[id];
object._onDelete(options, game.user.id);
object.destroy({ children: true });
canvas.scene.data.terrain.findSplice(t => { return t._id == id; });
let key = `flags.TerrainLayer.-=terrain${id}`;
updates[key] = null;
}

this.storeHistory("delete", originals);
if (!options.isUndo)
this.storeHistory("delete", originals);

this._costGrid = null;

Expand All @@ -236,6 +258,7 @@ export class TerrainLayer extends PlaceablesLayer {
_onClickLeft(event) {
super._onClickLeft(event);
if (game.activeTool == 'addterrain') {
this.originals = [];
let pos = event.data.getLocalPosition(canvas.app.stage);
let gridPt = canvas.grid.grid.getGridPositionFromPixels(pos.x, pos.y);
let [y, x] = gridPt; //Normalize the returned data because it's in [y,x] format
Expand Down Expand Up @@ -304,10 +327,11 @@ export class TerrainLayer extends PlaceablesLayer {

_onDragLeftDrop(e) {
if (game.activeTool == "select") {
canvas._onDragLeftDrop(event);
canvas._onDragLeftDrop(e);
}
else if (game.activeTool != 'addterrain') {
super._onDragLeftDrop(event);
else if (game.activeTool == 'addterrain') {
this.storeHistory("create", this.originals);
delete this.originals;
}
}

Expand Down Expand Up @@ -343,7 +367,10 @@ export class TerrainLayer extends PlaceablesLayer {
createTerrain(data, options = { }) {
if (!this.terrainExists(data.x, data.y)) {
data.multiple = data.multiple || this.defaultmultiple;
this.constructor.placeableClass.create(data, options);
this.constructor.placeableClass.create(data, options).then((terrain) => {
if (this.originals != undefined)
this.originals.push(terrain);
});
}
this._costGrid = null;
}
Expand Down
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "Terrain Layer",
"description": "A base module that adds a Terrain Layer to Foundry to paint difficult terrain squares and to be used as a dependency for other mods who might integrate it with their functionality.",
"authors": [{"name":"Will Saunders","email":"[email protected]" }, {"name":"IronMonk, ironmonk88#4075" }],
"version": "1.0.3",
"version": "1.0.4",
"minimumCoreVersion": "0.6.6",
"compatibleCoreVersion":"0.7.9",
"scripts":[
Expand Down Expand Up @@ -31,6 +31,6 @@
"socket":true,
"url":"https://github.com/ironmonk88/TerrainLayer",
"changelog":"https://raw.githubusercontent.com/ironmonk88/TerrainLayer/main/README.md",
"download": "https://github.com/ironmonk88/TerrainLayer/archive/1.0.3.zip",
"download": "https://github.com/ironmonk88/TerrainLayer/archive/1.0.4.zip",
"manifest": "https://github.com/ironmonk88/TerrainLayer/releases/latest/download/module.json"
}
29 changes: 12 additions & 17 deletions terrain-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { TerrainLayer } from './classes/terrainlayer.js';
import { TerrainHUD } from './classes/terrainhud.js';
import { registerSettings } from "./js/settings.js";

let theLayers = Canvas.layers;
theLayers.terrain = TerrainLayer;

/*
let oldConfig = Scene.prototype.constructor.config;
Scene.prototype.constructor.config = function () {
let result = oldConfig.call(this);
result.embeddedEntities.Terrain = "terrains";
return result;
}*/
function registerLayer() {
const layers = mergeObject(Canvas.layers, {
terrain: TerrainLayer
});
Object.defineProperty(Canvas, 'layers', {
get: function () {
return layers;
}
});
}

Hooks.on('canvasInit', () => {
canvas.hud.terrain = new TerrainHUD();
Expand All @@ -25,6 +25,7 @@ Hooks.on('init', () => {
});

registerSettings();
registerLayer();

let oldOnDragLeftStart = Token.prototype._onDragLeftStart;
Token.prototype._onDragLeftStart = function (event) {
Expand All @@ -47,7 +48,7 @@ Hooks.on('init', () => {

oldOnDragLeftCancel.apply(this, [event])
}
let handleDragCancel = MouseInteractionManager.prototype._handleDragCancel;
//let handleDragCancel = MouseInteractionManager.prototype._handleDragCancel;

/*
MouseInteractionManager.prototype._handleDragCancel = function (event) {
Expand All @@ -56,9 +57,3 @@ Hooks.on('init', () => {
handleDragCancel.apply(this, [event])
}*/
})

Object.defineProperty(Canvas, 'layers', {
get: function () {
return theLayers;
}
})

0 comments on commit ce76ea7

Please sign in to comment.