using System; using System.Collections.Generic; using ET; using FairyGUI; using Cal.DataTable; using UnityEngine; namespace ET { public class OperaComponentAwakeSystem: AwakeSystem { public override void Awake(OperaComponent self) { self.Awake(); } } public class OperaComponentUpdateSystem: UpdateSystem { public override void Update(OperaComponent self) { self.Update(); } } public class OperaComponent: Entity { public Vector3 ClickPoint; public int mapMask; private int m_OtherPlayerLayerMask; private int m_NPCLayerMask; private int m_SceneUnitLayerMask; private int m_MonsterLayerMask; private Dictionary shortKeyDic = new Dictionary(); private Unit unit; private const float AtkDis = 2.5f; private readonly Frame_ClickMap frameClickMap = new Frame_ClickMap(); private Scene zoneScene; public void Awake() { this.mapMask = LayerMask.GetMask("Map"); m_OtherPlayerLayerMask = 1 << LayerMask.NameToLayer("Player"); m_NPCLayerMask = 1 << LayerMask.NameToLayer("NPC"); m_MonsterLayerMask = 1 << LayerMask.NameToLayer("Monster"); m_SceneUnitLayerMask = 1 << LayerMask.NameToLayer("SceneUnit"); frameClickMap.UnitInfo = new UnitPosInfo(); zoneScene = this.ZoneScene(); unit = zoneScene.GetComponent().MyUnit; InitShortKey(); } private void InitShortKey() { var zoneScene = this.ZoneScene(); shortKeyDic[(int) KeyCode.F1] = () => { SendMainUISlot(0).Coroutine(); }; shortKeyDic[(int) KeyCode.F2] = () => { SendMainUISlot(1).Coroutine(); }; shortKeyDic[(int) KeyCode.F3] = () => { SendMainUISlot(2).Coroutine(); }; shortKeyDic[(int) KeyCode.F4] = () => { SendMainUISlot(3).Coroutine(); }; shortKeyDic[(int) KeyCode.F5] = () => { SendMainUISlot(4).Coroutine(); }; shortKeyDic[(int) KeyCode.F6] = () => { SendMainUISlot(5).Coroutine(); }; shortKeyDic[(int) KeyCode.F7] = () => { SendMainUISlot(6).Coroutine(); }; shortKeyDic[(int) KeyCode.F8] = () => { SendMainUISlot(7).Coroutine(); }; shortKeyDic[(int) KeyCode.F9] = () => { SendMainUISlot(8).Coroutine(); }; shortKeyDic[(int) KeyCode.Escape] = () => { ET.FUIHelper.CloseAllWindows(); }; shortKeyDic[(int) KeyCode.B] = () => { Game.EventSystem.Publish(new ET.EventType.OpenBagUI { zoneScene = MainUI.ui.ZoneScene() }).Coroutine(); }; shortKeyDic[(int) KeyCode.C] = () => { zoneScene.GetComponent().IsMineCharacter = true; Game.EventSystem.Publish(new ET.EventType.OpenCharacterUI { zoneScene = MainUI.ui.ZoneScene() }).Coroutine(); }; shortKeyDic[(int) KeyCode.S] = () => { Game.EventSystem.Publish(new ET.EventType.OpenSkillUI { zoneScene = MainUI.ui.ZoneScene() }).Coroutine(); }; shortKeyDic[(int) KeyCode.T] = () => { Game.EventSystem.Publish(new ET.EventType.TaskUI_Open { zoneScene = MainUI.ui.ZoneScene() }).Coroutine(); }; shortKeyDic[(int) KeyCode.M] = () => { Game.EventSystem.Publish(new ET.EventType.OpenMarketUI { zoneScene = MainUI.ui.ZoneScene() }).Coroutine(); }; shortKeyDic[(int) KeyCode.Keypad0] = async () => { M2C_RequestPersonalPvp ret = await zoneScene.GetComponent() .Call(new C2M_RequestPersonalPvp { }); if (!ret.Message.IsNullOrEmpty()) { TipHelper.OpenUI(ret.Message); } }; shortKeyDic[(int) KeyCode.O] = async () => { M2C_OpenGMUI ret = await zoneScene.GetComponent().Call(new C2M_OpenGMUI { }); if (!ret.Message.IsNullOrEmpty()) { return; } Game.EventSystem.Publish(new ET.EventType.OpenGMUI { zoneScene = MainUI.ui.ZoneScene() }).Coroutine(); }; } private async ETTask SendMainUISlot(int index) { MainUISlot mainUISlot = MainUISlotComponent.Instance.Get(index); if (mainUISlot.MainUIType == MainUIType.SkillSlot) { M2C_UseMainUISkill ret = await zoneScene.GetComponent() .Call(new C2M_UseMainUISkill() { SlotId = index }); if (!ret.Message.IsNullOrEmpty()) { return; } return; } else { M2C_UseMainUIGoods ret = await zoneScene.GetComponent() .Call(new C2M_UseMainUIGoods() { SlotId = index }); if (!ret.Message.IsNullOrEmpty()) { TipHelper.OpenUI(ret.Message); return; } Game.EventSystem.Publish_Sync(new ET.EventType.UpdateBagUI { list = ret.BagMapList }); Game.EventSystem.Publish(new ET.EventType.UpdateMainUISlot { list = ret.MainUISlotList }).Coroutine(); return; } } #if UNITY_ANDROID private int escapeCount; private float lastQuitTime; #endif public void Update() { if (!unit) return; //暂时回城 if (!unit.IsFight && Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.G)) { if (Stage.inst.focus is InputTextField input) { return; } Game.EventSystem.Publish(new ET.EventType.BackMainCity {zoneScene = this.zoneScene}).Coroutine(); } if (Input.anyKeyDown) { if (Stage.inst.focus is InputTextField input) { return; } foreach (var kv in shortKeyDic) { try { KeyCode keyCode = (KeyCode) kv.Key; if (Input.GetKeyDown(keyCode)) { kv.Value?.Invoke(); } } catch (Exception e) { Log.Error(e); } } } //FGUI if (FairyGUI.Stage.isTouchOnUI) return; int hasTarget = ClickTarget(); switch (hasTarget) { default: break; case -1: //!没按左键 break; case 0: //!不用移动 break; case 1: //!移动操作 if (!unit.IsFight) { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, layerMask: mapMask); { if (hit.collider != null) { this.ClickPoint = hit.point; frameClickMap.UnitInfo.X = ClickPoint.x; frameClickMap.UnitInfo.Y = ClickPoint.y; frameClickMap.UnitInfo.YAngle = ClickPoint.x >= unit.Position.x? 0 : 180; zoneScene.GetComponent().Session.Send(frameClickMap); } } } break; } //!点击目标 if (Input.GetMouseButtonUp(0) && unit.IsFight) { Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //射线检测怪物 RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_MonsterLayerMask); if (hit.collider != null) { GameObject go = hit.collider.gameObject; zoneScene.GetComponent().SelectBattleUnitId = ((UnitView) go.GetComponentInParent().Component).Parent.Id; Game.EventSystem.Publish(new ET.EventType.ClickBattleTarget { zoneScene = this.ZoneScene(), unitType = UnitType.Enermy }) .Coroutine(); } else { //射线检测玩家 hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_OtherPlayerLayerMask); if (hit.collider != null) { GameObject go = hit.collider.gameObject; zoneScene.GetComponent().SelectBattleUnitId = ((UnitView) go.GetComponent().Component).Parent.Id; Game.EventSystem.Publish(new ET.EventType.ClickBattleTarget { zoneScene = this.ZoneScene(), unitType = UnitType.TeamMember }) .Coroutine(); } } } //!关闭游戏 #if UNITY_ANDROID if (Input.GetKey(KeyCode.Escape)) { var now = Time.time; if( now-lastQuitTime > 1) { lastQuitTime = now; escapeCount = 1; } else { escapeCount++; if (escapeCount >= 2) ET.Init.Quit(); } } #endif } /// /// /// /// -1: 没点击; 0:不移动; 1:移动; private int ClickTarget() { #if UNITY_STANDALONE if (Input.GetMouseButtonUp(0)) { return 1; } if (Input.GetMouseButtonUp(1) && !unit.IsFight) #elif UNITY_ANDROID if (Input.GetMouseButtonUp(0) && !unit.IsFight) #endif { var zoneScene = this.ZoneScene(); //!移动的时候不能操作 Team team = zoneScene.GetComponent().Get(zoneScene.GetComponent().LeaderId); foreach (Unit unit in team.GetAll()) { if (unit.GetComponent().MoveTimer != 0) #if UNITY_STANDALONE return -1; #else return 1; #endif } Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //射线检测怪物 RaycastHit2D hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_MonsterLayerMask); if (hit.collider != null) { GameObject go = hit.collider.gameObject; if (CheckDis(go)) return 0; UnitView unit = go.GetComponentInParent().Component as UnitView; MonsterInfo monsterInfo = unit.Parent.GetComponent(); Game.EventSystem.Publish(new ET.EventType.OnClickMonster {zoneScene=zoneScene, monsterInfo = monsterInfo }).Coroutine(); ClickActionComponent clickAction = unit.Parent.GetComponent(); if (clickAction) clickAction.Run(); } else { //射线检测玩家 hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_OtherPlayerLayerMask); if (hit.collider != null) { GameObject go = hit.collider.gameObject; if (CheckDis(go)) return 0; if (!go.Equals(zoneScene.GetComponent().MyUnit.GetComponent()?.gameObject)) { UnitView unit = go.GetComponentInParent().Component as UnitView; zoneScene.GetComponent().SelectUnitId = unit.Parent.Id; Game.EventSystem.Publish(new ET.EventType.ClickOtherPlayer { zoneScene = zoneScene }).Coroutine(); ClickActionComponent clickAction = unit.Parent.GetComponent(); if (clickAction) clickAction.Run(); } } else { //射线检测NPC hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_NPCLayerMask); if (hit.collider != null) { GameObject go = hit.collider.gameObject; if (CheckDis(go)) return 0; UnitView unit = go.GetComponentInParent().Component as UnitView; Game.EventSystem.Publish(new ET.EventType.OnClickNPC { zoneScene = zoneScene,configId = unit.GetParent().ConfigId }).Coroutine(); ClickActionComponent clickAction = unit.Parent.GetComponent(); if (clickAction) clickAction.Run(); } else { //射线检测Unit hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_SceneUnitLayerMask); if (hit.collider != null) { GameObject go = hit.collider.gameObject; if (CheckDis(go)) return 0; UnitView unit = go.GetComponentInParent().Component as UnitView; ClickActionComponent clickAction = unit.Parent.GetComponent(); if (clickAction) clickAction.Run(); } else { #if UNITY_STANDALONE return -1; #else return 1; #endif } } } } return 0; } return -1; } private bool CheckDis(GameObject go) { Vector2 v2 = go.transform.position - zoneScene.GetComponent().MyUnit.Position; if (v2.sqrMagnitude > AtkDis * AtkDis) { TipHelper.OpenUI("您离目标太远!"); return true; } return false; } } }