using UnityEngine; public class sistemaInterno : ...

Creado el: 2 de marzo de 2025

Respondido usando O1 por Chat01

Pregunta

using UnityEngine;

public class sistemaInterno : MonoBehaviour
{
public ciclos scriptCiclos;
public recursos scriptRecursos;

text
public ofertaMercado scriptOfertaMercado; public demandaMercado scriptDemandaMercado; public preciosMercado scriptPreciosMercado; void OnEnable() { scriptCiclos.onCicloCompleto += VenderRecursos; } void OnDisable() { scriptCiclos.onCicloCompleto -= VenderRecursos; } private void VenderRecursos() { int cantidadMP1 = scriptRecursos.mP1; float precioMP1 = scriptPreciosMercado.precioFinalMP1; float dineroMP1 = precioMP1 * cantidadMP1; scriptRecursos.dinero += dineroMP1; scriptRecursos.mP1 = 0; Debug.Log($"[VentaRecursos] Vendido {cantidadMP1} de Grano a {precioMP1:0.00}. " + $"Dinero obtenido: {dineroMP1}. Dinero total: {scriptRecursos.dinero}"); int cantidadMP2 = scriptRecursos.mP2; float precioMP2 = scriptPreciosMercado.precioFinalMP2; float dineroMP2 = precioMP2 * cantidadMP2; scriptRecursos.dinero += dineroMP2; scriptRecursos.mP2 = 0; Debug.Log($"[VentaRecursos] Vendido {cantidadMP2} de Hortalizas a {precioMP2:0.00}. " + $"Dinero obtenido: {dineroMP2}. Dinero total: {scriptRecursos.dinero}"); int cantidadMP3 = scriptRecursos.mP3; float precioMP3 = scriptPreciosMercado.precioFinalMP3; float dineroMP3 = precioMP3 * cantidadMP3; scriptRecursos.dinero += dineroMP3; scriptRecursos.mP3 = 0; Debug.Log($"[VentaRecursos] Vendido {cantidadMP3} de Frutas a {precioMP3:0.00}. " + $"Dinero obtenido: {dineroMP3}. Dinero total: {scriptRecursos.dinero}"); }

}


