Files
test/Assets/PerfectWorld/Scripts/UI/pickupItem.cs
T
2025-10-03 15:39:04 +07:00

312 lines
9.0 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using BrewMonster.Network;
using PerfectWorld.Scripts.Managers.BrewMonster.Managers;
using CSNetwork.GPDataType;
using System.Collections.Generic;
using BrewMonster;
public class pickupItem : MonoBehaviour
{
public InputField mid;
public InputField tid;
public Button send;
// Dictionary to store created cubes with their matter IDs
private Dictionary<int, GameObject> matterCubes = new Dictionary<int, GameObject>();
// Dictionary to track pending pickups by TID
private Dictionary<int, int> pendingPickups = new Dictionary<int, int>(); // TID -> MatterID
// Reference to the matter manager
private EC_ManMatter matterManager;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
// Add button click listener
if (send != null)
{
send.onClick.AddListener(OnPickupButtonClicked);
}
// Get reference to matter manager
if (EC_ManMessageMono.Instance != null)
{
matterManager = EC_ManMessageMono.Instance.GetECManMatter;
}
else
{
Debug.LogError("EC_ManMessageMono.Instance is null - matter manager not available");
}
// Subscribe to matter enter world events
SubscribeToMatterEvents();
}
// Update is called once per frame
void Update()
{
// Check for mouse clicks on cubes
if (Input.GetMouseButtonDown(0))
{
HandleCubeClick();
}
}
private void SubscribeToMatterEvents()
{
// We'll need to hook into the matter manager's events
// For now, we'll check for new matters in Update
}
private void HandleCubeClick()
{
// Find the main camera
Camera mainCamera = Camera.main;
if (mainCamera == null)
{
mainCamera = FindFirstObjectByType<Camera>();
}
if (mainCamera == null)
{
Debug.LogWarning("No camera found for raycast");
return;
}
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject clickedObject = hit.collider.gameObject;
// Check if the clicked object is one of our matter cubes
foreach (var kvp in matterCubes)
{
if (kvp.Value == clickedObject)
{
int matterId = kvp.Key;
SendPickupCommand(matterId);
break;
}
}
}
}
public void SendPickupCommand(int matterId)
{
if (matterManager != null)
{
var matterData = matterManager.GetMatterData(matterId);
if (matterData.HasValue)
{
// Track this pickup request
pendingPickups[matterData.Value.tid] = matterId;
UnityGameSession.RequestPickupItem(matterData.Value.mid, matterData.Value.tid);
Debug.Log($"Pickup request sent for matter - MID: {matterData.Value.mid}, TID: {matterData.Value.tid}");
}
else
{
Debug.LogWarning($"No matter data found for matter ID: {matterId}");
}
}
else
{
Debug.LogError("Matter manager is null - cannot send pickup command");
}
}
public void CreateMatterCube(int matterId)
{
if (matterManager == null) return;
var matterData = matterManager.GetMatterData(matterId);
if (!matterData.HasValue) return;
// Check if cube already exists
if (matterCubes.ContainsKey(matterId))
{
Debug.Log($"Cube for matter {matterId} already exists");
return;
}
// Create cube GameObject
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.name = $"Matter_{matterData.Value.mid}_TID_{matterData.Value.tid}";
// Position the cube based on matter position
Vector3 position = new Vector3(
matterData.Value.pos.x,
matterData.Value.pos.y,
matterData.Value.pos.z
);
cube.transform.position = position;
// Add a collider if it doesn't have one
if (cube.GetComponent<Collider>() == null)
{
cube.AddComponent<BoxCollider>();
}
// Add a script to handle click events
MatterCubeClickHandler clickHandler = cube.AddComponent<MatterCubeClickHandler>();
clickHandler.Initialize(matterId, this);
// Store reference to the cube
matterCubes[matterId] = cube;
Debug.Log($"Created cube for matter {matterId} at position {position}");
}
public void RemoveMatterCube(int matterId)
{
if (matterCubes.ContainsKey(matterId))
{
GameObject cube = matterCubes[matterId];
if (cube != null)
{
Destroy(cube);
}
matterCubes.Remove(matterId);
Debug.Log($"Removed cube for matter {matterId}");
}
}
public void UpdateMatterCubes()
{
if (matterManager == null) return;
// Get all current matter IDs
int[] allMatterIds = matterManager.GetAllMatterIds();
// Create cubes for new matters
foreach (int matterId in allMatterIds)
{
if (!matterCubes.ContainsKey(matterId))
{
CreateMatterCube(matterId);
}
}
// Remove cubes for matters that no longer exist
List<int> cubesToRemove = new List<int>();
foreach (var kvp in matterCubes)
{
if (!matterManager.HasMatterData(kvp.Key))
{
cubesToRemove.Add(kvp.Key);
}
}
foreach (int matterId in cubesToRemove)
{
RemoveMatterCube(matterId);
}
}
/// <summary>
/// Called when a pickup is successful. Removes the cube for the picked up item.
/// </summary>
/// <param name="tid">Template ID of the picked up item</param>
public void OnPickupSuccess(int tid)
{
if (pendingPickups.ContainsKey(tid))
{
int matterId = pendingPickups[tid];
// Remove the cube
RemoveMatterCube(matterId);
// Remove from pending pickups
pendingPickups.Remove(tid);
Debug.Log($"Successfully picked up item with TID: {tid}, removed cube for matter ID: {matterId}");
}
else
{
Debug.LogWarning($"Received pickup success for TID {tid} but no pending pickup found");
}
}
/// <summary>
/// Called when a pickup fails. Removes the pending pickup tracking.
/// </summary>
/// <param name="tid">Template ID of the failed pickup</param>
public void OnPickupFailed(int tid)
{
if (pendingPickups.ContainsKey(tid))
{
pendingPickups.Remove(tid);
Debug.Log($"Pickup failed for TID: {tid}, removed from pending pickups");
}
}
private void OnPickupButtonClicked()
{
// Validate input fields
if (mid == null || tid == null)
{
Debug.LogError("PickupItem: Mid or Tid InputField is not assigned!");
return;
}
// Parse the input values
if (int.TryParse(mid.text, out int midValue) && int.TryParse(tid.text, out int tidValue))
{
// Call the pickup item request
UnityGameSession.RequestPickupItem(midValue, tidValue);
Debug.Log($"Pickup request sent - MID: {midValue}, TID: {tidValue}");
}
else
{
Debug.LogError("PickupItem: Invalid input values. Please enter valid integers for MID and TID.");
}
}
private void OnDestroy()
{
// Clean up button listener
if (send != null)
{
send.onClick.RemoveListener(OnPickupButtonClicked);
}
// Clean up all matter cubes
foreach (var kvp in matterCubes)
{
if (kvp.Value != null)
{
Destroy(kvp.Value);
}
}
matterCubes.Clear();
// Clean up pending pickups
pendingPickups.Clear();
}
}
// Helper class to handle click events on matter cubes
public class MatterCubeClickHandler : MonoBehaviour
{
private int matterId;
private pickupItem pickupItemScript;
public void Initialize(int id, pickupItem script)
{
matterId = id;
pickupItemScript = script;
}
private void OnMouseDown()
{
if (pickupItemScript != null)
{
pickupItemScript.SendPickupCommand(matterId);
}
}
}