[ad_1]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Generatortrihex : MonoBehaviour
{
[Tooltip("Prefab for hexagon")]
[SerializeField] private GameObject HexagonPrefab;
[Tooltip("Number of hexagonal layers")]
[SerializeField] private uint Radius;
[Tooltip("Multiplier for hexagon side length")]
[SerializeField] private float HexSideMultiplier = 1;
private const float sq3 = 1.7320508075688772935274463415059f;
private void Start()
{
GenerateHexagonFloor();
}
void GenerateHexagonFloor()
{
// Point of the next hexagon to be spawned
Vector2 currentPoint = transform.position;
// Check if the HexagonPrefab has uniform scale
if (!Mathf.Approximately(HexagonPrefab.transform.localScale.x, HexagonPrefab.transform.localScale.y))
{
Debug.LogError("HexagonPrefab has non-uniform scale: cannot determine its side. Aborting");
return;
}
// Array defining the movement vectors for hexagon spawning
Vector2[] mv = {
new Vector2(1.5f, -sq3 * 0.5f), // Down Right
new Vector2(0, -sq3), // Down
new Vector2(-1.5f, -sq3 * 0.5f), // Down Left
new Vector2(-1.5f, sq3 * 0.5f), // Up Left
new Vector2(0, sq3), // Up
new Vector2(1.5f, sq3 * 0.5f) // Up Right
};
int lmv = mv.Length; // Length of the movement vector array
float HexSide = HexagonPrefab.transform.localScale.x * HexSideMultiplier; // Calculate hexagon side length
int hn = 1; // Initialize hexagon number counter
// Loop through layers of hexagons (from center to outer layers)
for (int mult = 0; mult <= Radius; mult++)
{
// Loop through each movement vector
for (int j = 0; j < lmv; j++)
{
// Loop to instantiate hexagons in the current layer
for (int i = 0; i < mult; i++, hn++)
{
GameObject hexagon = Instantiate(HexagonPrefab, currentPoint, Quaternion.identity, transform);
Tile newTile = hexagon.GetComponent<Tile>();
if (newTile != null)
{
newTile.Init();
if (mult == 0) newTile.OnFill();
}
hexagon.name = string.Format("Hex n: {0}", hn);
currentPoint += mv[j] * HexSide;
}
// Special case for the last movement vector (Up)
if (j == 4)
{
GameObject hexagon = Instantiate(HexagonPrefab, currentPoint, Quaternion.identity, transform);
Tile newTile = hexagon.GetComponent<Tile>();
if (newTile != null)
{
newTile.Init();
}
hexagon.name = string.Format("Hex n: {0}", hn);
currentPoint += mv[j] * HexSide;
hn++;
if (mult == Radius)
break; // Finished if it's the last layer
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile : MonoBehaviour
{
[SerializeField] private SpriteRenderer spriteRenderer;
[SerializeField] private Transform[] checkPoint;
private float radCheckPoint = 0.1f;
[SerializeField] private Color emptyColor;
[SerializeField] private Color fillColor;
[SerializeField] private Color checkColor_empty;
[SerializeField] private Color checkColor_fill;
[SerializeField] private Color selectColor_empty;
[SerializeField] private Color selectColor_fill;
[SerializeField] private Color avaliableColor;
private bool isAvaliable;
public int layer;
public bool isEmpty;
private List<Tile> checkTiles = new List<Tile>();
public void Init()
{
spriteRenderer.color = emptyColor;
isEmpty = true;
}
public void OnFill()
{
if (!isEmpty)
return;
spriteRenderer.color = fillColor;
isEmpty = false;
}
public void OnCheck()
{
if (isEmpty)
{
spriteRenderer.color = checkColor_empty;
}
else
{
spriteRenderer.color = checkColor_fill;
}
}
public void OffCheck()
{
if (isEmpty)
{
spriteRenderer.color = emptyColor;
}
else
{
spriteRenderer.color = fillColor;
}
}
private bool CheckFillTile()
{
if (checkTiles.Count < 1)
return true;
foreach (Tile t in checkTiles)
{
if (t.GetFill())
return true;
}
return false;
}
public void Avaliable()
{
if (!isEmpty)
return;
spriteRenderer.color = avaliableColor;
isAvaliable = true;
}
public bool GetFill()
{
return !isEmpty;
}
private void OnMouseDown()
{
if (isEmpty && !CheckFillTile())
return;
OnFill();
}
private void OnMouseEnter()
{
if (isEmpty)
{
spriteRenderer.color = selectColor_empty;
}
else
{
spriteRenderer.color = selectColor_fill;
}
for (int i = 0; i < checkPoint.Length; i++)
{
RaycastHit2D hit = Physics2D.CircleCast(checkPoint[i].position, radCheckPoint, Vector2.zero);
if (hit.transform != null)
{
if (hit.transform.GetComponent<Tile>() != null)
{
var tileScript = hit.transform.GetComponent<Tile>();
tileScript.OnCheck();
checkTiles.Add(tileScript);
}
}
}
}
private void OnMouseExit()
{
if (isEmpty)
{
if (isAvaliable)
spriteRenderer.color = avaliableColor;
else
spriteRenderer.color = emptyColor;
}
else
{
spriteRenderer.color = fillColor;
}
if (checkTiles.Count < 1)
return;
foreach (Tile t in checkTiles)
{
t.OffCheck();
}
}
}
[ad_2]