116 lines
4.4 KiB
C#
116 lines
4.4 KiB
C#
using System.Collections;
|
|||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Такси [T]: если договорились о встрече — довезет к продавцу, иначе — обратно в гараж.
|
||
|
|
/// Экран плавно затемняется, игрок переносится на место. В карьере поездка стоит денег.
|
||
|
|
/// </summary>
|
||
|
|
public class TaxiService : MonoBehaviour
|
||
|
|
{
|
||
|
|
[SerializeField] private KeyCode key = KeyCode.T;
|
||
|
|
[SerializeField] private float price = 10f;
|
||
|
|
[SerializeField] private float minDistance = 40f;
|
||
|
|
|
||
|
|
private float fade;
|
||
|
|
private bool busy;
|
||
|
|
private string caption;
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
if (busy || !Input.GetKeyDown(key) || GameInputLock.IsLocked || VehicleInteraction.Current != null) return;
|
||
|
|
MainMenuAndBootManager menu = MainMenuAndBootManager.Instance;
|
||
|
|
if (menu != null && !menu.IsPlaying) return;
|
||
|
|
|
||
|
|
PlayerMovement pm = FindAnyObjectByType<PlayerMovement>();
|
||
|
|
if (pm == null) return;
|
||
|
|
|
||
|
|
if (!TryGetDestination(pm.transform.position, out Vector3 dest, out Quaternion rot, out string name))
|
||
|
|
{
|
||
|
|
GameHUD.Notify("Такси не нужно: вы уже на месте", GameUI.Muted);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
GameEconomyManager eco = GameEconomyManager.Instance;
|
||
|
|
if (eco != null && !eco.TrySpend(price, "Такси", null, false)) return;
|
||
|
|
StartCoroutine(Ride(pm, dest, rot, name));
|
||
|
|
}
|
||
|
|
|
||
|
|
private bool TryGetDestination(Vector3 from, out Vector3 dest, out Quaternion rot, out string name)
|
||
|
|
{
|
||
|
|
dest = Vector3.zero;
|
||
|
|
rot = Quaternion.identity;
|
||
|
|
name = "";
|
||
|
|
|
||
|
|
CarMarketManager market = FindAnyObjectByType<CarMarketManager>();
|
||
|
|
CarListing meeting = market != null ? market.ActiveMeetingListing : null;
|
||
|
|
if (meeting != null && meeting.spawnedInstance != null)
|
||
|
|
{
|
||
|
|
CarOwnership own = meeting.spawnedInstance.GetComponent<CarOwnership>();
|
||
|
|
Transform car = meeting.spawnedInstance.transform;
|
||
|
|
if ((own == null || !own.isOwned) && Vector3.Distance(from, car.position) > minDistance)
|
||
|
|
{
|
||
|
|
dest = car.position - car.right * 3.2f + car.forward * 1.5f + Vector3.up * 1.1f;
|
||
|
|
Vector3 look = car.position - dest;
|
||
|
|
look.y = 0f;
|
||
|
|
rot = Quaternion.LookRotation(look);
|
||
|
|
name = "к продавцу";
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
GameEconomyManager eco = GameEconomyManager.Instance;
|
||
|
|
if (eco != null && eco.playerSpawnPoint != null && Vector3.Distance(from, eco.playerSpawnPoint.position) > minDistance)
|
||
|
|
{
|
||
|
|
dest = eco.playerSpawnPoint.position;
|
||
|
|
rot = eco.playerSpawnPoint.rotation;
|
||
|
|
name = "в гараж";
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator Ride(PlayerMovement pm, Vector3 dest, Quaternion rot, string name)
|
||
|
|
{
|
||
|
|
busy = true;
|
||
|
|
caption = $"Такси едет {name}...";
|
||
|
|
GameInputLock.Lock(this);
|
||
|
|
ProceduralAudioManager.Instance?.Play(SfxId.Starter, null, 0.5f, 1.2f);
|
||
|
|
|
||
|
|
for (float t = 0f; t < 0.6f; t += Time.unscaledDeltaTime)
|
||
|
|
{
|
||
|
|
fade = t / 0.6f;
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
fade = 1f;
|
||
|
|
|
||
|
|
CharacterController cc = pm.GetComponent<CharacterController>();
|
||
|
|
if (cc != null) cc.enabled = false;
|
||
|
|
pm.transform.SetPositionAndRotation(dest, rot);
|
||
|
|
if (cc != null) cc.enabled = true;
|
||
|
|
PlayerLook look = pm.GetComponentInChildren<PlayerLook>();
|
||
|
|
if (look != null) look.ResetPitch(8f);
|
||
|
|
|
||
|
|
yield return new WaitForSecondsRealtime(0.5f);
|
||
|
|
for (float t = 0f; t < 0.6f; t += Time.unscaledDeltaTime)
|
||
|
|
{
|
||
|
|
fade = 1f - t / 0.6f;
|
||
|
|
yield return null;
|
||
|
|
}
|
||
|
|
fade = 0f;
|
||
|
|
GameInputLock.Unlock(this);
|
||
|
|
GameHUD.Notify($"Такси довезло вас {name}", GameUI.Accent);
|
||
|
|
busy = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnGUI()
|
||
|
|
{
|
||
|
|
if (fade <= 0.001f) return;
|
||
|
|
GameUI.Begin(-30);
|
||
|
|
float w = GameUI.VirtualWidth, h = GameUI.VirtualHeight;
|
||
|
|
GameUI.DrawRect(new Rect(0, 0, w, h), new Color(0.03f, 0.03f, 0.04f, fade));
|
||
|
|
Color prev = GUI.color;
|
||
|
|
GUI.color = new Color(1, 1, 1, fade);
|
||
|
|
GUI.Label(new Rect(0, h * 0.5f - 30, w, 60), caption, new GUIStyle(GameUI.Title) { fontSize = 34 });
|
||
|
|
GUI.color = prev;
|
||
|
|
}
|
||
|
|
}
|