반응형

 

 

 

 

1. 명령패턴 (Command pattern)

명령(이동, 점프 등)에 대한 함수를 클래스로 래핑하여 해당 클래스를 객체화 한 것이다.

사용자(캐릭터)를 명령에 대한 매개변수로 만들어 요청을 대기시키거나 로깅하여 되돌리기 기능의 연산을 지원할 수도 있다.

※ 일반적으로 실행 취소 기능을 구현하려면 힘들지만 명령 패턴을 사용하면 쉬워진다.

 

 

 

2. 명령패턴 예시 구조 및 코드

InputHandler 클래스에서 틱마다 키 입력을 받을 수 있는 로직을 구성하고, 점프키에 해당하는 조건문이 통과하게 되면 JumpCommand 객체를 반환하고 반환된 Command 객체에 Actor 객체를 매개변수를 넘겨서 Command의 Execute 함수에서 Actor의 Jump함수를 호출해주는 구조이다.

 

2-1) Command(추상 클래스)

public abstract class Command
{
    public abstract void Execute(Actor actor);
}

 

2-2) JumpCommand(Command 상속)

public class JumpCommand : Command
{
    public override void Execute(Actor actor)
    {
        actor.Jump();
    }
}

 

2-3) Actor

using UnityEngine;

public class Actor : MonoBehaviour
{
    private Rigidbody2D rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    public void Jump()
    {
        rb.AddForce(Vector2.up * 5f, ForceMode2D.Impulse);
    }
}

 

2-4) InputHandler

using UnityEngine;

public class InputHandler : MonoBehaviour
{
    [SerializeField]
    private Actor targetActor;

    private Command jumpCommand = new JumpCommand();

    private void Update()
    {
        Command targetCommand = HandleInput();
        if(targetCommand != null && targetActor != null)
        {
            targetCommand.Execute(targetActor);
        }
    }

    private Command HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            return jumpCommand;
        }

        return null;
    }
}

 

각 명령(Command)에 대한 객체를 생성함으로써 직접적으로 함수를 호출하는 방식 대신, 한 겹 우회하는 계층을 만들어 입력 키의 교체가 가능하게 되었고, 각 명령 객체의 Execute에 Actor를 인수로 받게 함으로써 플레이어 캐릭터만이 아닌 여러 NPC, 몬스터 등의 캐릭터도 조작이 가능할 수 있도록 구성되어 있다.

※ 키 변경의 경우, 멤버 변수의 객체를 교환함으로써 키 변경이 가능하다는걸 알려주는건 이해하는데, 이게 과연 효율적인가 싶은 의문이 든다. 차라리 키 변경은 Command 객체를 교체하는것이 아닌 키를 변수로 둬서 키 값을 교체하는게 더 효율적일거같은 생각이 든다.

 

 

 

 

 

반응형

'Stack > DesignPattern' 카테고리의 다른 글

명령패턴 실행취소 기능  (0) 2024.02.27

+ Recent posts