Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Silverkeep/Guardian of Silverkeep/Assets/Scripts/EnemyGenerator.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
79 lines (72 sloc)
1.91 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class EnemyGenerator : MonoBehaviour | |
{ | |
#region Field | |
public GameObject Enemy01; | |
public GameObject Villager01; | |
public float spawnRate; | |
private Vector3 SelfPosition; | |
private Vector3 SpawnLocationRandomize; | |
private float posX; | |
private float posZ; | |
public float speedUpRate = 0.1f; | |
// Use this for initialization | |
enum CycleStat | |
{ | |
day, | |
night | |
} | |
static CycleStat cycle = CycleStat.night; | |
#endregion | |
void Start () | |
{ | |
StartCoroutine(Generate()); | |
//start generating enemy | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
SelfPosition = new Vector3(transform.position.x, 0, transform.position.z); | |
//keep track of self position for further usage | |
} | |
#region functions | |
//Generate() is a coroutine that spawns different stuff according to day or night, semi randomly location wise | |
IEnumerator Generate() | |
{ | |
while(true) | |
{ | |
while(cycle == CycleStat.night) | |
{ | |
yield return new WaitForSeconds(spawnRate); | |
posX = Random.Range (0, 3); | |
posZ = Random.Range (0, 3); | |
SpawnLocationRandomize = new Vector3(posX, 0, posZ); | |
Instantiate(Enemy01 , SelfPosition + SpawnLocationRandomize, Quaternion.identity); | |
} | |
while(cycle == CycleStat.day) | |
{ | |
//fill day stuff here: spawn villagers and shit | |
yield return new WaitForSeconds(spawnRate); | |
posX = Random.Range (0, 3); | |
posZ = Random.Range (0, 3); | |
SpawnLocationRandomize = new Vector3(posX, 0, posZ); | |
Instantiate(Villager01 , SelfPosition + SpawnLocationRandomize, Quaternion.identity); | |
} | |
} | |
} | |
//cycleswitch() is a function that usually called by Timer_noGUI for day/night cycle change | |
void CycleSwitch() | |
{ | |
if (cycle == CycleStat.night) | |
cycle = CycleStat.day; | |
else cycle = CycleStat.night; | |
Debug.Log("day/night cycle switched!"); | |
} | |
#endregion | |
void SpeedUp() | |
{ | |
this.spawnRate = this.spawnRate * speedUpRate; | |
Debug.Log("Speed Uped"); | |
} | |
} |