Files
test/Assets/PerfectWorld/Scripts/Objet/CECMatter.cs
T
2025-12-17 12:36:50 +07:00

140 lines
5.7 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;
namespace PerfectWorld.Scripts
{
public class CECMatter : CECObject
{
// 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)
{
fileMatterField = matterType.GetField("file_model", 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<Renderer>().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;
}
private new void Update()
{
bool inputPressed = false;
Vector2 screenPosition = Vector2.zero;
// Check for touch input (mobile)
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
inputPressed = true;
screenPosition = touch.position;
}
}
// Check for mouse input (desktop)
else if (Input.GetMouseButtonDown(0))
{
inputPressed = true;
screenPosition = Input.mousePosition;
}
if (inputPressed)
{
Camera mainCamera = Camera.main;
if (mainCamera == null)
return;
Ray ray = mainCamera.ScreenPointToRay(screenPosition);
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit hit in hits)
{
if (hit.collider.gameObject == this.gameObject ||
hit.collider.transform.IsChildOf(this.transform))
{
Debug.Log($"CECMatter::RaycastHit():: mid: {m_MatterInfo.mid}");
UnityGameSession.RequestPickupItem(m_MatterInfo.mid, m_MatterInfo.tid);
break;
}
}
}
}
}
}