| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using DG.Tweening;
- /* 联机镜像-箭 */
- public class ArrowSync : MonoBehaviour
- {
- void Awake()
- {
- //箭模型平时属于ArmBow层,因为ArmBow层在飞行镜头中不被渲染,所以箭出去后要切换layer
- this.transform.Find("Head/_hunse_jian").gameObject.layer = 0;
- }
- void Update()
- {
- if (!hasAttachAnything) {
- UpdatePosAndRot(Time.deltaTime * 15);
- }
- }
- void UpdatePosAndRot(float t) {
- transform.position = Vector3.Lerp(transform.position, position, t);
-
- transform.rotation = Quaternion.Lerp(transform.rotation, rotation, t);
- //这里处理应该联网的时候拖尾,跟随有一点误差位置
- if (guadiantuowei)
- {
- Head().position = transform.position + new Vector3(0, 0, 0.78f);//这里之前应该是只是同步父节点的,所以可能有点显示问题
- guadiantuowei.localPosition = new Vector3(Head().localPosition.x, Head().localPosition.y, guadiantuowei.localPosition.z);
- }
- else if (tuowei) {
- //旧的特效加个 -0.78f
- Head().position = transform.position + new Vector3(0, 0, -0.78f);//这里之前应该是只是同步父节点的,所以可能有点显示问题
- tuowei.localPosition = new Vector3(Head().localPosition.x, Head().localPosition.y, tuowei.localPosition.z);
- }
- }
- void UpdatePosAndRotImmediate() {
- transform.position = position;
- Head().position = transform.position;
- transform.rotation = rotation;
- }
- public Transform Head() {
- return transform.Find("Head");
- }
- #region ---------箭矢特效---------
- public void activeEffectCyclone(bool value)
- {
- if (isHit) {
- //加强校验,怕ArrowSync关了该特效,又被ArrowCameraTemplate打开这个特效
- value = false;
- }
- this.transform.Find("Head/EF_kuosanquan").gameObject.SetActive(value);
- if (!value) return;
- ParticleSystemRenderer ps = this.transform.Find("Head/EF_kuosanquan/kuosan").GetComponent<ParticleSystemRenderer>();
- ParticleSystemRenderer ps1 = this.transform.Find("Head/EF_kuosanquan/kuosan (1)").GetComponent<ParticleSystemRenderer>();
- DOTween.To(() => ps.minParticleSize, value => {
- ps.minParticleSize = value;
- ps.maxParticleSize = value;
- }, 0.4f, 0.6f);
- DOTween.To(() => ps1.minParticleSize, value => {
- ps1.minParticleSize = value;
- ps1.maxParticleSize = value;
- }, 0.8f, 0.6f);
- }
- void activeEffectBomb(bool value)
- {
- this.transform.Find("Head/EF_baodian").gameObject.SetActive(value);
- }
- Transform guadiantuowei;
- Transform tuowei;
- void activeEffectTrail(bool value)
- {
- if (GlobalData.MyDeviceMode == DeviceMode.Archery)
- {
- //this.transform.Find("EF_tuowei").gameObject.SetActive(value);
- //this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
- guadiantuowei = this.transform.Find("guadiantuowei");
- if (guadiantuowei == null)
- {
- tuowei = this.transform.Find("EF_tuowei");
- tuowei.gameObject.SetActive(value);
- //this.transform.Find("EF_tuowei/Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
- tuowei.Find("Trail").GetComponent<TrailRenderer>().time = 1.6f / mySpeed;
- }
- else
- {
- guadiantuowei.gameObject.SetActive(value);
- }
- }
- else
- {
- StartCoroutine(showTrail(value));
- }
- }
- IEnumerator showTrail(bool value)
- {
- this.transform.Find("EF_tuowei_bullet").gameObject.SetActive(value);
- yield return new WaitForEndOfFrame();
- this.transform.Find("EF_tuowei_bullet/GB_flame_FX_1").gameObject.SetActive(true);
- }
- #endregion
- Quaternion rotation;
- Vector3 position;
- float mySpeed;
- public float mySyncSpeed => mySpeed;
- bool hasPlayHitAudio = false;
- bool hasPlayHitEffect = false;
- string[] hitTargetAnimalInfo;
- bool needCheckHitAnimal = false;//需要检测是否击中了动物?
- bool hasAttachAnything = false;//是否附着在什么东西上,比如动物身上,这时候其实就不需要通过同步状态更新坐标了
- ArrowCamera arrowCameraComp;
-
- [System.NonSerialized] public bool isHit;
- [System.NonSerialized] public bool hasDoneNextShoot;
- [System.NonSerialized] public bool canUserLockCamera;
- [System.NonSerialized] public bool canUseSideCamera;
- [System.NonSerialized] public bool hasSetSyncData = false; //外部运算用的
- public void SetSyncData(SyncData syncData, bool apply = false) {
- rotation.x = syncData.rx;
- rotation.y = syncData.ry;
- rotation.z = syncData.rz;
- rotation.w = syncData.rw;
- position.x = syncData.px;
- position.y = syncData.py;
- position.z = syncData.pz;
- canUserLockCamera = syncData.cl;
- canUseSideCamera = syncData.cs;
- isHit = syncData.ih;
- hasDoneNextShoot = syncData.dns;
- hitTargetAnimalInfo = syncData.htai;
- mySpeed = syncData.sp;
- if (apply) {
- transform.position = position;
- transform.rotation = rotation;
- if (!isHit || !hasDoneNextShoot) {
- needCheckHitAnimal = true;
- // Debug.Log("sync data:" + GlobalData.MyDeviceMode);
- if (GlobalData.MyDeviceMode == DeviceMode.Archery)
- {
- //激活镜头
- Transform cameraTF = this.transform.Find("Camera");
- cameraTF.gameObject.SetActive(true);
- arrowCameraComp = cameraTF.gameObject.AddComponent<ArrowCamera>();
- arrowCameraComp.SetArrowSync(this);
- //射出的声音
- AudioMgr.ins.PlayShoot(AudioMgr.GetAudioSource(this.gameObject));
- }
-
- //拖尾
- activeEffectTrail(true);
- }
- else if (isHit) {
- hasPlayHitAudio = true;
- hasPlayHitEffect = true;
- activeEffectCyclone(false);
- activeEffectTrail(false);
- }
- } else {
- if (arrowCameraComp && isHit && needCheckHitAnimal) {
- needCheckHitAnimal = false;
- if (arrowCameraComp.arrowCameraTemplate == null) {
- needCheckHitAnimal = true;
- } else if (arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
- bool hasBlockByTree = false;
- if (syncData.HasArrowCameraTemplate3()) {
- hasBlockByTree = syncData.GetArrowCameraTemplate3().Item1;
- }
- // if (hitTargetAnimalInfo != null) {
- // //因为击中的是动物,这里就使用射中时第一帧的数据,可避免跟主机的表现产生差异
- // position = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[4]);
- // rotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[5]);
- // }
- UpdatePosAndRotImmediate();
- ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).beforHitWhenSync(hasBlockByTree);
- if (hitTargetAnimalInfo != null) {
- int onlineID = int.Parse(hitTargetAnimalInfo[0]);
- int partIndex = int.Parse(hitTargetAnimalInfo[1]);
- TargetAnimal[] targetAnimals = GameObject.FindObjectsOfType<TargetAnimal>();
- foreach (var item in targetAnimals) {
- if (item.GetOnlineID() == onlineID) {
- //因为击中的是动物,箭挂在动物身上,之后不需要用网络数据来同步表现,而且这样表现力更好
- this.transform.SetParent(item.targetAnimalParts[partIndex]);
- transform.localPosition = SyncDataUtil.StrToVec3(hitTargetAnimalInfo[2]);
- transform.localRotation = SyncDataUtil.StrToQuat(hitTargetAnimalInfo[3]);
- hasAttachAnything = true;
- break;
- }
- }
- }
- }
- }
- if (syncData.HasArrowCameraTemplate3()) {
- if (arrowCameraComp && arrowCameraComp.arrowCameraTemplate != null && arrowCameraComp.arrowCameraTemplate.GetType().Equals(typeof(ArrowCameraTemplate3))) {
- ((ArrowCameraTemplate3) arrowCameraComp.arrowCameraTemplate).quicklyNextShoot = syncData.GetArrowCameraTemplate3().Item2;
- }
- }
- }
- if (isHit && !hasPlayHitAudio) {
- hasPlayHitAudio = true;
- int hitType = syncData.ht;
- // Debug.Log("sync data isHit hitType:" + hitType);
- if (hitType == Arrow.HitType.TargetInRing || hitType == Arrow.HitType.TargetOutRing) {
- if (GlobalData.MyDeviceMode == DeviceMode.Archery)
- {
- AudioMgr.ins.PlayHit(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
- }
- else {
- AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
- }
- //AudioMgr.ins.PlayCheer(hitType == 1 ? true : false);
- } else if (hitType == Arrow.HitType.Animal) {
- if (GlobalData.MyDeviceMode == DeviceMode.Archery)
- {
- AudioMgr.ins.PlayArrowEnter();
- }
- else
- {
- AudioMgr.ins.PlayGunShoot(AudioMgr.GetAudioSource(TargetBody.ins.gameObject));
- }
- } else if (hitType == Arrow.HitType.NotTarget) {
- if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Game")
- {
- AudioMgr.ins.PlayCheer(false);
- }
- else
- {
- AudioMgr.ins.PlayArrowEnter();
- }
- }
- }
- if (isHit && !hasPlayHitEffect) {
- hasPlayHitEffect = true;
- activeEffectBomb(true);
- activeEffectCyclone(false);
- activeEffectTrail(false);
- }
- }
- public class SyncData {
- [System.NonSerialized] public bool inited;
- static int autoID;
- public int id;
- public float rx;
- public float ry;
- public float rz;
- public float rw;
- public float px;
- public float py;
- public float pz;
- public bool cl;
- public bool cs;
- public bool ih;
- public int ht;
- public float sp;
- public bool dns; //hasDoneNextShoot
- public string act3; // ArrowCameraTemplate3
- public string[] htai;
- public void SetData(Arrow arrow) {
- Quaternion r = arrow.transform.rotation;
- Vector3 p = arrow.Head().transform.position;
- rx = r.x;
- ry = r.y;
- rz = r.z;
- rw = r.w;
- px = p.x;
- py = p.y;
- pz = p.z;
- cs = arrow.canUseSideCamera;
- cl = arrow.canUserLockCamera;
- ih = arrow.isHit;
- ht = arrow.hitType;
- sp = arrow.mySpeed;
- dns = arrow.hasDoneNextShoot;
- htai = arrow.hitTargetAnimalInfo;
- if (!inited) {
- id = autoID++;
- if (autoID > 1000) autoID = 0;
- }
- inited = true;
- }
- public void SetArrowCameraTemplate3(bool hasBlockByTree, bool quicklyNextShoot) {
- act3 = hasBlockByTree + "," + quicklyNextShoot;
- }
- public bool HasArrowCameraTemplate3() {
- return act3 != null;
- }
- public System.Tuple<bool, bool> GetArrowCameraTemplate3() {
- string[] strs = act3.Split(',');
- return new System.Tuple<bool, bool>(bool.Parse(strs[0]), bool.Parse(strs[1]));
- }
- }
- }
|