Hello everyone.
Yesterday I came back on the Glitch garden project because I want to change some game Mechanics.
I want to add an enemy currency that with every attacker spawn it decrease and when it reaches 0 or a positive value near that it stops spawning. I want to add it because with the spawn timer the game spawns infinite enemies (it stops only when the game stops) so there will be only a predefined max number of enemies (if strat money is 500 and and the lizard cost 10 and the fox 5, there could be max 100 foxes or 50 lizards or some random number of foxes and lizards that costs 500 coins)
In the Attackers.cs script I’ve added a public int value called SpawnCost (public int SpawnCost
)
and in the Spawner.cs in “if (IsTimeToSpawn(thisAttacker)
” script I’ve added &&StartSpawnMoney-spawnCost>=0)
StarSpawnMoney is the total money in the level (it’s a public int setted at 500 by default)
so now it looks like this if (IsTimeToSpawn(thisAttacker)&&StartSpawnMoney-spawnCost>=0)
I know that I’ve to add int spawnCost = attackers.SpawnCost;
somewhere in the spawner code but I don’t know where. this is my Spawner.cs script
public class Spawner : MonoBehaviour {
public GameObject[] attackerPrefabArray;
public int StartSpawnMoney = 500;
public int SpawnCost;
private Attackers attackers;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
foreach(GameObject thisAttacker in attackerPrefabArray)
{
if (IsTimeToSpawn(thisAttacker)&&StartSpawnMoney-spawnCost>=0)
{
Spawn(thisAttacker);
}
}
Debug.Log(StartSpawnMoney);
}
void Spawn(GameObject myGameObject)
{
GameObject myAttacker = Instantiate(myGameObject) as GameObject;
myAttacker.transform.parent = transform;
myAttacker.transform.position = transform.position;
}
bool IsTimeToSpawn(GameObject attackerGameObject)
{
Attackers attackers = attackerGameObject.GetComponent<Attackers>();
float meanSpawnDelay = attackers.seenEverySeconds;
int spawnCost = attackers.SpawnCost;
float spawnsPerSecond = 1 / meanSpawnDelay;
if (Time.deltaTime > meanSpawnDelay)
{
}
float threshold = spawnsPerSecond * Time.deltaTime/5;
return (Random.value < threshold);
}
}
I’ve tried to add it in the IsTimeToSpawn method but it’s not ok, neither in the Update method.
Thank you and sorry for my bad English.