diff --git a/ZoFo/Content/MapData/MapSession.tiled-session b/ZoFo/Content/MapData/MapSession.tiled-session index ce9fa0b..b075b59 100644 --- a/ZoFo/Content/MapData/MapSession.tiled-session +++ b/ZoFo/Content/MapData/MapSession.tiled-session @@ -14,36 +14,30 @@ "scaleInDock": 1 }, "TileMaps/main.tmj": { - "scale": 1, + "scale": 0.25, "selectedLayer": 0, "viewCenter": { - "x": 534.5, - "y": 441 + "x": 1734, + "y": 1652 } }, "TileSets/TileSet 1.tsj": { "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": [ - "TileMaps/main.tmj", - "TileSets/TileSet 1.tsj" + "TileSets/TileSet 1.tsj", + "TileSets/WallSet.tsj", + "TileMaps/main.tmj" ], - "project": "MapSession.tiled-project", "recentFiles": [ "TileMaps/main.tmj", + "TileSets/WallSet.tsj", "TileSets/TileSet 1.tsj" - ], - "tileset.lastUsedFormat": "json", - "tileset.tileSize": { - "height": 16, - "width": 16 - } + ] } diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 4bc324e..f49020f 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -39,11 +39,12 @@ namespace ZoFo.GameCore } public void GameEndedUnexpectedly() { } - public void JoinRoom(string ip) + + 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 mapObjects = new List(); diff --git a/ZoFo/GameCore/GUI/SelectModeMenu.cs b/ZoFo/GameCore/GUI/SelectModeMenu.cs index ad4db7f..5b1320b 100644 --- a/ZoFo/GameCore/GUI/SelectModeMenu.cs +++ b/ZoFo/GameCore/GUI/SelectModeMenu.cs @@ -42,8 +42,7 @@ public class SelectModeMenu : AbstractGUI Server server = new Server(); Client client = new Client(); server.CreateRoom(1); - client.JoinYourself(); - + client.JoinYourself(server.MyIp.Port); AppManager.Instance.SetServer(server); AppManager.Instance.SetClient(client); AppManager.Instance.ChangeState(GameState.HostPlaying); @@ -71,14 +70,7 @@ public class SelectModeMenu : AbstractGUI { AppManager.Instance.SetGUI(new SelectingServerGUI()); // 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); diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index 5553946..fafc259 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Net; using System.Xml; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; @@ -62,10 +63,18 @@ public class SelectingServerGUI : AbstractGUI }; joinButton.LeftButtonPressed += () => { - AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + // 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); Button hostButton = new Button(Manager) @@ -79,10 +88,15 @@ public class SelectingServerGUI : AbstractGUI }; 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); diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index 08d80eb..12a67b9 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -17,6 +17,7 @@ public class WaitingForPlayersGUI : AbstractGUI { private DrawableUIElement menuBackground; private bool isHost; + private Label ip; public WaitingForPlayersGUI(bool isHost) { @@ -26,13 +27,13 @@ public class WaitingForPlayersGUI : AbstractGUI { int width = AppManager.Instance.CurentScreenResolution.X; 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); menuBackground.LoadTexture(AppManager.Instance.Content); - - 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"}); - + // string pcIp = + 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) { Button startButton = new Button(Manager) @@ -47,7 +48,7 @@ public class WaitingForPlayersGUI : AbstractGUI startButton.LeftButtonPressed += () => { // start - + // ваш код здесь }; Elements.Add(startButton); diff --git a/ZoFo/GameCore/GameManagers/AppManager.cs b/ZoFo/GameCore/GameManagers/AppManager.cs index afff18d..fba2e17 100644 --- a/ZoFo/GameCore/GameManagers/AppManager.cs +++ b/ZoFo/GameCore/GameManagers/AppManager.cs @@ -59,7 +59,7 @@ namespace ZoFo.GameCore.GameManagers SettingsManager.LoadSettings(); SoundManager = new SoundManager(); SoundManager.LoadSounds(); - + currentGUI = new MainMenuGUI(); debugHud = new DebugHUD(); @@ -70,7 +70,7 @@ namespace ZoFo.GameCore.GameManagers protected override void Initialize() { currentGUI.Initialize(); - debugHud.Initialize(); + debugHud.Initialize(); base.Initialize(); @@ -83,15 +83,13 @@ namespace ZoFo.GameCore.GameManagers currentGUI.LoadContent(); animationBuilder = new AnimationBuilder(); animationBuilder.LoadAnimations(); - - } protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || - Keyboard.GetState().IsKeyDown(Keys.Escape)) - Exit(); + Keyboard.GetState().IsKeyDown(Keys.Escape)) { server.CloseConnection(); Exit(); } + debugHud.Set("key", "value"); @@ -121,7 +119,9 @@ namespace ZoFo.GameCore.GameManagers currentGUI.Draw(_spriteBatch); debugHud.Draw(_spriteBatch); - _spriteBatch.Begin(); + + // Pointwrap + _spriteBatch.Begin(samplerState: SamplerState.PointWrap); switch (gamestate) { case GameState.ClientPlaying: diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollectionComponent.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollectionComponent.cs new file mode 100644 index 0000000..7143312 --- /dev/null +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollectionComponent.cs @@ -0,0 +1,6 @@ +namespace ZoFo.GameCore.GameManagers.CollisionManager +{ + public class CollectionComponent + { + } +} \ No newline at end of file diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs index 0ba9816..880e159 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionComponent.cs @@ -1,34 +1,41 @@ -using System; +using Microsoft.Xna.Framework; +using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Reflection.Metadata; using System.Text; using System.Threading.Tasks; +using ZoFo.GameCore.GameObjects; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; namespace ZoFo.GameCore.GameManagers.CollisionManager { public class CollisionComponent { - //поля - public Rectangle Bounds { get; set; } + //==ПОЛЯ== + + public GameObject gameObject { get; set; } + //public Rectangle Bounds { get; set; } + + //public Rectangle Rectangle => new Rectangle(); + + - //остановлен ли перс bool doesStop; - Rectangle stopRectangle; + public Rectangle stopRectangle; // triggers for rectangle bool isTrigger; - Rectangle triggerRectangle; + public Rectangle triggerRectanglee; //delegate public delegate void EventHandler(object sender, EventArgs e); - public CollisionComponent(int x, int y, int width, int height) - { - Bounds = new Rectangle(x, y, width, height); - } + //public CollisionComponent(int x, int y, int width, int height) + //{ + // Bounds = new Rectangle(x, y, width, height); + //} + //events diff --git a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs index 5242d4a..96c9cbc 100644 --- a/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs +++ b/ZoFo/GameCore/GameManagers/CollisionManager/CollisionManager.cs @@ -1,7 +1,6 @@ using Microsoft.VisualBasic; using System; using System.Collections.Generic; -using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,51 +8,106 @@ using ZoFo.GameCore.GameObjects; using ZoFo.GameCore.GameManagers.CollisionManager; using Microsoft.Xna.Framework; using ZoFo.GameCore.GameManagers.MapManager.MapElements; +using ZoFo.GameCore.GameObjects.Entities; +using ZoFo.GameCore.GameObjects.Entities.LivingEntities; namespace ZoFo.GameCore.GameManagers.CollisionManager { public class CollisionManager { - public List CollisionComponent; - public List TriggerComponent; + //листики - public void RegisterComponent() - { - - } + public List ObjectsWithCollisions; + public List EntitiesWithMovements; + public List ObjectsWithTriggers; - public static bool CheckComponentCollision(List collisionComponents, CollisionComponent component) + + //чекаем коллизии в листе + public void CheckComponentCollision(LivingEntity entity) { - foreach (var collisionComponent in collisionComponents) - { - if (component.Bounds.IntersectsWith(collisionComponent.Bounds)) + //for (int i = 0; i < ObjectsWithCollisions.Count; i++) + //{ + var currentRect = entity.collisionComponent.stopRectangle;//задаём РЕК + var newRect = currentRect; // задаём значение старого РЕК новому РЕК + bool flagRemovedObject = false; //флаг удаления + + + var collidedX = false; // соприкосновение + var tryingRectX = currentRect;//переменная для попытки перемещения по X + + tryingRectX.Offset((int)(entity.velocity.X), 0);//задаём значения для tryingRectX по X и по Y + + foreach (var item in ObjectsWithCollisions)//фильтрация { - return true; + if (Math.Abs(item.stopRectangle.X - entity.collisionComponent.stopRectangle.X) < 550 + && Math.Abs(item.stopRectangle.Y - entity.collisionComponent.stopRectangle.Y) < 550 + && tryingRectX.Intersects(item.stopRectangle)) + + { + collidedX = true;// меняем значение соприкосновения на true + entity.OnCollision(item);//подписываем entity на ивент коллизии + + break;// выход + } + } + + if (collidedX)// срабатывает, если перемещение блокируется + { + entity.velocity.X = 0;// задаём значение смещения entity на 0 } + else + { + newRect.X = tryingRectX.X;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК + } + + //==ПОВТОРЯЕМ ТОЖЕ САМОЕ ДЛЯ Y== + + var collidedY = false; // соприкосновение + var tryingRectY = currentRect;//переменная для попытки перемещения по X + + tryingRectY.Offset(new Point(0, (int)entity.velocity.Y));//задаём значения для tryingRectX по X и по Y + + foreach (var item in ObjectsWithCollisions)//фильтрация + { + if (Math.Abs(item.stopRectangle.X - entity.collisionComponent.stopRectangle.X) < 550 + && Math.Abs(item.stopRectangle.Y - entity.collisionComponent.stopRectangle.Y) < 550 + && tryingRectY.Intersects(item.stopRectangle)) + + { + collidedY = true;// меняем значение соприкосновения на true + entity.OnCollision(item);//подписываем entity на ивент коллизии + + break;// выход + } } - return false; + if (collidedY)// срабатывает, если перемещение блокируется + { + entity.velocity.Y = 0;// задаём значение смещения entity на 0 + } + else + { + newRect.Y = tryingRectY.Y;//значение по X для нового РЕК приравниваем к значению испытуемого РЕК + } } - public void UpdateComponentCollision(List collisionComponents) + //обновление позиции объекта + public void UpdateObjectsPositions() { } - public void UpdatePositions() + + //регистрация компонента(его коллизии) + public void Register(CollisionComponent component) { - + ObjectsWithCollisions.Add(component); + if (component.gameObject is Entity) + { + EntitiesWithMovements.Add(component); + } } - - //public void GetObjectInArea(Rectangle area) - //{ - - //} - - //public void Register(Rectangle rectangle) - //{ - - //} + } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index c178154..2a93f71 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -15,13 +15,14 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { public class ClientNetworkManager { - private int port = 7632; - private EndPoint endPoint; + private int port = 0; + private IPEndPoint endPoint; private Socket socket; List updates = new List(); public delegate void OnDataSent(string Data); public event OnDataSent GetDataSent; // event public bool IsConnected { get { return socket.Connected; } } + public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint; public ClientNetworkManager() { @@ -60,28 +61,47 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// /// - public void JoinRoom(string ip) // multyplayer + public void JoinRoom(string ip, int port) // multyplayer { endPoint = new IPEndPoint(IPAddress.Parse(ip), port); socket.Connect(endPoint); SendData(); 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(); } /// /// создается одиночная комната к которой ты подключаешься /// - 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); SendData(); Thread listen = new Thread(StartListening); + listen.IsBackground = true; 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 public void StartListening() { diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 658605e..76e72c2 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -18,8 +18,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { public class ServerNetworkManager { - private IPAddress ip =IPAddress.Parse("127.0.0.1"); //IPAddress.Any - private int port = 7632; + private IPAddress ip = IPAddress.Parse("127.0.0.1"); + private const int port = 0; private IPEndPoint endPoint; private Socket socket; private List clients; @@ -28,6 +28,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public event OnDataSend GetDataSend; // event Dictionary managerThread; Thread serverTheread; + public IPEndPoint InfoConnect => (IPEndPoint)socket.LocalEndPoint ?? endPoint; public ServerNetworkManager() { Init(); } @@ -36,6 +37,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// private void Init() { + ip = GetIp(); endPoint = new IPEndPoint(ip, port); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); managerThread = new Dictionary(); @@ -45,10 +47,21 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager socket.Bind(endPoint); } + /// + /// Получает IP устройства + /// + /// + 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); + } + /// /// отправляет клиенту Data /// - public void SendData() + public void SendData() { for (int i = 0; i < updates.Count; i++) { @@ -84,7 +97,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// /// По сути конец игры и отключение игроков /// - public void CloseConnection() + public void CloseConnection() { foreach (var item in clients) { @@ -112,11 +125,11 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void Start(object players) { serverTheread = new Thread(StartWaitingForPlayers); + serverTheread.IsBackground = true; serverTheread.Start(players); } //Потоки Клиентов - /// /// Слушает игроков, которые хотят подключиться /// @@ -124,12 +137,13 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public void StartWaitingForPlayers(object players) { int playNumber = (int)players; - + socket.Listen(playNumber); for (int i = 0; i < playNumber; i++) { Socket client = socket.Accept(); Thread thread = new Thread(StartListening); + thread.IsBackground = true; thread.Start(client); managerThread.Add(client, thread); clients.Add(client); //добавляем клиентов в лист @@ -152,7 +166,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager string response = Encoding.UTF8.GetString(buff, 0, answ); GetDataSend(response); } - Thread.Sleep(-1); + Task.Delay(-1); } } diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs index 6842723..84e7d45 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs @@ -4,6 +4,7 @@ using System; using ZoFo.GameCore.GameObjects.Entities; using ZoFo.GameCore.ZoFo_graphics; using ZoFo.GameCore.GameManagers; +using ZoFo.GameCore.GameManagers.CollisionManager; namespace ZoFo.GameCore.GameObjects.Entities.LivingEntities; public class LivingEntity : Entity @@ -26,6 +27,11 @@ public class LivingEntity : Entity }*/ #endregion + public void OnCollision(CollisionComponent component) + { + + } + } diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index f374401..4bea6cc 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Text.Json; using System.Threading.Tasks; @@ -20,6 +21,7 @@ namespace ZoFo.GameCore { private ServerNetworkManager networkManager; private int ticks = 0; + public IPEndPoint MyIp { get { return networkManager.InfoConnect; } } public Server() { networkManager = new ServerNetworkManager(); @@ -46,6 +48,11 @@ namespace ZoFo.GameCore //ТУТ Switch case будет честное слово } + public void CloseConnection() + { + networkManager.CloseConnection(); + } + /// /// Для красоты) Отдел Серверов /// добавляет в лист updates новую data