Optimize berry handling
This commit is contained in:
5 files changed
+77
-31
No files matched your search
@@ -44,6 +44,8 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
[SerializeField] private RectTransform canvasRect;
|
||||
[SerializeField] private BoxCollider2D groundCollider;
|
||||
private UnityEngine.Vector2 velocityBeforePause = new();
|
||||
|
||||
public readonly List<GameObject> berries = new();
|
||||
private readonly List<int> noGravityBerries = new();
|
||||
|
||||
internal bool IsGrounded() => Physics2D.Distance(bird.GetComponent<CircleCollider2D>(), groundCollider).distance <= 0.1f;
|
||||
@@ -321,6 +323,8 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
float spawnPositionX = UnityEngine.Random.Range(-(canvasRect.rect.width / 2 - newBerry.rectTransform.rect.width), canvasRect.rect.width / 2 - newBerry.rectTransform.rect.width);
|
||||
newBerry.transform.localPosition = new UnityEngine.Vector3(spawnPositionX, (canvasRect.rect.height / 2) + newBerry.rectTransform.rect.height, 0f);
|
||||
newBerry.gameObject.SetActive(true);
|
||||
|
||||
berries.Add(newBerry.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,10 +332,12 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
if (!pausePanel.activeSelf && rb.gravityScale != 100f * canvasRect.lossyScale.y) rb.gravityScale = 100f * canvasRect.lossyScale.y;
|
||||
foreach (AudioSource audio in FindObjectsByType<AudioSource>(FindObjectsSortMode.None)) audio.pitch = speedyLeft > 0f ? 1.3f : 1f;
|
||||
|
||||
var allBerries = Tools.GetAllBerries(berryParent.transform);
|
||||
|
||||
if (!pausePanel.activeSelf)
|
||||
foreach (GameObject berry in allBerries) {
|
||||
foreach (GameObject berry in berries.ToList()) {
|
||||
if (berry == null) {
|
||||
berries.Remove(berry);
|
||||
continue;
|
||||
}
|
||||
if (berry.GetComponent<RectTransform>().anchoredPosition.y < -(canvasRect.rect.height / 2) - berry.GetComponent<RectTransform>().rect.height) {
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
@@ -365,7 +371,6 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
}
|
||||
if (berry != null) berry.GetComponent<Rigidbody2D>().linearVelocity = noGravityBerries.Contains(berry.GetInstanceID()) ? UnityEngine.Vector2.zero : new UnityEngine.Vector2(0f, speedyLeft == 0.0f ? (-300f * canvasRect.lossyScale.y) : (-550f * canvasRect.lossyScale.y));
|
||||
}
|
||||
else foreach (GameObject berry in allBerries) berry.GetComponent<Rigidbody2D>().linearVelocity = UnityEngine.Vector2.zero;
|
||||
if ((Application.platform == RuntimePlatform.Android && Keyboard.current.escapeKey.wasPressedThisFrame) || !Application.isMobilePlatform && (Keyboard.current.escapeKey.wasPressedThisFrame || (Gamepad.current != null && (Gamepad.current.startButton.wasPressedThisFrame || Gamepad.current.buttonEast.wasPressedThisFrame)))) TogglePause();
|
||||
}
|
||||
|
||||
@@ -381,7 +386,9 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
antiLeft = 0f;
|
||||
UpdateStats(0, 1);
|
||||
|
||||
foreach (GameObject berry in Tools.GetAllBerries(berryParent.transform)) Destroy(berry);
|
||||
foreach (GameObject berry in berries) Destroy(berry);
|
||||
berries.Clear();
|
||||
noGravityBerries.Clear();
|
||||
}
|
||||
|
||||
void UpdateStats(BigInteger scoreAddAmount, BigInteger attemptAddAmount) {
|
||||
@@ -418,6 +425,8 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
backgroundMusic.GetComponent<GameMusicHandler>().PauseMusic();
|
||||
pausePanel.SetActive(true);
|
||||
|
||||
foreach (GameObject berry in berries) berry.GetComponent<Rigidbody2D>().linearVelocity = UnityEngine.Vector2.zero;
|
||||
}
|
||||
|
||||
internal void DisablePause() {
|
||||
@@ -450,6 +459,7 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
void DoNormalBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalNormalBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Eat"), Camera.main.transform.position, 1.2f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
UpdateStats(1, 0);
|
||||
@@ -465,6 +475,7 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
void DoSlowBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalSlowBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Downgrade"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -480,6 +491,7 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
void DoUltraBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalUltraBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Powerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
speedyLeft = 0f;
|
||||
@@ -497,6 +509,7 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
void DoSpeedyBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalSpeedyBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/SpeedyPowerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -523,6 +536,7 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
void DoAntiBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalAntiBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Downgrade"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -536,6 +550,7 @@ public class AutoGamePlayer : MonoBehaviour {
|
||||
void DoGoldenBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalGoldenBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Powerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
|
||||
@@ -48,6 +48,8 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
[SerializeField] private RectTransform canvasRect;
|
||||
[SerializeField] private BoxCollider2D groundCollider;
|
||||
private UnityEngine.Vector2 velocityBeforePause = new();
|
||||
|
||||
public readonly List<GameObject> berries = new();
|
||||
private readonly List<int> noGravityBerries = new();
|
||||
|
||||
internal bool IsGrounded() => Physics2D.Distance(bird.GetComponent<CircleCollider2D>(), groundCollider).distance <= 0.1f;
|
||||
@@ -487,6 +489,8 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
float spawnPositionX = Random.Range(-(canvasRect.rect.width / 2 - newBerry.rectTransform.rect.width), canvasRect.rect.width / 2 - newBerry.rectTransform.rect.width);
|
||||
newBerry.transform.localPosition = new UnityEngine.Vector3(spawnPositionX, (canvasRect.rect.height / 2) + newBerry.rectTransform.rect.height, 0f);
|
||||
newBerry.gameObject.SetActive(true);
|
||||
|
||||
berries.Add(newBerry.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,10 +498,12 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
if (!pausePanel.activeSelf && rb.gravityScale != 100f * canvasRect.lossyScale.y) rb.gravityScale = 100f * canvasRect.lossyScale.y;
|
||||
foreach (AudioSource audio in FindObjectsByType<AudioSource>(FindObjectsSortMode.None)) audio.pitch = speedyLeft > 0f ? 1.3f : 1f;
|
||||
|
||||
var allBerries = Tools.GetAllBerries(berryParent.transform);
|
||||
|
||||
if (!pausePanel.activeSelf)
|
||||
foreach (GameObject berry in allBerries) {
|
||||
foreach (GameObject berry in berries.ToList()) {
|
||||
if (berry == null) {
|
||||
berries.Remove(berry);
|
||||
continue;
|
||||
}
|
||||
if (berry.GetComponent<RectTransform>().anchoredPosition.y < -(canvasRect.rect.height / 2) - berry.GetComponent<RectTransform>().rect.height) {
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
@@ -534,7 +540,6 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
}
|
||||
if (berry != null) berry.GetComponent<Rigidbody2D>().linearVelocity = noGravityBerries.Contains(berry.GetInstanceID()) ? UnityEngine.Vector2.zero : new UnityEngine.Vector2(0f, speedyLeft == 0.0f ? (-300f * canvasRect.lossyScale.y) : (-550f * canvasRect.lossyScale.y));
|
||||
}
|
||||
else foreach (GameObject berry in allBerries) berry.GetComponent<Rigidbody2D>().linearVelocity = UnityEngine.Vector2.zero;
|
||||
if ((Application.platform == RuntimePlatform.Android && Keyboard.current.escapeKey.wasPressedThisFrame) || !Application.isMobilePlatform && (Keyboard.current.escapeKey.wasPressedThisFrame || (Gamepad.current != null && (Gamepad.current.startButton.wasPressedThisFrame || Gamepad.current.buttonEast.wasPressedThisFrame)))) TogglePause();
|
||||
}
|
||||
|
||||
@@ -550,7 +555,9 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
antiLeft = 0f;
|
||||
UpdateStats(0, 1);
|
||||
|
||||
foreach (GameObject berry in Tools.GetAllBerries(berryParent.transform)) Destroy(berry);
|
||||
foreach (GameObject berry in berries) Destroy(berry);
|
||||
berries.Clear();
|
||||
noGravityBerries.Clear();
|
||||
}
|
||||
|
||||
void UpdateStats(BigInteger scoreAddAmount, BigInteger attemptAddAmount) {
|
||||
@@ -575,6 +582,8 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
backgroundMusic.GetComponent<GameMusicHandler>().PauseMusic();
|
||||
pausePanel.SetActive(true);
|
||||
|
||||
foreach (GameObject berry in berries) berry.GetComponent<Rigidbody2D>().linearVelocity = UnityEngine.Vector2.zero;
|
||||
}
|
||||
|
||||
internal void DisablePause() {
|
||||
@@ -602,6 +611,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoNormalBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Eat"), Camera.main.transform.position, 1.2f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
UpdateStats(1, 0);
|
||||
@@ -615,6 +625,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoSlowBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Downgrade"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -629,6 +640,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoUltraBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Powerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
speedyLeft = 0f;
|
||||
@@ -645,6 +657,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoSpeedyBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/SpeedyPowerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -669,6 +682,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoAntiBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Downgrade"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -681,6 +695,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoGoldenBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Powerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -693,6 +708,7 @@ public class CustomGamePlayer : MonoBehaviour {
|
||||
|
||||
void DoNothingBerry(GameObject berry) {
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Eat"), Camera.main.transform.position, 1.2f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
}
|
||||
|
||||
@@ -52,6 +52,8 @@ public class GamePlayer : MonoBehaviour {
|
||||
[SerializeField] private RectTransform canvasRect;
|
||||
[SerializeField] private BoxCollider2D groundCollider;
|
||||
private UnityEngine.Vector2 velocityBeforePause = new();
|
||||
|
||||
public readonly List<GameObject> berries = new();
|
||||
private readonly List<int> noGravityBerries = new();
|
||||
|
||||
internal bool IsGrounded() => Physics2D.Distance(bird.GetComponent<CircleCollider2D>(), groundCollider).distance <= 0.1f;
|
||||
@@ -267,6 +269,8 @@ public class GamePlayer : MonoBehaviour {
|
||||
float spawnPositionX = UnityEngine.Random.Range(-(canvasRect.rect.width / 2 - newBerry.rectTransform.rect.width), canvasRect.rect.width / 2 - newBerry.rectTransform.rect.width);
|
||||
newBerry.transform.localPosition = new UnityEngine.Vector3(spawnPositionX, (canvasRect.rect.height / 2) + newBerry.rectTransform.rect.height, 0f);
|
||||
newBerry.gameObject.SetActive(true);
|
||||
|
||||
berries.Add(newBerry.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,10 +278,12 @@ public class GamePlayer : MonoBehaviour {
|
||||
if (!pausePanel.activeSelf && rb.gravityScale != 100f * canvasRect.lossyScale.y) rb.gravityScale = 100f * canvasRect.lossyScale.y;
|
||||
foreach (AudioSource audio in FindObjectsByType<AudioSource>(FindObjectsSortMode.None)) audio.pitch = speedyLeft > 0f ? 1.3f : 1f;
|
||||
|
||||
var allBerries = Tools.GetAllBerries(berryParent.transform);
|
||||
|
||||
if (!pausePanel.activeSelf)
|
||||
foreach (GameObject berry in allBerries) {
|
||||
foreach (GameObject berry in berries.ToList()) {
|
||||
if (berry == null) {
|
||||
berries.Remove(berry);
|
||||
continue;
|
||||
}
|
||||
if (berry.GetComponent<RectTransform>().anchoredPosition.y < -(canvasRect.rect.height / 2) - berry.GetComponent<RectTransform>().rect.height) {
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
@@ -314,7 +320,6 @@ public class GamePlayer : MonoBehaviour {
|
||||
}
|
||||
if (berry != null) berry.GetComponent<Rigidbody2D>().linearVelocity = noGravityBerries.Contains(berry.GetInstanceID()) ? UnityEngine.Vector2.zero : new UnityEngine.Vector2(0f, speedyLeft == 0.0f ? (-300f * canvasRect.lossyScale.y) : (-550f * canvasRect.lossyScale.y));
|
||||
}
|
||||
else foreach (GameObject berry in allBerries) berry.GetComponent<Rigidbody2D>().linearVelocity = UnityEngine.Vector2.zero;
|
||||
if ((Application.platform == RuntimePlatform.Android && Keyboard.current.escapeKey.wasPressedThisFrame) || !Application.isMobilePlatform && (Keyboard.current.escapeKey.wasPressedThisFrame || (Gamepad.current != null && (Gamepad.current.startButton.wasPressedThisFrame || Gamepad.current.buttonEast.wasPressedThisFrame)))) TogglePause();
|
||||
}
|
||||
|
||||
@@ -330,7 +335,9 @@ public class GamePlayer : MonoBehaviour {
|
||||
antiLeft = 0f;
|
||||
UpdateStats(0, 1);
|
||||
|
||||
foreach (GameObject berry in Tools.GetAllBerries(berryParent.transform)) Destroy(berry);
|
||||
foreach (GameObject berry in berries) Destroy(berry);
|
||||
berries.Clear();
|
||||
noGravityBerries.Clear();
|
||||
}
|
||||
|
||||
void UpdateStats(BigInteger scoreAddAmount, BigInteger attemptAddAmount) {
|
||||
@@ -383,6 +390,8 @@ public class GamePlayer : MonoBehaviour {
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
backgroundMusic.GetComponent<GameMusicHandler>().PauseMusic();
|
||||
pausePanel.SetActive(true);
|
||||
|
||||
foreach (GameObject berry in berries) berry.GetComponent<Rigidbody2D>().linearVelocity = UnityEngine.Vector2.zero;
|
||||
}
|
||||
|
||||
internal void DisablePause() {
|
||||
@@ -415,6 +424,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoNormalBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalNormalBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Eat"), Camera.main.transform.position, 1.2f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
UpdateStats(1, 0);
|
||||
@@ -430,6 +440,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoSlowBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalSlowBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Downgrade"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -445,6 +456,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoUltraBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalUltraBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Powerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
speedyLeft = 0f;
|
||||
@@ -462,6 +474,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoSpeedyBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalSpeedyBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/SpeedyPowerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -475,6 +488,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoCoinBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalCoinBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/CoinCollect"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
totalCoins += 10;
|
||||
@@ -498,6 +512,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoAntiBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalAntiBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Downgrade"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
@@ -511,6 +526,7 @@ public class GamePlayer : MonoBehaviour {
|
||||
void DoGoldenBerry(GameObject berry, bool increment) {
|
||||
if (increment) totalGoldenBerries++;
|
||||
AudioSource.PlayClipAtPoint(Resources.Load<AudioClip>("Sounds/Powerup"), Camera.main.transform.position, 0.35f * BazookaManager.Instance.GetSettingSFXVolume());
|
||||
berries.Remove(berry);
|
||||
noGravityBerries.Remove(berry.GetInstanceID());
|
||||
Destroy(berry);
|
||||
boostLeft = 0f;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using MikeSchweitzer.WebSocket;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -31,7 +32,7 @@ public class MultiplayerGamePlayer : MonoBehaviour {
|
||||
|
||||
private bool disconnecting = false;
|
||||
[SerializeField] private TMP_Text pingText;
|
||||
private readonly List<MultiplayerBerry> berries = new();
|
||||
private readonly Dictionary<MultiplayerBerry, GameObject> berries = new();
|
||||
private readonly List<MultiplayerUser> players = new();
|
||||
private float lastMoveSentTime = -1f;
|
||||
|
||||
@@ -118,7 +119,7 @@ public class MultiplayerGamePlayer : MonoBehaviour {
|
||||
Timestamp = timestamp,
|
||||
Spawned = false
|
||||
};
|
||||
berries.Add(berry);
|
||||
berries.Add(berry, null);
|
||||
} else if (data["type"].ToString() == "multiplayer_player_join") {
|
||||
var userFromResponse = data.ToObject<MultiplayerPlayerFromResponse>();
|
||||
CreatePlayer(userFromResponse);
|
||||
@@ -291,9 +292,12 @@ public class MultiplayerGamePlayer : MonoBehaviour {
|
||||
if (!pausePanel.activeSelf && rb.gravityScale != 100f * canvasRect.lossyScale.y) rb.gravityScale = 100f * canvasRect.lossyScale.y;
|
||||
ProcessSpawns();
|
||||
|
||||
var allBerries = Tools.GetAllBerries(berryParent.transform);
|
||||
|
||||
foreach (GameObject berry in allBerries) {
|
||||
foreach (GameObject berry in berries.Values.ToList()) {
|
||||
if (berry == null) {
|
||||
var key = berries.FirstOrDefault(kvp => kvp.Value == berry).Key;
|
||||
if (key != null) berries.Remove(key);
|
||||
continue;
|
||||
}
|
||||
if (berry.GetComponent<RectTransform>().anchoredPosition.y < -(canvasRect.rect.height / 2) - berry.GetComponent<RectTransform>().rect.height) Destroy(berry);
|
||||
else if (!pausePanel.activeSelf && Physics2D.Distance(bird.GetComponent<CircleCollider2D>(), berry.GetComponent<CircleCollider2D>()).distance <= 0.1f) {
|
||||
switch (berry.tag) {
|
||||
@@ -329,7 +333,7 @@ public class MultiplayerGamePlayer : MonoBehaviour {
|
||||
slownessLeft = 0f;
|
||||
UpdateStats(0, 1);
|
||||
|
||||
foreach (GameObject berry in Tools.GetAllBerries(berryParent.transform)) Destroy(berry);
|
||||
foreach (GameObject berry in berries.Values) RemoveBerry(berry);
|
||||
}
|
||||
|
||||
void UpdateStats(BigInteger scoreAddAmount, BigInteger attemptAddAmount) {
|
||||
@@ -450,7 +454,7 @@ public class MultiplayerGamePlayer : MonoBehaviour {
|
||||
}
|
||||
|
||||
void ProcessSpawns() {
|
||||
foreach (var berry in berries) {
|
||||
foreach (var berry in berries.Keys.ToList()) {
|
||||
if (berry.Spawned) continue;
|
||||
berry.Spawned = true;
|
||||
|
||||
@@ -491,12 +495,15 @@ public class MultiplayerGamePlayer : MonoBehaviour {
|
||||
float worldX = (berry.PosX * usableWidth) - (usableWidth / 2f);
|
||||
newBerry.GetComponent<RectTransform>().anchoredPosition = new UnityEngine.Vector2(worldX, y);
|
||||
newBerry.gameObject.SetActive(true);
|
||||
|
||||
berries[berry] = newBerry.gameObject;
|
||||
}
|
||||
}
|
||||
|
||||
void RemoveBerry(GameObject berry) {
|
||||
Destroy(berry);
|
||||
berries.RemoveAll(b => b.ID == berry.name[6..]);
|
||||
var key = berries.FirstOrDefault(kvp => kvp.Value == berry).Key;
|
||||
if (key != null) berries.Remove(key);
|
||||
}
|
||||
|
||||
void CreatePlayer(MultiplayerPlayerFromResponse player) {
|
||||
|
||||
@@ -166,12 +166,4 @@ public static class Tools {
|
||||
BazookaManager.Instance.GetAccountID() ?? 0
|
||||
);
|
||||
}
|
||||
|
||||
public static GameObject[] GetAllBerries(Transform berryParent) {
|
||||
GameObject[] children = new GameObject[berryParent.childCount];
|
||||
for (int i = 0; i < berryParent.childCount; i++) {
|
||||
children[i] = berryParent.GetChild(i).gameObject;
|
||||
}
|
||||
return children;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user