diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index 686400a..b91c6a2 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -25,6 +25,8 @@ using ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies; using ZoFo.GameCore.Graphics; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; namespace ZoFo.GameCore { public class Client @@ -55,13 +57,21 @@ namespace ZoFo.GameCore public void OnDataSend(string data) { - List updateDatas = JsonSerializer.Deserialize>(data); + //List updateDatas = JsonSerializer.Deserialize>(data); + JArray jToken = JsonConvert.DeserializeObject(data) as JArray; + + //string[] brands = jToken.SelectToken("")?.ToObject(); + foreach (JToken update in jToken.Children()) + { + string a = update.ToString(); + UpdateTileCreated u = System.Text.Json.JsonSerializer.Deserialize(a); + } // тут будет switch AppManager.Instance.debugHud.Log(data); - foreach (var item in updateDatas) - { - GotData(item); - } + //foreach (var item in updateDatas) + //{ + // GotData(item); + //} } public void GameEndedUnexpectedly() { } @@ -113,22 +123,21 @@ namespace ZoFo.GameCore mapObjects.Add( new MapObject( (update as UpdateTileCreated).Position, - (update as UpdateTileCreated).Size.ToVector2(), - (update as UpdateTileCreated).sourceRectangle, + (update as UpdateTileCreated).Size.GetPoint().ToVector2(), + (update as UpdateTileCreated).sourceRectangle.GetRectangle(), (update as UpdateTileCreated).tileSetName )); } - else if (update is UpdateStopObjectCreated) - { - stopObjects.Add( - new StopObject( - (update as UpdateStopObjectCreated).Position, - (update as UpdateStopObjectCreated).Size.ToVector2(), - (update as UpdateStopObjectCreated).sourceRectangle, - (update as UpdateStopObjectCreated).tileSetName, - (update as UpdateStopObjectCreated).collisions - )); - } + //else if (update is UpdateStopObjectCreated) + //{ + // stopObjects.Add( + // new StopObject( + // (update as UpdateStopObjectCreated).Position, + // (update as UpdateStopObjectCreated).Size.ToVector2(), + // (update as UpdateStopObjectCreated).sourceRectangle, + // (update as UpdateStopObjectCreated).tileSetName + // )); + //} else if (update is UpdateGameObjectCreated) { GameObject created_gameObject; diff --git a/ZoFo/GameCore/GameManagers/MapManager/MapManager.cs b/ZoFo/GameCore/GameManagers/MapManager/MapManager.cs index 94bf9e3..72dcdea 100644 --- a/ZoFo/GameCore/GameManagers/MapManager/MapManager.cs +++ b/ZoFo/GameCore/GameManagers/MapManager/MapManager.cs @@ -33,13 +33,13 @@ namespace ZoFo.GameCore.GameManagers.MapManager PropertyNameCaseInsensitive = true }; TileMap tileMap = - JsonSerializer.Deserialize(File.ReadAllText(string.Format(_templatePath, mapName)), options); + JsonSerializer.Deserialize(File.ReadAllText(Path.Combine(AppContext.BaseDirectory, string.Format(_templatePath, mapName))), options); // Загрузка TileSet-ов по TileSetInfo List tileSets = new List(); foreach (TileSetInfo tileSetInfo in tileMap.TileSets) { - TileSet tileSet = LoadTileSet(Path.Combine("Content", "MapData", "TileMaps", tileSetInfo.Source)); + TileSet tileSet = LoadTileSet(Path.Combine(AppContext.BaseDirectory, "Content", "MapData", "TileMaps", tileSetInfo.Source)); tileSet.FirstGid = tileSetInfo.FirstGid; tileSets.Add(tileSet); } @@ -75,14 +75,15 @@ namespace ZoFo.GameCore.GameManagers.MapManager (i / chunk.Height) * tileMap.TileHeight + chunk.Y * tileMap.TileHeight); Tile tile = tileSet.Tiles[number]; // По факту может быть StopObjectom, но на уровне Tiled это все в первую очередь Tile - + string textureName = Path.Combine(AppContext.BaseDirectory, "Content", "Textures", "TileSetImages", + Path.GetFileName(tileSet.Image).Replace(".png", "")); switch (tile.Type) { case "Tile": AppManager.Instance.server.RegisterGameObject(new MapObject(position, new Vector2(tileSet.TileWidth, tileSet.TileHeight), sourceRectangle, - "Textures/TileSetImages/" + Path.GetFileName(tileSet.Image).Replace(".png", ""))); + textureName)); break; case "StopObject": @@ -91,7 +92,7 @@ namespace ZoFo.GameCore.GameManagers.MapManager AppManager.Instance.server.RegisterGameObject(new StopObject(position, new Vector2(tileSet.TileWidth, tileSet.TileHeight), sourceRectangle, - "Textures/TileSetImages/" + Path.GetFileName(tileSet.Image).Replace(".png", ""), + textureName, collisionRectangles.ToArray())); break; diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index 8c09944..6e1e6b4 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -116,8 +116,8 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager { while(socket.Connected) { - byte[] bytes = new byte[2048]; - var countAnsw = socket.Receive(bytes); //Вылетает если кто то закрыл + byte[] bytes = new byte[65535]; + var countAnsw = socket.Receive(bytes, SocketFlags.Partial); //Вылетает если кто то закрыл string update = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновление отосланные сервером GetDataSent(update); } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs new file mode 100644 index 0000000..4480a8c --- /dev/null +++ b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializablePoint.cs @@ -0,0 +1,18 @@ +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO +{ + public class SerializablePoint + { + public int X; + public int Y; + + public SerializablePoint(Point point) { X = point.X; Y = point.Y;} + public Point GetPoint() { return new Point(X, Y);} + } +} diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs new file mode 100644 index 0000000..720344e --- /dev/null +++ b/ZoFo/GameCore/GameManagers/NetworkManager/SerializableDTO/SerializableRectangle.cs @@ -0,0 +1,33 @@ +using Microsoft.Xna.Framework; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; + +namespace ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO +{ + [Serializable] + [JsonSerializable(typeof(SerializableRectangle))] + public class SerializableRectangle + { + public SerializablePoint Size { get; set; } + public SerializablePoint Location { get; set; } + public int X { get; set; } + public int Y { get; set; } + public SerializableRectangle() + { + + } + + public SerializableRectangle(Rectangle rectangle) { X = rectangle.X; Y = rectangle.Y; + Size = new SerializablePoint(rectangle.Size); Location = new SerializablePoint(rectangle.Location); } + + public Rectangle GetRectangle() + { + return new Rectangle() { X = X, Y = Y, Size = Size.GetPoint(), Location = Location.GetPoint() }; + } + } +} diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index e625ce4..697c61d 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -53,7 +53,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager public static IPAddress GetIp() { string hostName = Dns.GetHostName(); // Retrive the Name of HOST - var ipList = Dns.GetHostByName(hostName).AddressList; + var ipList = Dns.GetHostEntry(hostName).AddressList; foreach (var ip in ipList) { @@ -80,15 +80,15 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager //Что это? //по 10 паков за раз TODO FIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXIT List datasToSend = new List(); - for (int i = 0; i < 5 && i - /// При создании тайла TODO move to another file - /// - public class UpdateStopObjectCreated : UpdateData - { - public UpdateStopObjectCreated() { UpdateType = "UpdateStopObjectCreated"; } - public Texture2D TextureTile { get; set; } - public Vector2 Position { get; set; } - public Point Size { get; set; } - public Rectangle sourceRectangle { get; set; } - public string tileSetName { get; set; } - public Rectangle[] collisions { get; set; } - } } diff --git a/ZoFo/GameCore/GameManagers/SoundManager.cs b/ZoFo/GameCore/GameManagers/SoundManager.cs index 97a5922..204d965 100644 --- a/ZoFo/GameCore/GameManagers/SoundManager.cs +++ b/ZoFo/GameCore/GameManagers/SoundManager.cs @@ -24,15 +24,7 @@ namespace ZoFo.GameCore.GameManagers { //List sounds = AppManager.Instance.Content.Load>("sounds/"); - string a = Path.Combine("Content", "sounds"); - string[] k; - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { - k = Directory.GetFiles(Path.Combine("bin", "Debug", "net8.0", "Content", "sounds")).Where(x => x.EndsWith("xnb")).ToArray(); - } - else{ - k = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(), "Content", "sounds")).Where(x => x.EndsWith("xnb")).ToArray(); - } + string[] k = Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "Content", "sounds")).Where(x => x.EndsWith("xnb")).ToArray(); if (k.Length > 0) { diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs index 484ad90..b6b7d45 100644 --- a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -164,6 +164,7 @@ namespace ZoFo.GameCore.Graphics } interval--; + } public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) @@ -188,8 +189,9 @@ namespace ZoFo.GameCore.Graphics destinationRectangle.Y -= CameraPosition.Y; destinationRectangle = Scaling(destinationRectangle); - _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); + + _spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White, Rotation, + Vector2.Zero, Flip, 0); } public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) { @@ -214,7 +216,8 @@ namespace ZoFo.GameCore.Graphics destinationRectangle = Scaling(destinationRectangle); _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); + destinationRectangle, sourceRectangle, Color.White, 0, + Vector2.Zero, Flip, 0); } private void buildSourceRectangle() { diff --git a/ZoFo/GameCore/Graphics/AnimationBuilder.cs b/ZoFo/GameCore/Graphics/AnimationBuilder.cs index e85ec28..50f9162 100644 --- a/ZoFo/GameCore/Graphics/AnimationBuilder.cs +++ b/ZoFo/GameCore/Graphics/AnimationBuilder.cs @@ -12,7 +12,7 @@ namespace ZoFo.GameCore.Graphics public void LoadAnimations() { Animations = new List(); - string[] animationFilesNames = Directory.GetFiles("Content/Textures/Animations"); + string[] animationFilesNames = Directory.GetFiles(Path.Combine(AppContext.BaseDirectory, "Content", "Textures", "Animations")); StreamReader reader; foreach (var fileName in animationFilesNames) diff --git a/ZoFo/GameCore/Graphics/GraphicsComponent.cs b/ZoFo/GameCore/Graphics/GraphicsComponent.cs index c71b0c3..3fa8fa4 100644 --- a/ZoFo/GameCore/Graphics/GraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/GraphicsComponent.cs @@ -9,6 +9,9 @@ public abstract class GraphicsComponent public static int scaling = 1; public string mainTextureName;//TODO костыль - пофиксить + public SpriteEffects Flip = SpriteEffects.None; + public float Rotation; + public abstract void LoadContent(); public abstract void Update(); public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch); diff --git a/ZoFo/GameCore/Graphics/IGraphicsComponent.cs b/ZoFo/GameCore/Graphics/IGraphicsComponent.cs deleted file mode 100644 index 50e2424..0000000 --- a/ZoFo/GameCore/Graphics/IGraphicsComponent.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; - -namespace ZoFo.GameCore.Graphics; - -public interface IGraphicsComponent -{ - public Rectangle ObjectDrawRectangle { get; set; } - public static int scaling = 1; - public string mainTextureName { get; set; }//TODO костыль - пофиксить - - public abstract void LoadContent(); - public abstract void Update(); - public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch); - public abstract void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle); -} \ No newline at end of file diff --git a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs index 3e2fd6c..a631c5d 100644 --- a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs @@ -8,7 +8,6 @@ using ZoFo.GameCore.GUI; namespace ZoFo.GameCore.Graphics { - public class StaticGraphicsComponent : GraphicsComponent { private Texture2D texture; @@ -18,7 +17,7 @@ namespace ZoFo.GameCore.Graphics { LoadContent(); } - + public StaticGraphicsComponent(string textureName) { BuildComponent(textureName); @@ -29,7 +28,7 @@ namespace ZoFo.GameCore.Graphics { _textureName = textureName; } - + public override void LoadContent() { @@ -37,7 +36,7 @@ namespace ZoFo.GameCore.Graphics { return; } - + texture = AppManager.Instance.Content.Load(_textureName); } @@ -53,11 +52,14 @@ namespace ZoFo.GameCore.Graphics destinationRectangle.X -= CameraPosition.X; destinationRectangle.Y -= CameraPosition.Y; destinationRectangle = Scaling(destinationRectangle); - _spriteBatch.Draw(texture, destinationRectangle, Color.White); + _spriteBatch.Draw(texture, destinationRectangle, texture.Bounds, Color.White, Rotation, + Vector2.Zero, Flip, 0); } public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) { + // Uncomment to go brrrr + //Rotation = new Random().Next(1, 365); DebugHUD.Instance.Log("draw "); destinationRectangle.X -= CameraPosition.X; @@ -65,7 +67,8 @@ namespace ZoFo.GameCore.Graphics destinationRectangle = Scaling(destinationRectangle); _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); + destinationRectangle, sourceRectangle, Color.White, Rotation, + Vector2.Zero, Flip, 0); } } -} +} \ No newline at end of file diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index dbbfa94..89b1bbb 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -21,6 +21,7 @@ using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.MapObjects; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; using ZoFo.GameCore.Graphics; +using ZoFo.GameCore.GameManagers.NetworkManager.SerializableDTO; namespace ZoFo.GameCore { @@ -172,30 +173,13 @@ namespace ZoFo.GameCore { gameObjects.Add(gameObject); - if (gameObject is StopObject) - { - AddData(new UpdateStopObjectCreated() - { - Position = (gameObject as StopObject).position, - sourceRectangle = (gameObject as StopObject).sourceRectangle, - Size = (gameObject as StopObject).graphicsComponent.ObjectDrawRectangle.Size, - collisions = (gameObject as StopObject).collisionComponents.Select(x=>x.stopRectangle).ToArray(), - tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName - });//TODO - foreach (var item in (gameObject as StopObject).collisionComponents) - { - collisionManager.Register(item); - - } - return; - } if (gameObject is MapObject) { AddData(new UpdateTileCreated() { Position = (gameObject as MapObject).position, - sourceRectangle = (gameObject as MapObject).sourceRectangle, - Size = (gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size, + sourceRectangle = new SerializableRectangle((gameObject as MapObject).sourceRectangle), + Size = new SerializablePoint((gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size), tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName });//TODO return;