Skip to content

Commit

Permalink
sett
Browse files Browse the repository at this point in the history
  • Loading branch information
theripper93 committed Jul 18, 2021
1 parent 31b205d commit e37a05b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 73 deletions.
2 changes: 2 additions & 0 deletions languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
162 changes: 89 additions & 73 deletions scripts/BloodSplatter.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit e37a05b

Please sign in to comment.