服务端
using Google.Protobuf;
using Pb;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;
namespace Server007
{
internal class Player : Singleton<Player>
{
public void Init()
{
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_SELECTHERO, CS_SELECTHERO);//选英雄回调
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_SX, CS_SX);//上线回调
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_XX, CS_XX);//下线回调
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_MOVE, CS_MOVE);//移动
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_ZD, CS_ZD);//发射子弹
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_HP, CS_HP);//HP
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_JX, CS_SubtractHP);//减血
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_DIE, CS_DIE);//死亡
MessageCenter<MsaData>.Instance.OnAdd(MsgID.CS_ACTION, CS_ACTION);
}
private void CS_ACTION(MsaData data)
{
foreach (var item in NetManager.Instance.clients)
{
if (item.name != null)
{
NetManager.Instance.OnSend(MsgID.CS_ACTION, data.client.playerPos.ToByteArray(), item);
}
}
}
/// <summary>
/// 血量为零删除
/// </summary>
/// <param name="data"></param>
private void CS_DIE(MsaData data)
{
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
if (data.client.playerPos.Id != NetManager.Instance.clients[i].playerPos.Id)
{
NetManager.Instance.OnSend(MsgID.SC_DIE, data.client.playerPos.ToByteArray(), NetManager.Instance.clients[i]);
}
}
}
/// <summary>
/// 减血
/// </summary>
/// <param name="data"></param>
private void CS_SubtractHP(MsaData data)
{
data.client.playerPos.Nowhp -= 1;
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
NetManager.Instance.OnSend(MsgID.SC_JX, data.client.playerPos.ToByteArray(), NetManager.Instance.clients[i]);
}
}
/// <summary>
/// 血条同步
/// </summary>
/// <param name="data"></param>
private void CS_HP(MsaData data)
{
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
if (data.client.playerPos.Id != NetManager.Instance.clients[i].playerPos.Id)
{
NetManager.Instance.OnSend(MsgID.SC_HP, data.client.playerPos.ToByteArray(), NetManager.Instance.clients[i]);
}
}
}
/// <summary>
/// 发射子弹同步
/// </summary>
/// <param name="data"></param>
private void CS_ZD(MsaData data)
{
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
if (data.client.playerPos.Id != NetManager.Instance.clients[i].playerPos.Id)
{
NetManager.Instance.OnSend(MsgID.SC_ZD, data.client.playerPos.ToByteArray(), NetManager.Instance.clients[i]);
Console.WriteLine(data.client.playerPos.Id);
}
}
}
/// <summary>
/// 移动同步
/// </summary>
/// <param name="data"></param>
private void CS_MOVE(MsaData data)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(data.data);
data.client.playerPos.X = playerPos.X;
data.client.playerPos.Z = playerPos.Z;
data.client.playerPos.Roty = playerPos.Roty;
data.client.playerPos.Start = playerPos.Start;
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
if (playerPos.Id != NetManager.Instance.clients[i].playerPos.Id)
{
NetManager.Instance.OnSend(MsgID.SC_MOVE, playerPos.ToByteArray(), NetManager.Instance.clients[i]);
}
}
}
/// <summary>
/// 下线同步
/// </summary>
/// <param name="data"></param>
private void CS_XX(MsaData data)
{
NetManager.Instance.clients.Remove(data.client);
//如果下线英雄的id和缓存里的id一样就删除
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
NetManager.Instance.OnSend(MsgID.SC_XX, data.client.playerPos.ToByteArray(), NetManager.Instance.clients[i]);
}
}
/// <summary>
/// 上线同步
/// </summary>
/// <param name="data"></param>
private void CS_SX(MsaData data)
{
OnLionPlayer list = new OnLionPlayer();
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
if (data.client.playerPos.Id != NetManager.Instance.clients[i].playerPos.Id)
{
list.Player.Add(NetManager.Instance.clients[i].playerPos);
}
}
NetManager.Instance.OnSend(MsgID.SC_SX, list.ToByteArray(), data.client);
}
int id = 1;
int pos = 0;
/// <summary>
/// 选英雄
/// </summary>
/// <param name="data"></param>
private void CS_SELECTHERO(MsaData data)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(data.data);
playerPos.Id = id;
playerPos.X = pos;
playerPos.Roty = 180;
playerPos.Nowhp = 3;
playerPos.Hp = 3;
data.client.playerPos = playerPos;
id++;
pos += 2;
NetManager.Instance.clients.Add(data.client);
NetManager.Instance.OnSend(MsgID.SC_SELECTHERO, playerPos.ToByteArray(), data.client);
for (int i = 0; i < NetManager.Instance.clients.Count; i++)
{
if (data.client.playerPos.Id != NetManager.Instance.clients[i].playerPos.Id)
{
NetManager.Instance.OnSend(MsgID.SC_NEWSX, playerPos.ToByteArray(), NetManager.Instance.clients[i]);
}
}
}
}
}
unity客户端
using Pb;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameAtk : MonoBehaviour
{
List<OtherPlayer> otherPlayers = new List<OtherPlayer>();
private void Awake()
{
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_SX, SC_SX);
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_NEWSX, SC_NEWSX);//主动推送(后来上线的)
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_XX, SC_XX);
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_MOVE, SC_MOVE);
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_ZD, SC_ZD);
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_JX, SC_JX);
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_DIE, SC_DIE);
MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_ACTION, SC_ACTION);
// MessageCenter<byte[]>.Instance.OnAdd(MsgID.SC_HP, SC_HP);
}
private void SC_ACTION(byte[] obj)
{
Actio actio = Actio.Parser.ParseFrom(obj);
if(actio.Action==1)
{
GameObject g=GameObject.CreatePrimitive(PrimitiveType.Sphere);
g.transform.localPosition = Vector3.one * 0.5f;
}
}
/// <summary>
/// 死亡回调
/// </summary>
/// <param name="obj"></param>
private void SC_DIE(byte[] obj)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
for (int i = 0; i < otherPlayers.Count; i++)
{
if (otherPlayers[i].playerPos.Id == playerPos.Id)
{
Destroy(otherPlayers[i].gameObject);
otherPlayers.Remove(otherPlayers[i]);
}
}
}
/// <summary>
/// 减血回调
/// </summary>
/// <param name="obj"></param>
private void SC_JX(byte[] obj)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
if (playerPos.Id == PlayerModel.Instance.PlayerPos.Id)
{
PlayerModel.Instance.PlayerPos = playerPos;
player1.GetComponent<PlayerMove>().sliderHP.SetHP(playerPos.Nowhp, playerPos.Hp);
}
else
{
for (int i = 0; i < otherPlayers.Count; i++)
{
if (otherPlayers[i].playerPos.Id == playerPos.Id)
{
otherPlayers[i].playerPos = playerPos;
otherPlayers[i].sliderHP.SetHP(playerPos.Nowhp, playerPos.Hp);
}
}
}
}
//private void SC_HP(byte[] obj)
//{
// PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
// for (int i = 0; i < otherPlayers.Count; i++)
// {
// if (otherPlayers[i].playerPos.Id == playerPos.Id)
// {
// otherPlayers[i].HP();
// }
// }
//}
/// <summary>
/// 发射子弹
/// </summary>
/// <param name="obj"></param>
private void SC_ZD(byte[] obj)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
for (int i = 0; i < otherPlayers.Count; i++)
{
if (otherPlayers[i].playerPos.Id == playerPos.Id)
{
otherPlayers[i].Fs();
}
}
}
/// <summary>
/// 移动
/// </summary>
/// <param name="obj"></param>
/// <exception cref="NotImplementedException"></exception>
private void SC_MOVE(byte[] obj)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
for (int i = 0; i < otherPlayers.Count; i++)
{
if (otherPlayers[i].playerPos.Id == playerPos.Id)
{
otherPlayers[i].SetMove(playerPos.X, playerPos.Z, playerPos.Roty);
otherPlayers[i].SetAni(playerPos.Start);
}
}
}
/// <summary>
/// 下线
/// </summary>
/// <param name="obj"></param>
private void SC_XX(byte[] obj)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
for (int i = 0; i < otherPlayers.Count; i++)
{
if (otherPlayers[i].playerPos.Id == playerPos.Id)
{
Destroy(otherPlayers[i].gameObject);
otherPlayers.Remove(otherPlayers[i]);
}
}
}
/// <summary>
/// 新上线的玩家
/// </summary>
/// <param name="obj"></param>
private void SC_NEWSX(byte[] obj)
{
PlayerPos playerPos = PlayerPos.Parser.ParseFrom(obj);
GameObject player = Instantiate(Resources.Load<GameObject>(playerPos.Roleid.ToString()));
player.transform.position = new Vector3(playerPos.X, 0, playerPos.Z);
player.transform.eulerAngles = new Vector3(0, playerPos.Roty, 0);
player.transform.localScale = Vector3.one;
player.AddComponent<OtherPlayer>().playerPos = playerPos;
otherPlayers.Add(player.GetComponent<OtherPlayer>());
}
/// <summary>
/// 前面上线的玩家
/// </summary>
/// <param name="obj"></param>
private void SC_SX(byte[] obj)
{
OnLionPlayer onLionPlayer = OnLionPlayer.Parser.ParseFrom(obj);
for (int i = 0; i < onLionPlayer.Player.Count; i++)
{
GameObject player = Instantiate(Resources.Load<GameObject>(onLionPlayer.Player[i].Roleid.ToString()));
player.transform.position = new Vector3(onLionPlayer.Player[i].X, 0, onLionPlayer.Player[i].Z);
player.transform.eulerAngles = new Vector3(0, onLionPlayer.Player[i].Roty, 0);
player.transform.localScale = Vector3.one;
player.AddComponent<OtherPlayer>().playerPos = onLionPlayer.Player[i];
otherPlayers.Add(player.GetComponent<OtherPlayer>());
}
}
GameObject player1;
// Start is called before the first frame update
void Start()
{
player1 = Instantiate(Resources.Load<GameObject>(PlayerModel.Instance.PlayerPos.Roleid.ToString()));
player1.transform.position = new Vector3(PlayerModel.Instance.PlayerPos.X, 0, 0);
player1.transform.localScale = Vector3.one;
player1.AddComponent<PlayerMove>();
NetManager.Instance.OnSend(MsgID.CS_SX, new byte[1]);
}
// Update is called once per frame
void Update()
{
}
}