Small cleanup

- extrait de méthode pour tirage dans un array
- utilisation des operateur ?: / ??
This commit is contained in:
Vincent Vandemeulebrouck
2021-02-12 01:11:03 +01:00
parent 9f2e17537d
commit 1cb4a7dbf5
7 changed files with 70 additions and 78 deletions

View File

@ -1,6 +1,4 @@
import { DeDraconique } from "./de-draconique.js";
import { TMRRencontres } from "./tmr-rencontres.js";
import { Grammar } from "./grammar.js";
import { Misc } from "./misc.js";
/* -------------------------------------------- */
@ -270,6 +268,7 @@ export const tmrColors = {
sort: 0xFF8800,
tetes: 0xA000FF,
souffle: 0x804040,
queues: 0xAA4040,
trounoir: 0x401060,
demireve: 0x00FFEE,
rencontre: 0xFF0000,
@ -283,7 +282,9 @@ export const tmrColors = {
export class TMRUtility {
static init() {
for (let coord in TMRMapping) {
TMRMapping[coord].coord = coord;
const tmr = TMRMapping[coord];
tmr.coord = coord;
tmr.genre = TMRType[tmr.type].genre;
}
let tmrByType = Misc.classify(Object.values(TMRMapping));
for (const [type, list] of Object.entries(tmrByType)) {
@ -317,9 +318,14 @@ export class TMRUtility {
}
/* -------------------------------------------- */
static getTMR(coordTMR) {
return TMRMapping[coordTMR];
static getTMR(coord) {
return TMRMapping[coord];
}
static getTMRLabel(coord) {
return TMRMapping[coord]?.label ?? (coord+": case inconnue");
}
static isCaseHumide(tmr) {
return tmr.type == 'fleuve' || tmr.type == 'lac' || tmr.type == 'marais';
}
@ -355,22 +361,21 @@ export class TMRUtility {
/* -------------------------------------------- */
static getDirectionPattern() {
let roll = new Roll("1d" + tmrRandomMovePatten.length).evaluate().total;
return tmrRandomMovePatten[roll - 1];
return Misc.rollOneOf(tmrRandomMovePatten);
}
static deplaceTMRAleatoire(coord) {
return TMRUtility.deplaceTMRSelonPattern(coord, TMRUtility.getDirectionPattern(), 1);
static deplaceTMRAleatoire(actor, coord) {
return TMRUtility.deplaceTMRSelonPattern(actor, coord, TMRUtility.getDirectionPattern(), 1);
}
/* -------------------------------------------- */
static deplaceTMRSelonPattern(coord, direction, nTime) {
static async deplaceTMRSelonPattern(actor, coord, direction, nTime) {
for (let i = 0; i < nTime; i++) {
let currentPosXY = TMRUtility.convertToCellPos(coord);
currentPosXY.x = currentPosXY.x + direction.x;
currentPosXY.y = currentPosXY.y + direction.y;
if (this._checkTMRCoord(currentPosXY.x, currentPosXY.y)) { // Sortie de carte ! Ré-insertion aléatoire
coord = TMRUtility.convertToTMRCoord(currentPosXY);
let currentPos = TMRUtility.convertToCellPos(coord);
currentPos.x = currentPos.x + direction.x;
currentPos.y = currentPos.y + direction.y;
if (this._checkTMRCoord(currentPos.x, currentPos.y)) { // Sortie de carte ! Ré-insertion aléatoire
coord = TMRUtility.getTMR(TMRUtility.convertToTMRCoord(currentPos));
} else {
coord = this.getTMRAleatoire().coord;
}
@ -392,10 +397,8 @@ export class TMRUtility {
return TMRUtility.filterTMR(filter).map(it => it.coord);
}
static getTMRAleatoire(filter = undefined) {
let list = TMRUtility.filterTMR(filter);
let index = new Roll("1d" + list.length).evaluate().total - 1;
return list[index];
static getTMRAleatoire(filter = it => true) {
return Misc.rollOneOf(TMRUtility.filterTMR(filter))
}
/* -------------------------------------------- */
@ -433,21 +436,17 @@ export class TMRUtility {
/** Returns a list of case inside a given distance
*
*/
static getTMRPortee(centerCoord, portee) {
return TMRUtility.getTMRArea(centerCoord, portee, tmrConstants);
}
static getTMRArea(centerCoord, distance, tmrConstants) {
let centerPos = this.convertToCellPos(centerCoord);
static getTMRPortee(coord, portee) {
let centerPos = this.convertToCellPos(coord);
let posPic = this.computeRealPictureCoordinates(centerPos, tmrConstants);
let caseList = [];
for (let dx = -distance; dx <= distance; dx++) { // Loop thru lines
for (let dy = -distance; dy <= distance; dy++) { // Loop thru lines
for (let dx = -portee; dx <= portee; dx++) { // Loop thru lines
for (let dy = -portee; dy <= portee; dy++) { // Loop thru lines
const currentPos = { x: centerPos.x + dx, y: centerPos.y + dy };
if (this._checkTMRCoord(currentPos.x, currentPos.y)) { // Coordinate is valie
let posPicNow = this.computeRealPictureCoordinates(currentPos, tmrConstants);
let dist = Math.sqrt(Math.pow(posPicNow.x - posPic.x, 2) + Math.pow(posPicNow.y - posPic.y, 2)) / tmrConstants.cellw;
if (dist < distance + 0.5) {
if (dist < portee + 0.5) {
caseList.push(this.convertToTMRCoord(currentPos)); // Inside the area
}
}