Files
2026-05-26 16:15:54 +08:00

127 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using com.fpnn.livedata;
using HexGrid.MapHelper;
using HexGrid.Math;
using TMPro;
using UnityEngine;
namespace HexGrid
{
// 看看用来触发事件可行吗
public class HexGridTiler : MonoBehaviour
{
//
private HexTile _tileData;
//组件
private Transform _on;
private Transform _off;
// 坐标显示
private TMP_Text _textCoords;
// 调停者
private IHexGridMediator _mediator;
// 动画
private Animation _animation;
private void Awake()
{
Log($"Awake ->{gameObject.name}");
//
_on = transform.Find("on");
_off = transform.Find("off");
_textCoords = transform.Find("text_coords").GetComponent<TMP_Text>();
//
_animation = GetComponent<Animation>();
}
private void Start()
{
Log($"Start()-> {gameObject.name}: {_tileData}");
}
public void SetMediator(IHexGridMediator mediator)
{
_mediator = mediator;
}
public void LoadTileData(HexTile tile)
{
Log($"LoadTileData() ->{tile}");
_tileData = tile;
UpdateName();
UpdateCoordsText();
}
private void UpdateCoordsText()
{
if (_textCoords)
{
_textCoords.text = _tileData.tileCoords.ToString();
}
}
private void UpdateName()
{
// var tileName = $"tile{_tileData.tileCoords}";
var tileName = HexUtils.MakeTileId(_tileData.tileCoords);
Log($"UpdateName()-> {tileName}");
gameObject.name = tileName;
}
//
public async void OnHexCellClicked()
{
if (_tileData == null)
{
return;
}
// 放到调停者当中去
if (_tileData.tileStatus == 1)
{
_animation.Play("rotation");
}
_mediator.OnHexCellClicked(_tileData);
}
public void UpdateVisual()
{
Log("UpdateVisual -> ");
var status = _tileData.tileStatus;
if (status == 0)
{
gameObject.SetActive(false);
}
else
{
gameObject.SetActive(true);
if (status == 1)
{
_on.gameObject.SetActive(true);
_off.gameObject.SetActive(true);
_off.localScale = Vector3.one;
_on.localScale = new Vector3(0, 0, 0);
_on.rotation = Quaternion.Euler(0,60,270);
}
else if (status == 2)
{
_on.gameObject.SetActive(true);
_off.gameObject.SetActive(false);
_off.localScale = Vector3.zero;
}
}
}
private static void Log(object t)
{
Debug.Log($"<color=yellow>HexGridTiler -> {t} </color>");
}
}
}