[Unity][3D-Game] Tower Defense Game (17)

suhan0304·2023년 12월 21일
0
post-thumbnail

강의영상 (19)


개발

노드 선택 오류 수정

상단 캔버스가 노드 공간을 차지하면서 노드가 선택이 안되는 오류가 있었는데 이 오류는 캔버스의 Graphic Raycaster 컴포넌트 때문에 발생한 오류이다. 따라서 해당 컴포넌트를 비활성화 해주면 노드 선택이 정상적으로 수행된다.

노드 스크립트 업데이트

기존의 노드 스크립트의 코드를 좀 더 논리적으로 업데이트한다. 빌드 매니저에서 건설해주는 것이 아니라 노드에서 자체적으로 터렛을 건설해주는 것이 좀 더 합리적이라서 코드를 수정해보자.

Node에서 제어하는 NodeUI 버튼으로 Turret의 업그레이드와 판매를 수행할 예정인데 건물에 대한 정보와 건설을 Node에서 모두 담당하는 것이 좀 더 논리적이고 합리적이라고 판단했다.
Node 스크립트에서 이제 해당 Node 자리에서의 건설, 판매, 업그레이드를 모두 구현할 것이다.

기존 빌드 매니저의 BuildTurretOn의 코드 부분을 모두 Node 부분으로 다음과 같이 옮겨준다.

BuildManager.cs

public TurretBlueprint GetTurretToBuild ()
{
    return turretToBuild;
}

BuildManager의 BuildTurretOn 함수는 삭제해준다.

Node.cs

void BuildTurret(TurretBlueprint blueprint)
{
    if (PlayerStats.Money < blueprint.cost) //플레이어의 돈이 turret의 cost보다 적다면
    {
        Debug.Log("Not Enough Money!"); //돈이 부족하다고 출력 후
        return;                         //건설하지 않고 리턴
    }

    PlayerStats.Money -= blueprint.cost; //터렛을 지었으므로 머니를 비용만큼 감소

    GameObject _turret = (GameObject)Instantiate(blueprint.prefab, GetBuildPosition(), Quaternion.identity);
    turret = _turret; //node의 turret을 turret으로 설정

    GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetBuildPosition(), Quaternion.identity); // 이펙트 복사해서 생성해주기
    Destroy(effect, 5f); // 생성하고 5초후에 이펙트 오브젝트 삭제

    Debug.Log("Turret Build!");
}

터렛 업그레이드 기능 구현

이제 노드 UI에서 업그레이드 버튼을 누르면 터렛 업그레이드가 구현되도록 기능을 추가해보자.

Node.cs

[HideInInspector] //인스펙터에서 숨김
public GameObject turret;
[HideInInspector] 
public TurretBlueprint turretBlueprint;
[HideInInspector]
public bool isUpgraded = false;

public void upgrade()
{
    if (PlayerStats.Money < turretBlueprint.upgradeCost) //플레이어의 돈이 turret의 cost보다 적다면
    {
        Debug.Log("Not Enough Money to upgrade that!"); //돈이 부족하다고 출력 후
        return;                         //건설하지 않고 리턴
    }

    PlayerStats.Money -= turretBlueprint.upgradeCost; //터렛을 지었으므로 머니를 비용만큼 감소

    //Get rid of the old turret
    Destroy(turret);//기존의 포탑을 파괴

    //Build a upgraded turret
    GameObject _turret = (GameObject)Instantiate(turretBlueprint.upgradedPrefab, GetBuildPosition(), Quaternion.identity);
    turret = _turret; //node의 turret을 turret으로 설정

    GameObject effect = (GameObject)Instantiate(buildManager.buildEffect, GetBuildPosition(), Quaternion.identity); // 이펙트 복사해서 생성해주기
    Destroy(effect, 5f); // 생성하고 5초후에 이펙트 오브젝트 삭제

    isUpgraded = true; //업그레이드 했다고 수정

    Debug.Log("Turret Upgrade!");
}

이제 TurretBluePrint에 upgradeCost를 추가해준다.

TurretBlueprint.cs

public class TurretBlueprint
{
    public GameObject prefab; //터렛 프리팹
    public int cost;          //터렛 건설비용

    public GameObject upgradedPrefab; //업그레이드 터렛 프리팹
    public int upgradeCost; //업그레이드 비용
}

NodeUI 업데이트

이제 NodeUI의 스크립트의 업그레이드 버튼을 누르면 업그레이드가 진행되도록 한다.

NodeUI.cs

public void Upgrade()
{
    target.UpgradeTurret();
    BuildManager.instance.DeselectNode();//업그레이드 하면 노드 선택 해제
}

인스펙터 초기화 설정

이제 UpgradeCost와 Upgraded Prefab을 설정해준다.
Upgraded Prefab은 기존 터렛과 Bullet의 범위와 데미지, 공격속도를 수정해서 추가해준다.

Standard Turret

포탑과 총알의 업그레이드 버전 재질도 새로 생성해서 추가해주었다.

미사일 런처와 레이저 포탑도 동일하게 업그레이드 버전을 만들어줄 수 있다. 일단 기본 포탑의 업그레이드 버전만 구현했다. 이제 업그레이드한 포탑을 Shop 오브젝트 Shop 스크립트 컴포넌트의 Upgrade Prefab에 추가해준다.

이제 NodeUI의 업그레이드 버튼이 Upgrade를 호출하도록 연결시켜준다.

NodeUI 수정

업그레이드 비용이 수정될 수 있도록 기존 업그레이드 버튼의 text를 두 개로 나눈후에 비용이 업데이트 될 수 있도록 NodeUI.cs를 수정한다. (Sell 버튼도 기존 것을 삭제하고 수정한 업그레이드 버튼을 Ctrl+D로 복사 붙여넣기 해서 만들어준다.)

NodeUI.cs

public GameObject ui;

public Text upgradeCost;
public Button upgradeButton;

private Node target;

public void SetTarget(Node _target)
{
    target = _target;

    transform.position = target.GetBuildPosition(); // 노드 포지션이 아니라 빌드 포지션을 가져온다
    if (!target.isUpgraded) // 업그레이드 하지 않았다면
    {
        upgradeCost.text = "$" + target.turretBlueprint.cost;
        upgradeButton.interactable = true; //버튼 클릭 활성화
    } 
    else
    {
        upgradeCost.text = "Done";
        upgradeButton.interactable = false; //버튼 클릭 비활성화
    }
                   
    ui.SetActive(true); //NodeUI 오브젝트를 활성화
}

이제 NodeUI의 인스펙터에 드래그드랍해서 초기화해준다.


결과물

profile
Be Honest, Be Harder, Be Stronger

0개의 댓글