You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
7.3 KiB
222 lines
7.3 KiB
4 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using AX.NetworkSystem;
|
||
|
using AX.Network.Protocols;
|
||
|
using AX.Serialization;
|
||
|
public struct ShuiPaoControl
|
||
|
{
|
||
|
public long gameObjectID;
|
||
|
public bool isControlling;
|
||
|
}
|
||
|
public struct ShuiPaoSyncData
|
||
|
{
|
||
|
public long gameObjectID;
|
||
|
public bool waterOpen;
|
||
|
public float ParticleSize;
|
||
|
public Quaternion dizuoRotation;
|
||
|
public Quaternion shuiQiangRotation;
|
||
|
}
|
||
|
public class GuDingShuiPaoControl : MonoBehaviour
|
||
|
{
|
||
|
|
||
|
// Use this for initialization
|
||
|
private ObjSelectCtrl objSelectCtrl;
|
||
|
private bool isControlling;
|
||
|
private long gameObjectID;
|
||
|
private GameObject dizuo;
|
||
|
private GameObject shuiQiang;
|
||
|
private GameObject water;
|
||
|
public bool waterOpen;
|
||
|
public float ParticleSize=1;
|
||
|
private GameObject waterStraightPrefab;
|
||
|
/// <summary>
|
||
|
/// 流量
|
||
|
/// </summary>
|
||
|
public float Flow;
|
||
|
/// <summary>
|
||
|
/// 剩余时间
|
||
|
/// </summary>
|
||
|
public int RemainTime;
|
||
|
/// <summary>
|
||
|
/// 剩余水量
|
||
|
/// </summary>
|
||
|
public int RemianWater;
|
||
|
public float AllUseWater;
|
||
|
private float timer = 1f;
|
||
|
|
||
|
public SprayMode spraymode = SprayMode.PS30;
|
||
|
void Start()
|
||
|
{
|
||
|
Init();
|
||
|
dizuo = transform.GetComponentInChildren<GuDingShuiPaoDiZuo>().gameObject;
|
||
|
shuiQiang = transform.GetComponentInChildren<GuDingShuiPaoShuiQiang>().gameObject;
|
||
|
gameObjectID = GetComponent<BaseGameObjInfo>().gameObjID;
|
||
|
waterStraightPrefab = Resources.Load<GameObject>("Particle/WaterStraight");
|
||
|
NetworkMessageDispatcher.AddListener("GUDINGSHUIPAO_CONTROL_SYNC", SetController);
|
||
|
NetworkMessageDispatcher.AddListener("GUDINGSHUIPAO_DATA_SYNC", SetShuiPao);
|
||
|
SprayParameter spray = SprayParameter.GetInfo(SprayMode.PS30);
|
||
|
float value= (ParticleSize - spray.minsize) / (spray.maxsize - spray.minsize);
|
||
|
Flow = (spray.maxflow - spray.minflow) * value + spray.minflow;
|
||
|
}
|
||
|
void OnDestroy()
|
||
|
{
|
||
|
NetworkMessageDispatcher.RemoveListener("GUDINGSHUIPAO_CONTROL_SYNC", SetController);
|
||
|
NetworkMessageDispatcher.RemoveListener("GUDINGSHUIPAO_DATA_SYNC", SetShuiPao);
|
||
|
}
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
if (CurrentUserInfo.role == Role.中队指挥 || CurrentUserInfo.role == Role.战斗班长)
|
||
|
{
|
||
|
if (objSelectCtrl.selected && !isControlling)
|
||
|
{
|
||
|
isControlling = true;
|
||
|
|
||
|
NetworkManager.Default.SendAsync("GUDINGSHUIPAO_CONTROL_SYNC", new ShuiPaoControl { gameObjectID = gameObjectID, isControlling = isControlling });
|
||
|
}
|
||
|
if (!objSelectCtrl.selected && isControlling)
|
||
|
{
|
||
|
isControlling = false;
|
||
|
NetworkManager.Default.SendAsync("GUDINGSHUIPAO_CONTROL_SYNC", new ShuiPaoControl { gameObjectID = gameObjectID, isControlling = isControlling });
|
||
|
}
|
||
|
}
|
||
|
if (waterOpen)
|
||
|
{
|
||
|
timer -= Time.deltaTime;
|
||
|
if (timer <= 0)
|
||
|
{
|
||
|
AllUseWater += Flow;
|
||
|
timer = 1f;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
private void Init()
|
||
|
{
|
||
|
if (CurrentUserInfo.role == Role.中队指挥 || CurrentUserInfo.role == Role.战斗班长)
|
||
|
{
|
||
|
GetComponent<BaseGameObjInfo>().UserID = CurrentUserInfo.mySelf.Id;
|
||
|
objSelectCtrl = gameObject.AddComponent<ObjSelectCtrl>();
|
||
|
SelectedObjs.gameObjs.Add(gameObject);
|
||
|
}
|
||
|
}
|
||
|
void SetController(BinaryMessage message)
|
||
|
{
|
||
|
if (CurrentUserInfo.role == Role.中队指挥 || CurrentUserInfo.role == Role.战斗班长)
|
||
|
{
|
||
|
var shuiPaoControl = message.Body.Deserialize<ShuiPaoControl>();
|
||
|
if (shuiPaoControl.gameObjectID == gameObjectID)
|
||
|
{
|
||
|
if (shuiPaoControl.isControlling)
|
||
|
{
|
||
|
objSelectCtrl.enabled = false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
objSelectCtrl.enabled = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public void SetWater(bool flag)
|
||
|
{
|
||
|
if (water == null)
|
||
|
{
|
||
|
water = Instantiate(waterStraightPrefab, shuiQiang.transform);
|
||
|
water.transform.Rotate(0, 180, 0);
|
||
|
ParticleSize = GetComponentInChildren<ParticleControlOfType>(true).GetScaleValue();
|
||
|
}
|
||
|
water.SetActive(flag);
|
||
|
waterOpen = flag;
|
||
|
DataSync();
|
||
|
}
|
||
|
public void ChangeSize(float value)
|
||
|
{
|
||
|
ParticleControlOfType currentparticle = GetComponentInChildren<ParticleControlOfType>(true);
|
||
|
if (currentparticle != null)
|
||
|
{
|
||
|
currentparticle.SetScaleValue(value);
|
||
|
ParticleSize = value;
|
||
|
}
|
||
|
DataSync();
|
||
|
}
|
||
|
public void Up()
|
||
|
{
|
||
|
float x = shuiQiang.transform.localEulerAngles.x;
|
||
|
x = CheckValue(x);
|
||
|
if (x < 70)
|
||
|
{
|
||
|
shuiQiang.transform.Rotate(10, 0, 0, Space.Self);
|
||
|
}
|
||
|
DataSync();
|
||
|
}
|
||
|
public void Down()
|
||
|
{
|
||
|
float x = shuiQiang.transform.localEulerAngles.x;
|
||
|
x = CheckValue(x);
|
||
|
Debug.Log(x);
|
||
|
if (x > -15)
|
||
|
{
|
||
|
shuiQiang.transform.Rotate(-10, 0, 0, Space.Self);
|
||
|
}
|
||
|
DataSync();
|
||
|
}
|
||
|
public void Left()
|
||
|
{
|
||
|
dizuo.transform.Rotate(0, -10, 0, Space.Self);
|
||
|
DataSync();
|
||
|
}
|
||
|
public void Right()
|
||
|
{
|
||
|
dizuo.transform.Rotate(0, 10, 0, Space.Self);
|
||
|
DataSync();
|
||
|
}
|
||
|
protected float CheckValue(float angles)
|
||
|
{
|
||
|
if (angles >= 180 && angles <= 360)
|
||
|
{
|
||
|
angles = angles - 360;
|
||
|
}
|
||
|
return angles;
|
||
|
//举例:假如获取到的localEulerAngles.x = 300,则在显示面板其显示的值为-60=300-360,用此显式值计算
|
||
|
}
|
||
|
private void DataSync()
|
||
|
{
|
||
|
var shuiPaoSyncData = new ShuiPaoSyncData();
|
||
|
shuiPaoSyncData.gameObjectID = gameObjectID;
|
||
|
shuiPaoSyncData.waterOpen = waterOpen;
|
||
|
shuiPaoSyncData.ParticleSize = ParticleSize;
|
||
|
shuiPaoSyncData.dizuoRotation = dizuo.transform.localRotation;
|
||
|
shuiPaoSyncData.shuiQiangRotation = shuiQiang.transform.localRotation;
|
||
|
NetworkManager.Default.SendAsync("GUDINGSHUIPAO_DATA_SYNC", shuiPaoSyncData);
|
||
|
}
|
||
|
void SetShuiPao(BinaryMessage message)
|
||
|
{
|
||
|
var shuiPaoControl = message.Body.Deserialize<ShuiPaoSyncData>();
|
||
|
if (shuiPaoControl.gameObjectID== gameObjectID)
|
||
|
{
|
||
|
//开关
|
||
|
var flag = shuiPaoControl.waterOpen;
|
||
|
if (water == null)
|
||
|
{
|
||
|
water = Instantiate(waterStraightPrefab, shuiQiang.transform);
|
||
|
water.transform.Rotate(0, 180, 0);
|
||
|
ParticleSize = GetComponentInChildren<ParticleControlOfType>(true).GetScaleValue();
|
||
|
}
|
||
|
water.SetActive(flag);
|
||
|
waterOpen = flag;
|
||
|
//大小
|
||
|
var value = shuiPaoControl.ParticleSize;
|
||
|
ParticleControlOfType currentparticle = GetComponentInChildren<ParticleControlOfType>(true);
|
||
|
if (currentparticle != null)
|
||
|
{
|
||
|
currentparticle.SetScaleValue(value);
|
||
|
ParticleSize = value;
|
||
|
}
|
||
|
//方向
|
||
|
shuiQiang.transform.localRotation = shuiPaoControl.shuiQiangRotation;
|
||
|
dizuo.transform.localRotation = shuiPaoControl.dizuoRotation;
|
||
|
}
|
||
|
}
|
||
|
}
|