modifica este script que estaba usando para que sea el GameManager que, cada fin de ciclo, active a los demas scripts (demanda, oferta, precios) y ademas cambie lo producido por el jugador por dinero (contenido en el script recursos. Algunas cosas ya no sirven por los cambios que hice en los otros scripts. Pego a continuacion los otros scripts para que los tengas en cuenta:


using UnityEngine;

public class demandaMercado : MonoBehaviour
{
public float poblacionTotal = 50f;
public float minDelta3 = -10f;
public float maxDelta3 = 10f;

text
public float[] demandaMP = new float[9]; public float[] consBaseMP = new float[9] { 0.75f, 0.70f, 0.60f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f, 0.00f }; void start() { for (int i = 0; i < demandaMP.Length; i++) { demandaMP[i] = consBaseMP[i] * poblacionTotal; } } public void CalcularDemanda() { float delta3 = Random.Range(minDelta3, maxDelta3); poblacionTotal += delta3; if (poblacionTotal < 0) poblacionTotal = 0; for (int i = 0; i < demandaMP.Length; i++) { float consumidores = consBaseMP[i] * poblacionTotal; float nuevaDemanda = consumidores; if (nuevaDemanda < 0) nuevaDemanda = 0; demandaMP[i] = nuevaDemanda; } Debug.Log(">>> DEMANDA RECALCULADA. " + "\nPoblación = " + poblacionTotal + "\nDemanda Grano = " + demandaMP[0] + "\nDemanda Hortaliza = " + demandaMP[1] + "\nDemanda Fruta = " + demandaMP[2]); }

}


using UnityEngine;

public class ofertaMercado : MonoBehaviour
{
public recursos scriptRecursos;

text
public float[] ofertaMP = new float[9]; // producción base de los NPCs public float[] prodBaseNPC1MP = new float[9] { 12, 3, 1, 0, 0, 0, 0, 0, 0 }; public float[] prodBaseNPC2MP = new float[9] { 6, 9, 2, 0, 0, 0, 0, 0, 0 }; public float[] prodBaseNPC3MP = new float[9] { 18, 6, 3, 0, 0, 0, 0, 0, 0 }; // variación en la produccón public float[] varProdNPC1MP = new float[9] { 12, 3, 1, 0, 0, 0, 0, 0, 0 }; public float[] varProdNPC2MP = new float[9] { 6, 9, 2, 0, 0, 0, 0, 0, 0 }; public float[] varProdNPC3MP = new float[9] { 18, 6, 3, 0, 0, 0, 0, 0, 0 }; // producción total de los NPCs public float[] produccionNPC1MP = new float[9]; public float[] produccionNPC2MP = new float[9]; public float[] produccionNPC3MP = new float[9]; public void CalcularOferta() { for (int i = 0; i < 9; i++) { float delta1 = Random.Range(-varProdNPC1MP[i], varProdNPC1MP[i]); produccionNPC1MP[i] = prodBaseNPC1MP[i] + delta1; if (produccionNPC1MP[i] < 0) produccionNPC1MP[i] = 0; float delta2 = Random.Range(-varProdNPC2MP[i], varProdNPC2MP[i]); produccionNPC2MP[i] = prodBaseNPC2MP[i] + delta2; if (produccionNPC2MP[i] < 0) produccionNPC2MP[i] = 0; float delta3 = Random.Range(-varProdNPC3MP[i], varProdNPC3MP[i]); produccionNPC3MP[i] = prodBaseNPC3MP[i] + delta3; if (produccionNPC3MP[i] < 0) produccionNPC3MP[i] = 0; } int[] prodJugadorMP = new int[] { scriptRecursos.mP1, scriptRecursos.mP2, scriptRecursos.mP3, scriptRecursos.mP4, scriptRecursos.mP5, scriptRecursos.mP6, scriptRecursos.mP7, scriptRecursos.mP8, scriptRecursos.mP9 }; for (int i = 0; i < 9; i++) { ofertaMP[i] = produccionNPC1MP[i] + produccionNPC2MP[i] + produccionNPC3MP[i] + prodJugadorMP[i]; } Debug.Log(">>>OFERTA RECALCULADA. " + "\nGrano = " + ofertaMP[0] + "\nHortalizas = " + ofertaMP[1] + "\nFruta = " + ofertaMP[2]); }

}


using UnityEngine;
using System;

public class preciosMercado : MonoBehaviour
{
public demandaMercado DemandaMercado;
public ofertaMercado OfertaMercado;

text
public float alfa = 0.1f; public float[] precioBase = new float[9] { 1f, 3f, 5f, 0f, 0f, 0f, 0f, 0f, 0f }; public float[] precioMercado = new float[9]; public void CalcularPreciosMP() { for (int i = 0; i < precioBase.Length; i++) { float demanda = DemandaMercado.demandaMP(i); float oferta = OfertaMercado.ofertaMP(i); float factor = 1f + alfa * (demanda - oferta); if (factor < 0) factor = 0; precioMercado[i] = precioBase[i] * factor; } Debug.Log("PRECIOS FINALES: " + "\nGrano = " + precioMercado[0] + "\nHortalizas = " + precioMercado[1] + "\nFrutas = " + precioMercado[2]); }

}

(tambien hay errores en el script de precios, si puedes corrigelos):
Assets\Scripts\inicio1\sistemaEconomico\mercado\preciosMercado.cs(17,44): error CS1955: Non-invocable member 'demandaMercado.demandaMP' cannot be used like a method.
Assets\Scripts\inicio1\sistemaEconomico\mercado\preciosMercado.cs(18,42): error CS1955: Non-invocable member 'ofertaMercado.ofertaMP' cannot be used like a method..


IMPORTANTE, SCRIPTS DE CICLOS Y RECURSOS:

using UnityEngine;
using System.Collections;
using System;

public class ciclos : MonoBehaviour
{
// ciclo productivo

text
public event Action onCicloCompleto; public float cicloProductivo = 30f; public int ciclosTotales = 0; private Coroutine cicloCorrutina; void Start() { cicloCorrutina = StartCoroutine(ciclosContador()); } IEnumerator ciclosContador() { while (true) { yield return new WaitForSeconds(cicloProductivo); ciclosTotales += 1; Debug.Log("Ciclo completado. Ciclos totales: " + ciclosTotales); onCicloCompleto.Invoke(); } } void OnDisable() { StopCoroutine(cicloCorrutina); }

}


using UnityEngine;

public class recursos : MonoBehaviour
{
// materias primas
public int mP1 = 0;
public int mP2 = 0;
public int mP3 = 0;
public int mP4 = 0;
public int mP5 = 0;
public int mP6 = 0;
public int mP7 = 0;
public int mP8 = 0;
public int mP9 = 0;

text
public int capacidadMP1 = 0; public int capacidadMP2 = 0; public int capacidadMP3 = 0; // bienes manufacturados public int bM1 = 0; public int bM2 = 0; public int bM3 = 0; public int bM4 = 0; public int bM5 = 0; public int bM6 = 0; // puntuación total del jugador public float dinero = 0f; public float conocimiento = 0f; public float fuerzaMilitar = 0f; // incremento de materias primas public void aumentarMP1(int cantidad) { mP1 = Mathf.Min(mP1 + cantidad, capacidadMP1); Debug.Log("Granos totales = " + mP1 + " / " + capacidadMP1); } public void aumentarMP2(int cantidad) { mP2 = Mathf.Min(mP2 + cantidad, capacidadMP2); Debug.Log("Hortalizas totales = " + mP2 + " / " + capacidadMP2); } public void aumentarMP3(int cantidad) { mP3 = Mathf.Min(mP3 + cantidad, capacidadMP3); Debug.Log("Frutas totales = " + mP3 + " / " + capacidadMP3); }

}

Pensamiento

Starting the game session

I’m laying out the scriptInternal game manager. This involves activating other scripts and converting harvested resources into money, all regulated by the cycles script.

Selling resources

I’m detailing how the internal script sells different resources—grain, vegetables, and fruits—using market prices to calculate total earnings. Each resource sale updates the total money count.

Correcting syntax

I’m addressing array access errors in the demandaMercado and OfertaMercado scripts, ensuring correct syntax and updating the CalcularPreciosMP function to calculate market prices based on demand and supply.

Aligning scripts

I’m compiling the GameManager script, which now sequentially calls specific functions from demandaMercado, ofertaMercado, and preciosMercado, and manages resource selling post-cycle.

Setting up the GameManager

It's coming together: the GameManager script now triggers demandas, offers, and prices functionalities, followed by resource selling. This structure ensures a clear, sequential approach to managing game progression.

Focusing on the essentials

I’m concentrating on the first three resources—grain, vegetables, and fruits—to ensure clarity and consistency, while considering a broader range if necessary.

Finalizing resource sales

I’m closing in on the GameManager script, ensuring it accurately sells grain, vegetables, and fruits, updating quantities, prices, and total money, while addressing the "preciosMercado" script error.

Calculating market prices

I’m fine-tuning the CalcularPreciosMP function to dynamically adjust market prices based on supply and demand, taking a closer look at factors like the demand and offer difference.

Reasoned about Unity Game Manager implementation for 25 seconds

Respuesta

A continuación tienes un ejemplo de cómo podrías refactorizar tu antiguo sistemaInterno para que funcione como un “GameManager”. Este se suscribe al evento de fin de ciclo, llama a los métodos de cálculo de demanda, oferta y precios, y finalmente vende los recursos del jugador.

GameManager (antes llamado sistemaInterno)

csharp
using UnityEngine; public class GameManager : MonoBehaviour { public ciclos scriptCiclos; public recursos scriptRecursos; public demandaMercado scriptDemandaMercado; public ofertaMercado scriptOfertaMercado; public preciosMercado scriptPreciosMercado; private void OnEnable() { // Nos suscribimos al evento para que, cuando termine el ciclo, // se ejecuten las acciones del mercado y la venta de recursos. scriptCiclos.onCicloCompleto += OnCicloCompleto; } private void OnDisable() { scriptCiclos.onCicloCompleto -= OnCicloCompleto; } private void OnCicloCompleto() { // 1) Recalcular Demanda scriptDemandaMercado.CalcularDemanda(); // 2) Recalcular Oferta scriptOfertaMercado.CalcularOferta(); // 3) Calcular Precios de mercado scriptPreciosMercado.CalcularPreciosMP(); // 4) Vender los recursos del jugador VenderRecursos(); } private void VenderRecursos() { // Vender MP1 (Grano) int cantidadMP1 = scriptRecursos.mP1; float precioMP1 = scriptPreciosMercado.precioMercado[0]; float dineroMP1 = precioMP1 * cantidadMP1; scriptRecursos.dinero += dineroMP1; scriptRecursos.mP1 = 0; Debug.Log($"[Venta] {cantidadMP1} Grano a {precioMP1:0.00} = {dineroMP1}. Dinero total: {scriptRecursos.dinero}"); // Vender MP2 (Hortalizas) int cantidadMP2 = scriptRecursos.mP2; float precioMP2 = scriptPreciosMercado.precioMercado[1]; float dineroMP2 = precioMP2 * cantidadMP2; scriptRecursos.dinero += dineroMP2; scriptRecursos.mP2 = 0; Debug.Log($"[Venta] {cantidadMP2} Hortalizas a {precioMP2:0.00} = {dineroMP2}. Dinero total: {scriptRecursos.dinero}"); // Vender MP3 (Frutas) int cantidadMP3 = scriptRecursos.mP3; float precioMP3 = scriptPreciosMercado.precioMercado[2]; float dineroMP3 = precioMP3 * cantidadMP3; scriptRecursos.dinero += dineroMP3; scriptRecursos.mP3 = 0; Debug.Log($"[Venta] {cantidadMP3} Frutas a {precioMP3:0.00} = {dineroMP3}. Dinero total: {scriptRecursos.dinero}"); } }

Correcciones en preciosMercado

En tu script de precios, la principal corrección es usar demandaMP[i] y ofertaMP[i] en lugar de tratarlos como métodos. Quedaría así:

csharp
using UnityEngine; public class preciosMercado : MonoBehaviour { public demandaMercado DemandaMercado; public ofertaMercado OfertaMercado; public float alfa = 0.1f; // Precios bases public float[] precioBase = new float[9] { 1f, 3f, 5f, 0f, 0f, 0f, 0f, 0f, 0f }; // Precios que se recalculan public float[] precioMercado = new float[9]; public void CalcularPreciosMP() { for (int i = 0; i < precioBase.Length; i++) { float demanda = DemandaMercado.demandaMP[i]; float oferta = OfertaMercado.ofertaMP[i]; float factor = 1f + alfa * (demanda - oferta); if (factor < 0) factor = 0; precioMercado[i] = precioBase[i] * factor; } Debug.Log("PRECIOS FINALES: " + "\nGrano = " + precioMercado[0] + "\nHortalizas = " + precioMercado[1] + "\nFrutas = " + precioMercado[2]); } }

Notas adicionales

  1. En el script demandaMercado, asegúrate de que el método sea void Start() con S mayúscula (Unity lo requiere para que se ejecute automáticamente).
  2. Ajusta y amplía la venta de recursos (por ejemplo, a mP4, mP5, etc.) según la cantidad de tipos de materias primas que realmente necesites transformar en dinero.
  3. Si necesitas seguir usando el mismo nombre de clase (sistemaInterno) en lugar de GameManager, simplemente cambia la cabecera de la clase y el nombre de archivo, pero la lógica interna es la misma.

Con estos cambios, tu “GameManager” (antes sistemaInterno) se encargará de orquestar el flujo al final de cada ciclo: recalculando la Demanda, la Oferta, actualizando Precios y, por último, vendiendo automáticamente lo producido por el jugador.

Compartir este Q&A