选择正确的施法机制设计模式

时间:2022-01-16 11:59:41

I'm new in OOD and I don't really know how to handle the spell-casting mechanism which i want to implement in my game.

我是OOD的新手,我真的不知道如何处理我想在游戏中实现的拼写机制。

  • I want to produce following classesTeam, Actor, Spell and interface SpellEffect
  • 我想生成以下类,团队,演员,法术和界面SpellEffect

  • Actor can cast spells on other actors however the spell can have different behaviour while being casted on allies and enemies ( ally is an actor from the same team )
  • 演员可以对其他演员施放法术,但是法术可以在盟友和敌人身上施放不同的行为(盟友是来自同一团队的演员)

I tried this way:

我试过这种方式:

  • I implement SpellEffect in Damaging, Stunning, Healing classes which overrides executeOn(Actor a)
  • 我在Damaging,Stunning,Healing类中实现了SpellEffect,它覆盖了executeOn(Actor a)

  • In Spell class I have two lists of SpellEffect - one for allies and second for enemies so in the Spell class I need to know is the attacked actor is and ally or enemy of the caster
  • 在法术课中我有两个SpellEffect列表 - 一个用于盟友,第二个用于敌人,因此在法术课中我需要知道的是被攻击的演员是施法者的盟友或敌人

  • I was thinking about StrategyPattern for spell so I can do something like this:

    我正在考虑使用StrategyPattern进行拼写,所以我可以这样做:

    actor.setCastingStrategy( new TargetingAllyStrategy() )
    
  • Strategies would be then something like:

    策略将是这样的:

    execute a spell on the targeted actor using Spells's allyCast() method
    

However I'm not sure about passing Actor object to the lower level object. Moreover what if not every actor can be stunned? Should I make any inherited class UnstunnableActor ? Or maybe use instanceOf to check implemented interfaces. I'm pretty new in the topic so any help would be great.

但是我不确定将Actor对象传递给较低级别​​的对象。此外,如果不是每个演员都可以被击晕?我应该创建任何继承的类UnstunnableActor吗?或者也许使用instanceOf来检查已实现的接口。我在这个主题上很新,所以任何帮助都会很棒。

1 个解决方案

#1


Since you will never have the third class of actors (only allies, enemies) I don't think strategy pattern is needed here.

既然你永远不会有第三类演员(只有盟友,敌人),我不认为这里需要策略模式。

I think you can make your actors implement either AllyActorInterface or EnemyActorInterface. And Spell will just test what interface actor supports and apply chosen effects by calling allyCast or enemyCast.

我想你可以让你的actor实现AllyActorInterface或EnemyActorInterface。并且Spell将测试接口actor支持哪些并通过调用allyCast或enemyCast来应用所选择的效果。

You can also think of splitting Spell class into EnemySpell and AllySpell if your logic of applying effects differ too much. You can implement a template method to select which class of spells to apply to which actor then.

如果您应用效果的逻辑差异太大,您还可以考虑将Spell类拆分为EnemySpell和AllySpell。您可以实现一个模板方法来选择要应用于哪个类的法术类。

#1


Since you will never have the third class of actors (only allies, enemies) I don't think strategy pattern is needed here.

既然你永远不会有第三类演员(只有盟友,敌人),我不认为这里需要策略模式。

I think you can make your actors implement either AllyActorInterface or EnemyActorInterface. And Spell will just test what interface actor supports and apply chosen effects by calling allyCast or enemyCast.

我想你可以让你的actor实现AllyActorInterface或EnemyActorInterface。并且Spell将测试接口actor支持哪些并通过调用allyCast或enemyCast来应用所选择的效果。

You can also think of splitting Spell class into EnemySpell and AllySpell if your logic of applying effects differ too much. You can implement a template method to select which class of spells to apply to which actor then.

如果您应用效果的逻辑差异太大,您还可以考虑将Spell类拆分为EnemySpell和AllySpell。您可以实现一个模板方法来选择要应用于哪个类的法术类。