备份CatanBuilding瘦身独立工程
This commit is contained in:
196
Assets/Scripts/HexGrid/HexMapManager.cs
Normal file
196
Assets/Scripts/HexGrid/HexMapManager.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using asap.core;
|
||||
using Cinemachine;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
// 格子管理
|
||||
// 作为 调停者 使用
|
||||
namespace HexGrid{
|
||||
|
||||
public class HexGridMapUserData
|
||||
{
|
||||
//
|
||||
private int zoneNum = 2;
|
||||
//
|
||||
public int zoneCurrentIndex = 0; // [0,1];
|
||||
//
|
||||
public int subZoneCurrentIndex = 0; // []
|
||||
// 已经完成的地块
|
||||
public List<string> completeTiles = new();
|
||||
// 属性
|
||||
public Dictionary<string, int> Properties1 = new();
|
||||
//
|
||||
public Dictionary<string, int> Properties2 = new();
|
||||
|
||||
// 当前tile块的数值
|
||||
// <Id,status>
|
||||
// private Dictionary<int, int> tiles = new Dictionary<int, int>();
|
||||
// private Dictionary<int, int> buildings = new Dictionary<int, int>();
|
||||
|
||||
}
|
||||
public class HexMapDataManager
|
||||
{
|
||||
public const string SaveKey = "HexMapData";
|
||||
public HexGridMapUserData UserMapData { get; private set; }
|
||||
|
||||
public void Init()
|
||||
{
|
||||
UserMapData ??= new HexGridMapUserData
|
||||
{
|
||||
zoneCurrentIndex = 0,
|
||||
subZoneCurrentIndex = 0
|
||||
};
|
||||
SaveUserData();
|
||||
}
|
||||
|
||||
public void ClearMapData()
|
||||
{
|
||||
UserMapData = null;
|
||||
SaveUserData();
|
||||
}
|
||||
//
|
||||
public void SaveUserData()
|
||||
{
|
||||
PlayFabMgr.Instance.UpdateUserDataValue(SaveKey, JsonConvert.SerializeObject(UserMapData));
|
||||
}
|
||||
|
||||
public void LoadUserData(string dataValue)
|
||||
{
|
||||
UserMapData = JsonConvert.DeserializeObject<HexGridMapUserData>(dataValue);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public class HexMapManager : MonoBehaviour
|
||||
{
|
||||
//
|
||||
public HexZoneManager zoneManager;
|
||||
public TerrainManager terrainManager;
|
||||
public BuildingManager buildingManager;
|
||||
public HexGridMediator hexGridMediator;
|
||||
public Transform hexMapRoot;
|
||||
public Camera mainCamera;
|
||||
public string mapName = "hex_build_01";
|
||||
|
||||
// 设置的
|
||||
private CinemachineVirtualCamera _vCam;
|
||||
// 数据
|
||||
private HexGridMap _mapInfo;
|
||||
private HexMapDataManager _dataManager;
|
||||
//
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
mainCamera = Camera.main;
|
||||
_dataManager = GContext.container.Resolve<HexMapDataManager>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
zoneManager.SetRoot(hexMapRoot);
|
||||
zoneManager.SetMediator(hexGridMediator);
|
||||
zoneManager.SetHexMapManager(this);
|
||||
//
|
||||
buildingManager.SetMediator(hexGridMediator);
|
||||
//
|
||||
hexGridMediator.RegisterZoneManager(zoneManager);
|
||||
hexGridMediator.RegisterBuildingManager(buildingManager);
|
||||
_ = InitMapInfo();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.touchCount > 0 )
|
||||
{
|
||||
if (Input.GetTouch(0).phase == TouchPhase.Began)
|
||||
{
|
||||
CheckHit(Input.GetTouch(0).position);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 鼠标点击(PC)
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
CheckHit(Input.mousePosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_vCam)
|
||||
{
|
||||
_vCam.m_Lens.Orthographic = false;
|
||||
if (!Camera.main) return;
|
||||
var brain = Camera.main.GetComponent<CinemachineBrain>();
|
||||
brain?.ManualUpdate();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 检测点击或触摸位置击中了哪个物体
|
||||
/// </summary>
|
||||
private void CheckHit(Vector3 screenPosition)
|
||||
{
|
||||
if (mainCamera == null)
|
||||
mainCamera = Camera.main;
|
||||
var ray = mainCamera.ScreenPointToRay(screenPosition);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit))
|
||||
{
|
||||
Debug.Log("点击到了: " + hit.collider.name);
|
||||
var hexGridTiler = hit.collider.GetComponent<HexGridTiler> ();
|
||||
if (hexGridTiler)
|
||||
{
|
||||
hexGridTiler.OnHexCellClicked();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void UnlockZone(int zoneId)
|
||||
{
|
||||
var zone = zoneManager.GetZoneById(zoneId);
|
||||
if (zone != null && !zone.isUnlocked)
|
||||
{
|
||||
// if (GameManager.Instance.SpendMoney(zone.unlockCost))
|
||||
{
|
||||
zone.isUnlocked = true;
|
||||
zone.zoneObject.SetActive(true);
|
||||
terrainManager.UpdateTerrainForZone(zoneId);
|
||||
// 假设用 zoneId 推导 Chunk 坐标
|
||||
Vector2Int chunkCoord = new Vector2Int(zoneId % 3, zoneId / 3);
|
||||
// ChunkManager.Instance.UnlockChunk(chunkCoord);
|
||||
// cameraController.FocusOn(zone.zoneObject.transform.position);
|
||||
// FXManager.Instance.PlayUnlockEffect(zone.zoneObject.transform.position);
|
||||
//SaveMapState();
|
||||
SaveUserData();
|
||||
}
|
||||
}
|
||||
}
|
||||
private async Task InitMapInfo()
|
||||
{
|
||||
var mapInfo = mapName + "_info";
|
||||
_mapInfo = (HexGridMap)await Addressables.LoadAssetAsync<object>(mapInfo).Task;
|
||||
LoadMapInfo(_mapInfo);
|
||||
}
|
||||
|
||||
public void LoadMapInfo(HexGridMap mapInfo)
|
||||
{
|
||||
zoneManager.LoadZonesFromSave(mapInfo,_dataManager.UserMapData);
|
||||
}
|
||||
//
|
||||
public void SaveUserData()
|
||||
{
|
||||
_dataManager.SaveUserData();
|
||||
}
|
||||
//
|
||||
public void LoadUserData(string dataValue)
|
||||
{
|
||||
_dataManager.LoadUserData(dataValue);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user