2016-07-25 72 views
1

所以今天我正在学习和实现Command Patterns来处理对象的输入和移动。命令模式是否足够有效,它的具体好处是什么?

所以我的问题是:

  1. 我收到的指令模式的执行权或是否需要修改?如果是这样,有人可以给我一个小例子来改善它。
  2. 我知道它改善了代码的可重用性。但是当我使用一个简单的MovementScript.cs到我的游戏对象组件时,它会产生什么样的差异?难道它不仅仅是相同的,而是花更少的时间来编写而不是制作一个完整的命令模式?

我附加到gameobject的只有InputHandler。这里是我的代码,其中包括移动对象:

这是我的输入处理程序或就我所知道的客户

public class InputHandler : MonoBehaviour 
{ 
GameObject theObject; 
public Command buttonA, buttonD; 
public float acceleration, maxSpeed; 

Movement moves; 

void Awake() 
{ 
    theObject = gameObject; 
    moves = new Movement(theObject, acceleration, maxSpeed); 
} 

void Start() 
{ 
    buttonA = new MoveLeft(moves); 
    buttonD = new MoveRight(moves); 
} 

void Update() 
{ 
    HandleInput(); 
} 

public void HandleInput() 
{ 
    if (Input.GetKey(KeyCode.A)) 
    { 
     buttonA.Execute(); 
    } 
    else if (Input.GetKey(KeyCode.D)) 
    { 
     buttonD.Execute(); 
    } 
} 
} 

命令抽象类

public abstract class Command 
{ 

//The Receiver of the command.. 
protected IReceiver receiver = null; 

public Command(IReceiver receiver) 
{ 
    this.receiver = receiver; 
} 

public abstract void Execute(); 
} 

接收器类(我实现逻辑,这是运动)

public class Movement : IReceiver 
{ 
public ACTION_LIST currentMoves; 
private GameObject theObject; 
private float acceleration; 
private float maxspeed; 

public Movement(GameObject theObject, float acceleration, float maxspeed) 
{ 
    this.theObject = theObject; 
    this.acceleration = acceleration; 
    this.maxspeed = maxspeed; 
} 

public void Action(ACTION_LIST moves) 
{ 
    if (moves == ACTION_LIST.MOVERIGHT) 
     MoveRight(theObject); 
    else if (moves == ACTION_LIST.MOVELEFT) 
     MoveLeft(theObject); 
} 

public void MoveRight(GameObject obj) 
{ 
    obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(acceleration, obj.GetComponent<Rigidbody2D>().velocity.y)); 
} 

public void MoveLeft(GameObject obj) 
{ 
    obj.GetComponent<Rigidbody2D>().AddForce(new Vector2(-acceleration, obj.GetComponent<Rigidbody2D>().velocity.y)); 
} 

} 

接口接收器,以使事情更容易..

public enum ACTION_LIST 
{ 
    MOVERIGHT, 
    MOVELEFT 
} 

public interface IReceiver 
{ 
    void Action(ACTION_LIST moves); 
} 

具体命令。我只贴了运动的1 ..

public class MoveRight : Command 
{ 
public MoveRight(IReceiver receiver):base(receiver) 
{ 

} 

public override void Execute() 
{ 
    receiver.Action(ACTION_LIST.MOVERIGHT); 
} 
} 
+3

这也许是一个很好的问题[代码审查](http://codereview.stackexchange.com/) –

+0

除了每个人都是天真的,没有人理解ECS系统:)令人尴尬的是,在代码审查方面,人们会开始试图封装它(就像Deni所说的那样),并没有意识到它是一个已经内置于Unity *的完全微不足道的操作。 – Fattie

+0

在Unity中如何做到这一点的完整说明:幸运的是它很容易http://stackoverflow.com/a/35891919/294884 – Fattie

回答

2

我不同意乔吹等人说,团结是一堆脚本,而不是面向对象编程。我使用具有单个入口点的主脚本并动态创建所有对象和组件。我甚至使用接口,模拟和单元测试。

因此,使用命令模式是可以的。但我没有看到在你的案件中使用它的理由。 命令模式可能非常方便,以防您需要一堆命令才能使用Do()Undo()您的命令(编辑器,策略游戏)。请在这里阅读更多:http://gameprogrammingpatterns.com/command.html

+1

团结***不是*** OO。为了上帝的缘故,它甚至没有继承***。这完全像使用Photoshop ***。当然,**语言**目前用于**编写** Unity的组件,恰好是面向对象的语言,所以在这种意义上,您必须绝对是面向对象的专家*。 (这可能会在明天改变 - 他们可能会改变为使用Lisp或其他东西。)在实际的例子中,除了使用Unity提供的一行代码之外,您完全不可能做任何事情。 – Fattie

+0

你绝对正确!当然,我的意思是语言。 –

相关问题