284 lines
13 KiB
C#
284 lines
13 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Scripting;
|
|
|
|
[Preserve]
|
|
public class ItemActionSpawnVehicleRebirth : ItemAction
|
|
{
|
|
public override ItemActionData CreateModifierData(ItemInventoryData _invData, int _indexInEntityOfAction)
|
|
{
|
|
return new ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle(_invData, _indexInEntityOfAction);
|
|
}
|
|
|
|
public override void ReadFrom(DynamicProperties _props)
|
|
{
|
|
base.ReadFrom(_props);
|
|
if (_props.Values.ContainsKey("Vehicle"))
|
|
{
|
|
this.entityToSpawn = _props.Values["Vehicle"];
|
|
}
|
|
this.vehicleSize = new Vector3(1f, 1.9f, 2f);
|
|
if (_props.Values.ContainsKey("VehicleSize"))
|
|
{
|
|
this.vehicleSize = StringParsers.ParseVector3(_props.Values["VehicleSize"], 0, -1);
|
|
}
|
|
foreach (KeyValuePair<int, EntityClass> keyValuePair in EntityClass.list.Dict)
|
|
{
|
|
if (keyValuePair.Value.entityClassName == this.entityToSpawn)
|
|
{
|
|
this.entityId = keyValuePair.Key;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void StartHolding(ItemActionData _actionData)
|
|
{
|
|
ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle itemActionDataSpawnVehicle = (ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle)_actionData;
|
|
EntityPlayerLocal entityPlayerLocal = itemActionDataSpawnVehicle.invData.holdingEntity as EntityPlayerLocal;
|
|
if (!entityPlayerLocal)
|
|
{
|
|
return;
|
|
}
|
|
if (itemActionDataSpawnVehicle.VehiclePreviewT)
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(itemActionDataSpawnVehicle.VehiclePreviewT.gameObject);
|
|
}
|
|
GameObject original = DataLoader.LoadAsset<GameObject>(entityPlayerLocal.inventory.holdingItem.MeshFile);
|
|
itemActionDataSpawnVehicle.VehiclePreviewT = UnityEngine.Object.Instantiate<GameObject>(original).transform;
|
|
Vehicle.SetupPreview(itemActionDataSpawnVehicle.VehiclePreviewT);
|
|
this.SetupPreview(itemActionDataSpawnVehicle);
|
|
GameManager.Instance.StartCoroutine(this.UpdatePreview(itemActionDataSpawnVehicle));
|
|
}
|
|
|
|
private void SetupPreview(ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle data)
|
|
{
|
|
data.ValidPosition = false;
|
|
if (data.PreviewRenderers == null || data.PreviewRenderers.Length == 0 || data.PreviewRenderers[0] == null)
|
|
{
|
|
data.PreviewRenderers = data.VehiclePreviewT.GetComponentsInChildren<Renderer>();
|
|
}
|
|
for (int i = 0; i < data.PreviewRenderers.Length; i++)
|
|
{
|
|
data.PreviewRenderers[i].material.color = new Color(2f, 0.25f, 0.25f);
|
|
}
|
|
}
|
|
|
|
private IEnumerator UpdatePreview(ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle data)
|
|
{
|
|
World world = data.invData.world;
|
|
while (data.VehiclePreviewT)
|
|
{
|
|
bool flag = this.CalcSpawnPosition(data, ref data.Position) && world.CanPlaceBlockAt(new Vector3i(data.Position), world.GetGameManager().GetPersistentLocalPlayer(), false);
|
|
if (data.ValidPosition != flag)
|
|
{
|
|
data.ValidPosition = flag;
|
|
if (data.PreviewRenderers == null || data.PreviewRenderers.Length == 0 || data.PreviewRenderers[0] == null)
|
|
{
|
|
data.PreviewRenderers = data.VehiclePreviewT.GetComponentsInChildren<Renderer>();
|
|
}
|
|
Color color = flag ? new Color(0.25f, 2f, 0.25f) : new Color(2f, 0.25f, 0.25f);
|
|
for (int i = 0; i < data.PreviewRenderers.Length; i++)
|
|
{
|
|
data.PreviewRenderers[i].material.color = color;
|
|
}
|
|
}
|
|
if (data.Position.y < 9999f)
|
|
{
|
|
Quaternion localRotation = Quaternion.Euler(0f, data.invData.holdingEntity.rotation.y + 90f, 0f);
|
|
data.VehiclePreviewT.localRotation = localRotation;
|
|
data.VehiclePreviewT.position = data.Position - Origin.position;
|
|
data.VehiclePreviewT.gameObject.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
data.VehiclePreviewT.gameObject.SetActive(false);
|
|
}
|
|
yield return new WaitForEndOfFrame();
|
|
}
|
|
yield break;
|
|
}
|
|
|
|
public override void CancelAction(ItemActionData _actionData)
|
|
{
|
|
ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle itemActionDataSpawnVehicle = (ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle)_actionData;
|
|
if (itemActionDataSpawnVehicle.VehiclePreviewT && itemActionDataSpawnVehicle.invData.holdingEntity is EntityPlayerLocal)
|
|
{
|
|
UnityEngine.Object.Destroy(itemActionDataSpawnVehicle.VehiclePreviewT.gameObject);
|
|
}
|
|
}
|
|
|
|
public override void StopHolding(ItemActionData _actionData)
|
|
{
|
|
ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle itemActionDataSpawnVehicle = (ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle)_actionData;
|
|
if (itemActionDataSpawnVehicle.VehiclePreviewT && itemActionDataSpawnVehicle.invData.holdingEntity is EntityPlayerLocal)
|
|
{
|
|
UnityEngine.Object.Destroy(itemActionDataSpawnVehicle.VehiclePreviewT.gameObject);
|
|
}
|
|
}
|
|
|
|
public override void OnHoldingUpdate(ItemActionData _actionData)
|
|
{
|
|
}
|
|
|
|
public override void ExecuteAction(ItemActionData _actionData, bool _bReleased)
|
|
{
|
|
EntityPlayerLocal entityPlayerLocal = _actionData.invData.holdingEntity as EntityPlayerLocal;
|
|
if (!entityPlayerLocal)
|
|
{
|
|
return;
|
|
}
|
|
if (!_bReleased)
|
|
{
|
|
return;
|
|
}
|
|
if (Time.time - _actionData.lastUseTime < this.Delay)
|
|
{
|
|
return;
|
|
}
|
|
if (Time.time - _actionData.lastUseTime < Constants.cBuildIntervall)
|
|
{
|
|
return;
|
|
}
|
|
ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle itemActionDataSpawnVehicle = (ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle)_actionData;
|
|
if (!itemActionDataSpawnVehicle.ValidPosition)
|
|
{
|
|
return;
|
|
}
|
|
if (this.entityId < 0)
|
|
{
|
|
foreach (KeyValuePair<int, EntityClass> keyValuePair in EntityClass.list.Dict)
|
|
{
|
|
if (keyValuePair.Value.entityClassName == this.entityToSpawn)
|
|
{
|
|
this.entityId = keyValuePair.Key;
|
|
break;
|
|
}
|
|
}
|
|
if (this.entityId == 0)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
ItemValue holdingItemItemValue = entityPlayerLocal.inventory.holdingItemItemValue;
|
|
|
|
if (!SingletonMonoBehaviour<ConnectionManager>.Instance.IsServer)
|
|
{
|
|
SingletonMonoBehaviour<ConnectionManager>.Instance.SendToServer(NetPackageManager.GetPackage<NetPackageSpawnVehicleFromItemRebirth>().Setup(this.entityToSpawn,
|
|
itemActionDataSpawnVehicle.Position,
|
|
new Vector3(0f, entityPlayerLocal.rotation.y + 90f, 0f),
|
|
holdingItemItemValue.Clone(),
|
|
entityPlayerLocal.entityId,
|
|
false
|
|
));
|
|
}
|
|
else
|
|
{
|
|
ItemValue[] itemValues = { ItemValue.None };
|
|
GameManager.Instance.StartCoroutine(RebirthUtilities.SpawnVehicleFromItemCoroutine(
|
|
this.entityToSpawn,
|
|
itemActionDataSpawnVehicle.Position,
|
|
new Vector3(0f, entityPlayerLocal.rotation.y + 90f, 0f),
|
|
holdingItemItemValue.Clone(),
|
|
entityPlayerLocal.entityId,
|
|
true
|
|
));
|
|
}
|
|
|
|
if (itemActionDataSpawnVehicle.VehiclePreviewT)
|
|
{
|
|
UnityEngine.Object.Destroy(itemActionDataSpawnVehicle.VehiclePreviewT.gameObject);
|
|
}
|
|
entityPlayerLocal.RightArmAnimationUse = true;
|
|
entityPlayerLocal.DropTimeDelay = 0.5f;
|
|
entityPlayerLocal.inventory.DecHoldingItem(1);
|
|
entityPlayerLocal.PlayOneShot((this.soundStart != null) ? this.soundStart : "placeblock", false);
|
|
}
|
|
|
|
private bool CalcSpawnPosition(ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle _actionData, ref Vector3 position)
|
|
{
|
|
World world = _actionData.invData.world;
|
|
Ray lookRay = _actionData.invData.holdingEntity.GetLookRay();
|
|
if (Vector3.Dot(lookRay.direction, Vector3.up) == 1f)
|
|
{
|
|
return false;
|
|
}
|
|
position.y = float.MaxValue;
|
|
float num = 4f + this.vehicleSize.x;
|
|
if (Voxel.Raycast(world, lookRay, num + 1.5f, 8454144, 69, 0f))
|
|
{
|
|
if ((Voxel.voxelRayHitInfo.hit.pos - lookRay.origin).magnitude > num)
|
|
{
|
|
position = Voxel.voxelRayHitInfo.hit.pos;
|
|
return false;
|
|
}
|
|
for (float num2 = 0.14f; num2 < 0.91f; num2 += 0.25f)
|
|
{
|
|
position = Voxel.voxelRayHitInfo.hit.pos;
|
|
position.y += num2;
|
|
BlockValue block = world.GetBlock(World.worldToBlockPos(position));
|
|
if (block.isair || block.Block.IsTerrainDecoration)
|
|
{
|
|
Vector3 normalized = Vector3.Cross(lookRay.direction, Vector3.up).normalized;
|
|
Vector3 vector = Vector3.Cross(normalized, Vector3.up);
|
|
Vector3 localPos = position - Origin.position;
|
|
localPos.y += this.vehicleSize.y * 0.5f + 0.05f;
|
|
if (this.CheckForSpace(localPos, normalized, this.vehicleSize.z, vector, this.vehicleSize.x, Vector3.up, this.vehicleSize.y) && this.CheckForSpace(localPos, vector, this.vehicleSize.x, normalized, this.vehicleSize.z, Vector3.up, this.vehicleSize.y) && this.CheckForSpace(localPos, Vector3.up, this.vehicleSize.y, normalized, this.vehicleSize.z, vector, this.vehicleSize.x))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private bool CheckForSpace(Vector3 localPos, Vector3 dirN, float length, Vector3 axis1N, float axis1Length, Vector3 axis2N, float axis2Length)
|
|
{
|
|
Vector3 b = dirN * length * 0.5f;
|
|
for (float num = -axis1Length * 0.5f; num <= axis1Length * 0.5f; num += 0.2499f)
|
|
{
|
|
Vector3 a = localPos + axis1N * num;
|
|
for (float num2 = -axis2Length * 0.5f; num2 <= axis2Length * 0.5f; num2 += 0.2499f)
|
|
{
|
|
Vector3 a2 = a + axis2N * num2;
|
|
if (Physics.Raycast(a2 - b, dirN, length, 28901376))
|
|
{
|
|
return false;
|
|
}
|
|
if (Physics.Raycast(a2 + b, -dirN, length, 28901376))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public override void Cleanup(ItemActionData _data)
|
|
{
|
|
base.Cleanup(_data);
|
|
ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle itemActionDataSpawnVehicle = _data as ItemActionSpawnVehicleRebirth.ItemActionDataSpawnVehicle;
|
|
if (itemActionDataSpawnVehicle != null && itemActionDataSpawnVehicle.invData != null && itemActionDataSpawnVehicle.invData.holdingEntity is EntityPlayerLocal && itemActionDataSpawnVehicle.VehiclePreviewT)
|
|
{
|
|
UnityEngine.Object.Destroy(itemActionDataSpawnVehicle.VehiclePreviewT.gameObject);
|
|
}
|
|
}
|
|
|
|
private const int cColliderMask = 28901376;
|
|
private string entityToSpawn;
|
|
private int entityId = -1;
|
|
private Vector3 vehicleSize;
|
|
protected class ItemActionDataSpawnVehicle : ItemActionAttackData
|
|
{
|
|
public ItemActionDataSpawnVehicle(ItemInventoryData _invData, int _indexInEntityOfAction) : base(_invData, _indexInEntityOfAction)
|
|
{
|
|
}
|
|
|
|
public Transform VehiclePreviewT;
|
|
public Renderer[] PreviewRenderers;
|
|
public bool ValidPosition;
|
|
public Vector3 Position;
|
|
}
|
|
}
|