using System; using System.Collections.Generic; using UnityEngine; namespace FunkySheep.Earth.Buildings { [Serializable] public class Building { public long id; public List points = new List(); public List innerPoints = new List(); public int stagesCount = 5; public Vector2 position; public float area; public float? lowPoint = null; public float? hightPoint = null; public List tags = new List(); public FunkySheep.Events.GameObjectEvent onBuildingCreation; public Building(long id) { this.id = id; } public void Initialize() { this.position = Position(); SetFirstPoint(); SetClockWise(); area = Area(); } /// /// Calculate the center relative to the points /// /// The center of all points public Vector2 Position() { // Calculate the center Vector2 center = Vector2.zero; for (int i = 0; i < points.Count; i++) { center += points[i]; } center /= points.Count; return center; } /// /// Calculate the building area /// /// public float Area() { float area = 0; for (int i = 0; i < points.Count; i++) { area += Vector2.Distance(points[i], points[(i + 1) % points.Count]); } return area; } /// /// If the Vector Array is clockwise, return it /// /// public void SetClockWise() { if (points.Count <= 2) { return; } int result = FunkySheep.Vectors.Utils.IsClockWise(points[1], points[points.Count - 1], points[0]); if (result < 0) { points.Reverse(); } } /// /// Set the first point of the building (the farest from the center) /// public void SetFirstPoint() { int maxPointIndex = 0; for (int i = 0; i < points.Count; i++) { if ((points[maxPointIndex] - position).magnitude < (points[i] - position).magnitude) { maxPointIndex = i; } } Vector2[] tempPoints = new Vector2[points.Count]; points.CopyTo(tempPoints); for (int i = 0; i < points.Count; i++) { points[i] = tempPoints[(i + maxPointIndex) % points.Count]; } } } }