Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let figureWounds = function(attributes,setObj){
- let hpPercent = attributes.hp/attributes.hp_max,
- woundsObj = {};
- if(hpPercent > 0.5){
- woundsObj.inconscient = 0;
- woundsObj.blessure = 0;
- woundsObj.blessure_grave = 0;
- }else if(hpPercent > 0.25){
- woundsObj.inconscient = 0;
- woundsObj.blessure = 1;
- woundsObj.blessure_grave = 0;
- }else if(hpPercent > 0){
- woundsObj.inconscient = 0;
- woundsObj.blessure = 1;
- woundsObj.blessure_grave = 1;
- }else{
- woundsObj.inconscient = 1;
- woundsObj.blessure = 1;
- woundsObj.blessure_grave = 1
- }
- Object.assign(setObj,woundsObj);//Because objects are passed as a reference in javascript. Any changes made in this function will affect the original version of setObj, so we don't need to return anything.
- };
- on('change:body change:mass_scale change:mind change:will change:agility',(event)=>{
- getAttrs(['body','mass_scale','mind','will','agility','hp'],(attributes)=>{
- let setObj = {
- hp_max:(10*attributes.body||10) + (10*attributes.mass_scale||10) + (attributes.body*1||1) + (attributes.mind*1||1) + (attributes.will*1||1) + (attributes.agility*1||1)
- };
- attributes.hp_max = setObj.hp_max;
- figureWounds(attributes,setObj);
- setAttrs(setObj,{silent:true});
- });
- });
- on('change:hp change:hp_max',(event)=>{
- getAttrs(['hp','hp_max'],(attributes)=>{
- let setObj = {};
- figureWounds(attributes,setObj);
- setAttrs(setObj,{silent:true});
- });
- });
- const woundlvls = {blessure: 1, blessure_grave: 2, inconscient: 9};
- const woundnames = Object.keys(woundlvls);
- const woundPenalty = wounds => {
- const penalty = wounds.reduce((total, current, index) => {
- return total + current * woundlvls[woundnames[index]];
- },0 );
- return penalty >= 9 ? 0: (penalty > 2 ? 2 : penalty);
- };
- // this one updates wound penalties when you manually click a wound box
- on(woundnames.map(wound => `change:${wound}`).join(' '), function() {
- getAttrs(woundnames, function(v) {
- let wounds = woundnames.map(lvl => +v[lvl] || 0);
- let penalty = woundPenalty(wounds);
- setAttrs({
- hurtlvl: penalty
- }, {silent: true});
- });
- });
- on('change:woundlvl', function(event) {
- getAttrs([...woundnames, 'woundlvl'], function(v) {
- // iterate through hurt levels, build an array of 0s and 1s to define whether that wound level is checked
- let wounds = woundnames.map(lvl => +v[lvl] || 0);
- // ==== GET VALUE OF NEW WOUND
- let newWound = parseInt(v.woundlvl) || 0;
- // if its 2+, need to add 2 to its value, to match the arrays above.
- if(newWound > 1) newWound += 2;
- // if its 0, routine ends, otherwise reduce value by 1, again to match above arrays.
- if(newWound < 1) {
- setAttrs({woundlvl: 0},{silent:true});
- return;
- }
- newWound -= 1;
- // ==== ASSIGN WOUND
- // need to check whether rolled wound is currently 1, and if not, check upwards till a 0 is found
- let woundAssigned = -1;
- for(let i = newWound; i < woundnames.length; i++) {
- if(wounds[i] === 0) {
- wounds[i] = 1;
- woundAssigned = i;
- break;
- }
- }
- // this marks one wound, then exits the loop
- // if woundAssigned is still -1, no wound was assigned - every wound slot is filled.
- // ========= PREPARE OUTPUT
- const settings = {};
- // resetthe wound box to 0.
- settings.woundlvl = 0;
- //calculate wound penalty
- settings['hurtlvl'] = woundPenalty(wounds);
- // update the wound boxes on the character sheet
- settings[woundAssigned < 0 ? "unconscious" : woundnames[woundAssigned]] = 1;
- //update the character sheet
- setAttrs(settings, {silent:true});
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement