-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerEmoteSystem.cs
More file actions
338 lines (286 loc) · 12.1 KB
/
Copy pathPlayerEmoteSystem.cs
File metadata and controls
338 lines (286 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using System.Collections;
// Oyuncu emoji/emote sistemi - çok oyunculu ağ üzerinden emoji gösterimini sağlar
public class PlayerEmoteSystem : MonoBehaviourPunCallbacks
{
[Header("UI References")]
[SerializeField] private GameObject emoteContainer; // Emoji container objesi
[SerializeField] private Image emoteImage; // Emoji gösterecek image bileşeni
[SerializeField] private GameObject chatEmote; // Chat emote GameObject referansı
[Header("Emote Settings")]
[SerializeField] private Sprite[] emoteSprites; // Kullanılabilecek emoji/emote sprite'ları
[SerializeField] private float displayDuration = 2f; // Gösterim süresi
[Header("Animation Settings")]
[SerializeField] private float scaleUpDuration = 0.3f; // Büyüme animasyon süresi
[SerializeField] private float scaleDownDuration = 0.3f; // Küçülme animasyon süresi
[SerializeField] private Vector3 maxScale = new Vector3(1f, 1f, 1f); // Maksimum büyüklük
[SerializeField] private Vector3 minScale = Vector3.zero; // Minimum büyüklük
// Özel değişkenler
private Camera mainCamera; // Ana kamera referansı
private Transform targetTransform; // Hedef transform (oyuncu)
private Canvas emoteCanvas; // Emoji canvas'ı
private bool showingEmote = false; // Şu an emoji gösteriliyor mu?
private Coroutine emoteDisplayCoroutine; // Emoji gösterim coroutine'i
private Vector3 originalScale; // Orijinal ölçek
private UIManager uiManager; // UIManager referansı
// Obje oluşturulduğunda çalışır
private void Awake()
{
targetTransform = transform.parent; // Oyuncu objesi
// Emoji container kontrolü ve ayarları
if (emoteContainer != null)
{
emoteCanvas = emoteContainer.GetComponent<Canvas>();
originalScale = emoteContainer.transform.localScale; // Orijinal ölçeği kaydet
}
// Gerekli bileşenlerin kontrolü
if (emoteContainer == null || emoteImage == null)
{
Debug.LogError("PlayerEmoteSystem: emoteContainer veya emoteImage atanmamış!");
enabled = false;
return;
}
// Başlangıçta emote'u gizle
emoteImage.gameObject.SetActive(false);
if (chatEmote != null)
{
chatEmote.SetActive(false);
}
// UIManager referansını bul
uiManager = FindObjectOfType<UIManager>();
if (uiManager == null)
{
Debug.LogWarning("PlayerEmoteSystem: UIManager bulunamadı!");
}
}
// Oyun başladığında çalışır
private void Start()
{
mainCamera = Camera.main; // Ana kamerayı al
// Canvas'ın world space olmasını sağla
if (emoteCanvas != null)
{
emoteCanvas.renderMode = RenderMode.WorldSpace;
emoteCanvas.worldCamera = mainCamera;
}
}
// Emote göster - Oyuncu kontrolleri tarafından çağrılır
public void ShowEmote(int emoteIndex)
{
// Geçerli bir emote index'i mi kontrol et
if (emoteIndex < 0 || emoteIndex >= emoteSprites.Length)
{
Debug.LogWarning($"Geçersiz emote index: {emoteIndex}");
return;
}
// Sadece kendi karakterimiz için RPC çağrısı yapalım
if (photonView.IsMine)
{
// İlk önce normal emote için RPC
photonView.RPC("RPC_ShowEmote", RpcTarget.All, emoteIndex);
// Ayrıca chat emote durumunu da güncelle
if (chatEmote != null)
{
bool isChatOpen = uiManager != null && uiManager.IsChatOpen();
string chatText = uiManager != null ? uiManager.GetChatInputText() : string.Empty;
bool shouldShowChatEmote = isChatOpen && !string.IsNullOrEmpty(chatText);
// Chat emoji durumunu tüm oyunculara gönder
photonView.RPC("RPC_UpdateChatEmoteVisibility", RpcTarget.All, shouldShowChatEmote, emoteIndex);
}
Debug.Log($"Emote RPC gönderildi: {emoteIndex}");
}
}
// Ağ üzerinden emoji gösterme fonksiyonu
[PunRPC]
private void RPC_ShowEmote(int emoteIndex)
{
// Geçerli sprite kontrolü
if (emoteIndex < 0 || emoteIndex >= emoteSprites.Length || emoteSprites[emoteIndex] == null)
{
Debug.LogWarning($"RPC_ShowEmote: Geçersiz emote index veya sprite: {emoteIndex}");
return;
}
// Önceki emote gösterimi varsa durdur
if (emoteDisplayCoroutine != null)
{
StopCoroutine(emoteDisplayCoroutine);
}
// Sprite'ı ayarla ve göster
emoteImage.sprite = emoteSprites[emoteIndex];
emoteImage.gameObject.SetActive(true);
showingEmote = true;
// Büyüme animasyonu başlat
StartCoroutine(ScaleUpAnimation());
// Chatbox açıksa chat emote'u da güncelle
if (chatEmote != null)
{
Image chatEmoteImage = chatEmote.GetComponentInChildren<Image>();
if (chatEmoteImage != null)
{
chatEmoteImage.sprite = emoteSprites[emoteIndex];
// Chat açık ve içeriği varsa chat emote'u göster
if (uiManager != null && photonView.IsMine)
{
bool isChatOpen = uiManager.IsChatOpen();
string chatText = uiManager.GetChatInputText();
bool shouldShowChatEmote = isChatOpen && !string.IsNullOrEmpty(chatText);
if (shouldShowChatEmote)
{
chatEmote.SetActive(true);
}
}
}
}
// Belirli bir süre sonra gizlemek için coroutine başlat
emoteDisplayCoroutine = StartCoroutine(HideEmoteAfterDelay());
Debug.Log($"Emote gösterildi (ID: {emoteIndex}, ViewID: {photonView.ViewID})");
}
// Büyüme animasyonu
private IEnumerator ScaleUpAnimation()
{
float elapsedTime = 0f; // Geçen süre
Vector3 startScale = minScale; // Başlangıç ölçeği
Vector3 targetScale = maxScale; // Hedef ölçek
emoteImage.transform.localScale = startScale; // Başlangıç ölçeğini ayarla
// Animasyon döngüsü
while (elapsedTime < scaleUpDuration)
{
elapsedTime += Time.deltaTime; // Zamanı güncelle
float t = elapsedTime / scaleUpDuration; // Yüzdelik hesaplama
t = Mathf.SmoothStep(0, 1, t); // Yumuşak geçiş için
// Sadece emote image'ını ölçeklendir
emoteImage.transform.localScale = Vector3.Lerp(startScale, targetScale, t);
yield return null; // Bir frame bekle
}
emoteImage.transform.localScale = targetScale; // Son ölçeği kesin olarak ayarla
}
// Küçülme animasyonu
private IEnumerator ScaleDownAnimation()
{
float elapsedTime = 0f; // Geçen süre
Vector3 startScale = emoteImage.transform.localScale; // Şu anki ölçek
Vector3 targetScale = minScale; // Hedef ölçek (küçük)
// Animasyon döngüsü
while (elapsedTime < scaleDownDuration)
{
elapsedTime += Time.deltaTime; // Zamanı güncelle
float t = elapsedTime / scaleDownDuration; // Yüzdelik hesaplama
t = Mathf.SmoothStep(0, 1, t); // Yumuşak geçiş için
// Sadece emote image'ını ölçeklendir
emoteImage.transform.localScale = Vector3.Lerp(startScale, targetScale, t);
yield return null; // Bir frame bekle
}
emoteImage.transform.localScale = targetScale; // Son ölçeği ayarla
emoteImage.gameObject.SetActive(false); // Emoji'yi gizle
// Chat emote'u da gizle
if (chatEmote != null)
{
chatEmote.SetActive(false);
}
}
// Belirli süre sonra emoji'yi gizleme coroutine'i
private IEnumerator HideEmoteAfterDelay()
{
// Gösterim süresi boyunca bekle
yield return new WaitForSeconds(displayDuration);
// Küçülme animasyonunu başlat
yield return StartCoroutine(ScaleDownAnimation());
showingEmote = false; // Artık emoji gösterilmiyor
emoteDisplayCoroutine = null; // Coroutine'i temizle
}
// Her frame sonunda çalışır (kamera takibi için)
private void LateUpdate()
{
// Kamera kontrolü
if (mainCamera == null)
{
mainCamera = Camera.main;
return;
}
// Hedef veya emoji yok mu kontrol et
if (targetTransform == null || !showingEmote)
{
return;
}
// Emote'u her zaman kameraya dönük tut (billboard effect)
emoteContainer.transform.rotation = mainCamera.transform.rotation;
}
// Her frame çalışır
void Update()
{
// Chat emoji durumunu kontrol et ve senkronize et
if (uiManager != null && photonView.IsMine)
{
bool isChatOpen = uiManager.IsChatOpen(); // Chat açık mı?
string chatText = uiManager.GetChatInputText(); // Chat metni
bool shouldShowChatEmote = isChatOpen && !string.IsNullOrEmpty(chatText); // Chat emoji gösterilmeli mi?
// Chat emoji durumu değiştiyse RPC ile herkese bildir
if (chatEmote != null && chatEmote.activeSelf != shouldShowChatEmote)
{
// Tüm oyunculara chat emoji durumunu gönder
photonView.RPC("RPC_UpdateChatEmoteVisibility", RpcTarget.All, shouldShowChatEmote, GetCurrentEmoteIndex());
}
}
}
// Chat emoji görünürlük güncelleme RPC'si
[PunRPC]
private void RPC_UpdateChatEmoteVisibility(bool isVisible, int emoteIndex)
{
// Chat emoji görünürlüğünü tüm oyuncularda güncelle
if (chatEmote != null)
{
// Önce emoji sprite'ını ayarla
Image chatEmoteImage = chatEmote.GetComponentInChildren<Image>();
if (chatEmoteImage != null && emoteSprites != null && emoteIndex >= 0 && emoteIndex < emoteSprites.Length)
{
chatEmoteImage.sprite = emoteSprites[emoteIndex];
}
// Sonra görünürlüğü ayarla
chatEmote.SetActive(isVisible);
}
}
// Şu anki emoji'nin index'ini bulma fonksiyonu
private int GetCurrentEmoteIndex()
{
// Şu an görünen emoji sprite'ının indexini bul
if (emoteImage != null && emoteImage.sprite != null)
{
for (int i = 0; i < emoteSprites.Length; i++)
{
if (emoteSprites[i] == emoteImage.sprite)
{
return i;
}
}
}
// Varsayılan olarak ilk emojiyi kullan
return 0;
}
// Şu anki emote sprite'ını dönüp gerektiğinde kullanmak için
public Sprite GetCurrentEmoteSprite()
{
// Şu anki emoji sprite'ını kontrol et
if (emoteImage != null && emoteImage.sprite != null)
{
return emoteImage.sprite;
}
// Varsayılan olarak ilk emote sprite'ını dön (eğer varsa)
if (emoteSprites != null && emoteSprites.Length > 0)
{
return emoteSprites[0];
}
return null; // Hiçbir sprite yok
}
// Belirli bir emote sprite'ını döndüren fonksiyon
public Sprite GetEmoteSprite(int index)
{
// Index kontrolü yap
if (emoteSprites != null && index >= 0 && index < emoteSprites.Length)
{
return emoteSprites[index];
}
return null; // Geçersiz index
}
}