mirror of
https://github.com/Xevion/Rebirth.git
synced 2025-12-08 12:08:12 -06:00
131 lines
3.7 KiB
C#
131 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
public class Planet : MonoBehaviour
|
|
{
|
|
private MeshFilter _meshFilter;
|
|
private LineRenderer _lineRenderer;
|
|
private CircleCollider2D _circleCollider;
|
|
private Network _network;
|
|
private HashSet<Unit> _units;
|
|
|
|
[SerializeField] public HashSet<Planet> neighbors;
|
|
|
|
[HideInInspector] public float Size; // 1.0 -
|
|
[HideInInspector] public int Bulbs; // 0 - 5+
|
|
public Color edgeColor = Color.white;
|
|
public Color fillColor = Color.white;
|
|
[HideInInspector] public float edgeWidth = 0.2f;
|
|
|
|
void Awake()
|
|
{
|
|
_lineRenderer = gameObject.GetComponent<LineRenderer>();
|
|
_network = FindObjectOfType<Network>();
|
|
_units = new HashSet<Unit>();
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
}
|
|
|
|
void OnMouseDown()
|
|
{
|
|
SpawnUnit();
|
|
}
|
|
|
|
public Vector2 GetSurfacePosition(float angle, float distance = 0)
|
|
{
|
|
var radius = Size / 100f + distance;
|
|
var theta = angle * Mathf.Deg2Rad;
|
|
return new Vector2(Mathf.Cos(theta), Mathf.Sin(theta)) * radius;
|
|
}
|
|
|
|
void SpawnUnit()
|
|
{
|
|
var unitPrefab = Resources.Load<GameObject>("BaseUnit");
|
|
var unitObject = Instantiate(unitPrefab);
|
|
unitObject.transform.position = transform.position;
|
|
unitObject.transform.parent = transform;
|
|
_units.Add(unitObject.GetComponent<Unit>());
|
|
Debug.Log("Spawned unit " + unitObject.GetComponent<Unit>().GetHashCode());
|
|
unitObject.name = "Unit " + _units.Count;
|
|
}
|
|
|
|
bool IsConnected(Planet other)
|
|
{
|
|
return neighbors.Contains(other);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_network.Destroyed(this);
|
|
}
|
|
|
|
public void UnitDestroyed(Unit unit)
|
|
{
|
|
_units.Remove(unit);
|
|
}
|
|
|
|
void Connect(params Planet[] others)
|
|
{
|
|
foreach (var other in others)
|
|
{
|
|
if (other == this) continue;
|
|
neighbors.Add(other);
|
|
}
|
|
}
|
|
|
|
public void Render()
|
|
{
|
|
_meshFilter = gameObject.GetComponent<MeshFilter>();
|
|
_lineRenderer = gameObject.GetComponent<LineRenderer>();
|
|
_circleCollider = gameObject.GetComponent<CircleCollider2D>();
|
|
|
|
_lineRenderer.widthMultiplier = edgeWidth;
|
|
_lineRenderer.positionCount = 0;
|
|
_lineRenderer.loop = true;
|
|
_lineRenderer.useWorldSpace = false;
|
|
_lineRenderer.startColor = edgeColor;
|
|
_lineRenderer.endColor = edgeColor;
|
|
_lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
|
|
|
|
var radius = Size / 100f;
|
|
const float segmentOffset = 40f;
|
|
const float segmentMultiplier = 50f;
|
|
var numSegments = (int)(radius * segmentMultiplier + segmentOffset);
|
|
|
|
// Create an array of points around a circle
|
|
var circleVertices = Enumerable.Range(0, numSegments)
|
|
.Select(i =>
|
|
{
|
|
var theta = 2 * Mathf.PI * i / numSegments;
|
|
return new Vector2(Mathf.Cos(theta), Mathf.Sin(theta)) * radius;
|
|
})
|
|
.ToArray();
|
|
|
|
// Find all the triangles in the shape
|
|
var triangles = new Triangulator(circleVertices).Triangulate();
|
|
|
|
// Assign each vertex the fill color
|
|
var colors = Enumerable.Repeat(fillColor, circleVertices.Length).ToArray();
|
|
|
|
var mesh = new Mesh
|
|
{
|
|
name = "Circle",
|
|
vertices = circleVertices.ToVector3(),
|
|
triangles = triangles,
|
|
colors = colors
|
|
};
|
|
|
|
mesh.RecalculateNormals();
|
|
mesh.RecalculateBounds();
|
|
mesh.RecalculateTangents();
|
|
|
|
_circleCollider.radius = radius;
|
|
|
|
_meshFilter.mesh = mesh;
|
|
_lineRenderer.positionCount = mesh.vertices.Length;
|
|
_lineRenderer.SetPositions(mesh.vertices);
|
|
}
|
|
} |