GraphicsAndAnimationFileCreator

This commit is contained in:
Timofey06 2023-08-14 15:04:30 +03:00
parent 4188745eed
commit 603ba0c899
12 changed files with 243 additions and 23 deletions

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DangerousD\DangerousD.csproj" />
</ItemGroup>
</Project>

View file

@ -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<Tuple<int, int>>();
container.FrameTime.Add(new Tuple<int, int>(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();
}
}
}

View file

@ -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

View file

@ -21,6 +21,7 @@
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MonogameLibrary\MonogameLibrary.csproj" />

View file

@ -1,4 +1,5 @@
using System;
using DangerousD.GameCore.Graphics;
using System;
using System.Collections.Generic;
using System.Text;

View file

@ -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<AnimationContainer> animations;
void LoadAnimations(string nameOfMainFile)
{
animations = new List<AnimationContainer>();
List<string> animationFilesNames = new List<string>();
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<AnimationContainer>(json);
animations.Add(animation);
}
}
}
}

View file

@ -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<Tuple<int, int>> FrameTime { get; set; }
[JsonProperty("textureFrameInterval")]
public int TextureFrameInterval { get; set; }
[JsonProperty("framesCount")]
public int FramesCount { get; set; }
}
}

View file

@ -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<AnimationContainer> animations;
private List<Texture2D> textures;
private List<string> 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<string> animationsId, SpriteBatch _spriteBatch)
{
this._spriteBatch = _spriteBatch;
currentFrame = 0;
lastInterval = 1;
lastAnimationId = null;
LoadAnimations(animationsId);
LoadTextures();
}
private void LoadAnimations(List<string> animationsId)
{
animations = new List<AnimationContainer>();
foreach (var id in animationsId)
{
animations.Add( GameManager.builder.animations.Find(x => x.Id == id));
}
}
private void LoadTextures()
{
textures = new List<Texture2D>();
texturesNames = new List<string>();
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<int, int> i = currentAnimation.FrameTime.Find(x => x.Item1 == currentFrame);
if (i != null)
{
interval = i.Item2;
lastInterval = interval;
}
else
{
interval = lastInterval;
}
}
}
}

View file

@ -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
) { }
}
}

View file

@ -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();

View file

@ -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<LivingEntity> livingEntities;
static List<MapObject> MapObjects;
public static AnimationBuilder builder;
internal static void Register(GameObject gameObject)
{
if (gameObject is LivingEntity)

View file

@ -10,6 +10,7 @@ namespace DangerousD
{
using (var game = new AppManager())
game.Run();
}
}
}