commit
2187bc4466
6 changed files with 83 additions and 57 deletions
|
@ -27,10 +27,12 @@ namespace ZoFo.GameCore
|
|||
|
||||
public void GameEndedUnexpectedly(){ }
|
||||
|
||||
public void JoinRoom(){ }
|
||||
|
||||
public void JoinYourself(){ }
|
||||
public void JoinRoom(string ip)
|
||||
{
|
||||
networkManager.JoinRoom(ip);
|
||||
}
|
||||
|
||||
public void JoinYourself(){ networkManager.JoinYourself(); }
|
||||
internal void Update(GameTime gameTime)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Xml;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
|
@ -25,7 +26,7 @@ public class MainMenuGUI : AbstractGUI
|
|||
Elements.Add(menuBackground);
|
||||
menuBackground.LoadTexture(AppManager.Instance.Content);
|
||||
|
||||
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 5, (int)(width / 4), (int)(height / 20)), text = "ZoFo", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font"});
|
||||
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 5, (int)(width / 4), (int)(height / 20)), text = "ZoFo", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font" });
|
||||
|
||||
|
||||
Button playButton = new Button(Manager)
|
||||
|
@ -73,7 +74,6 @@ public class MainMenuGUI : AbstractGUI
|
|||
|
||||
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
base.Update(gameTime);
|
||||
|
|
|
@ -37,7 +37,7 @@ namespace ZoFo.GameCore.GameManagers
|
|||
public InputManager InputManager;
|
||||
public ItemManager.ItemManager ItemManager;
|
||||
|
||||
public AnimationBuilder animationBuilder{get;set; }
|
||||
public AnimationBuilder animationBuilder { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
|
@ -45,7 +45,7 @@ namespace ZoFo.GameCore.GameManagers
|
|||
{
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
SetResolution(CurentScreenResolution.X, CurentScreenResolution.Y);
|
||||
FulscrreenSwitch();
|
||||
// FulscrreenSwitch();
|
||||
|
||||
|
||||
Content.RootDirectory = "Content";
|
||||
|
@ -68,8 +68,6 @@ namespace ZoFo.GameCore.GameManagers
|
|||
debugHud.Initialize();
|
||||
|
||||
|
||||
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
|
@ -160,5 +158,8 @@ namespace ZoFo.GameCore.GameManagers
|
|||
{
|
||||
_graphics.IsFullScreen = !_graphics.IsFullScreen;
|
||||
}
|
||||
|
||||
public void SetServer(Server server) { this.server = server; }
|
||||
public void SetClient(Client client) { this.client = client; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ using System.Net;
|
|||
using System.Net.Sockets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using ZoFo.GameCore.GameManagers.NetworkManager.Updates;
|
||||
|
@ -20,6 +21,12 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
List<IUpdateData> updates = new List<IUpdateData>();
|
||||
public delegate void OnDataSent(string Data);
|
||||
public event OnDataSent GetDataSent; // event
|
||||
|
||||
public ClientNetworkManager()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
public void Init() //create endPoint, socket
|
||||
{
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
@ -27,11 +34,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
|
||||
public void SendData()
|
||||
{
|
||||
while(socket.Connected)
|
||||
{
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(updates.ToString());
|
||||
byte[] bytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(updates)); //нужно сериализовать
|
||||
socket.Send(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddData(IUpdateData UpdateData)
|
||||
{
|
||||
updates.Add(UpdateData);
|
||||
}
|
||||
|
||||
public void StopConnection()
|
||||
|
@ -47,10 +56,9 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
/// <param name="port"></param>
|
||||
public void JoinRoom(string ip) // multyplayer
|
||||
{
|
||||
|
||||
endPoint = new IPEndPoint(IPAddress.Parse(ip), port);
|
||||
|
||||
socket.Connect(endPoint);
|
||||
|
||||
SendData();
|
||||
Thread listen = new Thread(StartListening);
|
||||
listen.Start();
|
||||
|
@ -62,9 +70,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
public void JoinYourself() // single player
|
||||
{
|
||||
endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
|
||||
|
||||
socket.Connect(endPoint);
|
||||
|
||||
SendData();
|
||||
Thread listen = new Thread(StartListening);
|
||||
listen.Start();
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
{
|
||||
public class ServerNetworkManager
|
||||
{
|
||||
private IPAddress ip = IPAddress.Any;
|
||||
private IPAddress ip =IPAddress.Parse("127.0.0.1"); //IPAddress.Any
|
||||
private int port = 7632;
|
||||
private IPEndPoint endPoint;
|
||||
private Socket socket;
|
||||
|
@ -25,12 +25,18 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
public delegate void OnDataSend(string data);
|
||||
public event OnDataSend GetDataSend; // event
|
||||
Dictionary<Socket, Thread> managerThread;
|
||||
Thread serverTheread;
|
||||
|
||||
public ServerNetworkManager() { Init(); }
|
||||
|
||||
public void Init() //create Socket
|
||||
{
|
||||
endPoint = new IPEndPoint(ip, port);
|
||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
managerThread = new Dictionary<Socket, Thread>();
|
||||
clients = new List<Socket>();
|
||||
updates = new List<IUpdateData>();
|
||||
managerThread = new Dictionary<Socket, Thread>();
|
||||
}
|
||||
/// <summary>
|
||||
/// отправляет клиенту Data
|
||||
|
@ -52,7 +58,6 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
{
|
||||
updates.Add(data);
|
||||
}
|
||||
|
||||
public void CloseConnection() //По сути коне игры и отключение игроков
|
||||
{
|
||||
foreach (var item in clients)
|
||||
|
@ -74,6 +79,12 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
|||
clients.Clear();
|
||||
}
|
||||
|
||||
public void Start(object players)
|
||||
{
|
||||
serverTheread = new Thread(StartWaitingForPlayers);
|
||||
serverTheread.Start(players);
|
||||
}
|
||||
|
||||
//Потоки Клиентов
|
||||
public void StartWaitingForPlayers(object players)//Слушает игроков, которые хотят подключиться
|
||||
{
|
||||
|
|
|
@ -37,20 +37,26 @@ namespace ZoFo.GameCore
|
|||
{
|
||||
networkManager.AddData(data);
|
||||
}
|
||||
public void CreateRoom(int players) //Создает комнату и запускает ожидание подключений
|
||||
/// <summary>
|
||||
/// Создает комнату и запускает ожидание подключений
|
||||
/// </summary>
|
||||
/// <param name="players"></param>
|
||||
public void CreateRoom(int players)
|
||||
{
|
||||
networkManager.StartWaitingForPlayers(players);
|
||||
networkManager.Start(players);
|
||||
}
|
||||
|
||||
// public void StartGame() { } принудительный запуск
|
||||
public void EndGame() //Добавляет UpdateGameEnded и отключает игроков
|
||||
|
||||
/// <summary>
|
||||
/// Добавляет UpdateGameEnded и отключает игроков
|
||||
/// </summary>
|
||||
public void EndGame()
|
||||
{
|
||||
UpdateGameEnded gameEnded = new UpdateGameEnded();
|
||||
networkManager.AddData(gameEnded);
|
||||
networkManager.CloseConnection();
|
||||
}
|
||||
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue