using AX.Network.Protocols; using AX.NetworkSystem; using AX.Serialization; using System.Collections; using System.Collections.Generic; using UnityEngine; public class WaterGunRotate : MonoBehaviour { // 旋转速度 public float Speed = 10f; // 水炮类型名称 public WaterGunType TypeName; // 车辆水炮 public Transform WaterGun; // 底座 public Transform WaterGunBottom; // 方向-上 private int UpDirection = -1; // 方向-左 private int LeftDirection = -1; // 最小旋转度数 private int HorizontalAngleMin = -45; // 最大旋转度数 private int HorizontalAngleMax = 45; // 最小旋转度数 private int VerticalAngleMin = -45; // 最大旋转度数 private int VerticalAngleMax = 45; // objId private long gameObjID; private void Start() { gameObjID = GetComponent().gameObjID; NetworkMessageDispatcher.AddListener("WATERGUN_ROTATE_SYNC", SetQuaternion); } private void OnDestroy() { NetworkMessageDispatcher.AddListener("WATERGUN_ROTATE_SYNC", SetQuaternion); } public void Up() { float x = WaterGun.localEulerAngles.x; x = CheckValue(x); SetDirection(); bool b = UpDirection == -1 ? x > VerticalAngleMin : x < VerticalAngleMax; if (b) { WaterGun.Rotate(UpDirection * Speed, 0, 0, Space.Self); } RotateSync(); } public void Down() { float x = WaterGun.localEulerAngles.x; x = CheckValue(x); SetDirection(); bool b = UpDirection == -1 ? x < VerticalAngleMax : x > VerticalAngleMin; if (b) { WaterGun.Rotate(-UpDirection * Speed, 0, 0, Space.Self); } RotateSync(); } public void Left() { float y = WaterGunBottom.localEulerAngles.y; y = CheckValue(y); SetDirection(); bool b = LeftDirection == -1 ? y > HorizontalAngleMin : y < HorizontalAngleMax; if (b) { WaterGunBottom.Rotate(0, LeftDirection * Speed, 0, Space.Self); } RotateSync(); } public void Right() { float y = WaterGunBottom.localEulerAngles.y; y = CheckValue(y); SetDirection(); bool b = LeftDirection == -1 ? y < HorizontalAngleMax : y > HorizontalAngleMin; if (b) { WaterGunBottom.Rotate(0, -LeftDirection * Speed, 0, Space.Self); } RotateSync(); } protected float CheckValue(float angles) { if (angles >= 180 && angles <= 360) { angles = angles - 360; } return angles; } protected void SetDirection() { switch (TypeName) { case WaterGunType.高喷车水炮: UpDirection = -1; LeftDirection = 1; HorizontalAngleMin = -45; HorizontalAngleMax = 45; VerticalAngleMin = -45; VerticalAngleMax = 45; break; case WaterGunType.云梯车水炮: UpDirection = -1; LeftDirection = -1; HorizontalAngleMin = -45; HorizontalAngleMax = 45; VerticalAngleMin = -45; VerticalAngleMax = 45; break; case WaterGunType.举高车水炮: UpDirection = -1; LeftDirection = -1; HorizontalAngleMin = -45; HorizontalAngleMax = 45; VerticalAngleMin = -45; VerticalAngleMax = 45; break; case WaterGunType.水罐车水炮: UpDirection = -1; LeftDirection = -1; HorizontalAngleMin = -360; HorizontalAngleMax = 360; VerticalAngleMin = -45; VerticalAngleMax = 45; break; case WaterGunType.排烟车水炮: UpDirection = -1; LeftDirection = -1; HorizontalAngleMin = -360; HorizontalAngleMax = 360; VerticalAngleMin = -45; VerticalAngleMax = 45; break; } } // 旋转同步 public void RotateSync() { var rotateSyncData = new RotateSyncData(); rotateSyncData.gameObjectID = GetComponent().gameObjID; rotateSyncData.WaterGunRotation = WaterGun.transform.localRotation; rotateSyncData.WaterGunBottomRotation = WaterGunBottom.transform.localRotation; NetworkManager.Default.SendAsync("WATERGUN_ROTATE_SYNC", rotateSyncData); } void SetQuaternion(BinaryMessage message) { var data = message.Body.Deserialize(); if (data.gameObjectID == gameObjID) { //SetQuaternion WaterGun.transform.localRotation = data.WaterGunRotation; WaterGunBottom.transform.localRotation = data.WaterGunBottomRotation; } } } public enum WaterGunType { 高喷车水炮, 云梯车水炮, 举高车水炮, 水罐车水炮, 排烟车水炮 } // public struct RotateSyncData { public long gameObjectID; public Quaternion WaterGunRotation; public Quaternion WaterGunBottomRotation; }