Unity進階–通過PhotonServer實現(xiàn)人物移動和攻擊–PhotonServer(五)
DLc: 消息類和通信類
-
Message
namespace Net { public class Message { public byte Type; public int Command; public object Content; public Message() { } public Message(byte type, int command, object content) { Type = type; Command = command; Content = content; } } //消息類型 public class MessageType { //unity //類型 public static byte Type_UI = 0; //賬號登錄注冊 public const byte Type_Account = 1; //用戶 public const byte Type_User = 2; //攻擊 public const byte Type_Battle = 3; //注冊賬號 public const int Account_Register = 100; public const int Account_Register_Res = 101; //登陸 public const int Account_Login = 102; public const int Account_Login_res = 103; //角色部分 //選擇角色 public const int User_Select = 204; public const int User_Select_res = 205; public const int User_Create_Event = 206; //刪除角色 public const int User_Remove_Event = 207; //攻擊和移動 //移動point[] public const int Battle_Move = 301; //移動響應(yīng)id point[] public const int Battle_Move_Event = 302; //攻擊 targetid public const int Battle_Attack = 303; //攻擊響應(yīng)id targetid 剩余血量 public const int Battle_Attack_Event = 304; } }
-
peer類
using System; using System.Collections.Generic; using Net; using Photon.SocketServer; using PhotonHostRuntimeInterfaces; using PhotonServerFirst.Bll; namespace PhotonServerFirst { public class PSPeer : ClientPeer { public PSPeer(InitRequest initRequest) : base(initRequest) { } //處理客戶端斷開的后續(xù)工作 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { //關(guān)閉管理器 BLLManager.Instance.accountBLL.OnDisconnect(this); BLLManager.Instance.userBLL.OnDisconnect(this); } //處理客戶端的請求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { PSTest.log.Info("收到客戶端的消息"); var dic = operationRequest.Parameters; //打包,轉(zhuǎn)為PhotonMessage Message message = new Message(); message.Type = (byte)dic[0]; message.Command = (int)dic[1]; List<object> objs = new List<object>(); for (byte i = 2; i < dic.Count; i++) { objs.Add(dic[i]); } message.Content = objs.ToArray(); //消息分發(fā) switch (message.Type) { case MessageType.Type_Account: //PSTest.log.Info("收到客戶端的登陸消息"); BLLManager.Instance.accountBLL.OnOperationRequest(this, message); break; case MessageType.Type_User: BLLManager.Instance.userBLL.OnOperationRequest(this, message); break; case MessageType.Type_Battle: PSTest.log.Info("收到攻擊移動命令"); BLLManager.Instance.battleMoveBLL.OnOperationRequest(this, message); break; } } } }
-
-
客戶端對接類
using System.Collections; using System.Collections.Generic; using UnityEngine; using ExitGames.Client.Photon; using Net; public class PhotonManager : MyrSingletonBase<PhotonManager>, IPhotonPeerListener { private PhotonPeer peer; void Awake() { base.Awake(); DontDestroyOnLoad(this); } // Start is called before the first frame update void Start() { peer = new PhotonPeer(this, ConnectionProtocol.Tcp); peer.Connect("127.0.0.1:4530", "PhotonServerFirst"); } void Update() { peer.Service(); } private void OnDestroy() { base.OnDestroy(); //斷開連接 peer.Disconnect(); } public void DebugReturn(DebugLevel level, string message) { } /// <summary> /// 接收服務(wù)器事件 /// </summary> /// <param name="eventData"></param> public void OnEvent(EventData eventData) { //拆包 Message msg = new Message(); msg.Type = (byte)eventData.Parameters[0]; msg.Command = (int)eventData. Parameters[1]; List<object> list = new List<object>(); for (byte i = 2; i < eventData.Parameters.Count; i++){ list.Add(eventData.Parameters[i]); } msg.Content = list.ToArray(); MessageCenter.SendMessage(msg); } /// <summary> /// 接收服務(wù)器響應(yīng) /// </summary> /// <param name="operationResponse"></param> public void OnOperationResponse(OperationResponse operationResponse) { if (operationResponse.OperationCode == 1){ Debug.Log(operationResponse.Parameters[1]); } } /// <summary> /// 狀態(tài)改變 /// </summary> /// <param name="statusCode"></param> public void OnStatusChanged(StatusCode statusCode) { Debug.Log(statusCode); } /// <summary> /// 發(fā)送消息 /// </summary> public void Send(byte type, int command, params object[] objs) { Dictionary<byte, object> dic = new Dictionary<byte,object>(); dic.Add(0,type); dic.Add(1,command); byte i = 2; foreach (object o in objs){ dic.Add(i++, o); } peer.OpCustom(0, dic, true); } }
服務(wù)器
-
BLL管理
using PhotonServerFirst.Bll.BattleMove; using PhotonServerFirst.Bll.User; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PhotonServerFirst.Bll { public class BLLManager { private static BLLManager bLLManager; public static BLLManager Instance { get { if(bLLManager == null) { bLLManager = new BLLManager(); } return bLLManager; } } //登錄注冊管理 public IMessageHandler accountBLL; //角色管理 public IMessageHandler userBLL; //移動和攻擊管理 public IMessageHandler battleMoveBLL; private BLLManager() { accountBLL = new PhsotonServerFirst.Bll.Account.AccountBLL(); userBLL = new UserBLL(); battleMoveBLL = new BattleMoveBLL(); } }
}
-
移動BLL
using Net; using PhotonServerFirst.Dal; using PhotonServerFirst.Model.User; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PhotonServerFirst.Bll.BattleMove { class BattleMoveBLL : IMessageHandler { public void OnDisconnect(PSPeer peer) { } public void OnOperationRequest(PSPeer peer, Message message) { object[] objs = (object[])message.Content; switch (message.Command) { case MessageType.Battle_Move: PSTest.log.Info("BattleMove收到移動命令"); Move(peer, objs); break; case MessageType.Battle_Attack: Attack(peer, objs); break; } } private void Attack(PSPeer peer, object[] objs) { //targetid //id targetid 剩余血量 int targetid = (int)objs[0]; //攻擊者 UserModel user = DALManager.Instance.userDAL.GetUserModel(peer); //被攻擊者 UserModel targetUser = DALManager.Instance.userDAL.GetUserModel(targetid); //計算傷害 targetUser.Hp -= user.userInfo.Attack; foreach (PSPeer otherpeer in DALManager.Instance.userDAL.GetuserPeers()) { SendMessage.Send(otherpeer, MessageType.Type_Battle, MessageType.Battle_Attack_Event, user.ID, targetUser.ID, targetUser.Hp); } } private void Move(PSPeer peer, object[] objs) { //位置 float[] points = (float[])objs[0]; //將要移動的客戶端 UserModel user = DALManager.Instance.userDAL.GetUserModel(peer); user.Points = points; //通知所有客戶端移動 foreach (PSPeer otherpeer in DALManager.Instance.userDAL.GetuserPeers()) { PSTest.log.Info("通知所有客戶端移動"); SendMessage.Send(otherpeer, MessageType.Type_Battle, MessageType.Battle_Move_Event, user.ID, points); } } } }
客戶端
-
邏輯類文章來源:http://www.zghlxwxcb.cn/news/detail-660331.html
using System.Collections; using System.Collections.Generic; using Net; using UnityEngine; public class BattleManager : ManagerBase { void Start() { MessageCenter.Instance.Register(this); } private UserControll user; public UserControll User{ get{ if (user == null){ user = UserControll.idUserDic[UserControll.ID]; } return user; } } void Update() { if (Input.GetMouseButtonDown(0)){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; bool res = Physics.Raycast(ray, out hit); if (res) { if (hit.collider.tag =="Ground"){ //Debug.Log("點擊到地面"); //移動到hit.point PhotonManager.Instance.Send(MessageType.Type_Battle, MessageType.Battle_Move, new float[] { hit.point.x,hit.point.y, hit.point.z }); //Debug.Log(hit.point); } if (hit.collider.tag =="User"){ //Debug.Log("點擊到玩家"); //獲取距離 float dis = Vector3.Distance(hit.collider.transform.position, User.transform.position); if (dis > 0 && dis < 3f){ UserControll targetUser = hit.collider.GetComponent<UserControll>(); PhotonManager.Instance.Send(MessageType.Type_Battle, MessageType.Battle_Attack, targetUser.id); } } } } } public override void ReceiveMessage(Message message){ base.ReceiveMessage(message); object[] objs = (object[])message.Content; switch (message. Command) { case MessageType.Battle_Move_Event: Debug.Log("移動"); Move(objs); break; case MessageType.Battle_Attack_Event: Attack(objs); break; } } //移動 void Move(object[] objs){ //移動的用戶id int userid = (int)objs[0]; //移動的位置 float[] points = (float[])objs[1]; UserControll.idUserDic[ userid].Move(new Vector3(points[0],points[1],points[2])); } public override byte GetMessageType(){ return MessageType.Type_Battle; } public void Attack(object[] objs){ //攻擊者id被攻擊者id 當前剩余血量 int userid = (int)objs[0]; int targetid = (int)objs[1]; int hp = (int)objs[2]; //攻擊 UserControll.idUserDic[userid].Attack(targetid); if (hp <= 0) { UserControll.idUserDic[targetid].Die(); } } }
-
角色綁定的類文章來源地址http://www.zghlxwxcb.cn/news/detail-660331.html
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class UserControll : MonoBehaviour { //保存了所有角色的集合 public static Dictionary<int, UserControll> idUserDic = new Dictionary<int, UserControll>(); //當前客戶端角色的id public static int ID; private Animator ani; //private NavMeshAgent agent; //目標位置 private Vector3 targetPos; private bool isRun; //角色id public int id; private bool isDie; // Start is called before the first frame update void Start() { ani = GetComponent<Animator>(); //agent = GetComponent<NavMeshAgent>(); } // Update is called once per frame void Update() { if(isDie){ return; } if (isRun){ //計算距離 float dis = Vector3.Distance(transform. position,targetPos); if (dis > 0.5f) { //移動 //agent.isStopped = false; // ani.SetBoo1( "IsRun", true); //agent.SetDestination(targetPos); transform.position = targetPos; } else { //停止 //agent.isStopped = true; // ani.setBoo1("IsRun", false); isRun = false; } } } public void Move(Vector3 target){ targetPos = target; isRun = true; } public void Attack( int targetId){ //獲得要攻擊的角色 UserControll targetUser = idUserDic[targetId]; transform.LookAt(targetUser.transform); //ani.SetTrigger( "Attack"); } public void Die(){ //ani. setTrigger("Die"); isDie = true; } } using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class UserControll : MonoBehaviour { //保存了所有角色的集合 public static Dictionary<int, UserControll> idUserDic = new Dictionary<int, UserControll>(); //當前客戶端角色的id public static int ID; private Animator ani; //private NavMeshAgent agent; //目標位置 private Vector3 targetPos; private bool isRun; //角色id public int id; private bool isDie; // Start is called before the first frame update void Start() { ani = GetComponent<Animator>(); //agent = GetComponent<NavMeshAgent>(); } // Update is called once per frame void Update() { if(isDie){ return; } if (isRun){ //計算距離 float dis = Vector3.Distance(transform. position,targetPos); if (dis > 0.5f) { //移動 //agent.isStopped = false; // ani.SetBoo1( "IsRun", true); //agent.SetDestination(targetPos); transform.position = targetPos; } else { //停止 //agent.isStopped = true; // ani.setBoo1("IsRun", false); isRun = false; } } } public void Move(Vector3 target){ targetPos = target; isRun = true; } public void Attack( int targetId){ //獲得要攻擊的角色 UserControll targetUser = idUserDic[targetId]; transform.LookAt(targetUser.transform); //ani.SetTrigger( "Attack"); } public void Die(){ //ani. setTrigger("Die"); isDie = true; } }
到了這里,關(guān)于Unity進階–通過PhotonServer實現(xiàn)人物移動和攻擊–PhotonServer(五)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!