StartCreateLogicMultiPlayer3
This commit is contained in:
commit
215b538715
9 changed files with 94 additions and 56 deletions
|
@ -14,36 +14,30 @@
|
||||||
"scaleInDock": 1
|
"scaleInDock": 1
|
||||||
},
|
},
|
||||||
"TileMaps/main.tmj": {
|
"TileMaps/main.tmj": {
|
||||||
"scale": 1,
|
"scale": 0.25,
|
||||||
"selectedLayer": 0,
|
"selectedLayer": 0,
|
||||||
"viewCenter": {
|
"viewCenter": {
|
||||||
"x": 534.5,
|
"x": 1734,
|
||||||
"y": 441
|
"y": 1652
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"TileSets/TileSet 1.tsj": {
|
"TileSets/TileSet 1.tsj": {
|
||||||
"scaleInDock": 1,
|
"scaleInDock": 1,
|
||||||
"scaleInEditor": 3
|
"scaleInEditor": 1.5
|
||||||
|
},
|
||||||
|
"TileSets/WallSet.tsj": {
|
||||||
|
"scaleInDock": 1,
|
||||||
|
"scaleInEditor": 1
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"last.externalTilesetPath": "D:/C#/Я смотрел ваши ХАКАТОНЫ/ZoFo/ZoFo/Content/MapData/TileSets",
|
|
||||||
"last.imagePath": "D:/C#/Я смотрел ваши ХАКАТОНЫ/ZoFo/ZoFo/Content/Textures/TileSet",
|
|
||||||
"map.fixedSize": false,
|
|
||||||
"map.lastUsedFormat": "json",
|
|
||||||
"map.tileHeight": 16,
|
|
||||||
"map.tileWidth": 16,
|
|
||||||
"openFiles": [
|
"openFiles": [
|
||||||
"TileMaps/main.tmj",
|
"TileSets/TileSet 1.tsj",
|
||||||
"TileSets/TileSet 1.tsj"
|
"TileSets/WallSet.tsj",
|
||||||
|
"TileMaps/main.tmj"
|
||||||
],
|
],
|
||||||
"project": "MapSession.tiled-project",
|
|
||||||
"recentFiles": [
|
"recentFiles": [
|
||||||
"TileMaps/main.tmj",
|
"TileMaps/main.tmj",
|
||||||
|
"TileSets/WallSet.tsj",
|
||||||
"TileSets/TileSet 1.tsj"
|
"TileSets/TileSet 1.tsj"
|
||||||
],
|
]
|
||||||
"tileset.lastUsedFormat": "json",
|
|
||||||
"tileset.tileSize": {
|
|
||||||
"height": 16,
|
|
||||||
"width": 16
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,11 +39,12 @@ namespace ZoFo.GameCore
|
||||||
|
|
||||||
}
|
}
|
||||||
public void GameEndedUnexpectedly() { }
|
public void GameEndedUnexpectedly() { }
|
||||||
public void JoinRoom(string ip, int port)
|
|
||||||
|
public void JoinRoom(string ip,int port)
|
||||||
{
|
{
|
||||||
networkManager.JoinRoom(ip);
|
networkManager.JoinRoom(ip,port);
|
||||||
}
|
}
|
||||||
public void JoinYourself() { networkManager.JoinYourself(); }
|
public void JoinYourself(int port) { networkManager.JoinYourself(port); }
|
||||||
|
|
||||||
|
|
||||||
List<MapObject> mapObjects = new List<MapObject>();
|
List<MapObject> mapObjects = new List<MapObject>();
|
||||||
|
|
|
@ -42,8 +42,7 @@ public class SelectModeMenu : AbstractGUI
|
||||||
Server server = new Server();
|
Server server = new Server();
|
||||||
Client client = new Client();
|
Client client = new Client();
|
||||||
server.CreateRoom(1);
|
server.CreateRoom(1);
|
||||||
client.JoinYourself();
|
client.JoinYourself(server.MyIp.Port);
|
||||||
|
|
||||||
AppManager.Instance.SetServer(server);
|
AppManager.Instance.SetServer(server);
|
||||||
AppManager.Instance.SetClient(client);
|
AppManager.Instance.SetClient(client);
|
||||||
AppManager.Instance.ChangeState(GameState.HostPlaying);
|
AppManager.Instance.ChangeState(GameState.HostPlaying);
|
||||||
|
@ -71,14 +70,7 @@ public class SelectModeMenu : AbstractGUI
|
||||||
{
|
{
|
||||||
AppManager.Instance.SetGUI(new SelectingServerGUI());
|
AppManager.Instance.SetGUI(new SelectingServerGUI());
|
||||||
// multi
|
// multi
|
||||||
Server server = new Server(); //Server Logic MultiPlayer
|
|
||||||
Client client = new Client();
|
|
||||||
server.CreateRoom(5);
|
|
||||||
client.JoinRoom("127.0.0.1"); //указать айпишник
|
|
||||||
AppManager.Instance.SetServer(server);
|
|
||||||
AppManager.Instance.SetClient(client);
|
|
||||||
string key = client.IsConnected.ToString();
|
|
||||||
AppManager.Instance.debugHud.Set(key, "MultiPlayer");
|
|
||||||
// ваш код здесь
|
// ваш код здесь
|
||||||
};
|
};
|
||||||
Elements.Add(optionButton);
|
Elements.Add(optionButton);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Content;
|
using Microsoft.Xna.Framework.Content;
|
||||||
|
@ -62,10 +63,18 @@ public class SelectingServerGUI : AbstractGUI
|
||||||
};
|
};
|
||||||
joinButton.LeftButtonPressed += () =>
|
joinButton.LeftButtonPressed += () =>
|
||||||
{
|
{
|
||||||
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false));
|
|
||||||
// join
|
// join
|
||||||
|
Client client = new Client();
|
||||||
// ваш код здесь
|
var endpoint = ipBox.text.Split(':');
|
||||||
|
int port;
|
||||||
|
if (int.TryParse(endpoint[1], out port))
|
||||||
|
{
|
||||||
|
client.JoinRoom(endpoint[0], port);
|
||||||
|
AppManager.Instance.SetClient(client);
|
||||||
|
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false));
|
||||||
|
}
|
||||||
|
// ваш код здесь
|
||||||
};
|
};
|
||||||
Elements.Add(joinButton);
|
Elements.Add(joinButton);
|
||||||
Button hostButton = new Button(Manager)
|
Button hostButton = new Button(Manager)
|
||||||
|
@ -79,10 +88,15 @@ public class SelectingServerGUI : AbstractGUI
|
||||||
};
|
};
|
||||||
hostButton.LeftButtonPressed += () =>
|
hostButton.LeftButtonPressed += () =>
|
||||||
{
|
{
|
||||||
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true));
|
|
||||||
// host
|
|
||||||
|
|
||||||
|
// host
|
||||||
|
Server server = new Server(); //Server Logic MultiPlayer
|
||||||
|
server.CreateRoom(5);
|
||||||
|
AppManager.Instance.SetServer(server);
|
||||||
|
string key = server.MyIp.ToString();
|
||||||
|
AppManager.Instance.debugHud.Set(key, "MultiPlayer");
|
||||||
// ваш код здесь
|
// ваш код здесь
|
||||||
|
AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true));
|
||||||
};
|
};
|
||||||
Elements.Add(hostButton);
|
Elements.Add(hostButton);
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ public class WaitingForPlayersGUI : AbstractGUI
|
||||||
{
|
{
|
||||||
private DrawableUIElement menuBackground;
|
private DrawableUIElement menuBackground;
|
||||||
private bool isHost;
|
private bool isHost;
|
||||||
|
private Label ip;
|
||||||
|
|
||||||
public WaitingForPlayersGUI(bool isHost)
|
public WaitingForPlayersGUI(bool isHost)
|
||||||
{
|
{
|
||||||
|
@ -26,13 +27,13 @@ public class WaitingForPlayersGUI : AbstractGUI
|
||||||
{
|
{
|
||||||
int width = AppManager.Instance.CurentScreenResolution.X;
|
int width = AppManager.Instance.CurentScreenResolution.X;
|
||||||
int height = AppManager.Instance.CurentScreenResolution.Y;
|
int height = AppManager.Instance.CurentScreenResolution.Y;
|
||||||
|
|
||||||
menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures/GUI/background/Waiting" };
|
menuBackground = new DrawableUIElement(Manager) { rectangle = new Rectangle(0, 0, width, height), mainColor = Color.White, textureName = "Textures\\GUI\\background\\Waiting" };
|
||||||
Elements.Add(menuBackground);
|
Elements.Add(menuBackground);
|
||||||
menuBackground.LoadTexture(AppManager.Instance.Content);
|
menuBackground.LoadTexture(AppManager.Instance.Content);
|
||||||
|
// string pcIp =
|
||||||
Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Waiting", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font"});
|
ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.server.MyIp.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" };
|
||||||
|
Elements.Add(ip);
|
||||||
if (isHost)
|
if (isHost)
|
||||||
{
|
{
|
||||||
Button startButton = new Button(Manager)
|
Button startButton = new Button(Manager)
|
||||||
|
@ -47,7 +48,7 @@ public class WaitingForPlayersGUI : AbstractGUI
|
||||||
startButton.LeftButtonPressed += () =>
|
startButton.LeftButtonPressed += () =>
|
||||||
{
|
{
|
||||||
// start
|
// start
|
||||||
|
|
||||||
// ваш код здесь
|
// ваш код здесь
|
||||||
};
|
};
|
||||||
Elements.Add(startButton);
|
Elements.Add(startButton);
|
||||||
|
|
|
@ -83,8 +83,6 @@ namespace ZoFo.GameCore.GameManagers
|
||||||
currentGUI.LoadContent();
|
currentGUI.LoadContent();
|
||||||
animationBuilder = new AnimationBuilder();
|
animationBuilder = new AnimationBuilder();
|
||||||
animationBuilder.LoadAnimations();
|
animationBuilder.LoadAnimations();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void Update(GameTime gameTime)
|
protected override void Update(GameTime gameTime)
|
||||||
|
@ -121,7 +119,9 @@ namespace ZoFo.GameCore.GameManagers
|
||||||
|
|
||||||
currentGUI.Draw(_spriteBatch);
|
currentGUI.Draw(_spriteBatch);
|
||||||
debugHud.Draw(_spriteBatch);
|
debugHud.Draw(_spriteBatch);
|
||||||
_spriteBatch.Begin();
|
|
||||||
|
// Pointwrap
|
||||||
|
_spriteBatch.Begin(samplerState: SamplerState.PointWrap);
|
||||||
switch (gamestate)
|
switch (gamestate)
|
||||||
{
|
{
|
||||||
case GameState.ClientPlaying:
|
case GameState.ClientPlaying:
|
||||||
|
|
|
@ -15,13 +15,14 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
{
|
{
|
||||||
public class ClientNetworkManager
|
public class ClientNetworkManager
|
||||||
{
|
{
|
||||||
private int port = 7632;
|
private int port = 0;
|
||||||
private EndPoint endPoint;
|
private IPEndPoint endPoint;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
List<UpdateData> updates = new List<UpdateData>();
|
List<UpdateData> updates = new List<UpdateData>();
|
||||||
public delegate void OnDataSent(string Data);
|
public delegate void OnDataSent(string Data);
|
||||||
public event OnDataSent GetDataSent; // event
|
public event OnDataSent GetDataSent; // event
|
||||||
public bool IsConnected { get { return socket.Connected; } }
|
public bool IsConnected { get { return socket.Connected; } }
|
||||||
|
public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint;
|
||||||
|
|
||||||
public ClientNetworkManager()
|
public ClientNetworkManager()
|
||||||
{
|
{
|
||||||
|
@ -67,21 +68,40 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
socket.Connect(endPoint);
|
socket.Connect(endPoint);
|
||||||
SendData();
|
SendData();
|
||||||
Thread listen = new Thread(StartListening);
|
Thread listen = new Thread(StartListening);
|
||||||
|
listen.IsBackground = true;
|
||||||
|
listen.Start();
|
||||||
|
}
|
||||||
|
public void JoinRoom(IPEndPoint endPoint) // multyplayer
|
||||||
|
{
|
||||||
|
|
||||||
|
this.endPoint = endPoint;
|
||||||
|
socket.Connect(endPoint);
|
||||||
|
SendData();
|
||||||
|
Thread listen = new Thread(StartListening);
|
||||||
|
listen.IsBackground = true;
|
||||||
listen.Start();
|
listen.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// создается одиночная комната к которой ты подключаешься
|
/// создается одиночная комната к которой ты подключаешься
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void JoinYourself() // single player
|
public void JoinYourself(int port) // single player
|
||||||
{
|
{
|
||||||
endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
|
endPoint = new IPEndPoint(GetIp(), port);
|
||||||
socket.Connect(endPoint);
|
socket.Connect(endPoint);
|
||||||
SendData();
|
SendData();
|
||||||
Thread listen = new Thread(StartListening);
|
Thread listen = new Thread(StartListening);
|
||||||
|
listen.IsBackground = true;
|
||||||
listen.Start();
|
listen.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IPAddress GetIp()
|
||||||
|
{
|
||||||
|
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
|
||||||
|
string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP
|
||||||
|
return IPAddress.Parse(myIP);
|
||||||
|
}
|
||||||
|
|
||||||
//поток 2
|
//поток 2
|
||||||
public void StartListening()
|
public void StartListening()
|
||||||
{
|
{
|
||||||
|
|
|
@ -18,8 +18,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
{
|
{
|
||||||
public class ServerNetworkManager
|
public class ServerNetworkManager
|
||||||
{
|
{
|
||||||
private IPAddress ip =IPAddress.Parse("127.0.0.1"); //IPAddress.Any
|
private IPAddress ip = IPAddress.Parse("127.0.0.1");
|
||||||
private int port = 7632;
|
private const int port = 0;
|
||||||
private IPEndPoint endPoint;
|
private IPEndPoint endPoint;
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private List<Socket> clients;
|
private List<Socket> clients;
|
||||||
|
@ -28,6 +28,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
public event OnDataSend GetDataSend; // event
|
public event OnDataSend GetDataSend; // event
|
||||||
Dictionary<Socket, Thread> managerThread;
|
Dictionary<Socket, Thread> managerThread;
|
||||||
Thread serverTheread;
|
Thread serverTheread;
|
||||||
|
public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint;
|
||||||
|
|
||||||
public ServerNetworkManager() { Init(); }
|
public ServerNetworkManager() { Init(); }
|
||||||
|
|
||||||
|
@ -36,6 +37,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void Init()
|
private void Init()
|
||||||
{
|
{
|
||||||
|
ip = GetIp();
|
||||||
endPoint = new IPEndPoint(ip, port);
|
endPoint = new IPEndPoint(ip, port);
|
||||||
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||||
managerThread = new Dictionary<Socket, Thread>();
|
managerThread = new Dictionary<Socket, Thread>();
|
||||||
|
@ -45,10 +47,21 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
socket.Bind(endPoint);
|
socket.Bind(endPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получает IP устройства
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IPAddress GetIp()
|
||||||
|
{
|
||||||
|
string hostName = Dns.GetHostName(); // Retrive the Name of HOST
|
||||||
|
string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP
|
||||||
|
return IPAddress.Parse(myIP);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// отправляет клиенту Data
|
/// отправляет клиенту Data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void SendData()
|
public void SendData()
|
||||||
{
|
{
|
||||||
for (int i = 0; i < updates.Count; i++)
|
for (int i = 0; i < updates.Count; i++)
|
||||||
{
|
{
|
||||||
|
@ -84,7 +97,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// По сути конец игры и отключение игроков
|
/// По сути конец игры и отключение игроков
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void CloseConnection()
|
public void CloseConnection()
|
||||||
{
|
{
|
||||||
foreach (var item in clients)
|
foreach (var item in clients)
|
||||||
{
|
{
|
||||||
|
@ -112,11 +125,11 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
public void Start(object players)
|
public void Start(object players)
|
||||||
{
|
{
|
||||||
serverTheread = new Thread(StartWaitingForPlayers);
|
serverTheread = new Thread(StartWaitingForPlayers);
|
||||||
|
serverTheread.IsBackground = true;
|
||||||
serverTheread.Start(players);
|
serverTheread.Start(players);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Потоки Клиентов
|
//Потоки Клиентов
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Слушает игроков, которые хотят подключиться
|
/// Слушает игроков, которые хотят подключиться
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -124,12 +137,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager
|
||||||
public void StartWaitingForPlayers(object players)
|
public void StartWaitingForPlayers(object players)
|
||||||
{
|
{
|
||||||
int playNumber = (int)players;
|
int playNumber = (int)players;
|
||||||
|
|
||||||
socket.Listen(playNumber);
|
socket.Listen(playNumber);
|
||||||
for (int i = 0; i < playNumber; i++)
|
for (int i = 0; i < playNumber; i++)
|
||||||
{
|
{
|
||||||
Socket client = socket.Accept();
|
Socket client = socket.Accept();
|
||||||
Thread thread = new Thread(StartListening);
|
Thread thread = new Thread(StartListening);
|
||||||
|
thread.IsBackground = true;
|
||||||
thread.Start(client);
|
thread.Start(client);
|
||||||
managerThread.Add(client, thread);
|
managerThread.Add(client, thread);
|
||||||
clients.Add(client); //добавляем клиентов в лист
|
clients.Add(client); //добавляем клиентов в лист
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
@ -20,6 +21,7 @@ namespace ZoFo.GameCore
|
||||||
{
|
{
|
||||||
private ServerNetworkManager networkManager;
|
private ServerNetworkManager networkManager;
|
||||||
private int ticks = 0;
|
private int ticks = 0;
|
||||||
|
public IPEndPoint MyIp { get { return networkManager.InfoConnect; } }
|
||||||
public Server()
|
public Server()
|
||||||
{
|
{
|
||||||
networkManager = new ServerNetworkManager();
|
networkManager = new ServerNetworkManager();
|
||||||
|
|
Loading…
Add table
Reference in a new issue