From 3849435115c3f41cbb2c4e5d24e989384de721e6 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 11:24:12 +0300 Subject: [PATCH 01/12] MbReadyMultiPlayer --- ZoFo/GameCore/Client.cs | 4 ++ ZoFo/GameCore/GUI/SelectingServerGUI.cs | 49 ++++++++++++++--------- ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs | 33 ++++++++++++--- 3 files changed, 62 insertions(+), 24 deletions(-) diff --git a/ZoFo/GameCore/Client.cs b/ZoFo/GameCore/Client.cs index f49020f..64d088c 100644 --- a/ZoFo/GameCore/Client.cs +++ b/ZoFo/GameCore/Client.cs @@ -14,6 +14,8 @@ using ZoFo.GameCore.GameObjects.MapObjects.Tiles; using System.Drawing; using System.Reflection; using ZoFo.GameCore.GameObjects.Entities; +using System.Net.Sockets; +using System.Net; namespace ZoFo.GameCore { @@ -22,6 +24,8 @@ namespace ZoFo.GameCore ClientNetworkManager networkManager; public bool IsConnected { get { return networkManager.IsConnected; } } + public IPEndPoint InfoConnect => networkManager.InfoConnect; + public Client() { networkManager = new ClientNetworkManager(); diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index fafc259..f2c911c 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -21,14 +21,14 @@ public class SelectingServerGUI : 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/join" }; 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 = "Select server", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font"}); - TextBox ipBox = new TextBox(Manager) + Elements.Add(new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = "Select server", fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts/Font" }); + + TextBox ipBox = new TextBox(Manager) { rectangle = new Rectangle(width / 4 - (width / 4) / 2, height / 4, (int)(width / 4), (int)(height / 20)), text = "ip", @@ -38,13 +38,15 @@ public class SelectingServerGUI : AbstractGUI textAligment = MonogameLibrary.UI.Enums.TextAligment.Left, fontName = "Fonts/Font" }; - ipBox.TextChanged += input => { + ipBox.TextChanged += input => + { if (input == "ip") { ipBox.text = ""; ipBox.fontColor = Color.White; } }; - ipBox.StopChanging += input => { + ipBox.StopChanging += input => + { if (input.Length == 0) { ipBox.fontColor = Color.White; @@ -52,7 +54,7 @@ public class SelectingServerGUI : AbstractGUI } }; Elements.Add(ipBox); - Button joinButton = new Button(Manager) + Button joinButton = new Button(Manager) { rectangle = new Rectangle(width / 4 + (width / 4) / 2, height / 4, (int)(width / 15), (int)(height / 20)), text = "Join", @@ -61,23 +63,34 @@ public class SelectingServerGUI : AbstractGUI mainColor = Color.Gray, fontName = "Fonts/Font" }; - joinButton.LeftButtonPressed += () => + joinButton.LeftButtonPressed += () => { // join Client client = new Client(); - var endpoint = ipBox.text.Split(':'); + var endpoint = ipBox.text.Split(':'); int port; - if (int.TryParse(endpoint[1], out port)) + try { - client.JoinRoom(endpoint[0], port); - AppManager.Instance.SetClient(client); - AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + if (int.TryParse(endpoint[1], out port)) + { + client.JoinRoom(endpoint[0], port); + AppManager.Instance.SetClient(client); + Server server = new Server(); + AppManager.Instance.SetServer(server); + AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); + } } + catch (Exception) + { + + // throw; + } + // ваш код здесь }; Elements.Add(joinButton); - Button hostButton = new Button(Manager) + Button hostButton = new Button(Manager) { rectangle = new Rectangle(width / 4 + (width / 4) / 2 + (width / 15), height / 4, (int)(width / 15), (int)(height / 20)), text = "Host", @@ -86,9 +99,9 @@ public class SelectingServerGUI : AbstractGUI mainColor = Color.Gray, fontName = "Fonts/Font" }; - hostButton.LeftButtonPressed += () => + hostButton.LeftButtonPressed += () => { - + // host Server server = new Server(); //Server Logic MultiPlayer server.CreateRoom(5); @@ -99,9 +112,9 @@ public class SelectingServerGUI : AbstractGUI AppManager.Instance.SetGUI(new WaitingForPlayersGUI(true)); }; Elements.Add(hostButton); - + Button bTExit = new Button(Manager) - { fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"}; + { fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off" }; Elements.Add(bTExit); bTExit.LeftButtonPressed += () => { diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index 12a67b9..dd9cd13 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -31,12 +31,13 @@ public class WaitingForPlayersGUI : AbstractGUI 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); - // 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); + // string pcIp = + if (isHost) { - Button startButton = new Button(Manager) + 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); + Button startButton = new Button(Manager) { rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)), text = "Start", @@ -45,14 +46,34 @@ public class WaitingForPlayersGUI : AbstractGUI mainColor = Color.Gray, fontName = "Fonts/Font" }; - startButton.LeftButtonPressed += () => + startButton.LeftButtonPressed += () => { // start - + // ваш код здесь }; Elements.Add(startButton); } + else { + ip = new Label(Manager) { rectangle = new Rectangle(width / 2 - (int)(width / 8), height / 7, (int)(width / 4), (int)(height / 20)), text = AppManager.Instance.client.InfoConnect.ToString(), fontColor = Color.White, mainColor = Color.Transparent, scale = 0.9f, fontName = "Fonts\\Font3" }; + Elements.Add(ip); + Button waitButton = new Button(Manager) + { + rectangle = new Rectangle(width / 2 - (width / 15) / 2, height / 2 + height / 4, (int)(width / 15), (int)(height / 20)), + text = "WAITING", + scale = 0.3f, + fontColor = Color.White, + mainColor = Color.Gray, + fontName = "Fonts/Font" + }; + waitButton.LeftButtonPressed += () => + { + // start + + // ваш код здесь + }; + Elements.Add(waitButton); + } Button bTExit = new Button(Manager) { fontName = "Fonts/Font3", scale = 0.4f, text = "<-", fontColor = Color.Black, mainColor = Color.Transparent, rectangle = new Rectangle(width / 30, height / 30, width / 40, width / 40), textureName = "Textures/GUI/checkboxs_off"}; From 2cce4ea9b92ce1ab434ed0a5a7f66b9ee3ece531 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 12:25:53 +0300 Subject: [PATCH 02/12] CreateConnectMultiplayer --- ZoFo/GameCore/GUI/SelectingServerGUI.cs | 8 +++----- ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs | 4 ++-- ZoFo/GameCore/GameManagers/AppManager.cs | 2 +- .../GameManagers/NetworkManager/ServerNetworkManager.cs | 7 +++++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index f2c911c..717e2b3 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -32,11 +32,11 @@ public class SelectingServerGUI : AbstractGUI { rectangle = new Rectangle(width / 4 - (width / 4) / 2, height / 4, (int)(width / 4), (int)(height / 20)), text = "ip", - scale = 0.2f, + scale = 0.5f, fontColor = Color.White, mainColor = Color.Gray, textAligment = MonogameLibrary.UI.Enums.TextAligment.Left, - fontName = "Fonts/Font" + fontName = "Fonts/Font3" }; ipBox.TextChanged += input => { @@ -76,8 +76,6 @@ public class SelectingServerGUI : AbstractGUI { client.JoinRoom(endpoint[0], port); AppManager.Instance.SetClient(client); - Server server = new Server(); - AppManager.Instance.SetServer(server); AppManager.Instance.SetGUI(new WaitingForPlayersGUI(false)); } } @@ -104,7 +102,7 @@ public class SelectingServerGUI : AbstractGUI // host Server server = new Server(); //Server Logic MultiPlayer - server.CreateRoom(5); + server.CreateRoom(1); AppManager.Instance.SetServer(server); string key = server.MyIp.ToString(); AppManager.Instance.debugHud.Set(key, "MultiPlayer"); diff --git a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs index dd9cd13..7bcf287 100644 --- a/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs +++ b/ZoFo/GameCore/GUI/WaitingForPlayersGUI.cs @@ -49,7 +49,7 @@ public class WaitingForPlayersGUI : AbstractGUI startButton.LeftButtonPressed += () => { // start - + AppManager.Instance.ChangeState(GameState.HostPlaying); // ваш код здесь }; Elements.Add(startButton); @@ -69,7 +69,7 @@ public class WaitingForPlayersGUI : AbstractGUI waitButton.LeftButtonPressed += () => { // start - + AppManager.Instance.ChangeState(GameState.ClientPlaying); // ваш код здесь }; Elements.Add(waitButton); diff --git a/ZoFo/GameCore/GameManagers/AppManager.cs b/ZoFo/GameCore/GameManagers/AppManager.cs index fba2e17..48d923c 100644 --- a/ZoFo/GameCore/GameManagers/AppManager.cs +++ b/ZoFo/GameCore/GameManagers/AppManager.cs @@ -101,7 +101,7 @@ namespace ZoFo.GameCore.GameManagers break; case GameState.HostPlaying: server.Update(gameTime); - client.Update(gameTime); + // client.Update(gameTime); break; case GameState.ClientPlaying: server.Update(gameTime); diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 76e72c2..1d0ae62 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -142,13 +142,16 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager for (int i = 0; i < playNumber; i++) { Socket client = socket.Accept(); + AppManager.Instance.debugHud.Set("Connect", client.ToString()); Thread thread = new Thread(StartListening); thread.IsBackground = true; thread.Start(client); managerThread.Add(client, thread); - clients.Add(client); //добавляем клиентов в лист + clients.Add(client); + //AppManager.Instance.ChangeState(GameState.HostPlaying); + //добавляем клиентов в лист } - + AppManager.Instance.ChangeState(GameState.HostPlaying); } /// From d95c65c12caf72d0adab18479b03ea9113727125 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 14:03:06 +0300 Subject: [PATCH 03/12] SwitchOnServer --- ZoFo/GameCore/GameManagers/AppManager.cs | 2 +- .../ServerToClient/UpdateInteraction.cs | 3 +- .../ServerToClient/UpdateInteractionReady.cs | 2 +- ZoFo/GameCore/Server.cs | 32 +++++++++++++++++-- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/ZoFo/GameCore/GameManagers/AppManager.cs b/ZoFo/GameCore/GameManagers/AppManager.cs index 48d923c..fba2e17 100644 --- a/ZoFo/GameCore/GameManagers/AppManager.cs +++ b/ZoFo/GameCore/GameManagers/AppManager.cs @@ -101,7 +101,7 @@ namespace ZoFo.GameCore.GameManagers break; case GameState.HostPlaying: server.Update(gameTime); - // client.Update(gameTime); + client.Update(gameTime); break; case GameState.ClientPlaying: server.Update(gameTime); diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs index e42f0f4..96ecef2 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteraction.cs @@ -5,6 +5,5 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager.Updates.ServerToClient; /// public class UpdateInteraction : UpdateData { - public int IdEntity { get; set; } - public string UpdateType { get; set; } + public UpdateInteraction() { UpdateType = "UpdateInteraction"; } } \ No newline at end of file diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteractionReady.cs b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteractionReady.cs index db4d01d..591bc1b 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteractionReady.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/Updates/ServerToClient/UpdateInteractionReady.cs @@ -9,6 +9,6 @@ public class UpdateInteractionReady(int idEntity, bool isReady) : UpdateData { public int IdEntity { get; set; } = idEntity; - public string UpdateType { get; set; } + public string UpdateType { get; set; } = "UpdateInteractionReady"; public bool IsReady { get; set; } = isReady; } \ No newline at end of file diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index 4bea6cc..8dd53dc 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -21,7 +21,7 @@ namespace ZoFo.GameCore { private ServerNetworkManager networkManager; private int ticks = 0; - public IPEndPoint MyIp { get { return networkManager.InfoConnect; } } + public IPEndPoint MyIp { get { return networkManager.InfoConnect; } } public Server() { networkManager = new ServerNetworkManager(); @@ -46,6 +46,32 @@ namespace ZoFo.GameCore { //ТУТ Switch case будет честное слово + switch (updateData.UpdateType) + { + case "UpdateAnimation": + break; + case "UpdateEntityHealth": + break; + case "UpdateGameEnded": + break; + case "UpdateGameObjectCreated": + break; + case "UpdateGameObjectDeleted": + break; + case "UpdateInteraction": + break; + case "UpdateInteractionReady": + break; + case "UpdateLoot": + break; + case "UpdatePlayerParametrs": + break; + case "UpdatePosition": + break; + case "UpdateTileCreated": + break; + + } } public void CloseConnection() @@ -136,8 +162,8 @@ namespace ZoFo.GameCore AddData(new UpdateGameObjectCreated() { GameObjectType = gameObject.GetType().Name } - ); - + ); + } } #endregion From 6c07557debc9b118356457768ba85c1518b6bbf5 Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 19:02:02 +0300 Subject: [PATCH 04/12] FixGetIp --- .../GameManagers/NetworkManager/ServerNetworkManager.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 1d0ae62..9ea5de2 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -53,8 +53,9 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager /// public static IPAddress GetIp() { - string hostName = Dns.GetHostName(); // Retrive the Name of HOST - string myIP = Dns.GetHostByName(hostName).AddressList[1].ToString();// Get the IP + string hostName = Dns.GetHostName(); // Retrive the Name of HOST + var ipList = Dns.GetHostByName(hostName).AddressList; + string myIP = ipList[ipList.Count()-1].ToString();// Get the IP return IPAddress.Parse(myIP); } From a06f9c7ddb738f048bc707b5c7e786d1794dfa1d Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sat, 17 Aug 2024 22:25:03 +0300 Subject: [PATCH 05/12] MaybeFinallyMultiplayer --- ZoFo/GameCore/GUI/SelectingServerGUI.cs | 5 ++++- ZoFo/GameCore/GameManagers/AppManager.cs | 4 ++-- .../GameManagers/NetworkManager/ServerNetworkManager.cs | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ZoFo/GameCore/GUI/SelectingServerGUI.cs b/ZoFo/GameCore/GUI/SelectingServerGUI.cs index 717e2b3..9460341 100644 --- a/ZoFo/GameCore/GUI/SelectingServerGUI.cs +++ b/ZoFo/GameCore/GUI/SelectingServerGUI.cs @@ -102,8 +102,11 @@ public class SelectingServerGUI : AbstractGUI // host Server server = new Server(); //Server Logic MultiPlayer - server.CreateRoom(1); + Client client = new Client(); + server.CreateRoom(2); + client.JoinYourself(server.MyIp.Port); AppManager.Instance.SetServer(server); + AppManager.Instance.SetClient(client); string key = server.MyIp.ToString(); AppManager.Instance.debugHud.Set(key, "MultiPlayer"); // ваш код здесь diff --git a/ZoFo/GameCore/GameManagers/AppManager.cs b/ZoFo/GameCore/GameManagers/AppManager.cs index fba2e17..f328803 100644 --- a/ZoFo/GameCore/GameManagers/AppManager.cs +++ b/ZoFo/GameCore/GameManagers/AppManager.cs @@ -91,7 +91,7 @@ namespace ZoFo.GameCore.GameManagers Keyboard.GetState().IsKeyDown(Keys.Escape)) { server.CloseConnection(); Exit(); } - debugHud.Set("key", "value"); + // debugHud.Set("key", "value"); InputManager.Update(); currentGUI.Update(gameTime); @@ -104,7 +104,7 @@ namespace ZoFo.GameCore.GameManagers client.Update(gameTime); break; case GameState.ClientPlaying: - server.Update(gameTime); + client.Update(gameTime); break; default: break; diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index 9ea5de2..f6452af 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -143,7 +143,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager for (int i = 0; i < playNumber; i++) { Socket client = socket.Accept(); - AppManager.Instance.debugHud.Set("Connect", client.ToString()); + AppManager.Instance.debugHud.Set("Connect", client.LocalEndPoint.ToString()); Thread thread = new Thread(StartListening); thread.IsBackground = true; thread.Start(client); From fd3c8305f2a766de9750ae134ebe112492dfe444 Mon Sep 17 00:00:00 2001 From: Mootfrost777 Date: Sat, 17 Aug 2024 23:45:08 +0300 Subject: [PATCH 06/12] Split GraphicsComponent.cs to animated and static --- .../Entities/EntittyForAnimationTests.cs | 2 +- .../Interactables/Collectables/Wood.cs | 2 +- .../Entities/Interactables/Door.cs | 8 +- .../Entities/Interactables/Interactable.cs | 2 +- .../Entities/LivingEntities/LivingEntity.cs | 2 +- .../Entities/LivingEntities/Player/Player.cs | 2 +- ZoFo/GameCore/GameObjects/GameObject.cs | 2 +- .../GameObjects/MapObjects/MapObject.cs | 4 +- .../Graphics/AnimatedGraphicsComponent.cs | 310 +++++++++++++++ ZoFo/GameCore/Graphics/GraphicsComponent.cs | 360 +++--------------- .../Graphics/StaticGraphicsComponent.cs | 61 +++ 11 files changed, 432 insertions(+), 323 deletions(-) create mode 100644 ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs create mode 100644 ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs diff --git a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs index 9336e76..3ee347d 100644 --- a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs +++ b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs @@ -12,7 +12,7 @@ namespace ZoFo.GameCore.GameObjects.Entities { //public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "тут пишите название анимации" }, "сдублируйте " + - public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public EntittyForAnimationTests(Vector2 position) : base(position) { graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs index 2c6b0b8..b5876d4 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs @@ -6,7 +6,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables; public class Wood : Collectable { - public override GraphicsComponent graphicsComponent { get; } = new(new List { "Wood" }, "Wood"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("Wood"); public Wood(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs index 640c9c1..39b52f0 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs @@ -9,16 +9,16 @@ public class Door : Interactable { public bool isOpened; - public override GraphicsComponent graphicsComponent { get; } = new(new List { "DoorInteraction" }, "DoorInteraction"); + public override StaticGraphicsComponent graphicsComponent { get; } = new("DoorClosed"); public Door(Vector2 position) : base(position) { - graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; }; + //graphicsComponent.OnAnimationEnd += _ => { isOpened = !isOpened; }; } public override void OnInteraction(object sender, CollisionComponent e) { - graphicsComponent.AnimationSelect("DoorInteraction", isOpened); - graphicsComponent.AnimationStep(); + //graphicsComponent.AnimationSelect("DoorInteraction", isOpened); + //graphicsComponent.AnimationStep(); } } \ No newline at end of file diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs index bfe1b42..01c9d7d 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs @@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables; public class Interactable : Entity { - public override GraphicsComponent graphicsComponent => throw new System.NotImplementedException(); + public override StaticGraphicsComponent graphicsComponent => throw new System.NotImplementedException(); public Interactable(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs index e7e15f1..c641bb8 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs @@ -21,7 +21,7 @@ public class LivingEntity : Entity inputManager = new InputManager(); } - public override GraphicsComponent graphicsComponent { get; } = null; + public override AnimatedGraphicsComponent graphicsComponent { get; } = null; #region Server side /*public override void Update() diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 026a746..d20a088 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -20,7 +20,7 @@ public class Player : LivingEntity public bool IsTryingToShoot { get; set; } private float speed; private int health; - public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public Player(Vector2 position) : base(position) { //InputWeaponRotation = new Vector2(0, 0); diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index 66cb30d..91c7b92 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -80,7 +80,7 @@ public abstract class GameObject /// public virtual void Draw(SpriteBatch spriteBatch) { - graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch); + graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch); //debug if (AppManager.Instance.InputManager.CollisionsCheat) DrawDebugRectangle(spriteBatch, graphicsComponent.ObjectDrawRectangle); diff --git a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs index 15ac53c..da53cfa 100644 --- a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs +++ b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs @@ -16,7 +16,7 @@ namespace ZoFo.GameCore.GameObjects.MapObjects { public virtual bool IsColliderOn { get; protected set; } = true;//Who added that? public Rectangle sourceRectangle; - public override GraphicsComponent graphicsComponent { get; } = new(); + public override StaticGraphicsComponent graphicsComponent { get; } = new(); /// /// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры @@ -35,7 +35,7 @@ namespace ZoFo.GameCore.GameObjects.MapObjects } public override void Draw(SpriteBatch spriteBatch) { - graphicsComponent.DrawAnimation(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle); + graphicsComponent.Draw(graphicsComponent.ObjectDrawRectangle, spriteBatch, sourceRectangle); } } diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs new file mode 100644 index 0000000..b56e844 --- /dev/null +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -0,0 +1,310 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ZoFo.GameCore.GameManagers; + +namespace ZoFo.GameCore.Graphics +{ + + public class AnimatedGraphicsComponent : GraphicsComponent + { + public Rectangle ObjectDrawRectangle; + + + + public event Action OnAnimationEnd; + private List animations; + private List textures; + public List texturesNames; //rethink public and following that errors + private AnimationContainer currentAnimation; + + public bool animating = true; + private int step = 1; + + public AnimationContainer CurrentAnimation + { + get + { + return currentAnimation; + } + } + public string LastAnimation { get; set; } + public string GetCurrentAnimation + { + get { return currentAnimation.Id; } + } + + private AnimationContainer idleAnimation; + //private SpriteBatch _spriteBatch; + + private int currentFrame; + public int CurrentFrame + { + get + { + return currentFrame; + } + } + private int interval; + private int lastInterval; + private Rectangle sourceRectangle; + public AnimatedGraphicsComponent(List animationsId, string neitralAnimationId) + { + //this._spriteBatch = _spriteBatch; + currentFrame = 0; + lastInterval = 1; + LoadAnimations(animationsId, neitralAnimationId); + currentAnimation = idleAnimation; + SetInterval(); + buildSourceRectangle(); + } + + + public AnimatedGraphicsComponent(string textureName) + { + BuildComponent(textureName); + } + public AnimatedGraphicsComponent() + { + } + public void BuildComponent(string textureName) + { + mainTextureName = textureName; + //texturesNames.Add(textureName);//Added by SD + animations = new List(); + textures = new List(); + var texture = AppManager.Instance.Content.Load(textureName); + textures.Add(texture); + AnimationContainer animationContainer = new AnimationContainer(); + animationContainer.StartSpriteRectangle = new Rectangle(0, 0, texture.Width, texture.Height); + animationContainer.TextureFrameInterval = 0; + animationContainer.TextureName = texture.Name; + animationContainer.IsCycle = true; + animationContainer.FramesCount = 1; + animationContainer.FrameTime = new List>() { new Tuple(0, 10) }; + animationContainer.Id = texture.Name; + currentAnimation = animationContainer; + idleAnimation = animationContainer; + animations.Add(animationContainer); + } + + private void LoadAnimations(List animationsId, string neitralAnimationId) + { + animations = new List(); + foreach (var id in animationsId) + { + animations.Add(AppManager.Instance.animationBuilder.Animations.Find(x => x.Id == id)); + if (id == neitralAnimationId) + { + idleAnimation = animations.Last(); + } + } + } + + public override void LoadContent() + { + textures = new List(); + texturesNames = new List(); + + if (animations is null) + { + return; + } + + foreach (var animation in animations) + { + if (!texturesNames.Contains(animation.TextureName)) + { + texturesNames.Add(animation.TextureName); + textures.Add(AppManager.Instance.Content.Load(animation.TextureName)); + } + } + } + + public void AnimationSelect(string animationId, bool reverse = false) + { + currentAnimation = animations.Find(x => x.Id == animationId); + if (reverse) + { + currentFrame = currentAnimation.FramesCount; + step = -1; + } + else + { + step = 1; + currentFrame = 1; + } + buildSourceRectangle(); + SetInterval(); + } + + public void StartAnimation() + { + animating = true; + } + + public void AnimationStep() + { + currentFrame += step; + } + + public void SetFrame(int frame) + { + currentFrame = frame; + } + + public void StopAnimation() + { + currentFrame = 0; + interval = 0; + currentAnimation = idleAnimation; + buildSourceRectangle(); + SetInterval(); + } + + private void AnimationEnd() + { + if (!currentAnimation.IsCycle) + { + if (OnAnimationEnd != null) + { + OnAnimationEnd(currentAnimation.Id); + } + currentAnimation = idleAnimation; + animating = false; + } + currentFrame = 0; + } + + public override void Update() + { + if (currentAnimation.FramesCount <= currentFrame || currentFrame < 0) + { + AnimationEnd(); + } + + if (!animating) + return; + + if (interval == 0) + { + currentFrame += step; + buildSourceRectangle(); + SetInterval(); + } + + interval--; + } + + + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) + { + Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; + float scale; + if (currentAnimation.Offset.X != 0) + { + destinationRectangle.X -= (int)currentAnimation.Offset.X; + scale = destinationRectangle.Height / sourceRectangle.Height; + destinationRectangle.Width = (int)(sourceRectangle.Width * scale); + + } + else if (currentAnimation.Offset.Y != 0) + { + destinationRectangle.Y -= (int)currentAnimation.Offset.Y; + scale = destinationRectangle.Width / sourceRectangle.Width; + destinationRectangle.Height = (int)(sourceRectangle.Height * scale); + } + + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, + destinationRectangle, sourceRectangle, Color.White); + } + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) + { + Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; + float scale; + if (currentAnimation.Offset.X != 0) + { + destinationRectangle.X -= (int)currentAnimation.Offset.X; + scale = destinationRectangle.Height / sourceRectangle.Height; + destinationRectangle.Width = (int)(sourceRectangle.Width * scale); + + } + else if (currentAnimation.Offset.Y != 0) + { + destinationRectangle.Y -= (int)currentAnimation.Offset.Y; + scale = destinationRectangle.Width / sourceRectangle.Width; + destinationRectangle.Height = (int)(sourceRectangle.Height * scale); + } + + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, + destinationRectangle, sourceRectangle, Color.White); + } + private void buildSourceRectangle() + { + sourceRectangle = new Rectangle(); + if (currentAnimation == null) + { + currentAnimation = idleAnimation; + } + sourceRectangle.X = currentAnimation.StartSpriteRectangle.X + currentFrame * + (currentAnimation.StartSpriteRectangle.Width + currentAnimation.TextureFrameInterval); + sourceRectangle.Y = currentAnimation.StartSpriteRectangle.Y; + sourceRectangle.Height = currentAnimation.StartSpriteRectangle.Height; + sourceRectangle.Width = currentAnimation.StartSpriteRectangle.Width; + } + + private void SetInterval() + { + Tuple i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame); + if (i != null) + { + interval = i.Item2; + lastInterval = interval; + } + else + { + interval = lastInterval; + } + } + public static void SetCameraPosition(Vector2 playerPosition) + { + CameraPosition = (playerPosition).ToPoint(); + CameraPosition.X -= 200; + CameraPosition.Y -= 120; + + // TODO + /* + if (CameraPosition.X > AppManager.Instance.GameManager.CameraBorder.Y - 460) + { + CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.Y - 460; + } + + if (CameraPosition.Y < AppManager.Instance.GameManager.CameraBorder.Z) + { + CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.Z; + } + if (CameraPosition.X < AppManager.Instance.GameManager.CameraBorder.X) + { + CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.X; + } + if (CameraPosition.Y > AppManager.Instance.GameManager.CameraBorder.W - 240) + { + CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.W - 240; + } + + AppManager.Instance.DebugHUD.Set("CameraPosition", $"{CameraPosition.X}, {CameraPosition.Y}"); + */ + } + public static Point CameraPosition = new Point(0, 0); + } +} diff --git a/ZoFo/GameCore/Graphics/GraphicsComponent.cs b/ZoFo/GameCore/Graphics/GraphicsComponent.cs index e8f9f17..c71b0c3 100644 --- a/ZoFo/GameCore/Graphics/GraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/GraphicsComponent.cs @@ -1,318 +1,56 @@ -using System; -using System.Collections.Generic; -using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using ZoFo.GameCore.GameManagers; -namespace ZoFo.GameCore.Graphics +namespace ZoFo.GameCore.Graphics; + +public abstract class GraphicsComponent { + public Rectangle ObjectDrawRectangle; + public static int scaling = 1; + public string mainTextureName;//TODO костыль - пофиксить - public class GraphicsComponent + 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); + + protected Rectangle Scaling(Rectangle destinationRectangle) { - public Rectangle ObjectDrawRectangle; - - - - public event Action OnAnimationEnd; - private List animations; - private List textures; - public List texturesNames; //rethink public and following that errors - private AnimationContainer currentAnimation; - public static int scaling = 1; - - public bool animating = true; - private int step = 1; - - public AnimationContainer CurrentAnimation - { - get - { - return currentAnimation; - } - } - public string LastAnimation { get; set; } - public string GetCurrentAnimation - { - get { return currentAnimation.Id; } - } - - private AnimationContainer idleAnimation; - //private SpriteBatch _spriteBatch; - - private int currentFrame; - public int CurrentFrame - { - get - { - return currentFrame; - } - } - private int interval; - private int lastInterval; - private Rectangle sourceRectangle; - public GraphicsComponent(List animationsId, string neitralAnimationId) - { - //this._spriteBatch = _spriteBatch; - currentFrame = 0; - lastInterval = 1; - LoadAnimations(animationsId, neitralAnimationId); - currentAnimation = idleAnimation; - SetInterval(); - buildSourceRectangle(); - } - - public string mainTextureName;//TODO костыль - пофиксить - public GraphicsComponent(string textureName) - { - BuildComponent(textureName); - } - public GraphicsComponent() - { - } - public void BuildComponent(string textureName) - { - mainTextureName = textureName; - //texturesNames.Add(textureName);//Added by SD - animations = new List(); - textures = new List(); - var texture = AppManager.Instance.Content.Load(textureName); - textures.Add(texture); - AnimationContainer animationContainer = new AnimationContainer(); - animationContainer.StartSpriteRectangle = new Rectangle(0, 0, texture.Width, texture.Height); - animationContainer.TextureFrameInterval = 0; - animationContainer.TextureName = texture.Name; - animationContainer.IsCycle = true; - animationContainer.FramesCount = 1; - animationContainer.FrameTime = new List>() { new Tuple(0, 10) }; - animationContainer.Id = texture.Name; - currentAnimation = animationContainer; - idleAnimation = animationContainer; - animations.Add(animationContainer); - } - - private void LoadAnimations(List animationsId, string neitralAnimationId) - { - animations = new List(); - foreach (var id in animationsId) - { - animations.Add(AppManager.Instance.animationBuilder.Animations.Find(x => x.Id == id)); - if (id == neitralAnimationId) - { - idleAnimation = animations.Last(); - } - } - } - - public void LoadContent() - { - textures = new List(); - texturesNames = new List(); - - if (animations is null) - { - return; - } - - foreach (var animation in animations) - { - if (!texturesNames.Contains(animation.TextureName)) - { - texturesNames.Add(animation.TextureName); - textures.Add(AppManager.Instance.Content.Load(animation.TextureName)); - } - } - } - - public void AnimationSelect(string animationId, bool reverse = false) - { - currentAnimation = animations.Find(x => x.Id == animationId); - if (reverse) - { - currentFrame = currentAnimation.FramesCount; - step = -1; - } - else - { - step = 1; - currentFrame = 1; - } - buildSourceRectangle(); - SetInterval(); - } - - public void StartAnimation() - { - animating = true; - } - - public void AnimationStep() - { - currentFrame += step; - } - - public void SetFrame(int frame) - { - currentFrame = frame; - } - - public void StopAnimation() - { - currentFrame = 0; - interval = 0; - currentAnimation = idleAnimation; - buildSourceRectangle(); - SetInterval(); - } - - private void AnimationEnd() - { - if (!currentAnimation.IsCycle) - { - if (OnAnimationEnd != null) - { - OnAnimationEnd(currentAnimation.Id); - } - currentAnimation = idleAnimation; - animating = false; - } - currentFrame = 0; - } - - public void Update() - { - if (currentAnimation.FramesCount <= currentFrame || currentFrame < 0) - { - AnimationEnd(); - } - - if (!animating) - return; - - if (interval == 0) - { - currentFrame += step; - buildSourceRectangle(); - SetInterval(); - } - - interval--; - } - - public void DrawAnimation(Rectangle destinationRectangle, SpriteBatch _spriteBatch) - { - Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; - float scale; - if (currentAnimation.Offset.X != 0) - { - destinationRectangle.X -= (int)currentAnimation.Offset.X; - scale = destinationRectangle.Height / sourceRectangle.Height; - destinationRectangle.Width = (int)(sourceRectangle.Width * scale); - - } - else if (currentAnimation.Offset.Y != 0) - { - destinationRectangle.Y -= (int)currentAnimation.Offset.Y; - scale = destinationRectangle.Width / sourceRectangle.Width; - destinationRectangle.Height = (int)(sourceRectangle.Height * scale); - } - - destinationRectangle.X -= CameraPosition.X; - destinationRectangle.Y -= CameraPosition.Y; - - destinationRectangle = Scaling(destinationRectangle); - _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); - } - public void DrawAnimation(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) - { - Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; - float scale; - if (currentAnimation.Offset.X != 0) - { - destinationRectangle.X -= (int)currentAnimation.Offset.X; - scale = destinationRectangle.Height / sourceRectangle.Height; - destinationRectangle.Width = (int)(sourceRectangle.Width * scale); - - } - else if (currentAnimation.Offset.Y != 0) - { - destinationRectangle.Y -= (int)currentAnimation.Offset.Y; - scale = destinationRectangle.Width / sourceRectangle.Width; - destinationRectangle.Height = (int)(sourceRectangle.Height * scale); - } - - destinationRectangle.X -= CameraPosition.X; - destinationRectangle.Y -= CameraPosition.Y; - - destinationRectangle = Scaling(destinationRectangle); - _spriteBatch.Draw(texture, - destinationRectangle, sourceRectangle, Color.White); - } - private Rectangle Scaling(Rectangle destinationRectangle) - { - destinationRectangle.X *= scaling; - destinationRectangle.Y *= scaling; - destinationRectangle.Width *= scaling; - destinationRectangle.Height *= scaling; - return destinationRectangle; - } - private void buildSourceRectangle() - { - sourceRectangle = new Rectangle(); - if (currentAnimation == null) - { - currentAnimation = idleAnimation; - } - sourceRectangle.X = currentAnimation.StartSpriteRectangle.X + currentFrame * - (currentAnimation.StartSpriteRectangle.Width + currentAnimation.TextureFrameInterval); - sourceRectangle.Y = currentAnimation.StartSpriteRectangle.Y; - sourceRectangle.Height = currentAnimation.StartSpriteRectangle.Height; - sourceRectangle.Width = currentAnimation.StartSpriteRectangle.Width; - } - - private void SetInterval() - { - Tuple i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame); - if (i != null) - { - interval = i.Item2; - lastInterval = interval; - } - else - { - interval = lastInterval; - } - } - public static void SetCameraPosition(Vector2 playerPosition) - { - CameraPosition = (playerPosition).ToPoint(); - CameraPosition.X -= 200; - CameraPosition.Y -= 120; - - // TODO - /* - if (CameraPosition.X > AppManager.Instance.GameManager.CameraBorder.Y - 460) - { - CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.Y - 460; - } - - if (CameraPosition.Y < AppManager.Instance.GameManager.CameraBorder.Z) - { - CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.Z; - } - if (CameraPosition.X < AppManager.Instance.GameManager.CameraBorder.X) - { - CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.X; - } - if (CameraPosition.Y > AppManager.Instance.GameManager.CameraBorder.W - 240) - { - CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.W - 240; - } - - AppManager.Instance.DebugHUD.Set("CameraPosition", $"{CameraPosition.X}, {CameraPosition.Y}"); - */ - } - public static Point CameraPosition = new Point(0, 0); + destinationRectangle.X *= scaling; + destinationRectangle.Y *= scaling; + destinationRectangle.Width *= scaling; + destinationRectangle.Height *= scaling; + return destinationRectangle; } -} + + public static void SetCameraPosition(Vector2 playerPosition) + { + CameraPosition = (playerPosition).ToPoint(); + CameraPosition.X -= 200; + CameraPosition.Y -= 120; + + // TODO + /* + if (CameraPosition.X > AppManager.Instance.GameManager.CameraBorder.Y - 460) + { + CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.Y - 460; + } + + if (CameraPosition.Y < AppManager.Instance.GameManager.CameraBorder.Z) + { + CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.Z; + } + if (CameraPosition.X < AppManager.Instance.GameManager.CameraBorder.X) + { + CameraPosition.X = (int)AppManager.Instance.GameManager.CameraBorder.X; + } + if (CameraPosition.Y > AppManager.Instance.GameManager.CameraBorder.W - 240) + { + CameraPosition.Y = (int)AppManager.Instance.GameManager.CameraBorder.W - 240; + } + + AppManager.Instance.DebugHUD.Set("CameraPosition", $"{CameraPosition.X}, {CameraPosition.Y}"); + */ + } + public static Point CameraPosition = new Point(0, 0); +} \ No newline at end of file diff --git a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs new file mode 100644 index 0000000..ea28b30 --- /dev/null +++ b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using ZoFo.GameCore.GameManagers; + +namespace ZoFo.GameCore.Graphics +{ + + public class StaticGraphicsComponent : GraphicsComponent + { + private Texture2D texture; + private string textureName; + + + public StaticGraphicsComponent() + { + + } + + public StaticGraphicsComponent(string textureName) + { + BuildComponent(textureName); + } + + public void BuildComponent(string textureName) + { + this.textureName = textureName; + } + + + public override void LoadContent() + { + texture = AppManager.Instance.Content.Load(textureName); + } + + public override void Update() + { + throw new NotImplementedException(); + } + + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) + { + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, destinationRectangle, Color.White); + } + + public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) + { + destinationRectangle.X -= CameraPosition.X; + destinationRectangle.Y -= CameraPosition.Y; + + destinationRectangle = Scaling(destinationRectangle); + _spriteBatch.Draw(texture, + destinationRectangle, sourceRectangle, Color.White); + } + } +} From bd25894407f92e5768edfd0bd651cd308c6acfc5 Mon Sep 17 00:00:00 2001 From: Mootfrost777 Date: Sun, 18 Aug 2024 00:26:36 +0300 Subject: [PATCH 07/12] Fix --- .../Entities/EntittyForAnimationTests.cs | 2 +- .../Entities/Interactables/Collectables/Wood.cs | 2 +- .../GameObjects/Entities/Interactables/Door.cs | 2 +- .../Entities/Interactables/Interactable.cs | 2 +- .../GameCore/GameObjects/MapObjects/MapObject.cs | 2 +- .../Graphics/AnimatedGraphicsComponent.cs | 6 ++++++ ZoFo/GameCore/Graphics/IGraphicsComponent.cs | 16 ++++++++++++++++ .../GameCore/Graphics/StaticGraphicsComponent.cs | 5 +++++ 8 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 ZoFo/GameCore/Graphics/IGraphicsComponent.cs diff --git a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs index 3ee347d..5ff1061 100644 --- a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs +++ b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs @@ -12,7 +12,7 @@ namespace ZoFo.GameCore.GameObjects.Entities { //public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "тут пишите название анимации" }, "сдублируйте " + - public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public EntittyForAnimationTests(Vector2 position) : base(position) { graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16); diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs index b5876d4..5686918 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Collectables/Wood.cs @@ -6,7 +6,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables.Collectables; public class Wood : Collectable { - public override StaticGraphicsComponent graphicsComponent { get; } = new("Wood"); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new("Wood"); public Wood(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs index 39b52f0..52f1eba 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Door.cs @@ -9,7 +9,7 @@ public class Door : Interactable { public bool isOpened; - public override StaticGraphicsComponent graphicsComponent { get; } = new("DoorClosed"); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new("DoorClosed"); public Door(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs index 270b986..0004328 100644 --- a/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs +++ b/ZoFo/GameCore/GameObjects/Entities/Interactables/Interactable.cs @@ -9,7 +9,7 @@ namespace ZoFo.GameCore.GameObjects.Entities.Interactables; public class Interactable : Entity { - public override StaticGraphicsComponent graphicsComponent => throw new System.NotImplementedException(); + public override AnimatedGraphicsComponent graphicsComponent => throw new System.NotImplementedException(); public Interactable(Vector2 position) : base(position) { diff --git a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs index da53cfa..a1f4fae 100644 --- a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs +++ b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs @@ -16,7 +16,7 @@ namespace ZoFo.GameCore.GameObjects.MapObjects { public virtual bool IsColliderOn { get; protected set; } = true;//Who added that? public Rectangle sourceRectangle; - public override StaticGraphicsComponent graphicsComponent { get; } = new(); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new(); /// /// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs index b56e844..cc2f87d 100644 --- a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -4,6 +4,7 @@ using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ZoFo.GameCore.GameManagers; +using ZoFo.GameCore.GUI; namespace ZoFo.GameCore.Graphics { @@ -202,6 +203,7 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) { Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; + float scale; if (currentAnimation.Offset.X != 0) { @@ -223,10 +225,14 @@ namespace ZoFo.GameCore.Graphics destinationRectangle = Scaling(destinationRectangle); _spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White); + DebugHUD.Instance.Log(texture.Name); + } public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) { + Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; + float scale; if (currentAnimation.Offset.X != 0) { diff --git a/ZoFo/GameCore/Graphics/IGraphicsComponent.cs b/ZoFo/GameCore/Graphics/IGraphicsComponent.cs new file mode 100644 index 0000000..50e2424 --- /dev/null +++ b/ZoFo/GameCore/Graphics/IGraphicsComponent.cs @@ -0,0 +1,16 @@ +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 ea28b30..5c36538 100644 --- a/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/StaticGraphicsComponent.cs @@ -4,6 +4,7 @@ using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ZoFo.GameCore.GameManagers; +using ZoFo.GameCore.GUI; namespace ZoFo.GameCore.Graphics { @@ -42,6 +43,8 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) { + DebugHUD.Instance.Log("draw "); + destinationRectangle.X -= CameraPosition.X; destinationRectangle.Y -= CameraPosition.Y; destinationRectangle = Scaling(destinationRectangle); @@ -50,6 +53,8 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) { + DebugHUD.Instance.Log("draw "); + destinationRectangle.X -= CameraPosition.X; destinationRectangle.Y -= CameraPosition.Y; From 5a7fc98597f421759cf54e9b8e45a7dded1f2232 Mon Sep 17 00:00:00 2001 From: Mootfrost777 Date: Sun, 18 Aug 2024 00:57:50 +0300 Subject: [PATCH 08/12] Fix entities displaying --- .../GameObjects/Entities/LivingEntities/Player/Player.cs | 4 ++-- ZoFo/GameCore/GameObjects/GameObject.cs | 2 +- ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index 5072c8d..ca0ddac 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -20,19 +20,19 @@ public class Player : LivingEntity public bool IsTryingToShoot { get; set; } private float speed; private int health; - public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); + public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate", new Rectangle(0,0,100,100)); public Player(Vector2 position) : base(position) { //InputWeaponRotation = new Vector2(0, 0); //InputPlayerRotation = new Vector2(0, 0); collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); - graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,100,100); } public override void Update() { + MovementLogic(); } diff --git a/ZoFo/GameCore/GameObjects/GameObject.cs b/ZoFo/GameCore/GameObjects/GameObject.cs index f569685..85189ea 100644 --- a/ZoFo/GameCore/GameObjects/GameObject.cs +++ b/ZoFo/GameCore/GameObjects/GameObject.cs @@ -13,7 +13,7 @@ public abstract class GameObject public Vector2 position; public Vector2 rotation; //вектор направления объекта - public abstract GraphicsComponent graphicsComponent { get; } + public virtual GraphicsComponent graphicsComponent { get; } #region ServerSide public GameObject(Vector2 position) diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs index cc2f87d..36edc5b 100644 --- a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -11,7 +11,6 @@ namespace ZoFo.GameCore.Graphics public class AnimatedGraphicsComponent : GraphicsComponent { - public Rectangle ObjectDrawRectangle; @@ -51,8 +50,9 @@ namespace ZoFo.GameCore.Graphics private int interval; private int lastInterval; private Rectangle sourceRectangle; - public AnimatedGraphicsComponent(List animationsId, string neitralAnimationId) + public AnimatedGraphicsComponent(List animationsId, string neitralAnimationId, Rectangle objectDrawRectangle = new()) { + ObjectDrawRectangle = objectDrawRectangle; //this._spriteBatch = _spriteBatch; currentFrame = 0; lastInterval = 1; @@ -202,6 +202,7 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) { + DebugHUD.Instance.Log($"{destinationRectangle.Width} : {destinationRectangle.Height}"); Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; float scale; From 7c9e13a490545bd758992bbc45fc0f068f0e0622 Mon Sep 17 00:00:00 2001 From: Monti Date: Sun, 18 Aug 2024 10:26:14 +0300 Subject: [PATCH 09/12] animationBronya --- .../Textures/Animations/player_idle_rotate_weapon.animation | 1 - ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 ZoFo/Content/Textures/Animations/player_idle_rotate_weapon.animation diff --git a/ZoFo/Content/Textures/Animations/player_idle_rotate_weapon.animation b/ZoFo/Content/Textures/Animations/player_idle_rotate_weapon.animation deleted file mode 100644 index 3240a26..0000000 --- a/ZoFo/Content/Textures/Animations/player_idle_rotate_weapon.animation +++ /dev/null @@ -1 +0,0 @@ -{"id":"player_idle_rotate_weapon","textureName":"Textures/AnimationTextures/Character/hr-level1_idle","startSpriteRectangle":{"X":0,"Y":116,"Width":92,"Height":116},"frameSecond":[{"Item1":0,"Item2":5}],"textureFrameInterval":0,"framesCount":22,"isCycle":true,"offset":"0, 0"} diff --git a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs index adafb5e..379e68b 100644 --- a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs +++ b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs @@ -12,7 +12,7 @@ namespace ZoFo.GameCore.GameObjects.Entities { //public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "тут пишите название анимации" }, "сдублируйте " + - public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); + public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "player_idle_top-right_noweapon" }, "player_idle_top-right_noweapon"); public EntittyForAnimationTests(Vector2 position) : base(position) { graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16); From 0afee1fb815ba052433c1d4d322b021f96edf84d Mon Sep 17 00:00:00 2001 From: rawer470 Date: Sun, 18 Aug 2024 10:43:02 +0300 Subject: [PATCH 10/12] WorkConnectMultiPlayer --- .../GameManagers/NetworkManager/ClientNetworkManager.cs | 2 +- .../GameManagers/NetworkManager/ServerNetworkManager.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs index 2a93f71..9c6137b 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ClientNetworkManager.cs @@ -108,7 +108,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager while(socket.Connected) { byte[] bytes = new byte[2048]; - var countAnsw = socket.Receive(bytes); + var countAnsw = socket.Receive(bytes); //Вылетает если кто то закрыл string update = Encoding.UTF8.GetString(bytes, 0, countAnsw); // обновление отосланные сервером GetDataSent(update); } diff --git a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs index f6452af..dcdaf1c 100644 --- a/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs +++ b/ZoFo/GameCore/GameManagers/NetworkManager/ServerNetworkManager.cs @@ -71,7 +71,7 @@ namespace ZoFo.GameCore.GameManagers.NetworkManager } updates.Clear(); return; //TODO TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK TODO REMOVE TO ADD NETWORK - + //Что это? //по 10 паков за раз TODO FIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXITFIXIT List datasToSend = new List(); for (int i = 0; i < 5 && i Date: Sun, 18 Aug 2024 10:53:42 +0300 Subject: [PATCH 11/12] Fix inheritance --- .../GameCore/GameObjects/Entities/EntittyForAnimationTests.cs | 2 +- .../GameObjects/Entities/LivingEntities/LivingEntity.cs | 2 +- .../GameObjects/Entities/LivingEntities/Player/Player.cs | 4 ++-- ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs | 2 -- ZoFo/GameCore/Server.cs | 3 +++ 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs index 5ff1061..46d2abe 100644 --- a/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs +++ b/ZoFo/GameCore/GameObjects/Entities/EntittyForAnimationTests.cs @@ -12,7 +12,7 @@ namespace ZoFo.GameCore.GameObjects.Entities { //public override GraphicsComponent graphicsComponent { get; } = new GraphicsComponent(new List { "тут пишите название анимации" }, "сдублируйте " + - public override AnimatedGraphicsComponent graphicsComponent { get; } = new(new List { "player_running_top_rotate" }, "player_running_top_rotate"); + public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public EntittyForAnimationTests(Vector2 position) : base(position) { graphicsComponent.ObjectDrawRectangle = new Rectangle(0,0,16*12, 16 * 16); diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs index 7d4d845..cc6cb9c 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/LivingEntity.cs @@ -21,7 +21,7 @@ public class LivingEntity : Entity inputManager = new InputManager(); } - public override AnimatedGraphicsComponent graphicsComponent { get; } = null; + public override GraphicsComponent graphicsComponent { get; } = null; #region Server side /*public override void Update() diff --git a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs index ca0ddac..6d1b34a 100644 --- a/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs +++ b/ZoFo/GameCore/GameObjects/Entities/LivingEntities/Player/Player.cs @@ -20,12 +20,12 @@ public class Player : LivingEntity public bool IsTryingToShoot { get; set; } private float speed; private int health; - public override AnimatedGraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate", new Rectangle(0,0,100,100)); + public override GraphicsComponent graphicsComponent { get; } = new AnimatedGraphicsComponent(new List { "player_running_top_rotate" }, "player_running_top_rotate"); public Player(Vector2 position) : base(position) { //InputWeaponRotation = new Vector2(0, 0); //InputPlayerRotation = new Vector2(0, 0); - + graphicsComponent.ObjectDrawRectangle = new Rectangle(0, 0, 100, 100); collisionComponent.stopRectangle = new Rectangle(0, 0, 100, 100); } diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs index 36edc5b..36b34ec 100644 --- a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -202,7 +202,6 @@ namespace ZoFo.GameCore.Graphics public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch) { - DebugHUD.Instance.Log($"{destinationRectangle.Width} : {destinationRectangle.Height}"); Texture2D texture = textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)]; float scale; @@ -226,7 +225,6 @@ namespace ZoFo.GameCore.Graphics destinationRectangle = Scaling(destinationRectangle); _spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White); - DebugHUD.Instance.Log(texture.Name); } public override void Draw(Rectangle destinationRectangle, SpriteBatch _spriteBatch, Rectangle sourceRectangle) diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index de52802..f60d911 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -99,6 +99,9 @@ namespace ZoFo.GameCore AppManager.Instance.server.RegisterGameObject(new EntittyForAnimationTests(new Vector2(40, 40))); AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(140, 140))); + AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(300, 140))); + AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(450, 140))); + AppManager.Instance.server.RegisterGameObject(new Player(new Vector2(140, 140))); } /// From b9c015455dea518eea4346eaf6c43763d6989f3e Mon Sep 17 00:00:00 2001 From: SergoDobro Date: Sun, 18 Aug 2024 13:02:33 +0300 Subject: [PATCH 12/12] Fixed graphics partially --- ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs | 12 +++++++----- ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs | 13 ++----------- ZoFo/GameCore/Graphics/AnimationComponent.cs | 6 ------ ZoFo/GameCore/Server.cs | 5 +++-- 4 files changed, 12 insertions(+), 24 deletions(-) delete mode 100644 ZoFo/GameCore/Graphics/AnimationComponent.cs diff --git a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs index c552101..e55976d 100644 --- a/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs +++ b/ZoFo/GameCore/GameObjects/MapObjects/MapObject.cs @@ -16,7 +16,8 @@ namespace ZoFo.GameCore.GameObjects.MapObjects { public virtual bool IsColliderOn { get; protected set; } = true;//Who added that? public Rectangle sourceRectangle; - public override StaticGraphicsComponent graphicsComponent { get; } = new StaticGraphicsComponent(); + public override GraphicsComponent graphicsComponent { get; } + = new StaticGraphicsComponent(); /// /// Создается простой объект на карте - no animations, только где, насколько крупно рисовать, по какой сорс ректанглу рисовать и из какой текстуры @@ -27,10 +28,11 @@ namespace ZoFo.GameCore.GameObjects.MapObjects /// public MapObject(Vector2 position, Vector2 size, Rectangle sourceRectangle, string textureName) : base(position) { - graphicsComponent._textureName = textureName; - graphicsComponent.BuildComponent(textureName); - graphicsComponent.ObjectDrawRectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); - graphicsComponent.LoadContent(); + (graphicsComponent as StaticGraphicsComponent)._textureName = textureName; + (graphicsComponent as StaticGraphicsComponent).BuildComponent(textureName); + (graphicsComponent as StaticGraphicsComponent).ObjectDrawRectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); + (graphicsComponent as StaticGraphicsComponent).LoadContent(); + this.sourceRectangle = sourceRectangle; } public override void Draw(SpriteBatch spriteBatch) diff --git a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs index 7de06b9..d590462 100644 --- a/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs +++ b/ZoFo/GameCore/Graphics/AnimatedGraphicsComponent.cs @@ -16,8 +16,7 @@ namespace ZoFo.GameCore.Graphics public List animations; private List textures; private List texturesNames; - private AnimationContainer currentAnimation; - static public int scaling = 6; + private AnimationContainer currentAnimation; static public int Camera_XW=800; static public int Camera_YH = 400; static public Vector2 CameraSize = new Vector2(1800, 960); @@ -215,15 +214,7 @@ namespace ZoFo.GameCore.Graphics destinationRectangle = Scaling(destinationRectangle); _spriteBatch.Draw(texture, destinationRectangle, sourceRectangle, Color.White); - } - private Rectangle Scaling(Rectangle destinationRectangle) - { - destinationRectangle.X *= scaling; - destinationRectangle.Y *= scaling; - destinationRectangle.Width *= scaling; - destinationRectangle.Height *= scaling; - return destinationRectangle; - } + } private void buildSourceRectangle() { sourceRectangle = new Rectangle(); diff --git a/ZoFo/GameCore/Graphics/AnimationComponent.cs b/ZoFo/GameCore/Graphics/AnimationComponent.cs deleted file mode 100644 index 084b912..0000000 --- a/ZoFo/GameCore/Graphics/AnimationComponent.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace ZoFo.GameCore.Graphics; - -public class AnimationComponent -{ - -} \ No newline at end of file diff --git a/ZoFo/GameCore/Server.cs b/ZoFo/GameCore/Server.cs index cfa5a6f..686cb96 100644 --- a/ZoFo/GameCore/Server.cs +++ b/ZoFo/GameCore/Server.cs @@ -20,6 +20,7 @@ using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Enemies; using ZoFo.GameCore.GameObjects.Entities.LivingEntities.Player; using ZoFo.GameCore.GameObjects.MapObjects; using ZoFo.GameCore.GameObjects.MapObjects.StopObjects; +using ZoFo.GameCore.Graphics; namespace ZoFo.GameCore { @@ -174,7 +175,7 @@ namespace ZoFo.GameCore 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.mainTextureName + tileSetName = ((gameObject as StopObject).graphicsComponent as StaticGraphicsComponent)._textureName });//TODO foreach (var item in (gameObject as StopObject).collisionComponents) { @@ -190,7 +191,7 @@ namespace ZoFo.GameCore Position = (gameObject as MapObject).position, sourceRectangle = (gameObject as MapObject).sourceRectangle, Size = (gameObject as MapObject).graphicsComponent.ObjectDrawRectangle.Size, - tileSetName = (gameObject as MapObject).graphicsComponent.mainTextureName + tileSetName = ((gameObject as MapObject).graphicsComponent as StaticGraphicsComponent)._textureName });//TODO return; }