using UnityEngine; using System; using System.Coll...
創建於:2025年8月17日
使用 GPT-5 Thinking 回答 Chat01
創建於:2025年8月17日
使用 GPT-5 Thinking 回答 Chat01
using UnityEngine;
using System;
using System.Collections.Generic;
public class recursos : MonoBehaviour
{
public event Action<int> aumentoMP;
textprivate int capacidadGlobal; private Dictionary<int, (int cantidad, int capacidad, bool primerAumento)> materiasPrimas = new Dictionary<int, (int, int, bool)>() { { 1, (0, 0, false) }, { 2, (0, 0, false) }, { 3, (0, 0, false) }, { 4, (0, 0, false) }, { 5, (0, 0, false) }, { 6, (0, 0, false) }, { 7, (0, 0, false) }, { 8, (0, 0, false) }, { 9, (0, 0, false) }, { 10, (0, 0, false) }, { 11, (0, 0, false) }, { 12, (0, 0, false) } }; public float dinero = 0f; public float fuerza = 0f; public float conocimiento = 0f; public void AumentarMP(int id, int cantidad) { var mp = materiasPrimas[id]; int suma = mp.cantidad + cantidad; if (suma > mp.capacidad) suma = mp.capacidad; if (!mp.primerAumento) { mp.primerAumento = true; aumentoMP?.Invoke(id); } mp.cantidad = suma; materiasPrimas[id] = mp; } public void DisminuirMP(int id, int cantidad) { var mp = materiasPrimas[id]; mp.cantidad -= cantidad; if (mp.cantidad < 0) mp.cantidad = 0; materiasPrimas[id] = mp; }
}
quiero que al aumentar un mp, se compruebe primero la capacidad individual de ese mp (ya lo hice, si hay espacio lo aumenta normalmente) y si no hay espacio que compruebe la capacidad global y aumente si hay espacio ahi. Si la cantidad a sumar excede ambas capacidades, el restante no se suma y se descarta.
¡Listo! Aquí tienes una implementación que respeta:
aumentoMP
se dispara la primera vez que ese MP aumenta (ya sea en su slot individual o en el global).Nota: para que el “global” no rompa el límite individual, guardo el excedente en un diccionario
almacenGlobal
separado. Así puedes consultar el total real de un MP comocantidadIndividual + cantidadEnGlobal
.
csharpusing UnityEngine; using System; using System.Collections.Generic; public class recursos : MonoBehaviour { public event Action<int> aumentoMP; [SerializeField] private int capacidadGlobal = 0; // cantidad, capacidad, primerAumento private Dictionary<int, (int cantidad, int capacidad, bool primerAumento)> materiasPrimas = new Dictionary<int, (int, int, bool)>() { { 1, (0, 0, false) }, { 2, (0, 0, false) }, { 3, (0, 0, false) }, { 4, (0, 0, false) }, { 5, (0, 0, false) }, { 6, (0, 0, false) }, { 7, (0, 0, false) }, { 8, (0, 0, false) }, { 9, (0, 0, false) }, { 10,(0, 0, false) }, { 11,(0, 0, false) }, { 12,(0, 0, false) } }; // Almacén global (overflow) por tipo de MP private Dictionary<int, int> almacenGlobal = new Dictionary<int, int>() { { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 }, { 5, 0 }, { 6, 0 }, { 7, 0 }, { 8, 0 }, { 9, 0 }, { 10,0 }, { 11,0 }, { 12,0 } }; public float dinero = 0f; public float fuerza = 0f; public float conocimiento = 0f; // --- Helpers --- private int OcupacionGlobalActual() { int total = 0; foreach (var kv in almacenGlobal) total += kv.Value; return total; } public int EspacioGlobalDisponible() { int disp = capacidadGlobal - OcupacionGlobalActual(); return disp > 0 ? disp : 0; } public int GetCantidadIndividual(int id) => materiasPrimas[id].cantidad; public int GetCantidadGlobal(int id) => almacenGlobal.TryGetValue(id, out var v) ? v : 0; public int GetCantidadTotal(int id) => GetCantidadIndividual(id) + GetCantidadGlobal(id); // --- Lógica principal --- public void AumentarMP(int id, int cantidad) { if (cantidad <= 0 || !materiasPrimas.ContainsKey(id)) return; var mp = materiasPrimas[id]; int aumentosTotales = 0; // 1) Intentar en capacidad individual int espacioIndividual = Math.Max(0, mp.capacidad - mp.cantidad); int aIndividual = Math.Min(cantidad, espacioIndividual); if (aIndividual > 0) { mp.cantidad += aIndividual; cantidad -= aIndividual; aumentosTotales += aIndividual; } // 2) Si queda resto, intentar en capacidad global (overflow) if (cantidad > 0) { int espacioGlobal = EspacioGlobalDisponible(); int aGlobal = Math.Min(cantidad, espacioGlobal); if (aGlobal > 0) { almacenGlobal[id] = GetCantidadGlobal(id) + aGlobal; cantidad -= aGlobal; aumentosTotales += aGlobal; } } // 3) Disparar evento solo la primera vez que este MP aumente algo if (aumentosTotales > 0 && !mp.primerAumento) { mp.primerAumento = true; aumentoMP?.Invoke(id); } materiasPrimas[id] = mp; // Si 'cantidad' > 0 aquí, significa que no cabía ni en individual ni en global -> descartado. } public void DisminuirMP(int id, int cantidad) { if (cantidad <= 0 || !materiasPrimas.ContainsKey(id)) return; var mp = materiasPrimas[id]; // 1) Restar del slot individual int desdeIndividual = Math.Min(cantidad, mp.cantidad); mp.cantidad -= desdeIndividual; cantidad -= desdeIndividual; // 2) Si aún falta por restar, consumir del global if (cantidad > 0) { int stockGlobal = GetCantidadGlobal(id); int desdeGlobal = Math.Min(cantidad, stockGlobal); almacenGlobal[id] = stockGlobal - desdeGlobal; cantidad -= desdeGlobal; } // (Si todavía queda 'cantidad' > 0, significa que no había suficiente stock total; se ignora.) materiasPrimas[id] = mp; } }
capacidadIndividual[id]=10
, capacidadGlobal=5
.AumentarMP(id, 3)
→ individual no tiene espacio, global sí (5): guarda 3 en global.AumentarMP(id, 7)
→ global tenía 2 libres (5 totales - 3 usados = 2): guarda 2 y descarta 5.Si prefieres que el límite global se aplique a todo (individual + global) en vez de usar un “overflow” separado, dímelo y te paso la variante donde el total de todos los MPs no puede superar capacidadGlobal
.