2020-07-05 21:45:25 +02:00
/ * *
* Extend the base Dialog entity by defining a custom window to perform spell .
* @ extends { Dialog }
* /
2020-07-06 09:03:21 +02:00
import { RdDUtility } from "./rdd-utility.js" ;
2020-07-17 22:04:35 +02:00
import { TMRUtility } from "./tmr-utility.js" ;
2020-07-05 21:45:25 +02:00
export class RdDTMRDialog extends Dialog {
/* -------------------------------------------- */
2020-07-25 10:29:28 +02:00
constructor ( html , actor , tmrData ) {
2020-07-05 21:45:25 +02:00
// Common conf
let dialogConf = {
content : html ,
buttons :
{
2020-07-22 00:12:13 +02:00
closeButton :
2020-07-05 21:45:25 +02:00
{
2020-07-14 22:19:29 +02:00
label : "Fermer" ,
2020-07-22 00:12:13 +02:00
callback : html => this . close ( html )
2020-07-05 21:45:25 +02:00
}
} ,
2020-07-22 00:12:13 +02:00
default : "closeButton"
2020-07-05 21:45:25 +02:00
}
let dialogOptions = { classes : [ "tmrdialog" ] }
2020-07-17 22:04:35 +02:00
2020-07-05 21:45:25 +02:00
dialogConf . title = "Terres Médianes de Rêve" ,
2020-07-14 22:19:29 +02:00
dialogOptions . width = 920 ;
2020-07-05 22:35:18 +02:00
dialogOptions . height = 960 ;
2020-07-14 22:19:29 +02:00
2020-07-05 21:45:25 +02:00
super ( dialogConf , dialogOptions ) ;
2020-07-25 10:29:28 +02:00
this . tmrdata = duplicate ( tmrData ) ;
2020-07-06 09:03:21 +02:00
this . col1 _y = 30 ;
this . col2 _y = 55 ;
this . cellh = 55 ;
this . cellw = 55 ;
2020-07-14 22:19:29 +02:00
this . actor = actor ;
2020-07-25 10:29:28 +02:00
this . nbFatigue = 1 ; // 1 premier point de fatigue du à la montée
2020-07-21 23:51:24 +02:00
this . rencontresExistantes = duplicate ( this . actor . data . data . reve . rencontre . list ) ;
//console.log(this.rencontresExistantes);
2020-07-14 22:19:29 +02:00
this . pixiApp = new PIXI . Application ( { width : 720 , height : 860 } ) ;
2020-07-17 22:04:35 +02:00
2020-07-14 22:19:29 +02:00
}
2020-07-21 23:51:24 +02:00
2020-07-25 10:29:28 +02:00
/* -------------------------------------------- */
close ( ) {
this . actor . santeIncDec ( "fatigue" , this . nbFatigue ) . then ( super . close ( ) ) ; // moving 1 cell costs 1 fatigue
}
2020-07-21 23:51:24 +02:00
/* -------------------------------------------- */
displayPreviousRencontres ( ) {
for ( let rencontre of this . rencontresExistantes ) {
rencontre . circle = new PIXI . Graphics ( ) ;
rencontre . circle . beginFill ( 0x101010 , 0.8 ) ;
rencontre . circle . drawCircle ( 0 , 0 , 6 ) ;
rencontre . circle . endFill ( ) ;
let coordXY = TMRUtility . convertToCellCoord ( rencontre . coord ) ;
let basey = ( coordXY . x % 2 == 0 ) ? this . col1 _y : this . col2 _y ;
let myx = 28 + ( coordXY . x * this . cellw ) ;
let myy = basey + 28 + ( coordXY . y * this . cellh ) ;
rencontre . circle . x = myx - ( this . cellw / 2 ) + 16 ;
rencontre . circle . y = myy - ( this . cellh / 2 ) + 16 ;
this . pixiApp . stage . addChild ( rencontre . circle ) ;
}
}
/* -------------------------------------------- */
updatePreviousRencontres ( ) {
for ( let rencontre of this . rencontresExistantes ) {
this . pixiApp . stage . removeChild ( rencontre . circle ) ;
}
this . rencontresExistantes = duplicate ( this . actor . data . data . reve . rencontre . list ) ;
this . displayPreviousRencontres ( ) ;
}
2020-07-14 22:19:29 +02:00
/* -------------------------------------------- */
2020-07-17 22:04:35 +02:00
async derober ( ) {
2020-07-21 23:51:24 +02:00
await this . actor . addTMRRencontre ( this . currentRencontre ) ;
2020-07-17 22:04:35 +02:00
console . log ( "-> derober" , this . currentRencontre ) ;
2020-07-21 23:51:24 +02:00
ChatMessage . create ( { title : "TMR" , content : game . user . name + " s'est dérobé et quitte les TMR." , user : game . user . _id , whisper : ChatMessage . getWhisperRecipients ( "GM" ) } ) ;
this . close ( ) ;
2020-07-14 22:19:29 +02:00
}
/* -------------------------------------------- */
async refouler ( data ) {
2020-07-21 23:51:24 +02:00
ChatMessage . create ( { title : "TMR" , content : game . user . name + " a refoulé une rencontre." , user : game . user . _id , whisper : ChatMessage . getWhisperRecipients ( "GM" ) } ) ;
await this . actor . deleteTMRRencontreAtPosition ( ) ; // Remove the stored rencontre if necessary
2020-07-17 22:04:35 +02:00
let result = await this . actor . ajouterRefoulement ( 1 ) ;
2020-07-21 23:51:24 +02:00
this . updatePreviousRencontres ( ) ;
2020-07-17 22:04:35 +02:00
if ( result == "souffle" ) {
let souffle = TMRUtility . getSouffle ( ) ;
}
2020-07-14 22:19:29 +02:00
await this . maximize ( ) ;
console . log ( "-> refouler" , this . currentRencontre )
2020-07-17 22:04:35 +02:00
this . updateValuesDisplay ( ) ;
2020-07-14 22:19:29 +02:00
}
/* -------------------------------------------- */
async matriser ( data ) {
2020-07-21 23:51:24 +02:00
this . actor . deleteTMRRencontreAtPosition ( ) ; // Remove the stored rencontre if necessary
this . updatePreviousRencontres ( ) ;
2020-07-14 22:19:29 +02:00
await this . maximize ( ) ;
2020-07-17 22:04:35 +02:00
let draconic = this . actor . getBestDraconic ( ) ;
let carac = this . actor . getCurrentReve ( ) ;
let level = draconic . data . niveau - this . currentRencontre . force ;
2020-07-21 23:51:24 +02:00
console . log ( "Maitriser" , carac , draconic . data . niveau , this . currentRencontre . force ) ;
2020-07-17 22:04:35 +02:00
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 ) ;
2020-07-21 23:51:24 +02:00
ChatMessage . create ( { title : "TMR" , content : game . user . name + " a perdu sa rencontre." , user : game . user . _id , whisper : ChatMessage . getWhisperRecipients ( "GM" ) } ) ;
this . close ( ) ;
2020-07-17 22:04:35 +02:00
} else {
TMRUtility . processRencontreReussite ( this . actor , this . rencontre ) ;
2020-07-21 23:51:24 +02:00
ChatMessage . create ( { title : "TMR" , content : game . user . name + " a gagné sa rencontre." , user : game . user . _id , whisper : ChatMessage . getWhisperRecipients ( "GM" ) } ) ;
2020-07-17 22:04:35 +02:00
}
console . log ( "-> matriser" , this . currentRencontre ) ;
this . updateValuesDisplay ( ) ;
2020-07-14 22:19:29 +02:00
}
/* -------------------------------------------- */
async manageRencontre ( coordTMR , cellDescr )
{
// Roll until diffent than '8'
let rencontre
2020-07-21 23:51:24 +02:00
this . currentRencontre = undefined ;
for ( let previousRencontre of this . rencontresExistantes ) {
if ( previousRencontre . coord == coordTMR )
rencontre = previousRencontre . rencontre ;
}
if ( rencontre == undefined ) {
let val = 8 ;
while ( val == 8 ) {
let myroll = new Roll ( "d7" ) ;
myroll . roll ( ) ;
val = myroll . total ;
if ( val == 7 ) {
rencontre = TMRUtility . rencontreTMRRoll ( coordTMR , cellDescr ) ;
rencontre . force = new Roll ( rencontre . data . force ) . roll ( ) . total ;
}
2020-07-14 22:19:29 +02:00
}
}
if ( rencontre ) { // Manages it
2020-07-21 23:51:24 +02:00
this . currentRencontre = duplicate ( rencontre ) ;
2020-07-14 22:19:29 +02:00
await this . minimize ( ) ;
let diag = new Dialog ( { title : "Rencontre en TMR!" ,
2020-07-21 23:51:24 +02:00
content : "Vous recontrez un " + rencontre . name + " de force " + rencontre . force + "<br>" ,
2020-07-14 22:19:29 +02:00
buttons : {
derober : {
icon : '<i class="fas fa-check"></i>' ,
label : "Se dérober" ,
callback : ( ) => this . derober ( )
} ,
refouler : {
icon : '<i class="fas fa-check"></i>' ,
label : "Refouler" ,
callback : ( ) => this . refouler ( )
} ,
maitiser : {
icon : '<i class="fas fa-check"></i>' ,
label : "Maîtriser" ,
callback : ( ) => this . matriser ( )
}
}
} ) ;
diag . render ( true ) ;
}
}
2020-07-05 21:45:25 +02:00
/* -------------------------------------------- */
performRoll ( html ) {
this . actor . performRoll ( this . rollData ) ;
}
2020-07-06 09:03:21 +02:00
2020-07-17 22:04:35 +02:00
/* -------------------------------------------- */
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 = "<table class='table-fatigue'>" + RdDUtility . makeHTMLfatigueMatrix ( this . actor . data . data . sante . fatigue . value , this . actor . data . data . sante . endurance . max ) . html ( ) + "</table>" ;
}
/* -------------------------------------------- */
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 ] ;
2020-07-25 10:29:28 +02:00
let result = new Roll ( "d100" ) . roll ( ) . total ;
2020-07-17 22:04:35 +02:00
let content = "" ;
let mycallback ;
console . log ( ">>>>" , scoreDef ) ;
2020-07-25 10:29:28 +02:00
if ( result > scoreDef . score ) {
content = "Vous êtes entré sur une case humide, et vous avez <strong>raté</strong> votre maîtrise ! Vous <strong>quittez les Terres Médianes</strong> ! (" + draconic . name + ") :" + carac + " / " + level + " -> " + result + " / " + scoreDef . score ;
if ( result >= scoreDef . etotal ) {
let souffle = TMRUtility . getSouffle ( true ) ;
content += "<br>Vous avez fait un Echec Total. Vous subissez un Souffle de Dragon : " + souffle . name ;
this . actor . createOwnedItem ( souffle ) ;
}
2020-07-17 22:04:35 +02:00
this . toclose = true ;
} else {
2020-07-25 10:29:28 +02:00
content = "Vous êtes entré sur une case humide, et vous avez <strong>réussi</strong> votre maîtrise ! (" + draconic . name + ") :" + carac + " / " + level + " -> " + result + " / " + scoreDef . score ;
if ( result <= scoreDef . part ) {
content += "<br>Vous avez fait une Réussite Particulière" ;
if ( level < 0 ) {
let xpcarac = Math . floor ( Math . abs ( level ) / 2 ) ;
let xpcomp = ( Math . abs ( level ) % 2 == 1 ) ? xpcarac + 1 : xpcarac ;
content += "<br>Points d'expérience gagné ! " + xpcarac + " - " + xpcomp ;
}
}
2020-07-17 22:04:35 +02:00
}
let humideDiag = new Dialog ( { title : "Case humide" ,
content : content ,
buttons : {
choice : { icon : '<i class="fas fa-check"></i>' ,
label : "Fermer" ,
callback : ( ) => this . manageCaseHumideResult ( )
}
}
}
) ;
await this . minimize ( ) ;
humideDiag . render ( true ) ;
}
}
2020-07-05 21:45:25 +02:00
/* -------------------------------------------- */
2020-07-14 22:19:29 +02:00
updateSprites ( myself )
{
2020-07-06 09:03:21 +02:00
let coordTMR = myself . actor . data . data . reve . tmrpos . coord ;
2020-07-17 22:04:35 +02:00
let coordXY = TMRUtility . convertToCellCoord ( coordTMR ) ;
2020-07-14 22:19:29 +02:00
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 ) ;
2020-07-06 09:03:21 +02:00
2020-07-14 22:19:29 +02:00
myself . circle . x = myx ;
2020-07-17 22:04:35 +02:00
myself . circle . y = myy ;
2020-07-05 22:35:18 +02:00
}
2020-07-14 22:19:29 +02:00
2020-07-05 22:35:18 +02:00
/* -------------------------------------------- */
2020-07-14 22:19:29 +02:00
async getCursorPosition ( event ) {
let origEvent = event . data . originalEvent ;
let myself = event . target . tmrObject ;
2020-07-06 09:03:21 +02:00
2020-07-14 22:19:29 +02:00
//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]
2020-07-06 09:03:21 +02:00
if ( cellx % 2 == 0 )
2020-07-14 22:19:29 +02:00
y -= myself . col1 _y ;
2020-07-06 09:03:21 +02:00
else
2020-07-14 22:19:29 +02:00
y -= myself . col2 _y ;
let celly = Math . floor ( y / myself . cellh ) ; // [From 0 -> 14]
2020-07-06 09:03:21 +02:00
2020-07-17 22:04:35 +02:00
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" ) ;
2020-07-14 22:19:29 +02:00
} else {
2020-07-17 22:04:35 +02:00
let coordTMR = TMRUtility . convertToTMRCoord ( cellx , celly ) ;
let cellDescr = TMRUtility . getTMRDescription ( coordTMR ) ;
2020-07-14 22:19:29 +02:00
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 ) ;
2020-07-25 10:29:28 +02:00
myself . nbFatigue += 1 ;
2020-07-17 22:04:35 +02:00
myself . updateValuesDisplay ( ) ;
myself . manageRencontre ( coordTMR , cellDescr ) ;
myself . manageCaseHumide ( cellDescr ) ;
2020-07-14 22:19:29 +02:00
}
2020-07-05 22:35:18 +02:00
}
2020-07-06 09:03:21 +02:00
2020-07-22 00:12:13 +02:00
/* -------------------------------------------- */
2020-07-23 22:09:40 +02:00
lancerSort ( ) {
this . actor . rollUnSort ( this . actor . data . data . reve . tmrpos . coord ) ;
2020-07-22 00:12:13 +02:00
}
2020-07-05 22:35:18 +02:00
/* -------------------------------------------- */
2020-07-06 09:03:21 +02:00
async activateListeners ( html ) {
2020-07-05 21:45:25 +02:00
super . activateListeners ( html ) ;
2020-07-14 22:19:29 +02:00
var row = document . getElementById ( "tmrrow1" ) ;
var cell1 = row . insertCell ( 1 ) ;
cell1 . append ( this . pixiApp . view ) ;
2020-07-22 00:12:13 +02:00
// Roll Sort
html . find ( '#lancer-sort' ) . click ( ( event ) => {
this . lancerSort ( ) ;
} ) ;
2020-07-14 22:19:29 +02:00
// load the texture we need
2020-07-21 23:51:24 +02:00
await this . pixiApp . loader . add ( 'tmr' , 'systems/foundryvtt-reve-de-dragon/styles/ui/tmp_main_r1.webp' ) . load ( ( loader , resources ) => {
2020-07-14 22:19:29 +02:00
// 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 ) ;
2020-07-21 23:51:24 +02:00
2020-07-14 22:19:29 +02:00
// Add the bunny to the scene we are building
this . pixiApp . stage . addChild ( mytmr ) ;
this . pixiApp . stage . addChild ( this . circle ) ;
2020-07-21 23:51:24 +02:00
this . displayPreviousRencontres ( ) ;
2020-07-06 09:03:21 +02:00
} ) ;
2020-07-17 22:04:35 +02:00
2020-07-25 10:29:28 +02:00
await this . actor . updatePointsDeReve ( ( this . tmrdata . isRapide ) ? - 2 : - 1 ) ; // 1 point defatigue
2020-07-17 22:04:35 +02:00
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 ) ;
2020-07-05 21:45:25 +02:00
}
}