ENH: Better init formatting
							parent
							
								
									30f68727dc
								
							
						
					
					
						commit
						ddeb26bd8a
					
				|  | @ -4,40 +4,58 @@ export class OseCombat { | |||
|     data.combatants = []; | ||||
|     let groups = {}; | ||||
|     combat.data.combatants.forEach((cbt) => { | ||||
|       groups[cbt.flags.ose.group] = {present: true}; | ||||
|       groups[cbt.flags.ose.group] = { present: true }; | ||||
|       data.combatants.push(cbt); | ||||
|     }); | ||||
|      | ||||
| 
 | ||||
|     // Roll init
 | ||||
|     Object.keys(groups).forEach((group) => { | ||||
|         let roll = new Roll("1d6").roll(); | ||||
|         roll.toMessage({flavor: `${CONFIG.OSE.colors[group]} group rolls initiative`}); | ||||
|         groups[group].initiative = roll.total; | ||||
|     }) | ||||
|      | ||||
|       let roll = new Roll("1d6").roll(); | ||||
|       roll.toMessage({ | ||||
|         flavor: `${CONFIG.OSE.colors[group]} group rolls initiative`, | ||||
|       }); | ||||
|       groups[group].initiative = roll.total; | ||||
|     }); | ||||
| 
 | ||||
|     // Set init
 | ||||
