143 lines
4.8 KiB
C#
143 lines
4.8 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using DG.Tweening;
|
|||
|
|
|
|||
|
|
//Thomas09
|
|||
|
|
|
|||
|
|
public class WeaponSway : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
[SerializeField] private FirstPersonController playerController;
|
|||
|
|
[SerializeField] private float amount;
|
|||
|
|
[SerializeField] private float maxAmount;
|
|||
|
|
[SerializeField] private float smoothAmount;
|
|||
|
|
|
|||
|
|
private Vector3 refVelocity;
|
|||
|
|
private const string MouseX = "Look X";
|
|||
|
|
private const string MouseY = "Look Y";
|
|||
|
|
|
|||
|
|
[System.NonSerialized] public float SwayX;
|
|||
|
|
[System.NonSerialized] public float SwayY;
|
|||
|
|
|
|||
|
|
private Vector3 initialPosition;
|
|||
|
|
|
|||
|
|
private bool isAirborn = false;
|
|||
|
|
private float refYVelocity;
|
|||
|
|
|
|||
|
|
[SerializeField] private List<ThrowableController> ThrowableControllers;
|
|||
|
|
private Weapon _last = null;
|
|||
|
|
private Weapon _current = null;
|
|||
|
|
private bool _isGrenade = false;
|
|||
|
|
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
initialPosition = transform.localPosition;
|
|||
|
|
playerController.OnLandEvent.AddListener(OnLanded);
|
|||
|
|
playerController.OnJumpEvent.AddListener(OnJump);
|
|||
|
|
foreach (var wChild in ThrowableControllers)
|
|||
|
|
{
|
|||
|
|
wChild.OnThrow += Throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
SwayWeapon();
|
|||
|
|
|
|||
|
|
if (isAirborn)
|
|||
|
|
{
|
|||
|
|
float appliedForce = Player.Instance.IsAiming ? 0.025f : 0.12f;
|
|||
|
|
float yVelocity = playerController.m_characterController.velocity.y;
|
|||
|
|
float clampedVelocity = Mathf.Clamp(yVelocity, -appliedForce, appliedForce);
|
|||
|
|
float smoothedVelocity = Mathf.SmoothDamp(transform.localPosition.y, -clampedVelocity, ref refYVelocity, 0.355f);
|
|||
|
|
transform.localPosition = new Vector3(transform.localPosition.x, smoothedVelocity, transform.localPosition.z);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void SwayWeapon()
|
|||
|
|
{
|
|||
|
|
if (!Player.Instance.IsAiming)
|
|||
|
|
{
|
|||
|
|
if (Player.Instance.updatePlayer)
|
|||
|
|
{
|
|||
|
|
var localCurrent = Player.Instance.GetWeaponData();
|
|||
|
|
if (localCurrent != null)
|
|||
|
|
{
|
|||
|
|
// isGrenade
|
|||
|
|
if (localCurrent.baseID == 23)
|
|||
|
|
_isGrenade = true;
|
|||
|
|
if (_current == null) _current = localCurrent;
|
|||
|
|
else if (_current.baseID != localCurrent.baseID)
|
|||
|
|
{
|
|||
|
|
_last = _current;
|
|||
|
|
_current = localCurrent;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
float alteredMaxAmount = localCurrent?.weaponType == Weapon.WEAPON_TYPE.PISTOL ? maxAmount / 2 : maxAmount;
|
|||
|
|
float mouseX = -SwayX * amount;
|
|||
|
|
float mouseY = -SwayY * amount;
|
|||
|
|
mouseX = Mathf.Clamp(mouseX, -alteredMaxAmount, alteredMaxAmount);
|
|||
|
|
mouseY = Mathf.Clamp(mouseY, -alteredMaxAmount, alteredMaxAmount);
|
|||
|
|
|
|||
|
|
Vector3 finalPosition = new Vector3(mouseX, mouseY, 0);
|
|||
|
|
transform.localPosition = Vector3.Lerp(transform.localPosition, finalPosition + initialPosition, Time.deltaTime * smoothAmount);
|
|||
|
|
//transform.localPosition = Vector3.SmoothDamp(transform.localPosition, finalPosition + initialPosition, ref refVelocity, Time.deltaTime * smoothAmount);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_current = Player.Instance.GetWeaponData();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
transform.localPosition = Vector3.Lerp(transform.localPosition, initialPosition, Time.deltaTime * smoothAmount);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnJump() => isAirborn = true;
|
|||
|
|
|
|||
|
|
private async void Throw(Weapon weapon)
|
|||
|
|
{
|
|||
|
|
if (weapon == null) return;
|
|||
|
|
if (weapon.baseID == 23)
|
|||
|
|
{
|
|||
|
|
int index = await FindLastWeaponInInventory();
|
|||
|
|
if (index != -1)
|
|||
|
|
{
|
|||
|
|
PacketManager.sendClickItem(InventoryManager.Instance.inventoryItems[index], index);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<int> FindLastWeaponInInventory()
|
|||
|
|
{
|
|||
|
|
var id = -1;
|
|||
|
|
for (int i = 0; i < InventoryManager.Instance.inventoryItems.Length; i++)
|
|||
|
|
{
|
|||
|
|
id = ItemDef.GetInstance().getItemID(InventoryManager.Instance.inventoryItems[i]);
|
|||
|
|
if (id == _last.baseID)
|
|||
|
|
{
|
|||
|
|
return i;
|
|||
|
|
}
|
|||
|
|
await Task.Delay(2); // fixed Update delay
|
|||
|
|
}
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void OnLanded()
|
|||
|
|
{
|
|||
|
|
isAirborn = false;
|
|||
|
|
|
|||
|
|
float bounceAmount = Player.Instance.IsAiming ? -0.001f : -0.05f;
|
|||
|
|
float bounceInTime = Player.Instance.IsAiming ? 0.1f : 0.125f;
|
|||
|
|
float bounceOutTime = Player.Instance.IsAiming ? 0.1f : 0.2f;
|
|||
|
|
|
|||
|
|
DOTween.Sequence()
|
|||
|
|
.Append(transform.DOLocalMoveY(bounceAmount, bounceInTime).SetEase(Ease.InSine))
|
|||
|
|
.Append(transform.DOLocalMoveY(0.0f, bounceOutTime).SetEase(Ease.OutSine));
|
|||
|
|
}
|
|||
|
|
}
|