我回来了。再次。 :3 现在,我正在开发我的RPG项目(只是为了好玩,我没有任何幻想,这将很容易),我已经到了我编写底层框架的地步,我现在想要写一个从精灵图中绘制精灵的优雅方法。
目前我正在使用神奇宝贝钻石的精灵地图(只是为了测试,因为它很容易获得。我没有制作下一个“宠物小精灵”游戏),其中主要英雄在所有三个方向上行走行,37px x 37px精灵,图像中有12个精灵。
图片在这里:http://spriters-resource.com/ds/pkmndiamondpearl/lucas.png (我目前正在使用“行走”子集。)
我创建了SpriteSheet类(以及SpriteSheetData类,它是spritesheets集合的XML文件的表示)和SpriteManager类,所有这些都列在下面:
SpriteSheet.cs
namespace RPG.Utils.Graphics
{
/// <summary>
/// Represents a Sprite Sheet. A Sprite Sheet is a graphic that contains a number of frames for animations.
/// These are laid out a set distance apart.
/// </summary>
public struct SpriteSheet
{
/// <summary>
/// The name for the texture. Used internally to reference the texture.
/// </summary>
[ContentSerializer]
public string TextureName
{
get;
private set;
}
/// <summary>
/// The file name of the texture.
/// </summary>
[ContentSerializer]
public string TextureFile
{
get;
private set;
}
/// <summary>
/// The width of each sprite in the sprite sheet.
/// </summary>
[ContentSerializer]
public int SpriteWidth
{
get;
private set;
}
/// <summary>
/// The height of each sprite in the sprite sheet.
/// </summary>
[ContentSerializer]
public int SpriteHeight
{
get;
private set;
}
/// <summary>
/// The interval between each frame of animation.
/// This should be (by default) 100f or 100ms.
/// </summary>
[ContentSerializer]
public float AnimationInterval
{
get;
set;
}
/// <summary>
/// The number of frames per each individual animation.
/// </summary>
[ContentSerializer]
public int AnimationLength
{
get;
set;
}
/// <summary>
/// The texture for this sprite sheet.
/// </summary>
[ContentSerializerIgnore]
public Texture2D Texture
{
get;
set;
}
}
SpriteManager.cs
/// <summary>
/// A sprite manager. Just loads sprites from a file and then stores them.
/// </summary>
public static class SpriteManager
{
private static Dictionary<string, SpriteSheetData> m_spriteSheets;
public static Dictionary<string, SpriteSheetData> SpriteSheets
{
get
{
if (m_spriteSheets == null)
m_spriteSheets = new Dictionary<string, SpriteSheetData>();
return m_spriteSheets;
}
}
/// <summary>
/// Loads all the sprites from the given directory using the content manager.
/// Sprites are loaded by iterating SpriteSheetData (.xml) files inside the /Sprites/ directory.
/// </summary>
/// <param name="mgr">Content Manager.</param>
/// <param name="subdir">Directory to load.</param>
public static void LoadAllSprites(ContentManager mgr, string subdir)
{
// Get the files in the subdirectory.
IEnumerable<string> files = Directory.EnumerateFiles(mgr.RootDirectory+"/"+subdir);
foreach (string f in files)
{
// Microsoft, why do you insist on not letting us load stuff with file extensions?
string fname = f.Replace("Content/", "").Replace(".xnb", "");
SpriteSheetData data = mgr.Load<SpriteSheetData>(fname);
string spriteSheetDir = subdir +"/" + data.SpriteSheetName + "/";
int loaded = 0;
for (int i = 0; i < data.SpriteSheets.Length; i++)
{
loaded++;
SpriteSheet current = data.SpriteSheets[i];
current.Texture = mgr.Load<Texture2D>(spriteSheetDir + current.TextureFile);
data.SpriteSheetMap[current.TextureName] = current;
}
Console.WriteLine("Loaded SpriteSheetData file \"{0}\".xml ({1} sprite sheet(s) loaded).", data.SpriteSheetName, loaded);
SpriteSheets[data.SpriteSheetName] = data;
}
}
/// <summary>
/// Query if a given Sprite definition file is loaded.
/// </summary>
/// <param name="spriteName">
/// The sprite definition file name (ie, "Hero"). This should correspond with the XML file
/// that contains the definition for the sprite sheets. It should NOT be the name OF a spritesheet.
/// </param>
/// <returns>True if the sprite definition file is loaded.</returns>
public static bool IsLoaded(string spriteName)
{
return SpriteSheets.ContainsKey(spriteName);
}
}
SpriteSheetData.cs
/// <summary>
/// Represents data for a SpriteSheet. These are stored in XML files.
/// </summary>
public struct SpriteSheetData
{
/// <summary>
/// The collective name for the sprite sheets.
/// </summary>
[ContentSerializer]
public string SpriteSheetName
{
get;
set;
}
/// <summary>
/// The SpriteSheets in this data file.
/// </summary>
[ContentSerializer]
internal SpriteSheet[] SpriteSheets
{
get;
set;
}
[ContentSerializerIgnore]
private Dictionary<string, SpriteSheet> m_map;
/// <summary>
/// The sprite sheet map.
/// </summary>
[ContentSerializerIgnore]
public Dictionary<string, SpriteSheet> SpriteSheetMap
{
get
{
if (m_map == null)
m_map = new Dictionary<string, SpriteSheet>();
return m_map;
}
}
}
我用来“读入”sprite的文件是: 精灵/ Hero.xml
<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<Asset Type="RPG.Utils.Graphics.SpriteSheetData">
<SpriteSheetName>Hero</SpriteSheetName>
<SpriteSheets>
<Item Type="RPG.Utils.Graphics.SpriteSheet">
<TextureName>HeroWalking</TextureName>
<TextureFile>hero_walk</TextureFile>
<SpriteWidth>37</SpriteWidth>
<SpriteHeight>37</SpriteHeight>
<AnimationInterval>400</AnimationInterval>
<AnimationLength>3</AnimationLength>
</Item>
</SpriteSheets>
</Asset>
</XnaContent>
我遇到的问题是我不确定如何优雅地组织1)精灵加载和2)精灵表的配对(这是游戏的“通用”部分,我打算在重新使用,所以我不知道我可能创建的实体的许多实例)实体/游戏对象 - 特别是玩家(因为那是我现在尝试做的)。我不确定如何继续,所以任何帮助都非常感激。
如果您需要更多代码(上帝保佑),请询问并给予您:3
答案 0 :(得分:0)
明智地组织起来,我一直喜欢以一种特定的方式去做,而你所使用的结构非常适合它。
我喜欢加载我的对象:
然后,在很大程度上取决于您如何致电您的关卡经理并实施它。我喜欢使用这些级别的配置文件以及级别管理器,因为这样您就可以在整个级别管理器中共享资源。因此,您可以在级别管理器中使用引用的纹理和字典,并将这些纹理传递到您加载的级别中的游戏对象,这样就不会有多个加载的纹理。
据我所知,在C#中,“通过引用传递的东西”往往会阻止这个问题,但我来自C ++领域,这种级别的内容管理是一个好主意。< / p>
对于您的播放器,您可以创建一个与您的关卡经理交互的播放器管理器类,以便播放器可以在级别和加载之间保持持久性。
这就是我组织它的方式。
干杯!