181 lines
6.2 KiB
C#
181 lines
6.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public enum GameMode { None, Career, Free }
|
||
|
||
/// <summary>
|
||
/// Главный менеджер игры и экономики перекупа.
|
||
/// isDiplomaVersion = true — «Дипломный проект»: бюджет, ДВС и жидкости, диагностика, торг, магазин, склад «Куфар», продажа с прибылью.
|
||
/// isDiplomaVersion = false — «Курсовая работа»: авторынок, GPS, мойка, колеса с домкратом, кузовной ремонт и тест-драйв.
|
||
/// </summary>
|
||
public class GameEconomyManager : MonoBehaviour
|
||
{
|
||
public static GameEconomyManager Instance { get; private set; }
|
||
|
||
[Header("Версия проекта")]
|
||
[Tooltip("true — полный Дипломный проект, false — базовая Курсовая работа")]
|
||
public bool isDiplomaVersion = true;
|
||
|
||
[Header("Экономика")]
|
||
[SerializeField] private float startCapitalUsd = 5000f;
|
||
[SerializeField] private float bynPerUsd = 3.27f;
|
||
[SerializeField] private float autosalonGoalUsd = 25000f;
|
||
|
||
[Header("Гараж игрока")]
|
||
public Transform garageCenter;
|
||
public float garageRadius = 24f;
|
||
public Transform garageCarSpot;
|
||
public Transform deliveryPoint;
|
||
public Transform playerSpawnPoint;
|
||
|
||
public GameMode Mode { get; private set; } = GameMode.None;
|
||
public float Money { get; private set; }
|
||
public int DealsCompleted { get; private set; }
|
||
public float TotalProfit { get; private set; }
|
||
public bool AutosalonOpened { get; private set; }
|
||
public float AutosalonGoal => autosalonGoalUsd;
|
||
public float StartCapital => startCapitalUsd;
|
||
|
||
/// <summary>Свободный режим: деньги не ограничены.</summary>
|
||
public bool Unlimited => Mode != GameMode.Career;
|
||
public bool DiplomaFeatures => isDiplomaVersion;
|
||
|
||
public event Action<float> OnMoneyChanged;
|
||
public event Action OnAutosalonOpened;
|
||
|
||
private readonly List<string> history = new List<string>();
|
||
public IReadOnlyList<string> History => history;
|
||
|
||
private void Awake()
|
||
{
|
||
if (Instance != null && Instance != this)
|
||
{
|
||
Destroy(this);
|
||
return;
|
||
}
|
||
Instance = this;
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
if (Instance == this) Instance = null;
|
||
}
|
||
|
||
public void StartGame(GameMode mode)
|
||
{
|
||
Mode = mode;
|
||
Money = mode == GameMode.Career ? startCapitalUsd : 0f;
|
||
DealsCompleted = 0;
|
||
TotalProfit = 0f;
|
||
AutosalonOpened = false;
|
||
history.Clear();
|
||
OnMoneyChanged?.Invoke(Money);
|
||
}
|
||
|
||
public string FormatMoney(float usd)
|
||
{
|
||
string sign = usd < 0 ? "-" : "";
|
||
float a = Mathf.Abs(usd);
|
||
return $"{sign}${a:N0} ({sign}{a * bynPerUsd:N0} BYN)";
|
||
}
|
||
|
||
public string FormatShort(float usd) => (usd < 0 ? "-$" : "$") + Mathf.Abs(usd).ToString("N0");
|
||
|
||
public bool CanAfford(float usd) => Unlimited || Money + 0.01f >= usd;
|
||
|
||
/// <summary>Списать деньги. Затраты автоматически записываются на машину (для отчета о прибыли).</summary>
|
||
public bool TrySpend(float usd, string reason, CarOwnership forCar = null, bool attributeToCar = true)
|
||
{
|
||
if (usd <= 0f) return true;
|
||
if (!CanAfford(usd))
|
||
{
|
||
GameHUD.Notify($"Недостаточно средств! Нужно {FormatShort(usd)}, на счету {FormatShort(Money)}", GameUI.Red);
|
||
ProceduralAudioManager.Instance?.Play(SfxId.Error);
|
||
return false;
|
||
}
|
||
|
||
if (!Unlimited)
|
||
{
|
||
Money -= usd;
|
||
OnMoneyChanged?.Invoke(Money);
|
||
}
|
||
|
||
if (attributeToCar)
|
||
{
|
||
if (forCar == null) forCar = FindCarForExpense();
|
||
if (forCar != null) forCar.AddExpense(usd, reason);
|
||
}
|
||
|
||
history.Add($"−{FormatShort(usd)} {reason}");
|
||
ProceduralAudioManager.Instance?.Play(SfxId.Cash);
|
||
return true;
|
||
}
|
||
|
||
public void Earn(float usd, string reason)
|
||
{
|
||
if (usd <= 0f) return;
|
||
if (!Unlimited)
|
||
{
|
||
Money += usd;
|
||
OnMoneyChanged?.Invoke(Money);
|
||
}
|
||
history.Add($"+{FormatShort(usd)} {reason}");
|
||
ProceduralAudioManager.Instance?.Play(SfxId.Cash);
|
||
CheckAutosalonGoal();
|
||
}
|
||
|
||
public void RegisterDeal(float profit)
|
||
{
|
||
DealsCompleted++;
|
||
TotalProfit += profit;
|
||
CheckAutosalonGoal();
|
||
}
|
||
|
||
private void CheckAutosalonGoal()
|
||
{
|
||
if (AutosalonOpened || Mode != GameMode.Career) return;
|
||
if (Money >= autosalonGoalUsd)
|
||
{
|
||
AutosalonOpened = true;
|
||
OnAutosalonOpened?.Invoke();
|
||
}
|
||
}
|
||
|
||
public bool IsInGarage(Vector3 worldPos)
|
||
{
|
||
if (garageCenter == null) return true;
|
||
Vector3 d = worldPos - garageCenter.position;
|
||
d.y = 0f;
|
||
return d.magnitude <= garageRadius;
|
||
}
|
||
|
||
/// <summary>Машина игрока, на которую списываются расходы (ближайшая к гаражу купленная).</summary>
|
||
public CarOwnership FindCarForExpense()
|
||
{
|
||
CarOwnership best = null;
|
||
float bestDist = float.MaxValue;
|
||
Vector3 center = garageCenter != null ? garageCenter.position : Vector3.zero;
|
||
|
||
foreach (CarOwnership c in CarOwnership.All)
|
||
{
|
||
if (c == null || !c.isOwned) continue;
|
||
float d = Vector3.Distance(c.transform.position, center);
|
||
if (d < bestDist)
|
||
{
|
||
bestDist = d;
|
||
best = c;
|
||
}
|
||
}
|
||
return best;
|
||
}
|
||
|
||
public Vector3 GetDeliveryPosition(int index = 0)
|
||
{
|
||
Vector3 basePos = deliveryPoint != null ? deliveryPoint.position : (garageCenter != null ? garageCenter.position + Vector3.up : Vector3.up);
|
||
Vector3 right = deliveryPoint != null ? deliveryPoint.right : Vector3.right;
|
||
int slot = index % 5;
|
||
return basePos + right * ((slot - 2) * 0.45f) + Vector3.up * (0.25f + (index / 5) * 0.35f);
|
||
}
|
||
}
|