459 lines
10 KiB
C#
459 lines
10 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Game
|
|||
|
// Author : Kimch
|
|||
|
// Created : 2020-09-02
|
|||
|
// Description :
|
|||
|
// Last Modified By :
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "Cha_Weapon" company="Kimch"></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
namespace G
|
|||
|
{
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 角色武器管理
|
|||
|
/// </summary>
|
|||
|
public class Cha_Weapon : MonoBehaviour
|
|||
|
{
|
|||
|
[System.Serializable]
|
|||
|
public class WeaponFx
|
|||
|
{
|
|||
|
public Transform fx1;
|
|||
|
public Transform fx2;
|
|||
|
public Material[] mats;
|
|||
|
}
|
|||
|
|
|||
|
public WeaponFx[] effects;
|
|||
|
|
|||
|
public Transform bowgun;
|
|||
|
|
|||
|
public Transform spear;
|
|||
|
|
|||
|
public int dock;
|
|||
|
|
|||
|
public Transform dummy_spine;
|
|||
|
|
|||
|
public Transform main_hand;
|
|||
|
|
|||
|
public Transform sub_hand;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
private Transform _mainWeapon;
|
|||
|
private Transform _subWeapon;
|
|||
|
|
|||
|
private Transform _generalMainWeapon;
|
|||
|
private Transform _generalSubWeapon;
|
|||
|
|
|||
|
private Transform _currentWeapon;
|
|||
|
|
|||
|
private Transform _bowgun;
|
|||
|
private Transform _spear;
|
|||
|
|
|||
|
private float _showDelay;
|
|||
|
private float _finishTime;
|
|||
|
private bool _changeStart;
|
|||
|
private int _changeSpeed = 1;
|
|||
|
|
|||
|
private Transform mytransform
|
|||
|
{
|
|||
|
get { return main_hand ?? transform; }
|
|||
|
}
|
|||
|
|
|||
|
#region Method
|
|||
|
|
|||
|
public void SetModel(GameObject root)
|
|||
|
{
|
|||
|
var list = F.ListPool<Transform>.Get();
|
|||
|
root.GetComponentsInChildren(list);
|
|||
|
foreach (var t in list)
|
|||
|
{
|
|||
|
var tn = t.name;
|
|||
|
if (tn == "weapon_dummy")
|
|||
|
{
|
|||
|
main_hand = t;
|
|||
|
}
|
|||
|
else if (tn == "weapon_dummy_sub")
|
|||
|
{
|
|||
|
sub_hand = t;
|
|||
|
}
|
|||
|
if (tn == "spine_dummy")
|
|||
|
{
|
|||
|
dummy_spine = t;
|
|||
|
}
|
|||
|
}
|
|||
|
F.ListPool<Transform>.Release(list);
|
|||
|
}
|
|||
|
|
|||
|
GameObject Load(string name)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
private System.Collections.IEnumerator SetWeaponEffect(Transform weapon, int quality, int element)
|
|||
|
{
|
|||
|
yield return null;
|
|||
|
int elementIndex = element;
|
|||
|
if (quality > 3)
|
|||
|
{
|
|||
|
var matList = F.ListPool<Material>.Get();
|
|||
|
var mr = weapon.GetComponent<MeshRenderer>();
|
|||
|
matList.AddRange(mr.sharedMaterials);
|
|||
|
matList.AddRange(effects[elementIndex].mats);
|
|||
|
#if UNITY_EDITOR
|
|||
|
var mats = new Material[matList.Count];
|
|||
|
for (int i = 0; i < mats.Length; i++)
|
|||
|
{
|
|||
|
mats[i] = Instantiate(matList[i]);
|
|||
|
}
|
|||
|
#else
|
|||
|
var mats = matList.ToArray();
|
|||
|
#endif
|
|||
|
for (int i = 1; i < mats.Length; i++)
|
|||
|
{
|
|||
|
mats[i].SetTexture("_Mask", mats[0].mainTexture);
|
|||
|
}
|
|||
|
mr.materials = mats;
|
|||
|
F.ListPool<Material>.Release(matList);
|
|||
|
|
|||
|
if (quality >= 4 && effects[elementIndex].fx1)
|
|||
|
{
|
|||
|
var ef_go = Instantiate(effects[elementIndex].fx1, weapon.Find("fx_pz"));
|
|||
|
}
|
|||
|
|
|||
|
if (quality >= 5 && effects[elementIndex].fx2)
|
|||
|
{
|
|||
|
var ef_go = Instantiate(effects[elementIndex].fx2, weapon.Find("fx_pz"));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int _weaponKind;
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="asset"></param>
|
|||
|
/// <param name="kind"></param>
|
|||
|
public async void SetStageWeapon(string asset, int kind, int quality, int element)
|
|||
|
{
|
|||
|
if (string.IsNullOrEmpty(asset))
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
_weaponKind = kind;
|
|||
|
|
|||
|
if (_mainWeapon)
|
|||
|
{
|
|||
|
Destroy(_mainWeapon.gameObject);
|
|||
|
}
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
Destroy(_subWeapon.gameObject);
|
|||
|
}
|
|||
|
|
|||
|
var handle = AssetProxy.Instance.TryGetTemporaryAssetAsync<GameObject>(asset);
|
|||
|
var weaponPrefab = await handle.Task;
|
|||
|
|
|||
|
_mainWeapon = Instantiate(weaponPrefab, dummy_spine).transform;
|
|||
|
_mainWeapon.localRotation = Quaternion.Euler(0f, 0f, 0f);
|
|||
|
_mainWeapon.localScale = Vector3.one;
|
|||
|
|
|||
|
StartCoroutine(SetWeaponEffect(_mainWeapon, quality, element));
|
|||
|
|
|||
|
if (kind == (int)WeaponKind.dual)
|
|||
|
{
|
|||
|
_mainWeapon.localPosition += new Vector3(0, 0, 0.01f);
|
|||
|
//_mainWeapon.position += dummy_spine.forward * 0.01f;
|
|||
|
//_subWeapon = Instantiate(weaponPrefab, dummy_spine.position - dummy_spine.up * 0.03f + dummy_spine.forward * 0.04f + dummy_spine.right * 0.01f, dummy_spine.rotation, dummy_spine).transform;
|
|||
|
_subWeapon = Instantiate(weaponPrefab, dummy_spine).transform;
|
|||
|
_subWeapon.localPosition += new Vector3(0.01f, -0.03f, 0.04f);
|
|||
|
_subWeapon.localRotation = Quaternion.Euler(260f, 180f, 0f);
|
|||
|
_subWeapon.localScale = Vector3.one;
|
|||
|
StartCoroutine(SetWeaponEffect(_subWeapon, quality, element));
|
|||
|
}
|
|||
|
|
|||
|
if (dock == 1)
|
|||
|
{
|
|||
|
_mainWeapon.localScale *= 0.8f;
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
_subWeapon.localScale *= 0.8f;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
//ReturnBlade();
|
|||
|
}
|
|||
|
|
|||
|
public void GeneralWeaponOn(int kind, int _weaponindex)
|
|||
|
{
|
|||
|
_mainWeapon.gameObject.SetActive(false);
|
|||
|
if (_subWeapon != null)
|
|||
|
{
|
|||
|
_subWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
if (_generalMainWeapon == null)
|
|||
|
{
|
|||
|
var blade = Load("wp_general" + _weaponindex);
|
|||
|
_generalMainWeapon = Instantiate(blade.transform, mytransform.position, mytransform.rotation, mytransform);
|
|||
|
|
|||
|
if (kind == (int)WeaponKind.dual)
|
|||
|
{
|
|||
|
_generalSubWeapon = Instantiate(blade.transform, sub_hand.position, sub_hand.rotation);
|
|||
|
_generalSubWeapon.parent = sub_hand;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_generalMainWeapon.gameObject.SetActive(true);
|
|||
|
if (kind == (int)WeaponKind.dual)
|
|||
|
{
|
|||
|
_generalSubWeapon.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void GeneralWeaponOff()
|
|||
|
{
|
|||
|
if (_generalMainWeapon)
|
|||
|
{
|
|||
|
_generalMainWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
if (_generalSubWeapon)
|
|||
|
{
|
|||
|
_generalSubWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
_mainWeapon.gameObject.SetActive(true);
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
_subWeapon.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public async void SetWeaponUI(string asset, int kind, int quality, int element)
|
|||
|
{
|
|||
|
if (_mainWeapon)
|
|||
|
{
|
|||
|
Destroy(_mainWeapon.gameObject);
|
|||
|
}
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
Destroy(_subWeapon.gameObject);
|
|||
|
}
|
|||
|
|
|||
|
var handle = AssetProxy.Instance.GetAssetAsync<GameObject>($"weapon/{asset}.prefab");
|
|||
|
var bladeGO = await handle.Task;
|
|||
|
|
|||
|
_mainWeapon = Instantiate(bladeGO, main_hand.position, main_hand.rotation).transform;
|
|||
|
_mainWeapon.parent = main_hand;
|
|||
|
_mainWeapon.localScale *= 0.618f;
|
|||
|
_mainWeapon.localRotation = Quaternion.Euler(0f, 270f, 180f);
|
|||
|
|
|||
|
StartCoroutine(SetWeaponEffect(_mainWeapon, quality, element));
|
|||
|
GameLayer.SetGameObjectLayer(_mainWeapon.gameObject, GameLayer.UILayer);
|
|||
|
|
|||
|
if (kind == (int)WeaponKind.dual)
|
|||
|
{
|
|||
|
_subWeapon = Instantiate(bladeGO, sub_hand.position, sub_hand.rotation).transform;
|
|||
|
_subWeapon.parent = sub_hand;
|
|||
|
_subWeapon.localScale *= 0.618f;
|
|||
|
_subWeapon.localRotation = Quaternion.Euler(0f, 270f, 180f);
|
|||
|
StartCoroutine(SetWeaponEffect(_subWeapon, quality, element));
|
|||
|
|
|||
|
GameLayer.SetGameObjectLayer(_subWeapon.gameObject, GameLayer.UILayer);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void SwitchWepon(int kind, int changeSpd, float finishTime, float showDelay)
|
|||
|
{
|
|||
|
_changeSpeed = changeSpd;
|
|||
|
_finishTime = finishTime;
|
|||
|
_showDelay = showDelay;
|
|||
|
|
|||
|
switch (kind)
|
|||
|
{
|
|||
|
case 0:
|
|||
|
if (_spear == null)
|
|||
|
{
|
|||
|
_spear = Instantiate(spear, mytransform.position, mytransform.rotation);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_spear.position = mytransform.position;
|
|||
|
_spear.rotation = mytransform.rotation;
|
|||
|
_spear.GetComponent<WeaponDrop>().DropCancel();
|
|||
|
_spear.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
_currentWeapon = _spear;
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
if (_bowgun == null)
|
|||
|
{
|
|||
|
_bowgun = Instantiate(bowgun, mytransform.position, mytransform.rotation);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_bowgun.position = mytransform.position;
|
|||
|
_bowgun.rotation = mytransform.rotation;
|
|||
|
_bowgun.GetComponent<WeaponDrop>().DropCancel();
|
|||
|
_bowgun.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
_currentWeapon = _bowgun;
|
|||
|
break;
|
|||
|
default:
|
|||
|
_currentWeapon = null;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
if (_currentWeapon)
|
|||
|
{
|
|||
|
_currentWeapon.localScale = Vector3.zero;
|
|||
|
_currentWeapon.parent = mytransform;
|
|||
|
_changeStart = true;
|
|||
|
}
|
|||
|
|
|||
|
if (_mainWeapon)
|
|||
|
{
|
|||
|
_mainWeapon.position = dummy_spine.position;
|
|||
|
_mainWeapon.rotation = dummy_spine.rotation;
|
|||
|
_mainWeapon.parent = dummy_spine;
|
|||
|
}
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
_mainWeapon.position += dummy_spine.forward * 0.01f;
|
|||
|
_subWeapon.position = dummy_spine.position - dummy_spine.up * 0.03f + dummy_spine.forward * 0.04f + dummy_spine.right * 0.01f;
|
|||
|
_subWeapon.parent = dummy_spine;
|
|||
|
_subWeapon.localRotation = Quaternion.Euler(0f, 270f, 180f);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public void ReturnBlade()
|
|||
|
{
|
|||
|
if (_currentWeapon != null)
|
|||
|
{
|
|||
|
_currentWeapon.parent = null;
|
|||
|
_currentWeapon.GetComponent<WeaponDrop>().Drop(false);
|
|||
|
}
|
|||
|
|
|||
|
if (_mainWeapon)
|
|||
|
{
|
|||
|
_mainWeapon.parent = main_hand;
|
|||
|
_mainWeapon.localPosition = Vector3.zero;
|
|||
|
_mainWeapon.localRotation = Quaternion.identity;
|
|||
|
_mainWeapon.localScale = Vector3.one;
|
|||
|
|
|||
|
if (_weaponKind == (int)WeaponKind.spear)
|
|||
|
{
|
|||
|
//_mainWeapon.rotation = Quaternion.LookRotation();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
_subWeapon.parent = sub_hand;
|
|||
|
_subWeapon.localPosition = Vector3.zero;
|
|||
|
_subWeapon.localRotation = Quaternion.identity;
|
|||
|
_subWeapon.localScale = Vector3.one;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public void HideBlade()
|
|||
|
{
|
|||
|
if (_mainWeapon)
|
|||
|
{
|
|||
|
_mainWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
_subWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
|
|||
|
if (_generalMainWeapon)
|
|||
|
{
|
|||
|
_generalMainWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
if (_generalSubWeapon)
|
|||
|
{
|
|||
|
_generalSubWeapon.gameObject.SetActive(false);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public void ShowBlade()
|
|||
|
{
|
|||
|
if (_mainWeapon)
|
|||
|
{
|
|||
|
_mainWeapon.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
if (_subWeapon)
|
|||
|
{
|
|||
|
_subWeapon.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
|
|||
|
if (_generalMainWeapon)
|
|||
|
{
|
|||
|
_generalMainWeapon.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
if (_generalSubWeapon)
|
|||
|
{
|
|||
|
_generalSubWeapon.gameObject.SetActive(true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void UpdateState()
|
|||
|
{
|
|||
|
if (_changeStart)
|
|||
|
{
|
|||
|
Vector3 localScale = _currentWeapon.localScale;
|
|||
|
if (localScale.x >= 1f)
|
|||
|
{
|
|||
|
_currentWeapon.localScale = Vector3.one;
|
|||
|
_changeStart = false;
|
|||
|
_showDelay = 0f;
|
|||
|
}
|
|||
|
else if (_showDelay > 1f)
|
|||
|
{
|
|||
|
_currentWeapon.localScale += Vector3.one * _changeSpeed * Time.deltaTime;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_showDelay += Time.deltaTime;
|
|||
|
}
|
|||
|
}
|
|||
|
else if (_finishTime > 0f)
|
|||
|
{
|
|||
|
_finishTime -= Time.deltaTime;
|
|||
|
if (_finishTime < 0f)
|
|||
|
{
|
|||
|
ReturnBlade();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Unity
|
|||
|
|
|||
|
private void Update()
|
|||
|
{
|
|||
|
UpdateState();
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|