/** * Extend the base Dialog entity by defining a custom window to perform spell. * @extends {Dialog} */ import { RdDUtility } from "./rdd-utility.js"; import { TMRUtility } from "./tmr-utility.js"; export class RdDTMRDialog extends Dialog { /* -------------------------------------------- */ constructor(sort, html, actor) { // Common conf let dialogConf = { content: html, buttons: { rollButton: { label: "Fermer", callback: html => this.performRoll(html) } }, default: "rollButton" } let dialogOptions = { classes: [ "tmrdialog"] } dialogConf.title = "Terres Médianes de Rêve", dialogOptions.width = 920; dialogOptions.height = 960; super(dialogConf, dialogOptions); this.col1_y = 30; this.col2_y = 55; this.cellh = 55; this.cellw = 55; this.actor = actor; this.sort = sort; this.pixiApp = new PIXI.Application( {width: 720, height: 860 } ); } /* -------------------------------------------- */ async derober() { await this.maximize(); let rencontre = duplicate(this.actor.data.data.reve.rencontre); rencontre.list.push( {coord: this.actor.data.data.reve.tmrpos.coord, rencontre: this.currentRencontre} ); console.log("-> derober", this.currentRencontre); } /* -------------------------------------------- */ async refouler(data) { let result = await this.actor.ajouterRefoulement(1); if (result == "souffle") { let souffle = TMRUtility.getSouffle(); } await this.maximize(); console.log("-> refouler", this.currentRencontre) this.updateValuesDisplay(); } /* -------------------------------------------- */ async matriser(data) { await this.maximize(); let draconic = this.actor.getBestDraconic(); let carac = this.actor.getCurrentReve(); let level = draconic.data.niveau - this.currentRencontre.force; let scoreDef = CONFIG.RDD.resolutionTable[carac][level+10]; let myroll = new Roll("d100"); myroll.roll(); if ( myroll.total > scoreDef.score ) { TMRUtility.processRencontreEchec( this.actor, this.rencontre); this.close(); } else { TMRUtility.processRencontreReussite( this.actor, this.rencontre); } console.log("-> matriser", this.currentRencontre); this.updateValuesDisplay(); } /* -------------------------------------------- */ async manageRencontre(coordTMR, cellDescr) { // Roll until diffent than '8' this.currentRencontre = undefined; let rencontre let val = 8; while (val == 8) { let myroll = new Roll("d7"); myroll.roll(); val = myroll.total; if ( val == 7 ) { rencontre = TMRUtility.rencontreTMRRoll(coordTMR, cellDescr); } } if (rencontre) { // Manages it let myroll = new Roll(rencontre.data.force); myroll.roll(); rencontre.force = myroll.total; this.currentRencontre = rencontre; await this.minimize(); let diag = new Dialog( { title: "Rencontre en TMR!", content: "Vous recontrez un " + rencontre.name + " de force " + myroll.total + "
", buttons: { derober: { icon: '', label: "Se dérober", callback: () => this.derober() }, refouler: { icon: '', label: "Refouler", callback: () => this.refouler() }, maitiser: { icon: '', label: "Maîtriser", callback: () => this.matriser() } } } ); diag.render(true); } } /* -------------------------------------------- */ performRoll (html) { this.actor.performRoll( this.rollData ); } /* -------------------------------------------- */ updateValuesDisplay() { let ptsreve = document.getElementById("pointsreve-value"); ptsreve.innerHTML = this.actor.data.data.reve.reve.value; let tmrpos = document.getElementById("tmr-pos"); let tmr = TMRUtility.getTMRDescription( this.actor.data.data.reve.tmrpos.coord ); tmrpos.innerHTML = this.actor.data.data.reve.tmrpos.coord + " (" + tmr.label + ")"; let etat = document.getElementById("etatgeneral-value"); etat.innerHTML = this.actor.data.data.compteurs.etat.value; let refoulement = document.getElementById("refoulement-value"); refoulement.innerHTML = this.actor.data.data.reve.refoulement.value; let fatigueItem = document.getElementById("fatigue-table"); fatigueItem.innerHTML = "" + RdDUtility.makeHTMLfatigueMatrix( this.actor.data.data.sante.fatigue.value, this.actor.data.data.sante.endurance.max ).html() + "
"; } /* -------------------------------------------- */ manageCaseHumideResult() { if ( this.toclose ) this.close(); else this.maximize(); } /* -------------------------------------------- */ async manageCaseHumide( cellDescr ) { if ( cellDescr.type == "lac" || cellDescr.type == "fleuve" || cellDescr.type == "marais" ) { let draconic = this.actor.getBestDraconic(); let carac = this.actor.getCurrentReve(); let level = draconic.data.niveau - 7; let scoreDef = CONFIG.RDD.resolutionTable[carac][level+10]; let myroll = new Roll("d100"); myroll.roll(); let content = ""; let mycallback; console.log(">>>>", scoreDef); if ( myroll.total > scoreDef.score ) { content = "Vous êtes entré sur une case humide, et vous avez raté votre maîtrise ! Vous quittez les Terres Médianes ! ("+ draconic.name +") :" + carac + " / " + level + " -> " + myroll.total + " / " + scoreDef.score; this.toclose = true; } else { content = "Vous êtes entré sur une case humide, et vous avez réussi votre maîtrise ! ("+ draconic.name +") :" + carac + " / " + level + " -> " + myroll.total + " / " + scoreDef.score; } let humideDiag = new Dialog( {title: "Case humide", content: content, buttons: { choice: { icon: '', label: "Fermer", callback: () => this.manageCaseHumideResult() } } } ); await this.minimize(); humideDiag.render(true); } } /* -------------------------------------------- */ updateSprites( myself ) { let coordTMR = myself.actor.data.data.reve.tmrpos.coord; let coordXY = TMRUtility.convertToCellCoord( coordTMR ); let basey = (coordXY.x % 2 == 0) ? myself.col1_y : myself.col2_y; let myx = 28+(coordXY.x * myself.cellw); let myy = basey+28+(coordXY.y * myself.cellh); myself.circle.x = myx; myself.circle.y = myy; } /* -------------------------------------------- */ async getCursorPosition(event) { let origEvent = event.data.originalEvent; let myself = event.target.tmrObject; //console.log("EVENT:", event); let canvasRect = origEvent.target.getBoundingClientRect(); let x = origEvent.clientX - canvasRect.left; let y = origEvent.clientY - canvasRect.top; let cellx = Math.floor( x / myself.cellw);// [From 0 -> 12] if (cellx % 2 == 0) y -= myself.col1_y; else y -= myself.col2_y; let celly = Math.floor( y / myself.cellh);// [From 0 -> 14] console.log(">>>>", cellx, celly ); let currentPos = TMRUtility.convertToCellCoord(myself.actor.data.data.reve.tmrpos.coord); if ( (Math.abs(cellx - currentPos.x) > 1 || Math.abs(celly - currentPos.y) > 1) || (currentPos.y == 0 && celly > currentPos.y && cellx != currentPos.x && currentPos.x % 2 == 0) || (celly == 0 && celly < currentPos.y && cellx != currentPos.x && currentPos.x % 2 == 1) ) { ui.notifications.error( "Vous ne pouvez vous déplacer que sur des cases adjacentes à votre position, et pas en diagonale" ); } else { let coordTMR = TMRUtility.convertToTMRCoord(cellx, celly); let cellDescr = TMRUtility.getTMRDescription( coordTMR ); console.log("TMR column is", coordTMR, cellx, celly, cellDescr, this); let tmrPos = duplicate(myself.actor.data.data.reve.tmrpos); tmrPos.coord = coordTMR; await myself.actor.update( { "data.reve.tmrpos": tmrPos } ); myself.updateSprites(myself); myself.actor.santeIncDec("fatigue", 1); // moving 1 cell costs 1 fatigue myself.updateValuesDisplay(); myself.manageRencontre(coordTMR, cellDescr); myself.manageCaseHumide( cellDescr ); } } /* -------------------------------------------- */ async activateListeners(html) { super.activateListeners(html); var row = document.getElementById("tmrrow1"); var cell1 = row.insertCell(1); cell1.append( this.pixiApp.view ); // load the texture we need this.pixiApp.loader.add('tmr', 'systems/foundryvtt-reve-de-dragon/styles/ui/tmp_main_r1.webp').load((loader, resources) => { // This creates a texture from a 'bunny.png' image const mytmr = new PIXI.Sprite(resources.tmr.texture); // Setup the position of the bunny mytmr.x = 0; mytmr.y = 0; mytmr.width = 720; mytmr.height = 860; // Rotate around the center mytmr.anchor.x = 0; mytmr.anchor.y = 0; mytmr.interactive = true; mytmr.buttonMode = true; mytmr.tmrObject = this; mytmr.on('pointerdown', this.getCursorPosition); this.circle = new PIXI.Graphics(); this.circle.beginFill(0x9966FF, 0.6); this.circle.drawCircle(0, 0, 15); this.circle.endFill(); this.updateSprites( this ); // Add the bunny to the scene we are building this.pixiApp.stage.addChild(mytmr); this.pixiApp.stage.addChild(this.circle); //this.updateSprites(); } ); await this.actor.santeIncDec("fatigue", 1); // 1 point defatigue await this.actor.updatePointsDeReve(-1); // 1 point defatigue this.updateValuesDisplay(); let cellDescr = TMRUtility.getTMRDescription(this.actor.data.data.reve.tmrpos.coord); this.manageRencontre( this.actor.data.data.reve.tmrpos.coord,cellDescr ); this.manageCaseHumide( cellDescr ); } }