-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevelGenerator.cs
More file actions
43 lines (37 loc) · 1.14 KB
/
LevelGenerator.cs
File metadata and controls
43 lines (37 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using UnityEngine;
public class LevelGenerator : MonoBehaviour
{
public GameObject brickPrefab;
public int rows = 5;
public int cols = 10;
public float spacing = 0.1f;
void Start()
{
GenerateBricks();
}
void GenerateBricks()
{
if (brickPrefab == null)
{
Debug.LogError("Brick Prefab is not assigned in LevelGenerator!");
return;
}
Vector2 brickSize = brickPrefab.GetComponent<SpriteRenderer>().bounds.size;
Vector2 startPos = new Vector2(-((cols * brickSize.x) + ((cols - 1) * spacing)) / 2f + brickSize.x/2f, 4f);
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
Vector2 pos = new Vector2(
startPos.x + col * (brickSize.x + spacing),
startPos.y - row * (brickSize.y + spacing)
);
Instantiate(brickPrefab, pos, Quaternion.identity, transform);
}
}
if (GameManager.Instance != null)
{
GameManager.Instance.SetBrickCount(rows * cols);
}
}
}