开发者

where should damage logic go Game Engine or Character Class

I am making a game and I am trying to decide what is the best practice for exchanging damage between two objects on the screen. Should the damage be passe开发者_StackOverflow中文版d directly between the two objects or should it be pass through a central game engine that decides the damage and different criteria's such as hit or miss or amount dealt.

So overall what is the best practice.


It depends in large part on the rules of the game. However, it's not unusual for multiple objects to need to get involved. However, I would advise against putting game-rules code into the MovieClip instance that displays the object; keeping them as peers allows game state to be separated and saved/relayed more easily.

Here's an example:

  • Actor A initiates an attack.
    • Actor A provides an accuracy value (hit roll).
    • The weapon (or ammunition, if applicable) provides a base damage value (damage roll) and damage type.
    • The type of Weapon determines how Actor A's abilities modify the hit roll and damage roll.
    • Modifiers on the weapon may modify the rolls, add additional damage components, or associate "on-striking" effects with the attack.
  • At this point it seems there's an attack object-class forming, containing the following:
    • Accuracy value
    • A collection of typed damages (maybe empty for effect-only attacks)
    • A collection of additional effects (maybe empty)
  • Now the attack reaches the target.
    • Special effects on the target that trigger when attacked may go off.
    • The target may have many layers of defenses -- cover, concealment, evasion, deflection, blocking, armor, and natural armor, for example. Any or all of these may contribute to the attack failing.
  • The attack connects.
    • Special effects on the target that trigger when struck may go off.
    • The target may have damage reduction or elimination capability as well, such as armor heavy enough to absorb part of an attack, or immunity/resistance/weakness to one or more specific attack types.
  • Actual damage can now be resolved. This may be zero if the attack was accurate but ineffective.
    • Special effects associated with the attack resolve.
      • Some may be contingent on damage, such as injury-delivered poison.
      • Some may be unconditional, such as a disabling burst.
    • Special effects on the target that trigger when damaged, such as reactive armor or "thorns" damage, may go off.


Have the engine handle all code that's redundant among damage calculations for each character. The characters will be forced to do the rest.

I don't know much about what kind of game you're making. But for some games, when the engine is told that A is attacking B, the code might look something like:

//somewhere in your engine:
damage = A.get_damage(B);
trace (damage); // {damage:60, hit_chance = 0.8}
B.send_damage(damage);

I believe it to be necessary in most cases to have B.send_damage() rather than the engine directly controlling the health attribute of B. AI's tend to be state-driven. There will likely be some variables that are exclusive to A or B that affect damage (such as invincibility), which (IMO) it's best that the engine have no idea of their existence (or you'll end up with huge switch blocks ;D)

You see how I had to send B as an argument in A.get_damage()? Unfortunately, that's often necessary in an an approach like this. A's damage/hit_chance could be influenced by how far away B is from A.


Put it in the game engine. The objects on the screen represent the views and controllers into your model, the engine (which is where such logic belongs). Though it may be tempting to put it directly in the view/controller side, your game's code will eventually turn to spaghetti once it gets big enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