一类Modying属性 [英] Modying Properties of a Class

查看:118
本文介绍了一类Modying属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到一个合适的问题标题和标签都有效这个问题让我吃惊相当困难。

Finding a suitable question title and -tags for this question struck me rather hard.

基本上,我已经建立了一个游戏,其中几台/小怪会存在。现在,我有兴趣构建一个系统,所以每个单元都可以应用这些效果/迷/减益将修改单元的属性。这样的效果的实例可以是霜效果,即会减慢单元的移动速度,一个健康升压等等。该计划是让这些影响通过脚本实现的,因此将有可能创建自定义用户的效果。

Basically I've built a game wherein several units/mobs will be present. Now I'm interested in building a system, so each unit can have effects/buffs/debuffs applied to them that will modify the properties of the unit. Examples of such effects could be a frost effect that'd slow the movement speed of the unit, a health boost and so forth. The plan was to have these effects implemented via scripting so it would be possible to create custom user effects.

我在寻找的建议我如何能实现这一点。现在我已经想过创建效果一流。然后将每个应用的效果将是类的一个实例,并且将不得不它被施加到单元的引用。效果将至少有两个方法,一个应用并用于分别applying-和unapplying取消应用方法

I'm looking for advice on how I could implement this. For now I've thought about creating a effect class. Then each applied effect would be an instance of the class, and would have a reference to the unit it was applied to. The effect would have at least two methods, an apply and unapply method for respectively applying- and unapplying.

这里的问题是,它不容许为不同的累计百分比的方法。像,应该值100等于120或121加两次10%。

The problem with this, is that it doesn't allow for different ways of cumulative percentages. Like, should two times 10% increase on a value 100 equal 120 or 121.

不在于它真的很重要,但它在C#

Not that it really matters, but it's being coded in C#

推荐答案

您是在正确的轨道Effect类上。但请各单位对效果的列表,而不是效应是指单位。如果你想获得一个属性值,查询你需要的,而不是使用适用()和不应用()函数来修改的基本值(这可能会导致漂流,由于浮点错误等潜在价值)。

You're on the right track with the Effect class. But keep a list of effects on each unit rather than having effects refer to units. When you want to get an attribute value, query the one you need, rather than using apply() and unapply() functions to modify the base values (which can result in the underlying value drifting due to floating point errors and so on).

您Effect类可以有一个方法修改(),它采用输入值作为浮点值,并返回修改后的值。查询物业最终看起来像这样的伪代码:

Your Effect class could have a method Modify() which takes the input value as a floating point value, and returns the modified value. Query a property ends up looking like this pseudocode:

def get_movement_speed():
    value = base_movement_speed() # this is intrinsic to the unit
    for effect in movement_speed_effects_list:
        value = effect.modify(value)
    return value

在万一你有这么多影响,这成为效率低下,缓存值,并在每次添加或删除效果的时间重新计算。

In the unlikely event that you have so many Effects that this becomes inefficient, cache the values and recalculate them each time you add or remove an effect.

这篇关于一类Modying属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