본문 바로가기
카테고리 없음

팀프로젝트 - 판타지 마블 (Fantasy marble) 03 - 인수,통행료 지불

by mynote6590 2025. 4. 12.

인수

수도 코드

1.플레이어가 이동을 마친다

2.해당 지역의 주인을 체크한다

3.해당 지역의 주인이 다른 사람일 경우 통행료를 지불한다

- 현재 통행료를 지불 가능한지?

4.통행료를 지불하고 인수

- 인수 가능 한지?

5.추가 건물 구매

- 추가 건물 구매 가능한지?

순서도 작성

 

 

1. 통행료 지불 후, 인수 가능하다면 인수 팝업 띄우기

 else if (currentTile.GetOwner(0) != _playerNum)
 {
 	// 해당 토지의 총 통행료
     double currentTileTollPrice = currentTile.TotalTollPrice(currentTile);
     // 해당 토지의 건물 건설 비용
     double currentTileBuyPrice = currentTile.TotalBuyPrice(currentTile);
     if (_view.IsMine == true)
     {
    	// 통행료 지불 가능 상태라면
         if (_money > currentTileTollPrice)
         {
			// 통행료 지불 - 동기화
             _view.RPC("DecreaseMoney", RpcTarget.All, currentTileTollPrice);

             // 토지주인의 돈 증가 함수 실행
             FindPlayer(currentTile.GetOwner(0))._view.RPC("IncreaseMoney", RpcTarget.All, currentTileTollPrice);
			
			// 건물 구매 가능 토지 라면?            
             if (currentTile._tileType == TileType.Ground)
             {
                 if (_money >= currentTileBuyPrice) // 인수 가능 상태라면
                 {
                     UIManager.instance.OnFactorUI(currentTile, FindPlayer(_playerNum), FindPlayer(currentTile.GetOwner(0)));
                 }
                 else
                 {
                     // 인수 불가 Ui 출력
                     UIManager.instance.OnFactorWarningUI();
                 }
             }
             else
             {
                 TurnMgr.Instance.endTurn();
             }
         }
     }
 }

 


파산

수도 코드

1. 통행료 지불이 불가하다면

2. 지불 금액이 될 때 까지 낮은 가격순으로 건물 자동 매각 

3. 지불 금액이 된다면 자동 매각 중지, 통행료 납부

4. 건물을 모두 매각해도 지불 금액이 충족되지 않는다면 파산

 

 

1. 플레이어마다 자신 소유의 건물 리스트를 가지고 있음

int 값을 저장하는 리스트를 사용하는 이유는 Photon으로 게임 오브젝트와 스크립트 자체를 주고 받을 수 없기 때문에 PhotoneView ID의 번호를 이용했습니다

public List<TileController> _playerOwnerTileList = new List<TileController>(); // 소유한 일반 타일들
public List<int> _playerOwnerTileViewList = new List<int>();    

 // 플레이어 소유 타일에 새 타일을 추가하는 RPC
[PunRPC]
public void AddPlayerOwnerTileList(int _tileViewNum)
{
    // 중복이 아닐 경우에만 추가
    if (_playerOwnerTileViewList.Contains(_tileViewNum) == false)
    {
        _playerOwnerTileViewList.Add(_tileViewNum); // 뷰 ID 저장
    }
}


 // 플레이어 소유 타일에서 타일을 제거하는 RPC
 [PunRPC]
 public void MinusPlayerOwnerTileList(int _tileViewNum)
 {
     if (_playerOwnerTileViewList.Contains(_tileViewNum) == true)
     {
         _playerOwnerTileViewList.Remove(_tileViewNum); // 뷰 ID 제거
     }
 }

 

 


2. viewID List => TileController List 로 변환

// ViewID 배열을 기반으로 실제 타일 객체들을 리스트에 저장
public void TileControllerListRecorder(int[] _playerOwnerTileViewArr)
{
    _playerOwnerTileList.Clear(); // 기존 리스트 초기화

    for (int i = 0; i < _playerOwnerTileViewArr.Length; i++)
    {
        PhotonView _tileViewId = PhotonView.Find(_playerOwnerTileViewArr[i]); // ViewID -> PhotonView 찾기
        TileController _currentTileController = _tileViewId.GetComponent<TileController>(); // 실제 TileController 컴포넌트 얻기
        _playerOwnerTileList.Add(_currentTileController); // 리스트에 추가
    }
}

 

 


