Files
5.24BuYu/Assets/MyGame/aa/UI/Game/GameMainPanel_XJMH.cs
T
2026-02-09 10:44:38 +08:00

447 lines
14 KiB
C#

using Assets.MyGame.Scripts.Net;
using Assets.MyGame.Scripts.Net.API;
using Assets.MyGame.Scripts.Net.WebResponses;
using Newtonsoft.Json;
using Spine.Unity;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.UI;
public class GameMainPanel_XJMH : UIBase
{
bool isOpenSetting;
public Transform[] tra_SettingListPos;
public Transform[] tra_SettingListComponent;
public Button btn_SettingList;
public GameObject turretParent_Left;
public Animator mySitAnim;
public Toggle toggle_SkillAuto;
public Toggle toggle_Lock;
public Toggle toggle_Three;
bool isLock = false;
bool isThree = false;
MonsterContral monCotl_Lock;
public GameObject LockFX;
public List<SitPosData> list_Sits = new List<SitPosData>();
public SitPosData currSitData;
int bulletCount = 0;
public void JoinGame(int sitNum, int goldNum, int consumeGlod, string currentTurretID, UserToken user = null)
{
foreach (var item in list_Sits)
{
if (item.posID == sitNum)
{
item.gameObject.SetActive(true);
if (user == null)
{
user = GameManager.Instance.user;
}
item.user = user;
item.goldNum = goldNum;
item.consumeGoldNum = consumeGlod;
item.tMP_GoldNum.text = goldNum.ToString();
item.currentTurretID = currentTurretID;
item.tMP_TurretConsumeNum.text = consumeGlod.ToString();
}
}
}
bool isZD;
public void Start()
{
CreateFishManager.Instance.EnterRoom(Assets.MyGame.Scripts.Net.APIResponses.Enum.GameTypeEnum.星际迷航);
btn_SettingList.onClick.AddListener(() =>
{
if (isOpenSetting)
{
StartCoroutine(CloseSettingList());
}
else
{
StartCoroutine(OpenSettingList());
}
});
toggle_Lock.onValueChanged.AddListener((a) =>
{
isLock = a;
if (!a)
{
CancelLock();
}
});
toggle_SkillAuto.onValueChanged.AddListener((a) =>
{
isZD = a;
});
toggle_Three.onValueChanged.AddListener((a) =>
{
isThree = a;
});
toggle_Shuang.onValueChanged.AddListener((a) =>
{
isShuang = a;
});
foreach (var item in tra_SettingListComponent)
{
if (item.name == "Setting")
{
item.GetComponent<Button>().onClick.AddListener(() =>
{
UIManager.Instance.Close(UIPanel.MainGamePanelXJMH);
//等待界面添加
UIManager.Instance.ShowAsync(UIPanel.HomeMainPanel, UIPanel.MainGamePanelXJMH);
});
}
}
}
#region 打开关闭右上角设置选项
private IEnumerator OpenSettingList()
{
tra_SettingListComponent[tra_SettingListComponent.Length - 1].localScale = Vector3.one + Vector3.left * 2;
isOpenSetting = true;
int n = 0;
while (true)
{
if (isOpenSetting == false)
{
yield break;
}
yield return null;
for (int i = 0; i < tra_SettingListPos.Length; i++)
{
tra_SettingListComponent[i].position = Vector3.Lerp(tra_SettingListComponent[i].position, tra_SettingListPos[i].position, 0.5f);
if (tra_SettingListComponent[i].position == tra_SettingListPos[i].position)
{
n++;
if (n == tra_SettingListPos.Length)
{
yield break;
}
}
}
}
}
private IEnumerator CloseSettingList()
{
tra_SettingListComponent[tra_SettingListComponent.Length - 1].localScale = Vector3.one;
isOpenSetting = false;
int n = 0;
while (true)
{
if (isOpenSetting)
{
yield break;
}
yield return null;
for (int i = 0; i < tra_SettingListPos.Length; i++)
{
tra_SettingListComponent[i].position = Vector3.Lerp(tra_SettingListComponent[i].position, tra_SettingListPos[tra_SettingListPos.Length - 1].position, 0.5f);
if (tra_SettingListComponent[i].position == tra_SettingListPos[i].position)
{
n++;
if (n == tra_SettingListPos.Length)
{
yield break;
}
}
}
}
}
#endregion
public float attackTime = 0.5f;
float clickTime = 0;
public GameObject bullet_Test;
public GameObject bulletParent;
float changeTurretTime = 0f;
bool isChangingTurret = false;
public void Update()
{
if (bulletCount >= 100)
{
SendBullet();
}
if (Touchscreen.current?.primaryTouch != null)
{
if (Touchscreen.current.primaryTouch.press.wasPressedThisFrame)
{
clickTime = attackTime;
}
#region 换炮台
//Change Turret
if (Touchscreen.current.primaryTouch.press.wasPressedThisFrame)
{
PointerEventData pointerData = new PointerEventData(EventSystem.current);
pointerData.position = Touchscreen.current.primaryTouch.position.ReadValue();
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);
foreach (RaycastResult result in results)
{
if (result.gameObject.tag == "ChangeTurret")
{
if (result.gameObject.GetComponentInParent<SitPosData>() == currSitData)
{
isChangingTurret = true;
return;
}
}
}
}
if (Touchscreen.current.primaryTouch.press.isPressed)
{
if (isChangingTurret)
{
changeTurretTime += Time.deltaTime;
if (changeTurretTime > 1f)
{
if (!currSitData.changeTurretList.activeSelf)
{
//切换炮台面板
currSitData.changeTurretList.SetActive(true);
}
}
return;
}
}
if (Touchscreen.current.primaryTouch.press.wasReleasedThisFrame)
{
if (isChangingTurret)
{
changeTurretTime = 0;
isChangingTurret = false;
return;
}
}
#endregion
if (isLock)
{
if (monCotl_Lock == null)
{
CancelLock();
}
//发射射线检测
//发射完成后根据要求已有的子弹拐弯
if (Touchscreen.current.primaryTouch.press.wasPressedThisFrame)
{
Vector2 worldPos = Camera.main.ScreenToWorldPoint(Touchscreen.current.primaryTouch.position.ReadValue());
RaycastHit2D hit = Physics2D.Raycast(worldPos, Vector2.zero, 50, LayerMask.GetMask("Fish"));
monCotl_Lock = hit.transform?.GetComponentInParent<MonsterContral>();
if (monCotl_Lock != null && monCotl_Lock.gameObject.activeSelf) //这里为之后加对象池铺垫
{
foreach (BullitMove item in bulletParent.GetComponentsInChildren<BullitMove>())
{
item.Lock(monCotl_Lock);
}
if (!LockFX.activeSelf)
{
LockFX.SetActive(true);
}
}
else
{
CancelLock();
}
}
if (monCotl_Lock != null)
{
PaoTaRotate();
LockFX.transform.position = monCotl_Lock.transform.position;
//FireBullit();
}
}
else
{
if (Touchscreen.current.primaryTouch.press.isPressed)
{
//FireBullit();
if (!isZD)
{
PaoTaRotate();
}
}
}
if (Touchscreen.current.primaryTouch.press.wasReleasedThisFrame)
{
SendBullet();
}
}
if (isZD&&isLock==false)
{
PaoTaRotate();
}
}
private void SendBullet()
{
//if (bulletCount > 0)
// WebSocketSendList.ShootBullet(GameManager.Instance.gameData.currRoomData.roomId, currSitData.user.id, currSitData.consumeGlodNum, bulletCount);
bulletCount = 0;
}
private void PaoTaRotate()
{
#region 计算点击方向
Vector2 touchPos;
// 获取触摸位置(屏幕坐标)
if (monCotl_Lock != null)
{
touchPos = Camera.main.WorldToScreenPoint(monCotl_Lock.transform.position);
}
else
{
touchPos = Touchscreen.current.primaryTouch.position.ReadValue();
}
// 转换Canvas坐标
Vector2 worldPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle(currSitData.tra_CurrTurret.transform.parent as RectTransform, touchPos, null, out worldPos);
// 计算方向
Vector2 dir = worldPos - currSitData.tra_CurrTurret.GetComponent<RectTransform>().anchoredPosition;
// 计算角度
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90f;
currSitData.tra_CurrTurret.transform.rotation = Quaternion.Euler(Vector3.forward * angle);
#endregion
clickTime += Time.deltaTime;
if (clickTime > attackTime)
{
clickTime = 0;
}
else
{
return;
}
FireBullit(angle);
}
private void CancelLock() { monCotl_Lock = null; LockFX.SetActive(false); }
private void FireBullit(float angle)
{
//计算方向
Vector2 startV2;
startV2 = CoordinateTransformation.AnchoredPos_ToCanvas(currSitData.tra_CurrTurret.transform.GetChild(1).GetComponent<RectTransform>(), UIManager.Instance.uiRoot.GetComponent<Canvas>());
startV2 = CoordinateTransformation.CanvasToWorld(startV2, UIManager.Instance.uiRoot.GetComponent<RectTransform>(), Camera.main);
//Vector2 endV2 = touchPos;
//var vectorTarget = endV2 - startV2;
//var a = Mathf.Atan2(vectorTarget.y, vectorTarget.x) * Mathf.Rad2Deg;
//obj_CurrTurret.GetComponent<SkeletonAnimation>()
if (isThree)
{
BullitMove bulletMove1 = CreateBullet(angle - 20, startV2);
BullitMove bulletMove2 = CreateBullet(angle, startV2);
BullitMove bulletMove3 = CreateBullet(angle + 20, startV2);
if (isLock)
{
bulletMove1?.Lock(monCotl_Lock);
bulletMove2?.Lock(monCotl_Lock);
bulletMove3?.Lock(monCotl_Lock);
}
}
else
{
BullitMove bulletMove = CreateBullet(angle, startV2);
if (isLock)
{
bulletMove?.Lock(monCotl_Lock);
}
}
}
public Toggle toggle_Shuang;
bool isShuang= false;
private BullitMove CreateBullet(float angle, Vector2 startV2)
{
GameObject bulletPrefab;
//这里有双倍炮台的需求
if (isShuang)
{
bulletPrefab = currSitData.currentTurret.bulletPrefab_Two;
if (!currSitData.ConsumeGold(2))
{
return null;
}
}
else
{
bulletPrefab = currSitData.currentTurret.bulletPrefab;
if (!currSitData.ConsumeGold())
{
return null;
}
}
var bulletMove = Instantiate(bulletPrefab, startV2, Quaternion.Euler(Vector3.forward * angle), bulletParent.transform).GetComponent<BullitMove>();
bulletMove.user = GameManager.Instance.gameData.roomJoinNotice;
bulletMove.playerID = GameManager.Instance.gameData.currUser;
currSitData.skeletonAnimation.AnimationState.SetAnimation(0, "shot", false);
bulletCount++;
return bulletMove;
}
protected override void OnShow(object args = null)
{
base.OnShow(args);
//UIManager.Instance.ShowBoard("太好看了");
var v = JsonConvert.DeserializeObject<WebSocketResponseBase<RoomJoinNotice>>(args.ToString());
if (v.arguments.Count > 0)
{
GameManager.Instance.gameData.roomJoinNotice = v.arguments[0];
RoomJoinNotice roomJoinNotice = v.arguments[0];
JoinGame(roomJoinNotice.position, roomJoinNotice.gold, roomJoinNotice.cannonMultiple, roomJoinNotice.cannonId);
currSitData = list_Sits[roomJoinNotice.position - 1];
currSitData.ChangeTurret(1);
}
else
{
UIManager.Instance.ShowBoard("获取信息失败");
}
}
protected override void OnHide()
{
base.OnHide();
CreateFishManager.Instance.ExitRoom();
}
}