private void AddHiddenChild(Transform spawnPointTrans) { EnemySpot sourceSpot = spawnPointTrans.GetComponent <EnemySpot>(); Assertion.Check(sourceSpot != null, "Each child of trigger must have an EnemySpot component!"); if (sourceSpot == null) { return; } GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere); go.transform.renderer.sharedMaterial = GetMaterialByEnemyType(sourceSpot.acceptedSpawnType); go.transform.parent = _hiddenRoot.transform; go.transform.position = spawnPointTrans.position; go.transform.rotation = spawnPointTrans.rotation; go.transform.localScale = spawnPointTrans.localScale; go.hideFlags = HideFlags.HideAndDontSave; go.name = spawnPointTrans.name; EnemySpot hiddenSpot = go.AddComponent <EnemySpot>(); hiddenSpot.acceptedSpawnType = sourceSpot.acceptedSpawnType; _dict.Add(spawnPointTrans, go.transform); }
public void Active(EnemySpot spot, float rotationY = 0) { _enemySpot = spot; _actionController.AIUse.HaveHpBar = _enemySpot.needHPBar; _actionController.AIUse.SlowTimeWhenDead = _enemySpot.needDeathCameraEffect; _actionController.AIUse.LockByCamera = _enemySpot.needCameraLock; _actionController.AIUse.HaveEnergyBar = false; base.Active(spot.transform.position); isActive = true; }
private void Awake() { instance = this; foreach (Transform spot in _spots) { EnemySpot enemySpot = new EnemySpot(); enemySpot.transform = spot; enemySpot.occupied = false; enemySpot.indexInAvailableEnemies = -1; _enemySpots.Add(enemySpot); } foreach (AIStateMachine stateMachine in _enemies) { Enemy enemy = new Enemy(); stateMachine.gameObject.SetActive(false); enemy.stateMachine = stateMachine; enemy.isOccupyingASpot = false; _availableEnemies.Add(enemy); } }
private void Update() { if (!Application.isPlaying) //run in design mode only { //add new nodes foreach (Transform t in transform) { if (!_dict.ContainsKey(t)) { AddHiddenChild(t); } } //remove redundant nodes List <Transform> removeList = new List <Transform>(); foreach (Transform t in _dict.Keys) { bool found = false; foreach (Transform tt in transform) { if (t == tt) { found = true; } } if (!found) { DestroyImmediate(_dict[t].gameObject); removeList.Add(t); } } foreach (Transform t in removeList) { _dict.Remove(t); } //align the spawn points and display objects foreach (KeyValuePair <Transform, Transform> kv in _dict) { if (kv.Key.position != kv.Value.position) { kv.Value.position = kv.Key.position; } if (kv.Key.rotation != kv.Value.rotation) { kv.Value.rotation = kv.Key.rotation; } if (kv.Key.localScale != kv.Value.localScale) { kv.Value.localScale = kv.Key.localScale; } //check the change of enemy type EnemySpot sourceSpot = kv.Key.GetComponent <EnemySpot>(); EnemySpot hiddenSpot = kv.Value.GetComponent <EnemySpot>(); if (sourceSpot.acceptedSpawnType != hiddenSpot.acceptedSpawnType) { hiddenSpot.transform.renderer.sharedMaterial = GetMaterialByEnemyType(sourceSpot.acceptedSpawnType); hiddenSpot.acceptedSpawnType = sourceSpot.acceptedSpawnType; } } } }
private IEnumerator ActivateEnemy() { // first enemy waiting time. float waitingTime = Random.Range(0, 101) / 100.0f; yield return(new WaitForSeconds(waitingTime)); //record which spot index has been used and must be avoided being used again until there is no vacancy List <int>[] mapping = new List <int> [(int)SpawnPointType.Count]; for (int i = _startEnemyIndex; i < _endEnemyIndex; ++i) { //randomly choose a spawn point for this enemy, the enemy type must fit //require: spawn points with the same enemy type must stay together SpawnPointType spawnType = LevelManager.Singleton.GetEnemySpawnPointTypeByIndex(i); List <int> spotList = mapping[(int)spawnType]; //available spot list not built yet or spots have been used up, in both cases, need to rebuild if (spotList == null) { spotList = new List <int>(); mapping[(int)spawnType] = spotList; int count = this.transform.childCount; while (spawnType >= SpawnPointType.Type1) { for (int childIndex = 0; childIndex < count; childIndex++) { EnemySpot spot = this.transform.GetChild(childIndex).GetComponent <EnemySpot>(); if (spot.acceptedSpawnType == spawnType) { spotList.Add(childIndex); } } if (spotList.Count > 0) { break; //found a suitable spawn point } else { if (spawnType == SpawnPointType.Type1) //the bottom has been reached, report error. { Assertion.Check(spotList.Count > 0, string.Format("Spawn point type {0} not found for trigger {1}", spawnType.ToString(), this.name)); } else { spawnType--; Debug.LogWarning(string.Format("Required spawn point {0} not found, degrad it and search again.", spawnType.ToString())); } } } //randomize the elements for (int k = 0; k < spotList.Count - 1; k++) { int rdm = Random.Range(k + 1, spotList.Count); int a = spotList[rdm]; spotList[rdm] = spotList[k]; spotList[k] = a; } //Debug show foreach (int spotIndex in spotList) { Debug.Log(string.Format("Spot index list for enemy type [{0}]: {1}", spawnType.ToString(), spotIndex)); } } //Get delay time of an ememy group, which is attached to the first enemy of the group float delayTime = LevelManager.Singleton.GetDelayTimeByEnemyIndex(i); if (delayTime > 0) { Debug.Log(string.Format("Waiting {0} seconds on enemy index {1}", delayTime, i)); yield return(new WaitForSeconds(delayTime)); } LevelManager.Singleton.ActivateEnemyAtSpot(i, this.transform.GetChild(spotList[0]).GetComponent <EnemySpot>()); Debug.Log(string.Format("\t\tEnemy created. Trigger name: {0} Spawn point: {1} Enemy index: {2}", this.name, spotList[0], i)); //move the 1st to last spotList.Add(spotList[0]); spotList.RemoveAt(0); waitingTime = Random.Range(0, 101) / 100.0f; yield return(new WaitForSeconds(waitingTime)); } }