using UnityEngine;
using System.Collections.Generic;
using WPM.ClipperLib;
namespace WPM {
public abstract class AdminEntity: IAdminEntity, IExtendableAttribute {
///
/// Entity name (country or province).
///
public string name { get; set; }
///
/// Setting hidden to true will hide completely the entity (border, label) and it won't be highlighted
///
public bool hidden;
protected List _regions;
///
/// List of all regions for the admin entity.
///
public abstract List regions { get; set; }
///
/// Index of the biggest region
///
public int mainRegionIndex { get; set; }
///
/// Returns the region object which is the main region of the country
///
public abstract Region mainRegion { get; }
public virtual float mainRegionArea { get { return mainRegion.rect2DArea; } }
///
/// Computed Rect area that includes all regions. Used to fast hovering.
///
public virtual Rect regionsRect2D { get; set; }
public virtual float regionsRect2DArea { get { return regionsRect2D.width * regionsRect2D.height; } }
protected Vector2 _latlonCenter;
///
/// Center of the admin entity in the plane
///
public virtual Vector2 latlonCenter {
get { return _latlonCenter; }
set {
_latlonCenter = value;
_sphereCenter = Conversion.GetSpherePointFromLatLon (_latlonCenter);
}
}
protected Vector3 _sphereCenter;
public virtual Vector3 localPosition { get { return _sphereCenter; } }
protected JSONObject _attrib;
///
/// Use this property to add/retrieve custom attributes for this country/province
///
public JSONObject attrib { get { if (_attrib == null) { _attrib = new JSONObject(); } return _attrib; } set { _attrib = value; } }
public bool hasAttributes { get { return _attrib != null; } }
///
/// Returns true if any region of this entity contains the position
///
/// Map position.
public bool Contains (Vector2 mapPosition, out int regionIndex) {
regionIndex = -1;
if (!regionsRect2D.Contains(mapPosition)) {
Vector2 wrappedPos = mapPosition;
wrappedPos.y += 360;
// check for world-edge-cross-countries
if (!regionsRect2D.Contains(wrappedPos)) {
return false;
}
}
if (regions == null)
return false;
int regionCount = regions.Count;
for (int k = 0; k < regionCount; k++) {
if (regions [k].Contains (mapPosition)) {
regionIndex = k;
return true;
}
}
return false;
}
///
/// Returns true if any region of this entity overlaps a given region
///
/// Other region.
public bool Overlaps (Region otherRegion) {
if (regions == null) {
return false;
}
int regionCount = regions.Count;
for (int k = 0; k < regionCount; k++) {
if (regions [k].Intersects (otherRegion)) {
return true;
}
}
return false;
}
///
/// Used internally by Map Editor.
///
public bool foldOut { get; set; }
///
/// Deletes the surfaces gameobjects related to this entity
///
public void DestroySurfaces() {
if (_regions == null) return;
int regionsCount = _regions.Count;
for (int r = 0; r < regionsCount; r++) {
if (_regions[r].surfaceGameObject != null) {
Object.DestroyImmediate(_regions[r].surfaceGameObject);
}
}
}
}
}