FlappyBirdMP/Assets/Scripts/LevelsManager.cs
2025-06-23 00:38:12 +05:30

64 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Mirror;
using UnityEngine;
public class LevelsManager : NetworkBehaviour
{
public bool hasStarted = false;
public float pipeVerticalOffsetRange = 1.8f;
public GameObject pipePrefab;
public Transform pipeSpawnPoint;
public float distanceBetweenPipes = 5f;
public float pipeOpeningSpace= -1f;
public static float pipeMovingSpeed = 3f;
public float pipeDestroyDistance = -10f;
public List<GameObject> spawnedPipes = new List<GameObject>();
float pipeSpawnTimer = 0;
public float pipeSpawnInterval => distanceBetweenPipes/pipeMovingSpeed;
public void StartPipes()
{
hasStarted=true;
}
void Update()
{
if(!isServer){return;}
if(!hasStarted) return;
CheckAndRemovePipes();
if(pipeSpawnTimer < pipeSpawnInterval){
pipeSpawnTimer += Time.deltaTime;
pipeOpeningSpace+=Time.deltaTime / 120f;
return;
}
pipeSpawnTimer = 0f;
SpawnPipe();
}
void CheckAndRemovePipes(){
foreach(GameObject pipe in spawnedPipes){
if(pipe.transform.position.x < pipeDestroyDistance){
spawnedPipes.Remove(pipe);
NetworkServer.Destroy(pipe);
}
}
}
void SpawnPipe(){
float verticalOffset =Random.Range(-pipeVerticalOffsetRange, pipeVerticalOffsetRange);
GameObject pipe = Instantiate(pipePrefab, pipeSpawnPoint.position + new Vector3(0, verticalOffset, 0), Quaternion.identity);
NetworkServer.Spawn(pipe);
Pipe pipeScript = pipe.GetComponent<Pipe>();
pipeScript.Setup(pipeSpawnPoint.position.x, pipeOpeningSpace);
spawnedPipes.Add(pipe);
}
}