3. 토지 낮은 가격순으로 오름차순 정렬 - 버블 정렬

 public void LowPriceSorting(int[] _playerOwnerTileViewArr)
 {
     TileControllerListRecorder(_playerOwnerTileViewList.ToArray());
     // 버블 정렬 (낮은 가격 순)
     for (int i = 0; i < _playerOwnerTileList.Count() - 1; i++)
     {
         for (int j = 0; j < _playerOwnerTileList.Count() - i - 1; j++)
         {
             if (_playerOwnerTileList[i].TotalBuyPrice(_playerOwnerTileList[i]) > _playerOwnerTileList[i + 1].TotalBuyPrice(_playerOwnerTileList[i + 1]))
             {
                 var _temp = _playerOwnerTileList[i];
                 _playerOwnerTileList[i] = _playerOwnerTileList[i + 1];
                 _playerOwnerTileList[i + 1] = _temp;
             }
         }
     }
 }

 

 

 


3. 지불 가능 금액이 될 때까지 토지 매각

낮은 가격순으로 정렬한 리스트를 이용해서 지불 가능 금액이 될 때까지 토지 매각. 만약 건물을 모두 매각해도 지불 금액이 마련되지않는다면 파산

[PunRPC]
public void AutomaticSale(double _SaleAmount, double currentTileTollPrice, int _tileOwner)
{
    // 1. 현재 소유 타일들을 가격 기준으로 정렬 (저가순 → 고가순)
    LowPriceSorting(_playerOwnerTileViewList.ToArray());

    _tempTotalMoney = _totalMoney;
    double _TotalMyLandPrice = 0;
    int i = 0;

    // 2. 필요한 돈이 확보될 때까지 타일 가격을 누적하며 매각할 개수 계산
    for (; i < _playerOwnerTileList.Count; i++)
    {
        if (_SaleAmount <= _TotalMyLandPrice)
        {
            break; // 누적 금액이 부족한 금액을 넘었으면 탈출
        }
        else
        {
            _TotalMyLandPrice += _playerOwnerTileList[i].TotalBuyPrice(_playerOwnerTileList[i]);
        }
    }

    // 3. 실제 매각 처리: 소유자 → 은행(0)으로 변경
    for (int j = 0; j < i; j++)
    {
        for (int k = 0; k < 4; k++)
        {
            if (_playerOwnerTileList[j].GetOwner(k) == _playerNum)
            {
                _playerOwnerTileList[j].SetOwner(k, 0);
            }
        }
    }

    // 4. 소유 리스트에서 매각한 타일 제거
    for (int h = 0; h < i; h++)
    {
        var viewID = _playerOwnerTileList[h].photonView.ViewID;
        _playerOwnerTileViewList.Remove(viewID);
    }

    // 5. 소유 리스트 재갱신
    TileControllerListRecorder(_playerOwnerTileViewList.ToArray());

    // 6. 매각 금액만큼 내 돈 증가 + 통행료만큼 내 돈 차감
    IncreaseMoney(_TotalMyLandPrice);
    DecreaseMoney(currentTileTollPrice);

    // 7. 매각 가능한 자산이 부족했을 경우 → 파산 처리
    if (_tempTotalMoney < _SaleAmount)
    {
        _isBankruptcy = true;
        TurnMgr.Instance.StopTurn(_playerNum, true); // 플레이어 턴 정지
        TurnMgr.Instance.endTurn(); // 턴 넘김
        FindPlayer(_tileOwner).IncreaseMoney(_tempTotalMoney); // 타일 주인에게 내가 가진 전 재산 지급
    }
    else
    {
        _isBankruptcy = false;
        FindPlayer(_tileOwner).IncreaseMoney(currentTileTollPrice); // 정상적으로 통행료 지급
    }

    TotalMoney();
}