Files
test/Assets/PerfectWorld/Scripts/Objet/CECMatter.cs
T
2025-11-25 12:10:43 +07:00

98 lines
4.2 KiB
C#

using System.Reflection;
using System.Threading.Tasks;
using BrewMonster;
using BrewMonster.Network;
using BrewMonster.Scripts;
using CSNetwork.GPDataType;
using ModelRenderer.Scripts.Common;
using PerfectWorld.Scripts.Managers;
using UnityEngine;
using UnityEngine.EventSystems;
namespace PerfectWorld.Scripts
{
public class CECMatter : CECObject, IPointerDownHandler
{
// Matter information got from server
public struct INFO
{
public int mid; // Matter id
public int tid; // Template id
}
protected EC_ManMatter m_pMatterMan;
protected INFO m_MatterInfo;
public void SetMatterInfo(INFO matterInfo)
{
m_MatterInfo = matterInfo;
}
public static async Task<CECMatter> Init(info_matter Info)
{
INFO matterInfo = new INFO();
matterInfo.mid = Info.mid;
matterInfo.tid = Info.tid & 0x0000ffff;
// get the matter template from elementdataman
DATA_TYPE DataType = DATA_TYPE.DT_INVALID;
var matterData = ElementDataManProvider.GetElementDataMan().get_data_ptr((uint)matterInfo.tid, ID_SPACE.ID_SPACE_ESSENCE, ref DataType);
if (matterData != null)
{
var matterType = matterData.GetType();
var fileMatterField = matterType.GetField("file_matter", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (fileMatterField != null)
{
var fileMatterValue = fileMatterField.GetValue(matterData);
string filePath = ByteToStringUtils.ByteArrayToCP936String((byte[])fileMatterValue);
var matterPrefab = await AddressableManager.Instance.LoadPrefabAsync(AFile.NormalizePath(filePath.ToLower(), true));
if (matterPrefab != null)
{
var matterObject = Instantiate(matterPrefab);
matterObject.transform.position = new Vector3(Info.pos.x, Info.pos.y, Info.pos.z);
matterObject.transform.localScale = new Vector3(1f, 1f, 1f);
matterObject.transform.localRotation = Quaternion.identity;
matterObject.SetActive(true);
// Add a collider if it doesn't have one
if (matterObject.GetComponent<Collider>() == null)
{
var collider = matterObject.AddComponent<BoxCollider>();
collider.size = matterObject.GetComponentInChildren<MeshRenderer>().bounds.size;
}
// Create text object to display item name above the cube
// CreateItemNameText(matterObject, info.tid);
// Add a script to handle click events
// MatterCubeClickHandler clickHandler = matterObject.AddComponent<MatterCubeClickHandler>();
// clickHandler.Initialize(Info.mid, this);
CECMatter matterScript = matterObject.AddComponent<CECMatter>();
matterScript.SetMatterInfo(matterInfo);
// Store reference to the cube
// matterGameObjects[info.mid] = matterObject;
return matterScript;
}
else
{
Debug.LogWarning($"Failed to load matter prefab from path: {filePath}");
}
}
else
{
Debug.LogWarning($"file_matter field not found on matter data type {matterType.FullName}");
}
}
return null;
}
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log($"CECMatter::OnPointerDown():: mid: {m_MatterInfo.mid}");
UnityGameSession.RequestPickupItem(m_MatterInfo.mid, m_MatterInfo.tid);
}
}
}