59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
|
|
public class LevelsManager : NetworkBehaviour
|
|
{
|
|
|
|
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;
|
|
|
|
void Update()
|
|
{
|
|
if(!isServer){return;}
|
|
|
|
if(!NetGameManager.instance.playerSpawned) 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);
|
|
}
|
|
}
|