Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EnemySpawner01 : MonoBehaviour
- {
- public GameObject enemyPrefab;
- public float width = 10;
- public float height = 5;
- public float speed = 5.0f;
- public float padding = 0;
- public float SpawnDelay = 1;
- public float Timer;
- public Transform TrainingBoss;
- private int direction = 1;
- private float boundaryRightEdge, boundaryLeftEdge;
- void Start()
- {
- Camera camera = Camera.main;
- float distance = transform.position.z - camera.transform.position.z;
- boundaryLeftEdge = camera.ViewportToWorldPoint(new Vector3(0, 0, distance)).x + padding;
- boundaryRightEdge = camera.ViewportToWorldPoint(new Vector3(1, 1, distance)).x - padding;
- SpawnUntilFull();
- }
- void SpawnEnemies()
- {
- foreach (Transform child in transform)
- {
- GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
- enemy.transform.parent = child;
- }
- }
- void SpawnUntilFull()
- {
- Transform FreePosition = NextFreePosition();
- if (FreePosition)
- {
- GameObject enemy = Instantiate(enemyPrefab, FreePosition.position, Quaternion.identity) as GameObject;
- enemy.transform.parent = FreePosition;
- }
- if (NextFreePosition())
- {
- Invoke("SpawnUntilFull", SpawnDelay);
- }
- }
- void OnDrawGizmos()
- {
- float xmin, xmax, ymin, ymax;
- xmin = transform.position.x - 0.5f * width;
- xmax = transform.position.x + 0.5f * width;
- ymin = transform.position.y - 0.5f * height;
- ymax = transform.position.y + 0.5f * height;
- Gizmos.DrawLine(new Vector3(xmin, ymin, 0), new Vector3(xmin, ymax));
- Gizmos.DrawLine(new Vector3(xmin, ymax, 0), new Vector3(xmax, ymax));
- Gizmos.DrawLine(new Vector3(xmax, ymax, 0), new Vector3(xmax, ymin));
- Gizmos.DrawLine(new Vector3(xmax, ymin, 0), new Vector3(xmin, ymin));
- }
- void Update()
- {
- float formationRightEdge = transform.position.x + 0.5f * width;
- float formationLeftEdge = transform.position.x - 0.5f * width;
- if (formationRightEdge > boundaryRightEdge)
- {
- direction = -1;
- }
- if (formationLeftEdge < boundaryLeftEdge)
- {
- direction = 1;
- }
- transform.position += new Vector3(direction * speed * Time.deltaTime, 0, 0);
- if (AllMembersDead())
- {
- SpawnUntilFull();
- }
- if (Timer > 0)
- {
- Timer -= Time.deltaTime;
- }
- else
- {
- Destroy(gameObject);
- if (TrainingBoss.gameObject.activeInHierarchy == false)
- {
- TrainingBoss.gameObject.SetActive(true);
- }
- else
- {
- TrainingBoss.gameObject.SetActive(false);
- }
- }
- }
- Transform NextFreePosition()
- {
- foreach (Transform childPositionGameObject in transform)
- {
- if (childPositionGameObject.childCount == 0)
- {
- return childPositionGameObject;
- }
- }
- return null;
- }
- bool AllMembersDead()
- {
- foreach (Transform childPositionGameObject in transform)
- {
- if (childPositionGameObject.childCount > 0)
- {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement