2023-06-21 00:59:14 +03:00

111 lines
3.5 KiB
C#

using Comfort.Common;
using EFT;
using EFT.Interactive;
using System.Collections.Generic;
using System.Threading.Tasks;
using Aki.Custom.Airdrops.Utils;
using UnityEngine;
namespace SamSWAT.HeliCrash
{
public class HeliCrash : MonoBehaviour
{
private AssetBundle _heliBundle;
public async void Init(string location)
{
var heliLocation = GetHeliCrashLocation(location);
var choppa = Instantiate(await LoadChoppaAsync(), heliLocation.Position, Quaternion.Euler(heliLocation.Rotation));
var container = choppa.GetComponentInChildren<LootableContainer>();
var itemCrate = Utils.CreateItem("goofyahcontainer", "6223349b3136504a544d1608");
LootItem.CreateLootContainer(container, itemCrate, "Heavy crate", Singleton<GameWorld>.Instance);
new ItemFactoryUtil().AddLoot(container);
}
private void OnDestroy()
{
_heliBundle.Unload(true);
}
private Location GetHeliCrashLocation(string location)
{
switch (location)
{
case "bigmap":
{
return PickRandom(Plugin.HeliCrashLocations.Customs);
}
case "Interchange":
{
return PickRandom(Plugin.HeliCrashLocations.Interchange);
}
case "RezervBase":
{
return PickRandom(Plugin.HeliCrashLocations.Rezerv);
}
case "Shoreline":
{
return PickRandom(Plugin.HeliCrashLocations.Shoreline);
}
case "Woods":
{
return PickRandom(Plugin.HeliCrashLocations.Woods);
}
case "Lighthouse":
{
return PickRandom(Plugin.HeliCrashLocations.Lighthouse);
}
case "TarkovStreets":
{
return PickRandom(Plugin.HeliCrashLocations.StreetsOfTarkov);
}
case "develop":
{
return PickRandom(Plugin.HeliCrashLocations.Develop);
}
default: return new Location();
}
}
private async Task<GameObject> LoadChoppaAsync()
{
string path = Plugin.Directory + "Assets/Content/Vehicles/sikorsky_uh60_blackhawk.bundle";
var bundleLoadRequest = AssetBundle.LoadFromFileAsync(path);
while (!bundleLoadRequest.isDone)
await Task.Yield();
_heliBundle = bundleLoadRequest.assetBundle;
if (_heliBundle == null)
{
Debug.LogError("[SamSWAT.HeliCrash]: Can't load UH-60 Blackhawk bundle");
return null;
}
var assetLoadRequest = _heliBundle.LoadAllAssetsAsync<GameObject>();
while (!assetLoadRequest.isDone)
await Task.Yield();
var requestedGO = assetLoadRequest.allAssets[0] as GameObject;
if (requestedGO == null)
{
Debug.LogError("[SamSWAT.HeliCrash]: failed to load asset");
return null;
}
return requestedGO;
}
private T PickRandom<T>(List<T> list)
{
return list[Random.Range(0, list.Count)];
}
}
}