bubble_shooter_2d/Assets/Scripts/LevelManager.cs
2025-06-02 09:21:48 +05:30

158 lines
3.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class LevelManager : NetworkBehaviour
{
#region Singleton
public static LevelManager instance;
private void Awake()
{
if (instance == null)
{
instance = this;
}
DontDestroyOnLoad(gameObject);
}
#endregion
public Grid grid;
public Transform bubblesArea;
public List<GameObject> bubblesPrefabs;
public GameObject specialBubblePrefab;
public List<GameObject> bubblesInScene;
public GameObject level;
public List<string> colorsInScene;
public int currentLevel = 0;
public GameObject levelText;
private void Start()
{
grid = GetComponent<Grid>();
}
public override void OnStartServer()
{
base.OnStartServer();
StartCoroutine(LoadLevel());
}
public void StartNewGame()
{
GameManager.instance.startUI.SetActive(false);
GameManager.instance.levelsUI.SetActive(true);
}
System.Collections.IEnumerator LoadLevel()
{
yield return new WaitForSeconds(0.1f);
ScoreManager.GetInstance().Reset();
GameObject levelToLoad = Instantiate(level);
FillWithBubbles(levelToLoad, bubblesPrefabs);
SnapChildrensToGrid(bubblesArea);
InsertSpecialBubbles();
UpdateListOfBubblesInScene();
GameManager.instance.shootScript_p1.CreateNewBubbles();
GameManager.instance.shootScript_p2.CreateNewBubbles();
}
public void InsertSpecialBubbles()
{
int specialCount = Random.Range(1, 6);
List<Transform> specials = new List<Transform>();
for (int i = 0; i < specialCount; i++)
{
int randomBubble = Random.Range(0, bubblesArea.childCount);
Transform bubble = bubblesArea.GetChild(randomBubble);
if (!specials.Contains(bubble))
{
specials.Add(bubble);
var bubbleToSpawn = Instantiate(specialBubblePrefab, bubble.position, Quaternion.identity, bubblesArea);
NetworkServer.Spawn(bubbleToSpawn);
NetworkServer.Destroy(bubble.gameObject);
}
}
}
public void ClearLevel()
{
foreach (Transform t in bubblesArea)
Destroy(t.gameObject);
}
public int GetBubbleAreaChildCount()
{
return bubblesArea.childCount;
}
#region Snap to Grid
private void SnapChildrensToGrid(Transform parent)
{
foreach (Transform t in parent)
{
SnapToNearestGripPosition(t);
}
}
public void SnapToNearestGripPosition(Transform t)
{
Vector3Int cellPosition = grid.WorldToCell(t.position);
t.position = grid.GetCellCenterWorld(cellPosition);
t.rotation = Quaternion.identity;
}
#endregion
private void FillWithBubbles(GameObject go, List<GameObject> _prefabs)
{
foreach (Transform t in go.transform)
{
var bubble = Instantiate(_prefabs[Random.Range(0, _prefabs.Count)], bubblesArea);
NetworkServer.Spawn(bubble);
bubble.transform.position = t.position;
}
Destroy(go);
}
public void UpdateListOfBubblesInScene()
{
List<string> colors = new List<string>();
List<GameObject> newListOfBubbles = new List<GameObject>();
foreach (Transform t in bubblesArea)
{
Bubble bubbleScript = t.GetComponent<Bubble>();
if (colors.Count < bubblesPrefabs.Count && !colors.Contains(bubbleScript.bubbleColor.ToString()))
{
string color = bubbleScript.bubbleColor.ToString();
foreach (GameObject prefab in bubblesPrefabs)
{
if (color.Equals(prefab.GetComponent<Bubble>().bubbleColor.ToString()))
{
colors.Add(color);
newListOfBubbles.Add(prefab);
}
}
}
}
colorsInScene = colors;
bubblesInScene = newListOfBubbles;
}
public void SetAsBubbleAreaChild(Transform bubble)
{
SnapToNearestGripPosition(bubble);
bubble.SetParent(bubblesArea);
}
}