136 lines
4.8 KiB
C#
136 lines
4.8 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI; // Image 컴포넌트를 조작하기 위해 필요!
|
|
using UnityEngine.InputSystem;
|
|
using Photon.Pun;
|
|
using System.Collections.Generic;
|
|
using TMPro; // Dictionary 사용
|
|
using Hashtable = ExitGames.Client.Photon.Hashtable;
|
|
|
|
public class Gazuaa : MonoBehaviourPunCallbacks
|
|
{
|
|
private GazuaInput _gazuaInput;
|
|
|
|
public GameObject map;
|
|
public GameObject avatarPrefab;
|
|
public TextMeshProUGUI logText; // [추가] 화면 오른쪽 로그 텍스트박스 연결
|
|
|
|
private int myAvatarCount = 0;
|
|
public static bool isGameActive = false;
|
|
|
|
// 핵심!: 각 플레이어(유저 번호)가 지금까지 '몇 개의 아바타를 화면에 그렸는지' 기억하는 사전
|
|
private Dictionary<int, int> knownAvatarCounts = new Dictionary<int, int>();
|
|
private List<string> logMessages = new List<string>();
|
|
private int maxLogLines = 15; // 텍스트박스 높이에 맞춰서 조절하세요!
|
|
|
|
private void Awake()
|
|
{
|
|
_gazuaInput = new GazuaInput();
|
|
_gazuaInput.Enable();
|
|
|
|
_gazuaInput.Player.Gacha.performed += Gacha;
|
|
|
|
if (logText != null) logText.text = ""; // 시작할 때 텍스트 비우기
|
|
}
|
|
|
|
private void Gacha(InputAction.CallbackContext obj)
|
|
{
|
|
// 포톤 방에 없거나, 타이머가 끝나서 게임이 멈췄다면 입력 무시!
|
|
if (!PhotonNetwork.InRoom || !isGameActive) return;
|
|
|
|
var result = Randomizer.GetResult();
|
|
myAvatarCount += result;
|
|
|
|
Hashtable hash = new Hashtable();
|
|
hash["AvatarCount"] = myAvatarCount;
|
|
PhotonNetwork.LocalPlayer.SetCustomProperties(hash);
|
|
}
|
|
|
|
// 서버가 "누군가의 정보가 바뀌었어!" 라고 알려줄 때 실행됨 (나의 변경 사항도 포함됨)
|
|
public override void OnPlayerPropertiesUpdate(Photon.Realtime.Player targetPlayer, Hashtable changedProps)
|
|
{
|
|
if (changedProps.ContainsKey("AvatarCount"))
|
|
{
|
|
int newTotalCount = (int)changedProps["AvatarCount"];
|
|
int actorNumber = targetPlayer.ActorNumber;
|
|
string playerName = targetPlayer.NickName; // [핵심] 아까 로비에서 적은 닉네임 가져오기!
|
|
|
|
int oldCount = 0;
|
|
if (knownAvatarCounts.ContainsKey(actorNumber))
|
|
{
|
|
oldCount = knownAvatarCounts[actorNumber];
|
|
}
|
|
|
|
int avatarsToSpawn = newTotalCount - oldCount;
|
|
|
|
// [추가] 새로 뽑힌 개수가 0개보다 크다면 로그 띄우기
|
|
if (avatarsToSpawn > 0)
|
|
{
|
|
AddLog($"<b>{playerName}</b>님이 <color=#FFD700>{avatarsToSpawn}</color>개 가챠에 성공했습니다!");
|
|
}
|
|
|
|
for (int i = 0; i < avatarsToSpawn; i++)
|
|
{
|
|
GameObject newAvatar = Instantiate(avatarPrefab, map.transform);
|
|
newAvatar.name = actorNumber.ToString();
|
|
|
|
if (GameManager.AvatarCache.ContainsKey(actorNumber))
|
|
{
|
|
Image avatarImage = newAvatar.GetComponent<Image>();
|
|
if (avatarImage != null)
|
|
{
|
|
avatarImage.sprite = GameManager.AvatarCache[actorNumber];
|
|
}
|
|
}
|
|
}
|
|
|
|
knownAvatarCounts[actorNumber] = newTotalCount;
|
|
}
|
|
}
|
|
|
|
// [추가] 로그를 관리하고 텍스트박스에 띄워주는 함수
|
|
private void AddLog(string message)
|
|
{
|
|
if (logText == null) return;
|
|
|
|
logMessages.Add(message);
|
|
|
|
// 정해진 줄 수를 넘어가면 가장 오래된(맨 위의) 로그를 지움
|
|
if (logMessages.Count > maxLogLines)
|
|
{
|
|
logMessages.RemoveAt(0);
|
|
}
|
|
|
|
// 리스트에 있는 텍스트들을 줄바꿈(\n)으로 합쳐서 텍스트박스에 쏙!
|
|
logText.text = string.Join("\n", logMessages);
|
|
}
|
|
|
|
public void HighlightWinner(int winnerActorNumber)
|
|
{
|
|
string winnerNameStr = winnerActorNumber.ToString();
|
|
|
|
// Map 밑에 생성된 모든 아바타를 순회합니다.
|
|
foreach (Transform child in map.transform)
|
|
{
|
|
Image img = child.GetComponent<Image>();
|
|
if (img == null) continue;
|
|
|
|
if (child.name == winnerNameStr)
|
|
{
|
|
// 당첨자의 아바타: 원래 색상 유지 및 살짝 크게 만들기
|
|
child.localScale = new Vector3(1.2f, 1.2f, 1.2f);
|
|
|
|
// (응용: 당첨자 아바타들 중 하나만 랜덤으로 골라 파티클을 터뜨려도 멋집니다!)
|
|
}
|
|
else
|
|
{
|
|
// 패배자의 아바타: 까맣게 만들고 반투명하게(Alpha 값 조절)
|
|
img.color = new Color(0.3f, 0.3f, 0.3f, 0.5f);
|
|
}
|
|
}
|
|
}
|
|
|
|
private new void OnDisable()
|
|
{
|
|
_gazuaInput.Disable();
|
|
}
|
|
} |