Refactoring and merge

This commit is contained in:
Pax1601 2023-08-28 17:14:03 +02:00
parent 70783dc828
commit 9547559e00

View File

@ -76,7 +76,7 @@ export class Unit extends CustomMarker {
};
#ammo: Ammo[] = [];
#contacts: Contact[] = [];
#activePath: LatLng[] = [];
#activePath: LatLng[] = [];
#isLeader: boolean = false;
#selectable: boolean;
@ -94,43 +94,43 @@ export class Unit extends CustomMarker {
#hotgroup: number | null = null;
#detectionMethods: number[] = [];
getAlive() {return this.#alive};
getHuman() {return this.#human};
getControlled() {return this.#controlled};
getCoalition() {return this.#coalition};
getCountry() {return this.#country};
getName() {return this.#name};
getUnitName() {return this.#unitName};
getGroupName() {return this.#groupName};
getState() {return this.#state};
getTask() {return this.#task};
getHasTask() {return this.#hasTask};
getPosition() {return this.#position};
getSpeed() {return this.#speed};
getHeading() {return this.#heading};
getIsTanker() {return this.#isTanker};
getIsAWACS() {return this.#isAWACS};
getOnOff() {return this.#onOff};
getFollowRoads() {return this.#followRoads};
getFuel() {return this.#fuel};
getDesiredSpeed() {return this.#desiredSpeed};
getDesiredSpeedType() {return this.#desiredSpeedType};
getDesiredAltitude() {return this.#desiredAltitude};
getDesiredAltitudeType() {return this.#desiredAltitudeType};
getLeaderID() {return this.#leaderID};
getFormationOffset() {return this.#formationOffset};
getTargetID() {return this.#targetID};
getTargetPosition() {return this.#targetPosition};
getROE() {return this.#ROE};
getReactionToThreat() {return this.#reactionToThreat};
getEmissionsCountermeasures() {return this.#emissionsCountermeasures};
getTACAN() {return this.#TACAN};
getRadio() {return this.#radio};
getGeneralSettings() {return this.#generalSettings};
getAmmo() {return this.#ammo};
getContacts() {return this.#contacts};
getActivePath() {return this.#activePath};
getIsLeader() {return this.#isLeader};
getAlive() { return this.#alive };
getHuman() { return this.#human };
getControlled() { return this.#controlled };
getCoalition() { return this.#coalition };
getCountry() { return this.#country };
getName() { return this.#name };
getUnitName() { return this.#unitName };
getGroupName() { return this.#groupName };
getState() { return this.#state };
getTask() { return this.#task };
getHasTask() { return this.#hasTask };
getPosition() { return this.#position };
getSpeed() { return this.#speed };
getHeading() { return this.#heading };
getIsTanker() { return this.#isTanker };
getIsAWACS() { return this.#isAWACS };
getOnOff() { return this.#onOff };
getFollowRoads() { return this.#followRoads };
getFuel() { return this.#fuel };
getDesiredSpeed() { return this.#desiredSpeed };
getDesiredSpeedType() { return this.#desiredSpeedType };
getDesiredAltitude() { return this.#desiredAltitude };
getDesiredAltitudeType() { return this.#desiredAltitudeType };
getLeaderID() { return this.#leaderID };
getFormationOffset() { return this.#formationOffset };
getTargetID() { return this.#targetID };
getTargetPosition() { return this.#targetPosition };
getROE() { return this.#ROE };
getReactionToThreat() { return this.#reactionToThreat };
getEmissionsCountermeasures() { return this.#emissionsCountermeasures };
getTACAN() { return this.#TACAN };
getRadio() { return this.#radio };
getGeneralSettings() { return this.#generalSettings };
getAmmo() { return this.#ammo };
getContacts() { return this.#contacts };
getActivePath() { return this.#activePath };
getIsLeader() { return this.#isLeader };
static getConstructor(type: string) {
if (type === "GroundUnit") return GroundUnit;
@ -156,7 +156,7 @@ export class Unit extends CustomMarker {
this.on('contextmenu', (e) => this.#onContextMenu(e));
this.on('mouseover', () => { if (this.belongsToCommandedCoalition()) this.setHighlighted(true); })
this.on('mouseout', () => { this.setHighlighted(false); })
getMap().on("zoomend", () => {this.#onZoom();})
getMap().on("zoomend", () => { this.#onZoom(); })
/* Deselect units if they are hidden */
document.addEventListener("toggleCoalitionVisibility", (ev: CustomEventInit) => {
@ -165,13 +165,13 @@ export class Unit extends CustomMarker {
document.addEventListener("toggleUnitVisibility", (ev: CustomEventInit) => {
window.setTimeout(() => { this.setSelected(this.getSelected() && !this.getHidden()) }, 300);
});
});
document.addEventListener("mapVisibilityOptionsChanged", (ev: CustomEventInit) => {
this.#updateMarker();
if (this.getSelected())
this.drawLines();
});
});
}
getCategory() {
@ -222,12 +222,12 @@ export class Unit extends CustomMarker {
case DataIndexes.radio: this.#radio = dataExtractor.extractRadio(); break;
case DataIndexes.generalSettings: this.#generalSettings = dataExtractor.extractGeneralSettings(); break;
case DataIndexes.ammo: this.#ammo = dataExtractor.extractAmmo(); break;
case DataIndexes.contacts: this.#contacts = dataExtractor.extractContacts(); document.dispatchEvent(new CustomEvent("contactsUpdated", {detail: this})); break;
case DataIndexes.contacts: this.#contacts = dataExtractor.extractContacts(); document.dispatchEvent(new CustomEvent("contactsUpdated", { detail: this })); break;
case DataIndexes.activePath: this.#activePath = dataExtractor.extractActivePath(); break;
case DataIndexes.isLeader: this.#isLeader = dataExtractor.extractBool(); break;
}
}
/* Dead units can't be selected */
this.setSelected(this.getSelected() && this.#alive && !this.getHidden())
@ -314,7 +314,7 @@ export class Unit extends CustomMarker {
getLiveryID(): string {
const liveryID = this.getDatabase()?.getByName(this.getName())?.liveryID;
return liveryID? liveryID: "";
return liveryID ? liveryID : "";
}
setAlive(newAlive: boolean) {
@ -327,7 +327,7 @@ export class Unit extends CustomMarker {
/* Only alive units can be selected. Some units are not selectable (weapons) */
if ((this.#alive || !selected) && this.getSelectable() && this.getSelected() != selected && this.belongsToCommandedCoalition()) {
this.#selected = selected;
if (selected) {
document.dispatchEvent(new CustomEvent("unitSelection", { detail: this }));
this.#updateMarker();
@ -346,7 +346,7 @@ export class Unit extends CustomMarker {
else
this.#updateMarker();
}
}
}
@ -390,7 +390,7 @@ export class Unit extends CustomMarker {
belongsToCommandedCoalition() {
if (getMissionHandler().getCommandModeOptions().commandMode !== GAME_MASTER && getMissionHandler().getCommandedCoalition() !== this.#coalition)
return false;
return true;
return true;
}
getType() {
@ -503,12 +503,12 @@ export class Unit extends CustomMarker {
updateVisibility() {
const hiddenUnits = getUnitsManager().getHiddenTypes();
var hidden = ((this.#human && hiddenUnits.includes("human")) ||
(this.#controlled == false && hiddenUnits.includes("dcs")) ||
(hiddenUnits.includes(this.getMarkerCategory())) ||
(hiddenUnits.includes(this.#coalition)) ||
(!this.belongsToCommandedCoalition() && (this.#detectionMethods.length == 0 || (this.#detectionMethods.length == 1 && this.#detectionMethods[0] === RWR))) ||
(getMap().getVisibilityOptions()[HIDE_GROUP_MEMBERS] && !this.#isLeader && this.getCategory() == "GroundUnit" && getMap().getZoom() < 13 && (this.belongsToCommandedCoalition() || (!this.belongsToCommandedCoalition() && this.#detectionMethods.length == 0)))) &&
!(this.getSelected());
(this.#controlled == false && hiddenUnits.includes("dcs")) ||
(hiddenUnits.includes(this.getMarkerCategory())) ||
(hiddenUnits.includes(this.#coalition)) ||
(!this.belongsToCommandedCoalition() && (this.#detectionMethods.length == 0 || (this.#detectionMethods.length == 1 && this.#detectionMethods[0] === RWR))) ||
(getMap().getVisibilityOptions()[HIDE_GROUP_MEMBERS] && !this.#isLeader && this.getCategory() == "GroundUnit" && getMap().getZoom() < 13 && (this.belongsToCommandedCoalition() || (!this.belongsToCommandedCoalition() && this.#detectionMethods.length == 0)))) &&
!(this.getSelected());
this.setHidden(hidden || !this.#alive);
}
@ -518,9 +518,9 @@ export class Unit extends CustomMarker {
/* Add the marker if not present */
if (!getMap().hasLayer(this) && !this.getHidden()) {
if (getMap().isZooming())
this.once("zoomend", () => {this.addTo(getMap())})
else
if (getMap().isZooming())
this.once("zoomend", () => { this.addTo(getMap()) })
else
this.addTo(getMap());
}
@ -563,19 +563,19 @@ export class Unit extends CustomMarker {
return loadouts.some((loadout: LoadoutBlueprint) => {
return (roles as string[]).some((role: string) => { return loadout.roles.includes(role) });
});
} else
} else
return false;
}
isInViewport() {
const mapBounds = getMap().getBounds();
const unitPos = this.getPosition();
const unitPos = this.getPosition();
return ( unitPos.lng > mapBounds.getWest()
&& unitPos.lng < mapBounds.getEast()
&& unitPos.lat > mapBounds.getSouth()
&& unitPos.lat < mapBounds.getNorth() );
return (unitPos.lng > mapBounds.getWest()
&& unitPos.lng < mapBounds.getEast()
&& unitPos.lat > mapBounds.getSouth()
&& unitPos.lat < mapBounds.getNorth());
}
@ -719,51 +719,29 @@ export class Unit extends CustomMarker {
/***********************************************/
#onClick(e: any) {
if (!this.#preventClick) {
console.log( getMap().getState() );
if (getMap().getState() === IDLE || getMap().getState() === MOVE_UNIT || e.originalEvent.ctrlKey) {
if (!e.originalEvent.ctrlKey)
if (!e.originalEvent.ctrlKey)
getUnitsManager().deselectAllUnits();
}
const unitIsSelected = !this.getSelected();
this.setSelected( unitIsSelected );
// Tell everyone a unit (de-)selection has happened, usually a panel or something.
//
const detail = {
"detail": {
"unit": this
}
};
if ( unitIsSelected ) {
document.dispatchEvent( new CustomEvent( "unitSelected", detail ) );
} else {
document.dispatchEvent( new CustomEvent( "unitDeselection", { "detail": this }));
}
this.setSelected(!this.getSelected());
const detail = { "detail": { "unit": this } };
if (this.getSelected())
document.dispatchEvent(new CustomEvent("unitSelected", detail));
else
document.dispatchEvent(new CustomEvent("unitDeselection", { "detail": this }));
}
}
this.#timer = window.setTimeout(() => {
this.#preventClick = false;
}, 200);
this.#timer = window.setTimeout(() => { this.#preventClick = false; }, 200);
}
#onDoubleClick(e: any) {
const unitsManager = getUnitsManager();
Object.values( unitsManager.getUnits() ).forEach( ( unit:Unit ) => {
if ( unit.getAlive() === true && unit.getControlled() && unit.getName() === this.getName() && unit.isInViewport() ) {
unitsManager.selectUnit( unit.ID, false );
}
Object.values(unitsManager.getUnits()).forEach((unit: Unit) => {
if (unit.getAlive() === true && unit.getName() === this.getName() && unit.isInViewport())
unitsManager.selectUnit(unit.ID, false);
});
clearTimeout(this.#timer);
this.#preventClick = true;
}
@ -803,7 +781,7 @@ export class Unit extends CustomMarker {
options["fire-at-area"] = { text: "Fire at area", tooltip: "Fire at a large area" };
}
if (selectedUnitTypes.length === 1 && ["NavyUnit", "GroundUnit"].includes(selectedUnitTypes[0]) && getUnitsManager().getSelectedUnitsVariable((unit: Unit) => {return unit.getCoalition()}) !== undefined)
if (selectedUnitTypes.length === 1 && ["NavyUnit", "GroundUnit"].includes(selectedUnitTypes[0]) && getUnitsManager().getSelectedUnitsVariable((unit: Unit) => { return unit.getCoalition() }) !== undefined)
options["group"] = { text: "Create group", tooltip: "Create a group from the selected units." };
if (Object.keys(options).length > 0) {
@ -832,7 +810,7 @@ export class Unit extends CustomMarker {
getMap().setState(CARPET_BOMBING);
else if (action === "fire-at-area")
getMap().setState(FIRE_AT_AREA);
}
#showFollowOptions(e: any) {
@ -853,7 +831,7 @@ export class Unit extends CustomMarker {
getMap().hideUnitContextMenu();
this.#applyFollowOptions(option);
});
getMap().showUnitContextMenu(e.originalEvent.x, e.originalEvent.y, e.latlng);
}
@ -1173,7 +1151,7 @@ export class GroundUnit extends Unit {
getType() {
var blueprint = groundUnitDatabase.getByName(this.getName());
return blueprint?.type? blueprint.type: "";
return blueprint?.type ? blueprint.type : "";
}
}
@ -1207,6 +1185,6 @@ export class NavyUnit extends Unit {
getType() {
var blueprint = navyUnitDatabase.getByName(this.getName());
return blueprint?.type? blueprint.type: "";
return blueprint?.type ? blueprint.type : "";
}
}