CTT/Unity/Assets/HotfixView/Entity/OperaComponent.cs

459 lines
16 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using ET;
using FairyGUI;
using Cal.DataTable;
using UnityEngine;
namespace ET
{
public class OperaComponentAwakeSystem : AwakeSystem<OperaComponent>
{
public override void Awake(OperaComponent self)
{
self.Awake();
}
}
public class OperaComponentUpdateSystem : UpdateSystem<OperaComponent>
{
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<int, Action> shortKeyDic = new Dictionary<int, Action>();
private Unit unit;
private const float AtkDis = 2.5f;
private readonly Frame_ClickMap frameClickMap = new Frame_ClickMap();
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();
unit = UnitComponent.MyUnit;
InitShortKey();
}
private void InitShortKey()
{
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] = () =>
{
GlobalVariable.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() =>
{
var ret = await SessionComponent.Instance.Call<M2C_RequestPersonalPvp>(new C2M_RequestPersonalPvp { });
if (!ret.Message.IsNullOrEmpty())
{
TipHelper.OpenUI(ret.Message);
}
};
shortKeyDic[(int)KeyCode.O] =async () =>
{
var ret = await SessionComponent.Instance.Call<M2C_OpenGMUI>(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)
{
var mainUISlot = MainUISlotComponent.Instance.Get(index);
if (mainUISlot.MainUIType == MainUIType.SkillSlot)
{
var ret = await SessionComponent.Instance.Call<M2C_UseMainUISkill>(new C2M_UseMainUISkill() { SlotId = index });
if (!ret.Message.IsNullOrEmpty())
{
return;
}
return;
}
else
{
var ret = await SessionComponent.Instance.Call<M2C_UseMainUIGoods>(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
{
}).Coroutine();
}
if (!unit.IsFight &&
Input.GetKey(KeyCode.LeftControl) &&
Input.GetKeyDown(KeyCode.H))
{
if (Stage.inst.focus is InputTextField input)
{
return;
}
SessionComponent.Instance.Session.Send(new C2M_GetBuffTime());
}
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;
SessionComponent.Instance.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)
{
var go = hit.collider.gameObject;
GlobalVariable.SelectBattleUnitId = ((UnitView)go.GetComponentInParent<ComponentView>().Component).Parent.Id;
Game.EventSystem.Publish(new ET.EventType.ClickBattleTarget
{
unitType = UnitType.Enermy
}).Coroutine();
}
else
{
//射线检测玩家
hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_OtherPlayerLayerMask);
if (hit.collider != null)
{
var go = hit.collider.gameObject;
GlobalVariable.SelectBattleUnitId = ((UnitView)go.GetComponent<ComponentView>().Component).Parent.Id;
Game.EventSystem.Publish(new ET.EventType.ClickBattleTarget
{
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
}
/// <summary>
///
/// </summary>
/// <returns>-1: 没点击; 0不移动 1移动</returns>
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
{
//!移动的时候不能操作
Team team = TeamComponent.Instance.Get(GlobalVariable.LeaderId);
foreach (var unit in team.GetAll())
{
if(unit.GetComponent<MoveComponent>().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)
{
var go = hit.collider.gameObject;
if (CheckDis(go)) return 0;
UnitView unit = go.GetComponentInParent<ComponentView>().Component as UnitView;
MonsterInfo monsterInfo = unit.Parent.GetComponent<MonsterInfo>();
Game.EventSystem.Publish(new ET.EventType.OnClickMonster
{
monsterInfo = monsterInfo
}).Coroutine();
ClickActionComponent clickAction = unit.Parent.GetComponent<ClickActionComponent>();
if (clickAction)
clickAction.Run();
}
else
{
//射线检测玩家
hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_OtherPlayerLayerMask);
if (hit.collider != null)
{
var go = hit.collider.gameObject;
if (CheckDis(go)) return 0;
if (!go.Equals(UnitComponent.MyUnit.GetComponent<UnitView>()?.gameObject))
{
UnitView unit = go.GetComponentInParent<ComponentView>().Component as UnitView;
GlobalVariable.SelectUnitId = unit.Parent.Id;
Game.EventSystem.Publish(new ET.EventType.ClickOtherPlayer
{
}).Coroutine();
ClickActionComponent clickAction = unit.Parent.GetComponent<ClickActionComponent>();
if (clickAction)
clickAction.Run();
}
}
else
{
//射线检测NPC
hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_NPCLayerMask);
if (hit.collider != null)
{
var go = hit.collider.gameObject;
if (CheckDis(go)) return 0;
UnitView unit = go.GetComponentInParent<ComponentView>().Component as UnitView;
Game.EventSystem.Publish(new ET.EventType.OnClickNPC
{
configId = unit.GetParent<Unit>().ConfigId
}).Coroutine();
ClickActionComponent clickAction = unit.Parent.GetComponent<ClickActionComponent>();
if (clickAction)
clickAction.Run();
}
else
{
//射线检测Unit
hit = Physics2D.Raycast(mousePos, Vector2.zero, 100, m_SceneUnitLayerMask);
if (hit.collider != null)
{
var go = hit.collider.gameObject;
if (CheckDis(go)) return 0;
UnitView unit = go.GetComponentInParent<ComponentView>().Component as UnitView;
ClickActionComponent clickAction = unit.Parent.GetComponent<ClickActionComponent>();
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 - UnitComponent.MyUnit.Position;
if (v2.sqrMagnitude > AtkDis * AtkDis)
{
TipHelper.OpenUI("您离目标太远!");
return true;
}
return false;
}
}
}