mirror of
https://github.com/Pax1601/DCSOlympus.git
synced 2025-10-29 16:56:34 +00:00
Migration to node.js completed
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1,43 +1,49 @@
|
||||
import { Marker, LatLng, Polyline } from 'leaflet';
|
||||
import { Marker, LatLng, Polyline, Icon } from 'leaflet';
|
||||
import { ConvertDDToDMS } from '../other/utils';
|
||||
import { getMap, getUnitsManager } from '..';
|
||||
import { UnitMarker, MarkerOptions } from './unitmarker';
|
||||
import { addDestination, attackUnit } from '../dcs/dcs';
|
||||
import { getMap, getUnitsManager, getVisibilitySettings } from '..';
|
||||
import { UnitMarker, MarkerOptions, AircraftMarker, HelicopterMarker, GroundUnitMarker, NavyUnitMarker, WeaponMarker } from './unitmarker';
|
||||
import { addDestination, attackUnit, changeAltitude, changeSpeed } from '../dcs/dcs';
|
||||
|
||||
export class Unit
|
||||
{
|
||||
ID : number;
|
||||
leader : boolean;
|
||||
wingman : boolean;
|
||||
wingmen : Unit[];
|
||||
formation : string;
|
||||
name : string;
|
||||
unitName : string;
|
||||
groupName : string;
|
||||
latitude : number;
|
||||
longitude : number;
|
||||
altitude : number;
|
||||
heading : number;
|
||||
coalitionID : number;
|
||||
alive : boolean;
|
||||
speed : number;
|
||||
currentTask : string;
|
||||
type : Object | null;
|
||||
flags : Object | null;
|
||||
activePath : any | null; // TODO: declare inteface
|
||||
missionData : Object | null;
|
||||
var pathIcon = new Icon({
|
||||
iconUrl: 'images/marker-icon.png',
|
||||
shadowUrl: 'images/marker-shadow.png',
|
||||
iconAnchor: [13, 41]
|
||||
});
|
||||
|
||||
#selectable : boolean;
|
||||
#selected : boolean;
|
||||
#preventClick : boolean;
|
||||
#pathMarkers : Marker[];
|
||||
#pathPolyline : Polyline;
|
||||
#targetsPolylines : Polyline[];
|
||||
#marker : UnitMarker;
|
||||
#timer : number;
|
||||
export class Unit {
|
||||
ID: number = -1;
|
||||
leader: boolean = false;
|
||||
wingman: boolean = false;
|
||||
wingmen: Unit[] = [];
|
||||
formation: string = "";
|
||||
name: string = "";
|
||||
unitName: string = "";
|
||||
groupName: string = "";
|
||||
latitude: number = 0;
|
||||
longitude: number = 0;
|
||||
altitude: number = 0;
|
||||
heading: number = 0;
|
||||
speed: number = 0;
|
||||
coalitionID: number = -1;
|
||||
alive: boolean = true;
|
||||
currentTask: string = "";
|
||||
fuel: number = 0;
|
||||
type: any = null;
|
||||
flags: any = null;
|
||||
activePath: any = null;
|
||||
ammo: any = null;
|
||||
targets: any = null;
|
||||
|
||||
static getConstructor(name: string)
|
||||
{
|
||||
#selectable: boolean;
|
||||
#selected: boolean = false;
|
||||
#preventClick: boolean = false;
|
||||
#pathMarkers: Marker[] = [];
|
||||
#pathPolyline: Polyline;
|
||||
#targetsPolylines: Polyline[];
|
||||
#marker: UnitMarker;
|
||||
#timer: number = 0;
|
||||
|
||||
static getConstructor(name: string) {
|
||||
if (name === "GroundUnit") return GroundUnit;
|
||||
if (name === "Aircraft") return Aircraft;
|
||||
if (name === "Helicopter") return Helicopter;
|
||||
@@ -46,73 +52,41 @@ export class Unit
|
||||
if (name === "NavyUnit") return NavyUnit;
|
||||
}
|
||||
|
||||
constructor(ID: number, marker: UnitMarker)
|
||||
{
|
||||
constructor(ID: number, marker: UnitMarker) {
|
||||
this.ID = ID;
|
||||
|
||||
/* Names */
|
||||
this.name = "";
|
||||
this.unitName = "";
|
||||
this.groupName = "";
|
||||
|
||||
/* Position and speed */
|
||||
this.latitude = 0;
|
||||
this.longitude = 0;
|
||||
this.altitude = 0;
|
||||
this.heading = 0;
|
||||
this.speed = 0;
|
||||
|
||||
/* Tasking */
|
||||
this.coalitionID = 0;
|
||||
this.alive = true;
|
||||
this.currentTask = "";
|
||||
|
||||
/* Formation */
|
||||
this.leader = false;
|
||||
this.wingman = false;
|
||||
this.wingmen = [];
|
||||
this.formation = "";
|
||||
|
||||
/* Structures */
|
||||
this.type = null;
|
||||
this.flags = null;
|
||||
this.activePath = null;
|
||||
this.missionData = null;
|
||||
|
||||
this.#selectable = true;
|
||||
this.#timer = 0;
|
||||
|
||||
/* The marker is set by the inherited class */
|
||||
this.#marker = marker;
|
||||
this.#marker.on('click', (e) => this.#onClick(e));
|
||||
this.#marker.on('dblclick', (e) => this.#onDoubleClick(e));
|
||||
|
||||
this.#selected = false;
|
||||
this.#preventClick = false;
|
||||
this.#pathMarkers = [];
|
||||
this.#pathPolyline = new Polyline([], {color: '#2d3e50', weight: 3, opacity: 0.5, smoothFactor: 1});
|
||||
|
||||
this.#pathPolyline = new Polyline([], { color: '#2d3e50', weight: 3, opacity: 0.5, smoothFactor: 1 });
|
||||
this.#pathPolyline.addTo(getMap());
|
||||
this.#targetsPolylines = [];
|
||||
}
|
||||
|
||||
update(response: JSON)
|
||||
{
|
||||
for (let entry in response)
|
||||
{
|
||||
update(response: JSON) {
|
||||
for (let entry in response) {
|
||||
// @ts-ignore
|
||||
this[entry] = response[entry];
|
||||
}
|
||||
|
||||
this.#updateMarker();
|
||||
|
||||
this.#clearTargets();
|
||||
if (this.getSelected())
|
||||
{
|
||||
this.#drawPath();
|
||||
this.#drawTargets();
|
||||
}
|
||||
else
|
||||
this.#clearPath();
|
||||
|
||||
|
||||
/*
|
||||
this.wingmen = [];
|
||||
if (response["wingmenIDs"] != undefined)
|
||||
if (response["wingmenIDs"] != null)
|
||||
{
|
||||
for (let ID of response["wingmenIDs"])
|
||||
{
|
||||
@@ -124,134 +98,114 @@ export class Unit
|
||||
this.missionData = missionData.getUnitData(this.ID)
|
||||
|
||||
this.setSelected(this.getSelected() && this.alive)
|
||||
|
||||
|
||||
|
||||
this.clearTargets();
|
||||
this.missionData = missionData.getUnitData(this.ID);
|
||||
if (this.missionData != undefined)
|
||||
{
|
||||
if (this.getSelected())
|
||||
{
|
||||
this.drawTargets();
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
setSelected(selected: boolean)
|
||||
{
|
||||
setSelected(selected: boolean) {
|
||||
/* Only alive units can be selected. Some units are not selectable (weapons) */
|
||||
if ((this.alive || !selected) && this.#selectable && this.#selected != selected)
|
||||
{
|
||||
if ((this.alive || !selected) && this.#selectable && this.#selected != selected) {
|
||||
this.#selected = selected;
|
||||
this.#marker.setSelected(selected);
|
||||
getUnitsManager().onUnitSelection();
|
||||
}
|
||||
}
|
||||
|
||||
getSelected()
|
||||
{
|
||||
getSelected() {
|
||||
return this.#selected;
|
||||
}
|
||||
|
||||
addDestination(latlng: L.LatLng)
|
||||
{
|
||||
addDestination(latlng: L.LatLng) {
|
||||
var path: any = {};
|
||||
if (this.activePath != undefined)
|
||||
{
|
||||
if (this.activePath != null) {
|
||||
path = this.activePath;
|
||||
path[(Object.keys(path).length + 1).toString()] = latlng;
|
||||
}
|
||||
else
|
||||
{
|
||||
path = {"1": latlng};
|
||||
else {
|
||||
path = { "1": latlng };
|
||||
}
|
||||
addDestination(this.ID, path);
|
||||
}
|
||||
|
||||
clearDestinations()
|
||||
{
|
||||
this.activePath = undefined;
|
||||
clearDestinations() {
|
||||
this.activePath = null;
|
||||
}
|
||||
|
||||
#onClick(e: any)
|
||||
{
|
||||
getHidden() {
|
||||
return false;
|
||||
}
|
||||
|
||||
#onClick(e: any) {
|
||||
this.#timer = setTimeout(() => {
|
||||
if (!this.#preventClick) {
|
||||
if (getMap().getState() === 'IDLE' || getMap().getState() === 'MOVE_UNIT' || e.originalEvent.ctrlKey)
|
||||
{
|
||||
if (!e.originalEvent.ctrlKey)
|
||||
{
|
||||
if (getMap().getState() === 'IDLE' || getMap().getState() === 'MOVE_UNIT' || e.originalEvent.ctrlKey) {
|
||||
if (!e.originalEvent.ctrlKey) {
|
||||
getUnitsManager().deselectAllUnits();
|
||||
}
|
||||
this.setSelected(true);
|
||||
}
|
||||
}
|
||||
this.#preventClick = false;
|
||||
}, 200);
|
||||
}, 200);
|
||||
}
|
||||
|
||||
#onDoubleClick(e: any)
|
||||
{
|
||||
#onDoubleClick(e: any) {
|
||||
clearTimeout(this.#timer);
|
||||
this.#preventClick = true;
|
||||
|
||||
var options = [
|
||||
{'tooltip': 'Attack', 'src': 'attack.png', 'callback': () => {getMap().hideSelectionWheel(); getUnitsManager().attackUnit(this.ID);}},
|
||||
{'tooltip': 'Go to tanker', 'src': 'tanker.png', 'callback': () => {getMap().hideSelectionWheel(); /*showMessage("Function not implemented yet");*/}},
|
||||
{'tooltip': 'RTB', 'src': 'rtb.png', 'callback': () => {getMap().hideSelectionWheel(); /*showMessage("Function not implemented yet");*/}}
|
||||
{ 'tooltip': 'Attack', 'src': 'attack.png', 'callback': () => { getMap().hideSelectionWheel(); getUnitsManager().attackUnit(this.ID); } },
|
||||
{ 'tooltip': 'Go to tanker', 'src': 'tanker.png', 'callback': () => { getMap().hideSelectionWheel(); /*showMessage("Function not implemented yet");*/ } },
|
||||
{ 'tooltip': 'RTB', 'src': 'rtb.png', 'callback': () => { getMap().hideSelectionWheel(); /*showMessage("Function not implemented yet");*/ } }
|
||||
]
|
||||
|
||||
if (!this.leader && !this.wingman)
|
||||
{
|
||||
options.push({'tooltip': 'Create formation', 'src': 'formation.png', 'callback': () => {getMap().hideSelectionWheel(); /*unitsManager.createFormation(this.ID);*/}});
|
||||
if (!this.leader && !this.wingman) {
|
||||
options.push({ 'tooltip': 'Create formation', 'src': 'formation.png', 'callback': () => { getMap().hideSelectionWheel(); /*unitsManager.createFormation(this.ID);*/ } });
|
||||
}
|
||||
|
||||
getMap().showSelectionWheel(e.originalEvent, options, false);
|
||||
}
|
||||
|
||||
#updateMarker()
|
||||
{
|
||||
#updateMarker() {
|
||||
/* Add the marker if not present */
|
||||
if (!getMap().hasLayer(this.#marker))
|
||||
{
|
||||
if (!getMap().hasLayer(this.#marker) && !this.getHidden()) {
|
||||
this.#marker.addTo(getMap());
|
||||
}
|
||||
|
||||
this.#marker.setLatLng(new LatLng(this.latitude, this.longitude));
|
||||
this.#marker.draw({
|
||||
heading: this.heading,
|
||||
speed: this.speed,
|
||||
altitude: this.altitude,
|
||||
alive: this.alive
|
||||
});
|
||||
/* Hide the marker if necessary*/
|
||||
if (getMap().hasLayer(this.#marker) && this.getHidden()) {
|
||||
getMap().removeLayer(this.#marker);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.#marker.setLatLng(new LatLng(this.latitude, this.longitude));
|
||||
this.#marker.draw({
|
||||
heading: this.heading,
|
||||
speed: this.speed,
|
||||
altitude: this.altitude,
|
||||
alive: this.alive
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#drawPath()
|
||||
{
|
||||
if (this.activePath != null)
|
||||
{
|
||||
#drawPath() {
|
||||
if (this.activePath != null) {
|
||||
var _points = [];
|
||||
_points.push(new LatLng(this.latitude, this.longitude));
|
||||
|
||||
/* Add markers if missing */
|
||||
while (this.#pathMarkers.length < Object.keys(this.activePath).length)
|
||||
{
|
||||
var marker = new Marker([0, 0]).addTo(getMap());
|
||||
while (this.#pathMarkers.length < Object.keys(this.activePath).length) {
|
||||
var marker = new Marker([0, 0], { icon: pathIcon }).addTo(getMap());
|
||||
this.#pathMarkers.push(marker);
|
||||
}
|
||||
|
||||
/* Remove markers if too many */
|
||||
while (this.#pathMarkers.length > Object.keys(this.activePath).length)
|
||||
{
|
||||
while (this.#pathMarkers.length > Object.keys(this.activePath).length) {
|
||||
getMap().removeLayer(this.#pathMarkers[this.#pathMarkers.length - 1]);
|
||||
this.#pathMarkers.splice(this.#pathMarkers.length - 1, 1)
|
||||
}
|
||||
|
||||
/* Update the position of the existing markers (to avoid creating markers uselessly) */
|
||||
for (let WP in this.activePath)
|
||||
{
|
||||
for (let WP in this.activePath) {
|
||||
var destination = this.activePath[WP];
|
||||
this.#pathMarkers[parseInt(WP) - 1].setLatLng([destination.lat, destination.lng]);
|
||||
_points.push(new LatLng(destination.lat, destination.lng));
|
||||
@@ -260,26 +214,24 @@ export class Unit
|
||||
}
|
||||
}
|
||||
|
||||
#clearPath()
|
||||
{
|
||||
for (let WP in this.#pathMarkers)
|
||||
{
|
||||
#clearPath() {
|
||||
for (let WP in this.#pathMarkers) {
|
||||
getMap().removeLayer(this.#pathMarkers[WP]);
|
||||
}
|
||||
this.#pathMarkers = [];
|
||||
this.#pathPolyline.setLatLngs([]);
|
||||
}
|
||||
|
||||
/*
|
||||
drawTargets()
|
||||
|
||||
#drawTargets()
|
||||
{
|
||||
for (let typeIndex in this.missionData['targets'])
|
||||
for (let typeIndex in this.targets)
|
||||
{
|
||||
for (let index in this.missionData['targets'][typeIndex])
|
||||
for (let index in this.targets[typeIndex])
|
||||
{
|
||||
var targetData = this.missionData['targets'][typeIndex][index];
|
||||
var target = unitsManager.getUnitByID(targetData.object["id_"])
|
||||
if (target != undefined){
|
||||
var targetData = this.targets[typeIndex][index];
|
||||
var target = getUnitsManager().getUnitByID(targetData.object["id_"])
|
||||
if (target != null){
|
||||
var startLatLng = new LatLng(this.latitude, this.longitude)
|
||||
var endLatLng = new LatLng(target.latitude, target.longitude)
|
||||
|
||||
@@ -301,72 +253,44 @@ export class Unit
|
||||
color = "#FFFFFF";
|
||||
}
|
||||
var targetPolyline = new Polyline([startLatLng, endLatLng], {color: color, weight: 3, opacity: 1, smoothFactor: 1});
|
||||
targetPolyline.addTo(map.getMap());
|
||||
targetPolyline.addTo(getMap());
|
||||
this.#targetsPolylines.push(targetPolyline)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clearTargets()
|
||||
#clearTargets()
|
||||
{
|
||||
for (let index in this.#targetsPolylines)
|
||||
{
|
||||
map.getMap().removeLayer(this.#targetsPolylines[index])
|
||||
getMap().removeLayer(this.#targetsPolylines[index])
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
attackUnit(targetID: number)
|
||||
{
|
||||
// Call DCS attackUnit function
|
||||
if (this.ID != targetID)
|
||||
{
|
||||
attackUnit(targetID: number) {
|
||||
/* Call DCS attackUnit function */
|
||||
if (this.ID != targetID) {
|
||||
attackUnit(this.ID, targetID);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// TODO: show a message
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
changeSpeed(speedChange: string)
|
||||
{
|
||||
changeSpeed(this.ID, speedChange);
|
||||
}
|
||||
|
||||
changeAltitude(altitudeChange: string)
|
||||
{
|
||||
changeAltitude(this.ID, altitudeChange);
|
||||
}
|
||||
|
||||
/*
|
||||
changeSpeed(speedChange)
|
||||
{
|
||||
// TODO move in dedicated file
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("PUT", RESTaddress);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === 4) {
|
||||
console.log(this.unitName + " speed change request: " + speedChange);
|
||||
}
|
||||
};
|
||||
|
||||
var command = {"ID": this.ID, "change": speedChange}
|
||||
var data = {"changeSpeed": command}
|
||||
|
||||
xhr.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
changeAltitude(altitudeChange)
|
||||
{
|
||||
// TODO move in dedicated file
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("PUT", RESTaddress);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === 4) {
|
||||
console.log(this.unitName + " altitude change request: " + altitudeChange);
|
||||
}
|
||||
};
|
||||
|
||||
var command = {"ID": this.ID, "change": altitudeChange}
|
||||
var data = {"changeAltitude": command}
|
||||
|
||||
xhr.send(JSON.stringify(data));
|
||||
}
|
||||
|
||||
setformation(formation)
|
||||
{
|
||||
// TODO move in dedicated file
|
||||
@@ -405,66 +329,98 @@ export class Unit
|
||||
*/
|
||||
}
|
||||
|
||||
export class AirUnit extends Unit
|
||||
{
|
||||
|
||||
export class AirUnit extends Unit {
|
||||
getHidden() {
|
||||
if (this.alive)
|
||||
{
|
||||
if (this.flags.user && getVisibilitySettings().user === "hidden")
|
||||
return true
|
||||
else if (!this.flags.user && getVisibilitySettings().ai === "hidden")
|
||||
return true
|
||||
}
|
||||
else
|
||||
return getVisibilitySettings().dead === "hidden"
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class Aircraft extends AirUnit
|
||||
{
|
||||
constructor(ID: number, options: MarkerOptions)
|
||||
{
|
||||
var marker = new UnitMarker(options);
|
||||
export class Aircraft extends AirUnit {
|
||||
constructor(ID: number, options: MarkerOptions) {
|
||||
var marker = new AircraftMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
}
|
||||
|
||||
export class Helicopter extends AirUnit
|
||||
{
|
||||
constructor(ID: number, options: MarkerOptions)
|
||||
{
|
||||
var marker = new UnitMarker(options);
|
||||
export class Helicopter extends AirUnit {
|
||||
constructor(ID: number, options: MarkerOptions) {
|
||||
var marker = new HelicopterMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
}
|
||||
|
||||
export class GroundUnit extends Unit
|
||||
{
|
||||
constructor(ID: number, options: MarkerOptions)
|
||||
{
|
||||
var marker = new UnitMarker(options);
|
||||
export class GroundUnit extends Unit {
|
||||
constructor(ID: number, options: MarkerOptions) {
|
||||
var marker = new GroundUnitMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
|
||||
getHidden() {
|
||||
if (this.alive)
|
||||
{
|
||||
if (this.flags.user && getVisibilitySettings().user === "hidden")
|
||||
return true
|
||||
else if (!this.flags.user && getVisibilitySettings().ai === "hidden")
|
||||
return true
|
||||
}
|
||||
else
|
||||
return getVisibilitySettings().dead === "hidden"
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class NavyUnit extends Unit {
|
||||
constructor(ID: number, options: MarkerOptions) {
|
||||
var marker = new NavyUnitMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
|
||||
getHidden() {
|
||||
if (this.alive)
|
||||
{
|
||||
if (this.flags.user && getVisibilitySettings().user === "hidden")
|
||||
return true
|
||||
else if (!this.flags.user && getVisibilitySettings().ai === "hidden")
|
||||
return true
|
||||
}
|
||||
else
|
||||
return getVisibilitySettings().dead === "hidden"
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class Weapon extends Unit {
|
||||
getHidden() {
|
||||
if (this.alive)
|
||||
{
|
||||
if (!this.flags.user && getVisibilitySettings().weapon === "hidden")
|
||||
return true
|
||||
}
|
||||
else
|
||||
return getVisibilitySettings().dead === "hidden"
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export class Missile extends Weapon {
|
||||
constructor(ID: number, options: MarkerOptions) {
|
||||
var marker = new WeaponMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
}
|
||||
|
||||
export class NavyUnit extends Unit
|
||||
{
|
||||
constructor(ID: number, options: MarkerOptions)
|
||||
{
|
||||
var marker = new UnitMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
}
|
||||
|
||||
export class Weapon extends Unit
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
export class Missile extends Weapon
|
||||
{
|
||||
constructor(ID: number, options: MarkerOptions)
|
||||
{
|
||||
var marker = new UnitMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
}
|
||||
|
||||
export class Bomb extends Weapon
|
||||
{
|
||||
constructor(ID: number, options: MarkerOptions)
|
||||
{
|
||||
var marker = new UnitMarker(options);
|
||||
export class Bomb extends Weapon {
|
||||
constructor(ID: number, options: MarkerOptions) {
|
||||
var marker = new WeaponMarker(options);
|
||||
super(ID, marker);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,37 @@
|
||||
import * as L from 'leaflet'
|
||||
import { Symbol } from 'milsymbol'
|
||||
import { getVisibilitySettings } from '..'
|
||||
|
||||
export interface MarkerOptions
|
||||
{
|
||||
unitName: string
|
||||
name: string
|
||||
human: boolean
|
||||
coalitionID: number
|
||||
type: any
|
||||
export interface MarkerOptions {
|
||||
unitName: string
|
||||
name: string
|
||||
human: boolean
|
||||
coalitionID: number
|
||||
type: any
|
||||
}
|
||||
|
||||
export interface MarkerData
|
||||
{
|
||||
heading: number
|
||||
speed: number
|
||||
altitude: number
|
||||
alive: boolean
|
||||
export interface MarkerData {
|
||||
heading: number
|
||||
speed: number
|
||||
altitude: number
|
||||
alive: boolean
|
||||
}
|
||||
|
||||
export class UnitMarker extends L.Marker
|
||||
{
|
||||
#unitName: string
|
||||
#name: string
|
||||
#human: boolean
|
||||
#coalitionID: number
|
||||
#alive: boolean
|
||||
export class UnitMarker extends L.Marker {
|
||||
#unitName: string
|
||||
#name: string
|
||||
#human: boolean
|
||||
#alive: boolean = true
|
||||
#selected: boolean = false
|
||||
|
||||
constructor(options: MarkerOptions)
|
||||
{
|
||||
super(new L.LatLng(0, 0), {riseOnHover: true});
|
||||
this.#unitName = options.unitName
|
||||
this.#name = options.name
|
||||
this.#human = options.human
|
||||
this.#coalitionID = options.coalitionID
|
||||
|
||||
this.#alive = true;
|
||||
constructor(options: MarkerOptions) {
|
||||
super(new L.LatLng(0, 0), { riseOnHover: true });
|
||||
this.#unitName = options.unitName;
|
||||
this.#name = options.name;
|
||||
this.#human = options.human;
|
||||
|
||||
var symbol = new Symbol(this.#computeMarkerCode(options), {size: 100});
|
||||
var img = symbol.asCanvas().toDataURL('image/png');
|
||||
var symbol = new Symbol(this.#computeMarkerCode(options), { size: 25 });
|
||||
var img = symbol.asCanvas().toDataURL('image/png');
|
||||
|
||||
var icon = new L.DivIcon({
|
||||
html: `<table class="unit-marker-container" id="container">
|
||||
@@ -50,25 +44,23 @@ export class UnitMarker extends L.Marker
|
||||
<div class="unit-marker-name" id="name">${this.#name}</div>
|
||||
</td>
|
||||
</tr>
|
||||
</table>`,
|
||||
className: 'unit-marker'});
|
||||
this.setIcon(icon);
|
||||
</table>`,
|
||||
className: 'unit-marker'
|
||||
});
|
||||
this.setIcon(icon);
|
||||
}
|
||||
|
||||
onAdd(map: L.Map): this
|
||||
{
|
||||
onAdd(map: L.Map): this {
|
||||
super.onAdd(map);
|
||||
var element = <HTMLElement>this.getElement();
|
||||
this.addEventListener('mouseover', function(e: any) { e.target?.setHovered(true);});
|
||||
this.addEventListener('mouseout', function(e: any) { e.target?.setHovered(false);});
|
||||
this.addEventListener('mouseover', function (e: any) { e.target?.setHovered(true); });
|
||||
this.addEventListener('mouseout', function (e: any) { e.target?.setHovered(false); });
|
||||
return this
|
||||
}
|
||||
|
||||
draw(data: MarkerData)
|
||||
{
|
||||
draw(data: MarkerData) {
|
||||
this.#alive = data.alive;
|
||||
var element = this.getElement();
|
||||
if (element != null)
|
||||
{
|
||||
if (element != null) {
|
||||
var nameDiv = <HTMLElement>element.querySelector("#name");
|
||||
var unitNameDiv = <HTMLElement>element.querySelector("#unitName");
|
||||
var container = <HTMLElement>element.querySelector("#container");
|
||||
@@ -76,33 +68,70 @@ export class UnitMarker extends L.Marker
|
||||
var altitudeDiv = <HTMLElement>element.querySelector("#altitude");
|
||||
var speedDiv = <HTMLElement>element.querySelector("#speed");
|
||||
|
||||
/* If visibility is full show all labels */
|
||||
nameDiv.style.display = '';
|
||||
unitNameDiv.style.display = '';
|
||||
altitudeDiv.style.display = '';
|
||||
speedDiv.style.display = '';
|
||||
|
||||
/* If visibility is partial shown only icon and unit name. If none, shown only icon. */
|
||||
if (this.getVisibility() === "partial" || this.getVisibility() === "none")
|
||||
{
|
||||
unitNameDiv.style.display = 'none';
|
||||
altitudeDiv.style.display = 'none';
|
||||
speedDiv.style.display = 'none';
|
||||
}
|
||||
if (this.getVisibility() === "none")
|
||||
nameDiv.style.display = 'none';
|
||||
|
||||
nameDiv.style.left = (-(nameDiv.offsetWidth - container.offsetWidth) / 2) + "px";
|
||||
unitNameDiv.style.left = (-(unitNameDiv.offsetWidth - container.offsetWidth) / 2) + "px";
|
||||
|
||||
|
||||
icon.style.transform = "rotate(" + data.heading + "rad)";
|
||||
altitudeDiv.innerHTML = String(Math.round(data.altitude / 0.3048 / 100) / 10);
|
||||
speedDiv.innerHTML = String(Math.round(data.speed * 1.94384));
|
||||
|
||||
if (!this.#alive)
|
||||
{
|
||||
this.getElement()?.querySelector("#icon")?.classList.add("unit-marker-dead");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setSelected(selected: boolean)
|
||||
{
|
||||
setSelected(selected: boolean) {
|
||||
this.#selected = selected;
|
||||
this.getElement()?.querySelector("#icon")?.classList.remove("unit-marker-hovered");
|
||||
this.getElement()?.querySelector("#icon")?.classList.toggle("unit-marker-selected", selected);
|
||||
}
|
||||
|
||||
setHovered(hovered: boolean)
|
||||
{
|
||||
getSelected() {
|
||||
return this.#selected;
|
||||
}
|
||||
|
||||
setHovered(hovered: boolean) {
|
||||
this.getElement()?.querySelector("#icon")?.classList.toggle("unit-marker-hovered", hovered && this.#alive);
|
||||
}
|
||||
|
||||
#computeMarkerCode(options: MarkerOptions)
|
||||
{
|
||||
getHuman() {
|
||||
return this.#human;
|
||||
}
|
||||
|
||||
getAlive() {
|
||||
return this.#alive;
|
||||
}
|
||||
|
||||
getVisibility() {
|
||||
return "full";
|
||||
}
|
||||
|
||||
#computeMarkerCode(options: MarkerOptions) {
|
||||
var identity = "00";
|
||||
var set = "00";
|
||||
var entity = "00";
|
||||
var entityType = "00";
|
||||
var entitySubtype = "00";
|
||||
var sectorOneModifier = "00";
|
||||
var sectorTwoModifier = "00";
|
||||
|
||||
/* Identity */
|
||||
if (options.coalitionID == 1)
|
||||
@@ -112,8 +141,8 @@ export class UnitMarker extends L.Marker
|
||||
else
|
||||
identity = "04" /* Neutral */
|
||||
|
||||
if (options.type.level1 == 1)
|
||||
{
|
||||
/* Air */
|
||||
if (options.type.level1 == 1) {
|
||||
set = "01"
|
||||
entity = "11"
|
||||
if (options.type.level2 == 1)
|
||||
@@ -134,14 +163,103 @@ export class UnitMarker extends L.Marker
|
||||
else if (options.type.level3 == 6)
|
||||
entitySubtype = "00";
|
||||
}
|
||||
|
||||
/* Ground */
|
||||
else if (options.type.level1 == 2)
|
||||
{
|
||||
set = "10"
|
||||
entity = "12"
|
||||
entityType = "05"
|
||||
}
|
||||
/* Navy */
|
||||
else if (options.type.level1 == 3)
|
||||
set = "30"
|
||||
else if (options.type.level1 == 2)
|
||||
/* Weapon */
|
||||
else if (options.type.level1 == 4)
|
||||
{
|
||||
set = "02"
|
||||
|
||||
|
||||
return `10${identity}${set}0000${entity}${entityType}${entitySubtype}0000`
|
||||
entity = "11"
|
||||
if (options.type.level3 == 7)
|
||||
{
|
||||
sectorOneModifier = "01"
|
||||
sectorTwoModifier = "01"
|
||||
}
|
||||
else if (options.type.level3 == 8)
|
||||
{
|
||||
sectorOneModifier = "01"
|
||||
sectorTwoModifier = "02"
|
||||
}
|
||||
else if (options.type.level3 == 34)
|
||||
{
|
||||
sectorOneModifier = "02"
|
||||
sectorTwoModifier = "01"
|
||||
}
|
||||
else if (options.type.level3 == 11)
|
||||
{
|
||||
sectorOneModifier = "02"
|
||||
sectorTwoModifier = "02"
|
||||
}
|
||||
}
|
||||
|
||||
return `10${identity}${set}0000${entity}${entityType}${entitySubtype}${sectorOneModifier}${sectorTwoModifier}`
|
||||
}
|
||||
}
|
||||
|
||||
export class AirUnitMarker extends UnitMarker {
|
||||
getVisibility() {
|
||||
if (this.getSelected())
|
||||
return "full";
|
||||
|
||||
if (this.getHuman())
|
||||
return getVisibilitySettings().user;
|
||||
else if (!this.getAlive())
|
||||
return "none";
|
||||
else
|
||||
return getVisibilitySettings().ai;
|
||||
}
|
||||
}
|
||||
|
||||
export class AircraftMarker extends AirUnitMarker {
|
||||
}
|
||||
|
||||
export class HelicopterMarker extends AirUnitMarker {
|
||||
}
|
||||
|
||||
export class GroundUnitMarker extends UnitMarker {
|
||||
/* Are user driven units recognized as human? */
|
||||
getVisibility() {
|
||||
if (this.getSelected())
|
||||
return "full";
|
||||
|
||||
if (this.getHuman())
|
||||
return getVisibilitySettings().user;
|
||||
else if (!this.getAlive())
|
||||
return "none";
|
||||
else
|
||||
return getVisibilitySettings().ai;
|
||||
}
|
||||
}
|
||||
|
||||
export class NavyUnitMarker extends UnitMarker {
|
||||
getVisibility() {
|
||||
if (this.getSelected())
|
||||
return "full";
|
||||
|
||||
if (!this.getAlive())
|
||||
return "none";
|
||||
else
|
||||
return getVisibilitySettings().ai;
|
||||
}
|
||||
}
|
||||
|
||||
export class WeaponMarker extends UnitMarker {
|
||||
getVisibility() {
|
||||
if (this.getSelected())
|
||||
return "full";
|
||||
|
||||
if (!this.getAlive())
|
||||
return "none";
|
||||
else
|
||||
return getVisibilitySettings().weapon;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,20 @@
|
||||
import { LatLng, LatLngBounds } from "leaflet";
|
||||
import { getMap, getUnitInfoPanel } from "..";
|
||||
import { Unit, GroundUnit } from "./unit";
|
||||
|
||||
export class UnitsManager
|
||||
{
|
||||
#units: { [ID: number]: Unit};
|
||||
export class UnitsManager {
|
||||
#units: { [ID: number]: Unit };
|
||||
#copiedUnits: Unit[];
|
||||
|
||||
constructor()
|
||||
{
|
||||
constructor() {
|
||||
this.#units = {};
|
||||
this.#copiedUnits = [];
|
||||
}
|
||||
|
||||
addUnit(ID: number, data: any)
|
||||
{
|
||||
addUnit(ID: number, data: any) {
|
||||
/* The name of the unit category is exactly the same as the constructor name */
|
||||
var constructor = Unit.getConstructor(data.category);
|
||||
if (constructor != undefined)
|
||||
{
|
||||
var constructor = Unit.getConstructor(data.category);
|
||||
if (constructor != undefined) {
|
||||
var options = {
|
||||
unitName: data.unitName,
|
||||
name: data.name,
|
||||
@@ -29,92 +26,75 @@ export class UnitsManager
|
||||
}
|
||||
}
|
||||
|
||||
getUnitByID(ID: number)
|
||||
{
|
||||
getUnitByID(ID: number) {
|
||||
return this.#units[ID];
|
||||
}
|
||||
|
||||
removeUnit(ID: number)
|
||||
{
|
||||
removeUnit(ID: number) {
|
||||
|
||||
}
|
||||
|
||||
deselectAllUnits()
|
||||
{
|
||||
for (let ID in this.#units)
|
||||
{
|
||||
deselectAllUnits() {
|
||||
for (let ID in this.#units) {
|
||||
this.#units[ID].setSelected(false);
|
||||
}
|
||||
}
|
||||
|
||||
update(data: any)
|
||||
{
|
||||
for (let ID in data["units"])
|
||||
{
|
||||
update(data: any) {
|
||||
for (let ID in data["units"]) {
|
||||
/* Create the unit if missing from the local array, then update the data. Drawing is handled by leaflet. */
|
||||
if (!(ID in this.#units))
|
||||
{
|
||||
if (!(ID in this.#units)) {
|
||||
this.addUnit(parseInt(ID), data["units"][ID]);
|
||||
}
|
||||
this.#units[parseInt(ID)].update(data["units"][ID]);
|
||||
}
|
||||
|
||||
if (this.getSelectedUnits().length == 1)
|
||||
{
|
||||
if (this.getSelectedUnits().length == 1) {
|
||||
getUnitInfoPanel().show();
|
||||
getUnitInfoPanel().update(this.getSelectedUnits()[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
getUnitInfoPanel().hide();
|
||||
}
|
||||
}
|
||||
|
||||
onUnitSelection()
|
||||
{
|
||||
if (this.getSelectedUnits().length > 0)
|
||||
{
|
||||
|
||||
onUnitSelection() {
|
||||
if (this.getSelectedUnits().length > 0) {
|
||||
getMap().setState("MOVE_UNIT");
|
||||
//unitControlPanel.setEnabled(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
getMap().setState("IDLE");
|
||||
//unitControlPanel.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
// selectFromBounds(bounds)
|
||||
// {
|
||||
// this.deselectAllUnits();
|
||||
// for (let ID in this.#units)
|
||||
// {
|
||||
// var latlng = new LatLng(this.#units[ID].latitude, this.#units[ID].longitude);
|
||||
// if (bounds.contains(latlng))
|
||||
// {
|
||||
// this.#units[ID].setSelected(true);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
getSelectedUnits()
|
||||
selectFromBounds(bounds: LatLngBounds)
|
||||
{
|
||||
var selectedUnits = [];
|
||||
this.deselectAllUnits();
|
||||
for (let ID in this.#units)
|
||||
{
|
||||
if (this.#units[ID].getSelected())
|
||||
var latlng = new LatLng(this.#units[ID].latitude, this.#units[ID].longitude);
|
||||
if (bounds.contains(latlng))
|
||||
{
|
||||
this.#units[ID].setSelected(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getSelectedUnits() {
|
||||
var selectedUnits = [];
|
||||
for (let ID in this.#units) {
|
||||
if (this.#units[ID].getSelected()) {
|
||||
selectedUnits.push(this.#units[ID]);
|
||||
}
|
||||
}
|
||||
return selectedUnits;
|
||||
}
|
||||
|
||||
addDestination(latlng: L.LatLng)
|
||||
{
|
||||
addDestination(latlng: L.LatLng) {
|
||||
var selectedUnits = this.getSelectedUnits();
|
||||
for (let idx in selectedUnits)
|
||||
{
|
||||
for (let idx in selectedUnits) {
|
||||
var commandedUnit = selectedUnits[idx];
|
||||
//if (selectedUnits[idx].wingman)
|
||||
//{
|
||||
@@ -124,11 +104,9 @@ export class UnitsManager
|
||||
}
|
||||
}
|
||||
|
||||
clearDestinations()
|
||||
{
|
||||
clearDestinations() {
|
||||
var selectedUnits = this.getSelectedUnits();
|
||||
for (let idx in selectedUnits)
|
||||
{
|
||||
for (let idx in selectedUnits) {
|
||||
var commandedUnit = selectedUnits[idx];
|
||||
//if (selectedUnits[idx].wingman)
|
||||
//{
|
||||
@@ -138,28 +116,23 @@ export class UnitsManager
|
||||
}
|
||||
}
|
||||
|
||||
// selectedUnitsMove()
|
||||
// {
|
||||
|
||||
// }
|
||||
selectedUnitsChangeSpeed(speedChange: string)
|
||||
{
|
||||
var selectedUnits = this.getSelectedUnits();
|
||||
for (let idx in selectedUnits)
|
||||
{
|
||||
selectedUnits[idx].changeSpeed(speedChange);
|
||||
}
|
||||
}
|
||||
|
||||
// selectedUnitsChangeSpeed(speedChange)
|
||||
// {
|
||||
// var selectedUnits = this.getSelectedUnits();
|
||||
// for (let idx in selectedUnits)
|
||||
// {
|
||||
// selectedUnits[idx].changeSpeed(speedChange);
|
||||
// }
|
||||
// }
|
||||
|
||||
// selectedUnitsChangeAltitude(altitudeChange)
|
||||
// {
|
||||
// var selectedUnits = this.getSelectedUnits();
|
||||
// for (let idx in selectedUnits)
|
||||
// {
|
||||
// selectedUnits[idx].changeAltitude(altitudeChange);
|
||||
// }
|
||||
// }
|
||||
selectedUnitsChangeAltitude(altitudeChange: string)
|
||||
{
|
||||
var selectedUnits = this.getSelectedUnits();
|
||||
for (let idx in selectedUnits)
|
||||
{
|
||||
selectedUnits[idx].changeAltitude(altitudeChange);
|
||||
}
|
||||
}
|
||||
|
||||
// handleKeyEvent(e)
|
||||
// {
|
||||
@@ -187,11 +160,9 @@ export class UnitsManager
|
||||
// }
|
||||
// }
|
||||
|
||||
attackUnit(ID: number)
|
||||
{
|
||||
attackUnit(ID: number) {
|
||||
var selectedUnits = this.getSelectedUnits();
|
||||
for (let idx in selectedUnits)
|
||||
{
|
||||
for (let idx in selectedUnits) {
|
||||
/* If a unit is a wingman, send the command to its leader */
|
||||
var commandedUnit = selectedUnits[idx];
|
||||
//if (selectedUnits[idx].wingman)
|
||||
|
||||
Reference in New Issue
Block a user