-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRockSpawner.cs
98 lines (84 loc) · 2.57 KB
/
RockSpawner.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// A teddy bear spawner
/// </summary>
public class RockSpawner : MonoBehaviour
{
// needed for spawning
[SerializeField]
GameObject prefabGreenRock;
[SerializeField]
GameObject prefabMagentaRock;
[SerializeField]
GameObject prefabWhiteRock;
// spawn control
const float MinSpawnDelay = 1;
const float MaxSpawnDelay = 2;
Timer spawnTimer;
// spawn location support
const int SpawnBorderSize = 100;
int minSpawnX;
int maxSpawnX;
int minSpawnY;
int maxSpawnY;
const float MinImpulseForce = 1f;
const float MaxImpulseForce = 2f;
/// <summary>
/// Use this for initialization
/// </summary>
void Start()
{
// save spawn boundaries for efficiency
minSpawnX = SpawnBorderSize;
maxSpawnX = Screen.width - SpawnBorderSize;
minSpawnY = SpawnBorderSize;
maxSpawnY = Screen.height - SpawnBorderSize;
// create and start timer
spawnTimer = gameObject.AddComponent<Timer>();
spawnTimer.Duration = Random.Range(MinSpawnDelay, MaxSpawnDelay);
spawnTimer.Run();
}
/// <summary>
/// Update is called once per frame
/// </summary>
void Update()
{
// check for time to spawn a new teddy bear
if (spawnTimer.Finished)
{
SpawnRock();
// change spawn timer duration and restart
spawnTimer.Duration = Random.Range(MinSpawnDelay, MaxSpawnDelay);
spawnTimer.Run();
}
}
/// <summary>
/// Spawns a new teddy bear at a random location
/// </summary>
void SpawnRock()
{
// generate random location and create new teddy bear
Vector3 location = new Vector3(Random.Range(minSpawnX, maxSpawnX),Random.Range(minSpawnY, maxSpawnY),-Camera.main.transform.position.z);
Vector3 worldLocation = Camera.main.ScreenToWorldPoint(location);
// spawn random prefab
// int prefabNumber = Random.Range(0, 3);
int prefabNumber = Random.Range(0,3);
if (prefabNumber == 0)
{
GameObject rock = Instantiate<GameObject>(prefabWhiteRock,worldLocation, Quaternion.identity);
rock.tag = "countRock";
}
else if (prefabNumber == 1)
{
GameObject rock1 = Instantiate<GameObject>(prefabMagentaRock,worldLocation, Quaternion.identity);
rock1.tag = "countRock";
}
else
{
GameObject rock2 = Instantiate<GameObject>(prefabGreenRock,worldLocation, Quaternion.identity);
rock2.tag = "countRock";
}
}
}