lundi 25 juillet 2016

Recently implementing Command Patterns for my UNITY2D game, is this efficient enough? And what's the concrete benefit of it?

So today I am learning and implementing Command Patterns for handling input and movement for object.

So my question is:

  1. Am I getting the implementation of Command Patterns right?, or do I need to modify it? If so, can somebody give me a little example on to improve it..
  2. I know that it improves code reusability.. But what difference does it make when I just use a simple MovementScript.cs to my game object component? Wouldn't it just be the same and took less time to write rather making a whole Command Pattern?

EDIT: The one i attached to the gameobject is only the InputHandler.

Here's my code which involves moving an object:

This is my Input Handler or as far as i know as The Client

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();
    }
}
}

The Command abstract class

public abstract class Command
{

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

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

public abstract void Execute();
}

The Receiver class (where i implements the logics, which is the movement)

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));
}

}

Interface of receiver, to make things easier..

public enum ACTION_LIST
{
    MOVERIGHT,
    MOVELEFT
}

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

The concrete command, i only posted 1 of the movement..

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

}

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

Thanks!

Aucun commentaire:

Enregistrer un commentaire