c#-一种游戏设计解决方案,用于映射具有许多可以应用的变异的大型属性数据集.
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-一种游戏设计解决方案,用于映射具有许多可以应用的变异的大型属性数据集.,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6661字,纯文字阅读大概需要10分钟。
内容图文
![c#-一种游戏设计解决方案,用于映射具有许多可以应用的变异的大型属性数据集.](/upload/InfoBanner/zyjiaocheng/675/d6c79d48bc664131869ea5564866b189.jpg)
我在设计声音设计方面遇到了一些麻烦,因为对我来说这是一个相当棘手的模块.实际的业务逻辑使我陷入混乱,所以我将其重新表述为更易于理解的形式.我用C#编写代码,尽管我猜实际的设计与语言无关.
假设您正在编写一个简单的幻想角色扮演游戏.您有一个具有一些基本属性的播放器.玩家可以装备一些物品,这可能会在某种程度上影响他的属性.项目具有一个或多个属性,这些属性定义要修改的属性.
public Character
{
public string charName;
public int charStrength;
public int charHitPoints;
...
public List<Item> equippedItems;
}
public Item
{
public string itemName;
public List<ItemProperty> itemProperties;
}
public ItemProperty
{
public int propertyValue;
public virtual void applyPropertyModification(Character charStatsToBeModified){}
}
字符数据为XML格式:
<Character>
<Name>John Doe</Name>
<BaseStrength>10</BaseStrength>
<BaseHitPoints>100</BaseHitPoints>
<Items>
<Item>
<Name>Short Sword</Name>
<Properties>
<Strength>10</Strength>
<HitPoints>200</HitPoints>
</Properties>
</Item>
</Items>
<Character>
我正在寻找一种从XML加载字符数据的方法,该方法使我可以轻松添加X通用新字符属性(大约50左右)和/或Y通用项属性(大约100-150左右).
我的设计思路:
每个可能的character属性都作为新属性添加到Character类.随着属性数量的增加,可能变得笨拙.使用适当的applyPropertyModification方法,将每个可能的Item属性添加为自己的ItemProperty子类.
public Strength : ItemProperty
{
public override void applyPropertyModification(Character charStatsToBeModified)
{
charStatsToBeModified.charStrength += this.propertyValue
}
}
这似乎确实是大量的多余代码,尤其是因为所有被重写的方法都在确定应修改哪个字符属性.
ItemProperty将使用反射创建,元素XML标签与相应的ItemProperty子类相对应(我认为替代方法是100条件开关吗?).
我的总体设计是否合理,还是我朝着错误的方向前进?我没有足够的知识来独自解决这个问题,但是我坚信有一种更简洁的方法可以做到这一点.另外,如果这个问题根本不清楚或难以理解,请告诉我,我将尝试澄清/重新措辞.谢谢.
解决方法:
好的,这里有些人走在正确的道路上……但是没有人碰到头上的钉子,所以一切顺利.看来您是从正确的道路开始的,但需要进一步走下去.我在代码中写了注释,以便它为我做一些解释.
开始:
//So you have a character...
public class Character
{
//You know you ALWAYS need Raw Attribs in a game
private Dictionary<string, Attrib> _rawAttributes;
//You know you will always need a record of modified attribs
private Dictionary<string, Attrib> _modifiedAttributes;
//And while your at it, take a light optimization to not have to recalculate everytime
private bool _areModifiedAttribsCurrent { get; set; }
//A character has gear! This is what makes the world go around
public List<Equipment> Equipment { get; private set; }
//You don't want to give public access to setting gear, this should be controlled.
//You'll want to do more as far as remove / change... but this'll get you started
public void AddEquipment(Equipment e)
{
Equipment.Add(e);
_areModifiedAttribsCurrent = false;
}
//And a way to add attribs and set base values..
//once again, you will want more but this will get you started
public void AddAttribute(Attrib x)
{
_rawAttributes.Add(x.Name, x);
}
//Finally you want a way to fetch the modified attribs
//Keep in mind you need to do the copy dance in the apply to not upset your
//base stats.
public Dictionary<string, Attrib> FetchModifiedAttributes()
{
if (!_areModifiedAttribsCurrent)
{
var traceAttribs = _rawAttributes;
foreach (var e in Equipment.OrderBy(x => x.ApplyOrder))
{
traceAttribs = e.ApplyModifiers(traceAttribs);
}
_modifiedAttributes = traceAttribs;
}
return _modifiedAttributes;
}
}
//Attrib, pretty simple.. Could go away but we all know attribs have effects so don't even start down that path
//You WILL need this class later on... but right now it looks pretty meaningless.
public class Attrib
{
public string Name { get; set; }
public decimal Value { get; set; }
}
//GEAR... yes, this is what all RPG lovers love...
//Grind for that awesome gear!
public class Equipment
{
//Ok so I put in some stuff unrelated to your problem but you need a name right?!
public string Name { get; set; }
//What order does gear effect stats... this is important if you do more than flat modifiers.
public int ApplyOrder { get; set; }
//What modifiers does this gear have?!
public List<Modifier> ItemModifiers { get; set; }
//Aha.... let the gear apply its modifiers to an attrib dictionary... I knew I loved OO for some reason
public Dictionary<string, Attrib> ApplyModifiers(Dictionary<string, Attrib> inParams)
{
//Copy / Clone... Whatever you want to call it this is important as to not
//unintentionally screw up yoru base collection.
var response = new Dictionary<string, Attrib>();
foreach (var m in ItemModifiers)
{
//If we have this attrib, keep going
if (inParams.ContainsKey(m.TargetName))
{
//If this is the first time the response ran into it, add it
if (!response.ContainsKey(m.TargetName))
{
response.Add(m.TargetName, inParams[m.TargetName]);
}
//And wait what's this... let the Modifier apply it!?
//yes... pass it down again... you'll see why in a second.
m.Apply(response[m.TargetName]);
}
}
return response;
}
}
//A modifier... what the!?
//Yep... abstraction is key to maintainable and expandable code
public class Modifier
{
public string TargetName { get; set; }
public decimal ModifierValue { get; set; }
//The other stuff is kind of pointless... but this is where the magic happens... All in a modifier type.
public ModifierType ModifierType { get; set; }
//Let the modifier apply it's own values... off the type... yea
//I did that on purpose ;-)
public void Apply(Attrib a)
{
a.Value = ModifierType.ApplyModifier(this, a.Value);
}
}
//Decoration... Wonderful
//This base class gives you a interface to work with... Hell, it could be an interface but I decided
//to type abstract today.
public abstract class ModifierType
{
public abstract string ModifierName { get; }
public abstract decimal ApplyModifier(Modifier m, decimal InitialValue);
}
//A concrete type of ModifierType... This is what determines how the modifier value is applied.
//This gives you more flexibility than hard coding modifier types. If you really wanted to you could
//serialize these and store lambda expressions in the DB so you not only have type driven logic, you could have
//data driven behavior.
public class FlatModifier : ModifierType
{
//The names can be really handy if you want to expose calculations to players.
public override string ModifierName { get { return "Flat Effect"; } }
//And finally... let the calculation happen! Time to bubble back up!
public override decimal ApplyModifier(Modifier m, decimal InitialValue)
{
return InitialValue + m.ModifierValue;
}
}
让您的对象为您完成工作.我知道它来自XML,但是让它们反序列化到您的对象中并调用一些选择方法来处理它.这样就避免了一些麻烦的问题,例如C#是参考文献,也是所有烦人的东西.此外,它还避免了多余的处理过程,从而避免了阻塞对象图.
让我知道您是否有任何疑问…今晚我很无聊.
内容总结
以上是互联网集市为您收集整理的c#-一种游戏设计解决方案,用于映射具有许多可以应用的变异的大型属性数据集.全部内容,希望文章能够帮你解决c#-一种游戏设计解决方案,用于映射具有许多可以应用的变异的大型属性数据集.所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。