๐Ÿ“š Computer Science/Design Pattern

[C#][Unity][๋””์ž์ธ ํŒจํ„ด] Command ํŒจํ„ด

ibelieveinme 2023. 9. 5. 11:09
728x90

* Commnad ํŒจํ„ด: ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ์‹ค์ฒดํ™”, ์ฆ‰ ๊ฐ์ฒด๋กœ ๊ฐ์‹ผ ๊ฒƒ.

                            : ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ๊ฐ์ฒด๋กœ ๋งŒ๋“  ์ด์œ ๋Š” ๋””์ปคํ”Œ๋ง์œผ๋กœ ์ฝ”๋“œ๊ฐ€ ์œ ์—ฐํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ.

 

ex) ์ž…๋ ฅํ‚ค ๋ณ€๊ฒฝ, ์‹คํ–‰์ทจ์†Œ/์žฌ์‹คํ–‰ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ.


1. Command ํŒจํ„ด์—†์ด ๋งŒ๋“  GetKey ๊ธฐ๋Šฅ

using System.Collections;
using UnityEngine;

public class Player : MonoBehaviour
{

    [SerializeField] private GameObject shield;
    [SerializeField] private GameObject cannon;
    [SerializeField] private Transform firePos;

    void Update()
    {
        if (Input.GetKeyDown("a"))
        {
            Attack();
        }
        else if(Input.GetKeyDown("b"))
        {
            Defense();
        }
    }

    private void Attack()
    {
        Debug.Log("Attack");
        Instantiate(cannon, firePos.position, firePos.rotation);
    }

    private void Defense()
    {
        Debug.Log("Defense");
        shield.SetActive(true);
        StartCoroutine(Defense(0.5f));
    }

    private IEnumerator Defense(float time)
    {
        yield return new WaitForSeconds(time);
        shield.SetActive(false);
    }
}

 

2. Command ํŒจํ„ด์œผ๋กœ ๋งŒ๋“  GetKey ๊ธฐ๋Šฅ

using UnityEngine;
using UnityEngine.UI;

public class PlayerCommand : MonoBehaviour
{
    [SerializeField] private Text text1;
    [SerializeField] private Text text2;

    private bool isCommand;
    CommandKey btnA, btnB;

    private void Start()
    {
        isCommand = true;
        SetCommand();
    }

    //setCommand() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์–ด๋–ค ๋™์ž‘์„ ์ˆ˜ํ–‰ํ• ์ง€๋ฅผ ๊ฐ ๋ฒ„ํŠผ์— ๋“ฑ๋ก
    private void SetCommand()
    {
        if (isCommand)
        {
            btnA = new CommandAttack();
            btnB = new CommandDefense();

            isCommand = false;
            text1.text = "A - Attack";
            text2.text = "B - Defense";
        }
        else
        {
            btnA = new CommandDefense();
            btnB = new CommandAttack();

            isCommand = true;
            text1.text = "A - Defense";
            text2.text = "B - Attack";
        }
    }

    //๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹จ์ง€ Execute() ๋งŒ ํ˜ธ์ถœ
    private void Update()
    {
        if (Input.GetKeyDown("a"))
        {
            btnA.Execute();
        }
        else if (Input.GetKeyDown("b"))
        {
            btnB.Execute();
        }
    }
}

 

ํ‚ค๋ฅผ ๋ฐ”๊ฟ€ ๋ฒ„ํŠผ์„ ํ•˜๋‚˜๋งŒ ๋“ค์–ด์„œ SetCommand() ํ•จ์ˆ˜๋ฅผ ๋‹ฌ์•„์ค€๋‹ค.

SetCommand ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค isCommand ๊ฐ’์ด ๋ฐ”๋€Œ๋ฉฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๋™์ž‘ํ•œ๋‹ค.

 

isCommand ๊ฐ€ true ์ผ ๋•Œ btnA - Attack / btnB - Defense

isCommand๊ฐ€ false ์ผ ๋•Œ btnA - Defense / btnB - Attack

 

์ด๋ ‡๊ฒŒ GetKey ๊ธฐ๋Šฅ์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

using UnityEngine;


public abstract class CommandKey
{
    public virtual void Execute() { }
}

public class CommandAttack : CommandKey
{
    public override void Execute()
    {
        Attack();
    }

    private void Attack()
    {
        Debug.Log("Attack");
    }
}

public class CommandDefense : CommandKey
{
    public override void Execute()
    {
        Defense();
    }

    private void Defense()
    {
        Debug.Log("Defense");
    }
}

 

728x90