From 603ba0c899f4ba3f4306985c9aab5ac5a4e5f0b5 Mon Sep 17 00:00:00 2001 From: Timofey06 Date: Mon, 14 Aug 2023 15:04:30 +0300 Subject: [PATCH] GraphicsAndAnimationFileCreator --- .../AnimationsFileCreator.csproj | 13 +++ AnimationsFileCreator/Program.cs | 55 +++++++++ DangerousD.sln | 12 +- DangerousD/DangerousD.csproj | 3 +- DangerousD/GameCore/GameObjects/GameObject.cs | 3 +- .../GameCore/Graphics/AnimationBuilder.cs | 31 ++++++ .../GameCore/Graphics/AnimationContainer.cs | 26 +++++ .../GameCore/Graphics/GraphicsComponent.cs | 105 ++++++++++++++++++ DangerousD/GameCore/GraphicsComponent.cs | 14 --- DangerousD/GameCore/Managers/AppManager.cs | 1 + DangerousD/GameCore/Managers/GameManager.cs | 2 + DangerousD/Program.cs | 1 + 12 files changed, 243 insertions(+), 23 deletions(-) create mode 100644 AnimationsFileCreator/AnimationsFileCreator.csproj create mode 100644 AnimationsFileCreator/Program.cs create mode 100644 DangerousD/GameCore/Graphics/AnimationBuilder.cs create mode 100644 DangerousD/GameCore/Graphics/AnimationContainer.cs create mode 100644 DangerousD/GameCore/Graphics/GraphicsComponent.cs delete mode 100644 DangerousD/GameCore/GraphicsComponent.cs diff --git a/AnimationsFileCreator/AnimationsFileCreator.csproj b/AnimationsFileCreator/AnimationsFileCreator.csproj new file mode 100644 index 0000000..9e982a1 --- /dev/null +++ b/AnimationsFileCreator/AnimationsFileCreator.csproj @@ -0,0 +1,13 @@ + + + + Exe + net6.0-windows + true + + + + + + + diff --git a/AnimationsFileCreator/Program.cs b/AnimationsFileCreator/Program.cs new file mode 100644 index 0000000..80062e2 --- /dev/null +++ b/AnimationsFileCreator/Program.cs @@ -0,0 +1,55 @@ +using DangerousD.GameCore.Graphics; +using Microsoft.Xna.Framework; +using Newtonsoft.Json; +using System; +using System.Windows.Forms; +using System.IO; + +namespace AnimationsFileCreator +{ + class Program + { + [STAThread] + static void Main(string[] args) + { + Console.WriteLine("Добро пожаловать в костыльную программу по созданию файлов анимации для игры DungerousD"); + Console.Write("Введите название текстуры (нажмите enter, чтобы выбрать файл во всплывающем окошке): "); + string textureName = Console.ReadLine(); + if (textureName == "") + { + OpenFileDialog dialog = new OpenFileDialog(); + dialog.ShowDialog(); + textureName = dialog.FileName; + } + Console.WriteLine("Введите количество кадров анимации: "); + int framesCount = int.Parse(Console.ReadLine()); + Console.WriteLine("Введите длительность кадра в анимации: "); + int interval = int.Parse(Console.ReadLine()); + Console.WriteLine("Введите начальную позицию X ректенгла анимации: "); + Rectangle rectangle = new Rectangle(); + rectangle.X = int.Parse(Console.ReadLine()); + Console.WriteLine("Введите начальную позицию Y ректенгла анимации: "); + rectangle.Y = int.Parse(Console.ReadLine()); + Console.WriteLine("Введите начальную позицию Width ректенгла анимации: "); + rectangle.Width = int.Parse(Console.ReadLine()); + Console.WriteLine("Введите начальную позицию Height ректенгла анимации: "); + rectangle.Height = int.Parse(Console.ReadLine()); + Console.WriteLine("Введите название для этого файла - id анимации"); + string id = Console.ReadLine(); + AnimationContainer container = new AnimationContainer(); + container.FramesCount = framesCount; + container.FrameTime = new System.Collections.Generic.List>(); + container.FrameTime.Add(new Tuple(0, interval)); + container.StartSpriteRectangle = rectangle; + container.TextureName = textureName; + container.TextureFrameInterval = 1; + container.Id = id; + string json = JsonConvert.SerializeObject(container); + StreamWriter writer = new StreamWriter(id); + writer.WriteLine(json); + writer.Close(); + + + } + } +} diff --git a/DangerousD.sln b/DangerousD.sln index 69b2c7f..a0ee3ad 100644 --- a/DangerousD.sln +++ b/DangerousD.sln @@ -1,9 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30907.101 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32611.2 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DangerousD", "DangerousD\DangerousD.csproj", "{1FC12F81-0E55-4142-83BD-23496EF29DC6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DangerousD", "DangerousD\DangerousD.csproj", "{1FC12F81-0E55-4142-83BD-23496EF29DC6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AnimationsFileCreator", "AnimationsFileCreator\AnimationsFileCreator.csproj", "{CE9485FC-F4C6-4E0D-8B8E-1843AA20CEDE}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonogameLibrary", "MonogameLibrary\MonogameLibrary.csproj", "{52ED99EF-9769-4587-8BD8-54D6B98E3E7E}" EndProject @@ -17,10 +19,6 @@ Global {1FC12F81-0E55-4142-83BD-23496EF29DC6}.Debug|Any CPU.Build.0 = Debug|Any CPU {1FC12F81-0E55-4142-83BD-23496EF29DC6}.Release|Any CPU.ActiveCfg = Release|Any CPU {1FC12F81-0E55-4142-83BD-23496EF29DC6}.Release|Any CPU.Build.0 = Release|Any CPU - {52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {52ED99EF-9769-4587-8BD8-54D6B98E3E7E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/DangerousD/DangerousD.csproj b/DangerousD/DangerousD.csproj index 5c34329..99e6800 100644 --- a/DangerousD/DangerousD.csproj +++ b/DangerousD/DangerousD.csproj @@ -21,6 +21,7 @@ + @@ -29,4 +30,4 @@ - \ No newline at end of file + diff --git a/DangerousD/GameCore/GameObjects/GameObject.cs b/DangerousD/GameCore/GameObjects/GameObject.cs index 3d73a45..183e0d2 100644 --- a/DangerousD/GameCore/GameObjects/GameObject.cs +++ b/DangerousD/GameCore/GameObjects/GameObject.cs @@ -1,4 +1,5 @@ -using System; +using DangerousD.GameCore.Graphics; +using System; using System.Collections.Generic; using System.Text; diff --git a/DangerousD/GameCore/Graphics/AnimationBuilder.cs b/DangerousD/GameCore/Graphics/AnimationBuilder.cs new file mode 100644 index 0000000..9b68161 --- /dev/null +++ b/DangerousD/GameCore/Graphics/AnimationBuilder.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.IO; +using Newtonsoft.Json; + +namespace DangerousD.GameCore.Graphics +{ + public class AnimationBuilder + { + public List animations; + void LoadAnimations(string nameOfMainFile) + { + animations = new List(); + List animationFilesNames = new List(); + StreamReader reader = new StreamReader(nameOfMainFile); + while (reader.Peek() != -1) + { + animationFilesNames.Add(reader.ReadLine()); + } + reader.Close(); + foreach (var fileName in animationFilesNames) + { + reader = new StreamReader(fileName); + string json = reader.ReadToEnd(); + AnimationContainer animation = JsonConvert.DeserializeObject(json); + animations.Add(animation); + } + } + } +} diff --git a/DangerousD/GameCore/Graphics/AnimationContainer.cs b/DangerousD/GameCore/Graphics/AnimationContainer.cs new file mode 100644 index 0000000..6aa981a --- /dev/null +++ b/DangerousD/GameCore/Graphics/AnimationContainer.cs @@ -0,0 +1,26 @@ +using Microsoft.Xna.Framework; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore.Graphics +{ + [Serializable] + public class AnimationContainer + { + [JsonProperty("id")] + public string Id { get; set; } + [JsonProperty("textureName")] + public string TextureName { get; set; } + [JsonProperty("startSpriteRectangle")] + public Rectangle StartSpriteRectangle { get; set; } + [JsonProperty("frameSecond")] + public List> FrameTime { get; set; } + [JsonProperty("textureFrameInterval")] + public int TextureFrameInterval { get; set; } + [JsonProperty("framesCount")] + public int FramesCount { get; set; } + + } +} diff --git a/DangerousD/GameCore/Graphics/GraphicsComponent.cs b/DangerousD/GameCore/Graphics/GraphicsComponent.cs new file mode 100644 index 0000000..7419a3f --- /dev/null +++ b/DangerousD/GameCore/Graphics/GraphicsComponent.cs @@ -0,0 +1,105 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using System; +using System.Collections.Generic; +using System.Text; + +namespace DangerousD.GameCore.Graphics +{ + class GraphicsComponent + { + private List animations; + private List textures; + private List texturesNames; + private AnimationContainer currentAnimation; + private SpriteBatch _spriteBatch; + private string lastAnimationId; + private int currentFrame; + private int interval; + private int lastInterval; + private Rectangle sourceRectangle; + public GraphicsComponent(List animationsId, SpriteBatch _spriteBatch) + { + this._spriteBatch = _spriteBatch; + currentFrame = 0; + lastInterval = 1; + lastAnimationId = null; + LoadAnimations(animationsId); + LoadTextures(); + + } + private void LoadAnimations(List animationsId) + { + animations = new List(); + foreach (var id in animationsId) + { + animations.Add( GameManager.builder.animations.Find(x => x.Id == id)); + } + } + private void LoadTextures() + { + textures = new List(); + texturesNames = new List(); + + foreach (var animation in animations) + { + if (!texturesNames.Contains(animation.TextureName)) + { + texturesNames.Add(animation.TextureName); + textures.Add(TextureManager.GetTexture(animation.TextureName)); + + } + } + } + public void DrawAnimation(Rectangle destinationRectangle, string animationId) + { + + if (animationId != lastAnimationId) + { + currentFrame = 0; + currentAnimation = animations.Find(x => x.Id == animationId); + buildSourceRectangle(); + SetInterval(); + } + + if (interval == 0) + { + currentFrame++; + if (currentAnimation.FramesCount - 1 <= currentFrame) + { + currentFrame = 0; + } + buildSourceRectangle(); + SetInterval(); + } + + interval--; + + _spriteBatch.Draw(textures[texturesNames.FindIndex(x => x == currentAnimation.TextureName)], destinationRectangle, sourceRectangle, Color.White); + } + private void buildSourceRectangle() + { + sourceRectangle = new Rectangle(); + 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; + } + } + } + +} diff --git a/DangerousD/GameCore/GraphicsComponent.cs b/DangerousD/GameCore/GraphicsComponent.cs deleted file mode 100644 index 8fffc08..0000000 --- a/DangerousD/GameCore/GraphicsComponent.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.Xna.Framework.Content; -using Microsoft.Xna.Framework.Graphics; -using System; -using System.Collections.Generic; -using System.Text; - -namespace DangerousD.GameCore -{ - class GraphicsComponent - { - public void Draw(SpriteBatch s - ) { } - } -} diff --git a/DangerousD/GameCore/Managers/AppManager.cs b/DangerousD/GameCore/Managers/AppManager.cs index d0c3284..827ec43 100644 --- a/DangerousD/GameCore/Managers/AppManager.cs +++ b/DangerousD/GameCore/Managers/AppManager.cs @@ -24,6 +24,7 @@ namespace DangerousD.GameCore _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; + TargetElapsedTime = TimeSpan.FromMilliseconds(1000 / 30); gameState = GameState.Menu; MenuGUI = new MenuGUI(); diff --git a/DangerousD/GameCore/Managers/GameManager.cs b/DangerousD/GameCore/Managers/GameManager.cs index dce6424..f5ed14e 100644 --- a/DangerousD/GameCore/Managers/GameManager.cs +++ b/DangerousD/GameCore/Managers/GameManager.cs @@ -1,4 +1,5 @@ using DangerousD.GameCore.GameObjects; +using DangerousD.GameCore.Graphics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; @@ -11,6 +12,7 @@ namespace DangerousD.GameCore { static List livingEntities; static List MapObjects; + public static AnimationBuilder builder; internal static void Register(GameObject gameObject) { if (gameObject is LivingEntity) diff --git a/DangerousD/Program.cs b/DangerousD/Program.cs index b6f0be5..0808f03 100644 --- a/DangerousD/Program.cs +++ b/DangerousD/Program.cs @@ -10,6 +10,7 @@ namespace DangerousD { using (var game = new AppManager()) game.Run(); + } } }