|     for (let i = 0; i < data.combatants.length; ++i) { | ||||
|         if (data.combatants[i].actor.data.data.isSlow) { | ||||
|           data.combatants[i].initiative = -789; | ||||
|         } else { | ||||
|           data.combatants[i].initiative = groups[data.combatants[i].flags.ose.group].initiative; | ||||
|         } | ||||
|       if (data.combatants[i].actor.data.data.isSlow) { | ||||
|         data.combatants[i].initiative = -789; | ||||
|       } else { | ||||
|         data.combatants[i].initiative = | ||||
|           groups[data.combatants[i].flags.ose.group].initiative; | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   static individualInitiative(combat, data) { | ||||
|     let ids = []; | ||||
|     combat.data.combatants.forEach(cbt => { | ||||
|     combat.data.combatants.forEach((cbt) => { | ||||
|       ids.push(cbt._id); | ||||
|     }) | ||||
|     }); | ||||
|     combat.rollInitiative(ids); | ||||
|   } | ||||
| 
 | ||||
|   static format(object, html, user) { | ||||
|     html.find(".initiative").each((_, span) => { | ||||
|       span.innerHTML = | ||||
|         span.innerHTML == "-789.00" | ||||
|           ? '<i class="fas fa-weight-hanging"></i>' | ||||
|           : span.innerHTML; | ||||
|     }); | ||||
|     let init = game.settings.get("ose", "individualInit"); | ||||
|     if (init) { | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     html.find('.combat-control[data-control="rollNPC"]').remove(); | ||||
|     html.find('.combat-control[data-control="rollAll"]').remove(); | ||||
|     let trash = html.find('.encounters .combat-control[data-control="endCombat"]'); | ||||
|     $('<a class="combat-control" data-control="reroll"><i class="fas fa-dice"></i></a>').insertBefore(trash); | ||||
|     let trash = html.find( | ||||
|       '.encounters .combat-control[data-control="endCombat"]' | ||||
|     ); | ||||
|     $( | ||||
|       '<a class="combat-control" data-control="reroll"><i class="fas fa-dice"></i></a>' | ||||
|     ).insertBefore(trash); | ||||
| 
 | ||||
|     html.find(".combatant").each((_, ct) => { | ||||
|       // Can't roll individual inits
 | ||||
|  | @ -56,6 +74,31 @@ export class OseCombat { | |||
|     OseCombat.addListeners(html); | ||||
|   } | ||||
| 
 | ||||
|   static updateCombatant(combat, combatant, data) { | ||||
|     let init = game.settings.get("ose", "individualInit"); | ||||
|     // Why do you reroll ?
 | ||||
|     if (combatant.actor.data.data.isSlow) { | ||||
|       data.initiative = -789; | ||||
|       return; | ||||
|     } | ||||
|     if (data.initiative && !init) { | ||||
|       let groupInit = data.initiative; | ||||
|       // Check if there are any members of the group with init
 | ||||
|       combat.combatants.forEach((ct) => { | ||||
|         if ( | ||||
|           ct.initiative && | ||||
|           ct.initiative != "-789.00" && | ||||
|           ct._id != data._id && | ||||
|           ct.flags.ose.group == combatant.flags.ose.group | ||||
|         ) { | ||||
|           groupInit = ct.initiative; | ||||
|           // Set init
 | ||||
|           data.initiative = parseInt(groupInit); | ||||
|         } | ||||
|       }); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   static addListeners(html) { | ||||
|     // Cycle through colors
 | ||||
|     html.find(".combatant-control.flag").click((ev) => { | ||||
|  | @ -74,10 +117,10 @@ export class OseCombat { | |||
|       }); | ||||
|     }); | ||||
| 
 | ||||
|     html.find('.combat-control[data-control="reroll"]').click(ev => { | ||||
|     html.find('.combat-control[data-control="reroll"]').click((ev) => { | ||||
|       let data = {}; | ||||
|       OseCombat.rollInitiative(game.combat, data); | ||||
|       game.combat.update({data: data}); | ||||
|       game.combat.update({ data: data }); | ||||
|     }); | ||||
|   } | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										31
									
								
								src/ose.js
								
								
								
								
							
							
						
						
									
										31
									
								
								src/ose.js
								
								
								
								
							|  | @ -84,7 +84,7 @@ Hooks.on("renderSidebarTab", async (object, html) => { | |||
|     const rendered = await renderTemplate(template); | ||||
|     html.find(".game-system").append(rendered); | ||||
|   } | ||||
| }) | ||||
| }); | ||||
| 
 | ||||
| Hooks.on("preCreateCombatant", (combat, data, options, id) => { | ||||
|   let init = game.settings.get("ose", "individualInit"); | ||||
|  | @ -93,35 +93,12 @@ Hooks.on("preCreateCombatant", (combat, data, options, id) => { | |||
|   } | ||||
| }); | ||||
| 
 | ||||
| Hooks.on("preUpdateCombatant", (combat, combatant, data, diff, id) => { | ||||
|   let init = game.settings.get("ose", "individualInit"); | ||||
|   if (data.initiative && !init) { | ||||
|     let groupInit = data.initiative; | ||||
|     combat.combatants.forEach((ct) => { | ||||
|       if ( | ||||
|         ct.initiative && | ||||
|         ct._id != data._id && | ||||
|         ct.flags.ose.group == combatant.flags.ose.group | ||||
|       ) { | ||||
|         groupInit = ct.initiative; | ||||
|         data.initiative = parseInt(groupInit); | ||||
|       } | ||||
|     }); | ||||
|   } else if (data.initiative && init) { | ||||
|     if (combatant.actor.data.data.isSlow) { | ||||
|       data.initiative = -789; | ||||
|     } | ||||
|   } | ||||
| Hooks.on("preUpdateCombatant", (combat, combatant, data) => { | ||||
|   OseCombat.updateCombatant(combat, combatant, data); | ||||
| }); | ||||
| 
 | ||||
| Hooks.on("renderCombatTracker", (object, html, data) => { | ||||
|   html.find('.initiative').each((_, span) => { | ||||
|     span.innerHTML = span.innerHTML == '-789.00' ? '<i class="fas fa-weight-hanging"></i>' : span.innerHTML; | ||||
|   }) | ||||
|   let init = game.settings.get("ose", "individualInit"); | ||||
|   if (!init) { | ||||
|     OseCombat.format(object, html, data); | ||||
|   } | ||||
|   OseCombat.format(object, html, data); | ||||
| }); | ||||
| 
 | ||||
| Hooks.on("preUpdateCombat", async (combat, data, diff, id) => { | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue