From e37a05b533f97b4b8c706225722b717f9633409a Mon Sep 17 00:00:00 2001 From: theripper93 Date: Sun, 18 Jul 2021 16:24:55 +0200 Subject: [PATCH] sett --- languages/en.json | 2 + scripts/BloodSplatter.js | 162 +++++++++++++++++++++------------------ scripts/config.js | 14 ++++ 3 files changed, 105 insertions(+), 73 deletions(-) diff --git a/languages/en.json b/languages/en.json index bccc0af..f5909bf 100644 --- a/languages/en.json +++ b/languages/en.json @@ -24,6 +24,8 @@ "splatter.settings.violence.hint":"Set how much blood to show and how frequent blood trails are", "splatter.settings.wallsBlockBlood.text":"Walls block blood", "splatter.settings.wallsBlockBlood.hint":"Enabling this option might lead to slow performance on lower end hardware", + "splatter.settings.onlyInCombat.text":"Only in combat", + "splatter.settings.onlyInCombat.hint":"Only show blood splats/trails when in combat", "splatter.controls.clearBlood.name":"Clear Blood", "splatter.controls.splatToken.name":"Bloodsplat Token", diff --git a/scripts/BloodSplatter.js b/scripts/BloodSplatter.js index 9980cce..03e4a8f 100644 --- a/scripts/BloodSplatter.js +++ b/scripts/BloodSplatter.js @@ -1,29 +1,26 @@ class BloodSplatter { constructor() { this.blood = new PIXI.Container(); - this.blood.name = "blood"; - const colorData = this.ColorStringToHexAlpha( - game.settings.get("splatter", "bloodColor") - ); - this.color = colorData?.color; - this.alpha = colorData?.alpha; - this.bloodSheet = game.settings.get("splatter", "useBloodsheet"); - this.bloodSheetData = game.settings.get("splatter", "BloodSheetData"); - this.violence = game.settings.get("splatter", "violence"); - this.wallsBlock = game.settings.get("splatter", "wallsBlockBlood") - this.scaleMulti = - (canvas.dimensions.size / 100) * - game.settings.get("splatter", "bloodsplatterScale"); + this.Update(); canvas.background.addChild(this.blood); canvas.background.BloodSplatter = this; } Splat(scale, color, alpha) { let scaleRandom = 0.8 + Math.random() * 0.4; - let cachedTex = PIXI.utils.TextureCache[`modules/splatter/bloodsplats/blood${Math.floor(Math.random() * 26)}.svg`] - let sprite = cachedTex ? new PIXI.Sprite.from(cachedTex) : new PIXI.Sprite.from( - `modules/splatter/bloodsplats/blood${Math.floor(Math.random() * 26)}.svg` - ); + let cachedTex = + PIXI.utils.TextureCache[ + `modules/splatter/bloodsplats/blood${Math.floor( + Math.random() * 26 + )}.svg` + ]; + let sprite = cachedTex + ? new PIXI.Sprite.from(cachedTex) + : new PIXI.Sprite.from( + `modules/splatter/bloodsplats/blood${Math.floor( + Math.random() * 26 + )}.svg` + ); sprite.anchor.set(0.5, 0.5); sprite.scale.set( scale * this.scaleMulti * scaleRandom, @@ -45,32 +42,33 @@ class BloodSplatter { if (colorFlag) { colorData = this.ColorStringToHexAlpha(colorFlag); } - const splatScale = token.data.scale * - Math.max(token.data.width, token.data.height) * - extraScale + const splatScale = + token.data.scale * + Math.max(token.data.width, token.data.height) * + extraScale; const violence = isTrail ? 1 : this.violence; let splatContainer = new PIXI.Container(); - splatContainer.x=token.center.x - splatContainer.y=token.center.y + splatContainer.x = token.center.x; + splatContainer.y = token.center.y; for (let i = 0; i < violence; i++) { - splatContainer.addChild(this.Splat( - splatScale, - colorData?.color, - colorData?.alpha - )); + splatContainer.addChild( + this.Splat(splatScale, colorData?.color, colorData?.alpha) + ); } - if(this.wallsBlock){ - const maxDimension = Math.max(splatContainer.width,splatContainer.height); - const radius = maxDimension > 10 ? maxDimension : 1000; + if (this.wallsBlock) { + const maxDimension = Math.max( + splatContainer.width, + splatContainer.height + ); + const radius = maxDimension > 10 ? maxDimension : 1000; const tokenMaxDim = Math.max(token.data.width, token.data.height); - if(radius >= tokenMaxDim){ - let mask = BloodSplatter.getMask(token.center,radius) - splatContainer.addChild(mask) - splatContainer.mask = mask + if (radius >= tokenMaxDim) { + let mask = BloodSplatter.getMask(token.center, radius); + splatContainer.addChild(mask); + splatContainer.mask = mask; } } this.blood.addChild(splatContainer); - } Destroy() { @@ -88,7 +86,11 @@ class BloodSplatter { this.bloodSheetData = game.settings.get("splatter", "BloodSheetData"); this.violence = game.settings.get("splatter", "violence"); this.scaleMulti = game.settings.get("splatter", "bloodsplatterScale"); - this.wallsBlock = game.settings.get("splatter", "wallsBlockBlood") + this.wallsBlock = game.settings.get("splatter", "wallsBlockBlood"); + this.inCombat = game.settings.get("splatter", "onlyInCombat"); + this.scaleMulti = + (canvas.dimensions.size / 100) * + game.settings.get("splatter", "bloodsplatterScale"); } ColorStringToHexAlpha(colorString) { @@ -105,6 +107,52 @@ class BloodSplatter { ); } + static getMask(origin, radius) { + const { rays, los, fov } = canvas.walls.computePolygon(origin, radius, { + type: "movement", + density: "12", + }); + let g = new PIXI.Graphics(); + g.beginFill(0xffffff); + g.drawPolygon(fov); + g.endFill(); + g.x -= origin.x; + g.y -= origin.y; + g.isMask = true; + return g; + } + + static bloodTrail(wrapped, ...args) { + if ( + !this.bleeding && + (!canvas.background.BloodSplatter?.inCombat || + (canvas.background.BloodSplatter?.inCombat && game.combat?.started)) + ) { + this.bleeding = true; + const timeout = canvas.background.BloodSplatter?.violence + ? 300 - canvas.background.BloodSplatter?.violence * 20 + : 100; + setTimeout(() => { + if (BloodSplatter.belowTreshold(this.actor)) { + if (canvas.background.BloodSplatter) { + canvas.background.BloodSplatter.SplatFromToken(this, { + extraScale: Math.random() * 0.5, + isTrail: true, + }); + } else { + new BloodSplatter(); + canvas.background.BloodSplatter.SplatFromToken(this, { + extraScale: Math.random() * 0.5, + isTrail: true, + }); + } + } + this.bleeding = false; + }, timeout); + } + return wrapped(...args); + } + static socketSplatFn(tokenIds) { for (let tokenId of tokenIds) { let token = canvas.tokens.get(tokenId); @@ -133,19 +181,6 @@ class BloodSplatter { return true; return false; } - - static getMask(origin,radius){ - const { rays, los, fov } = canvas.walls.computePolygon(origin, radius, {type: "movement", density: "12"}) - let g= new PIXI.Graphics() - g.beginFill(0xffffff) - g.drawPolygon(fov) - g.endFill() - g.x-=origin.x - g.y-=origin.y - g.isMask=true - return g - } - static getHpVal(actorData) { return Object.byString( actorData, @@ -167,29 +202,6 @@ class BloodSplatter { game.settings.get("splatter", "creatureTypeCustom") ); } - - static bloodTrail(wrapped, ...args) { - if (!this.bleeding) { - this.bleeding = true; - const timeout = canvas.background.BloodSplatter?.violence ? 300-canvas.background.BloodSplatter?.violence*20 : 100; - setTimeout(() => { - if (BloodSplatter.belowTreshold(this.actor)) { - if (canvas.background.BloodSplatter) { - canvas.background.BloodSplatter.SplatFromToken(this, { - extraScale: Math.random() * 0.5, isTrail: true, - }); - } else { - new BloodSplatter(); - canvas.background.BloodSplatter.SplatFromToken(this, { - extraScale: Math.random() * 0.5, isTrail: true, - }); - } - } - this.bleeding = false; - }, timeout); - } - return wrapped(...args); - } } let BloodSplatterSocket; @@ -200,7 +212,11 @@ Hooks.once("socketlib.ready", () => { }); Hooks.on("preUpdateActor", function (actor, updates) { - if (!game.settings.get("splatter", "enableBloodsplatter")) return; + if ( + !game.settings.get("splatter", "enableBloodsplatter") || + (game.settings.get("splatter", "onlyInCombat") && !game.combat?.started) + ) + return; let token = actor.parent ? canvas.tokens.get(actor.parent.id) : canvas.tokens.placeables.find((t) => t.actor.id == actor.id); diff --git a/scripts/config.js b/scripts/config.js index 53b3d87..f80420b 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -134,6 +134,20 @@ Hooks.once("init", function () { }, }); + game.settings.register("splatter", "onlyInCombat", { + name: game.i18n.localize("splatter.settings.onlyInCombat.text"), + hint: game.i18n.localize("splatter.settings.onlyInCombat.hint"), + scope: "world", + config: true, + type: Boolean, + default: false, + onChange: function () { + if (canvas.background.BloodSplatter) { + canvas.background.BloodSplatter.Update(); + } + }, + }); + game.settings.register("splatter", "creatureType", { name: game.i18n.localize("splatter.settings.creatureType.text"), hint: game.i18n.localize("splatter.settings.creatureType.hint"),