// Enemy main nutrient getting absorbed and provide nutrient(child cell) to enemy main cell void Absorb() { Vector2 vectorToTarget = EMHelper.Instance().Position - (Vector2)transform.position; transform.position = Vector2.MoveTowards(transform.position, EMHelper.Instance().Position, (vectorToTarget.magnitude * fAbsorbTime + fAbsorbSpeed) * Time.deltaTime); transform.localScale = Vector3.one * (Vector2.Distance((Vector2)transform.position, EMHelper.Instance().Position)) / EMHelper.Instance().Radius * Random.Range(.5f, 1f); if (Vector2.Distance((Vector2)transform.position, EMHelper.Instance().Position) < .1f || transform.localScale.x < .1f) { if (Level_Manager.LevelID < 4) { EMController.Instance().AddFewNutrient((int)(Random.Range(2, 6) * EMDifficulty.Instance().CurrentDiff)); } else { EMController.Instance().AddFewChildCells((int)(Random.Range(2, 6) * EMDifficulty.Instance().CurrentDiff)); } if (!EMAnimation.Instance().IsExpanding) { EMAnimation.Instance().IsExpanding = true; } Destroy(this.gameObject); } }
//Extract an Enemy Child Cell from the pool and spawn it to the position given in the perimeter public GameObject SpawnFromPool(Vector2 _SpawnPos, bool _Default) { if (!_Default) { EMHelper.Instance().StartProduceChild(); } m_nSpawnCount++; //Extract the enemy child cell pool from the pool and add it to the enemy child cell list if (s_ECPool.Peek() == null) { return(null); } GameObject newChild = s_ECPool.Dequeue(); newChild.transform.position = _SpawnPos; MessageDispatcher.Instance.DispatchMessage(this.gameObject, newChild, MessageType.Idle, 0); EnemyMainFSM.Instance().ECList.Add(newChild.GetComponent <EnemyChildFSM> ()); EnemyMainFSM.Instance().AvailableChildNum++; if (IsPoolEmpty()) { return(null); } return(newChild); }
// Spawn a few child cells when the game starts void StartWithChildCells() { for (int i = 0; i < fNumOfDefaultCells; i++) { EMHelper.Instance().ECPool.SpawnFromPool(EMHelper.Instance().Position, true); } }
void UpdateAggressiveness() { // Update Aggressiveness factor (Distance) m_EMFSM.AggressivenessDistance = EMHelper.Instance().MinToMaxYRatio + 1.0f; // Reset Aggressiveness factor (Enemy Child) if (m_EMFSM.AvailableChildNum > 0 && m_EMFSM.AggressivenessEnemyChild != Mathf.Sqrt(Mathf.Sqrt((float)m_EMFSM.AvailableChildNum))) { float fAggressivenessEnemyChild = Mathf.Sqrt(Mathf.Sqrt((float)m_EMFSM.AvailableChildNum)); if (fAggressivenessEnemyChild > 4f) { fAggressivenessEnemyChild = 4f; } m_EMFSM.AggressivenessEnemyChild = fAggressivenessEnemyChild; } else if (m_EMFSM.AvailableChildNum == 0 && m_EMFSM.AggressivenessEnemyChild > 0f) { m_EMFSM.AggressivenessEnemyChild = 0f; } // Reset Aggressiveness factor (Squad Captain) if (PlayerSquadFSM.Instance != null) { if (PlayerSquadFSM.Instance.IsAlive && m_EMFSM.AggressivenessSquadCap == 0f) { m_EMFSM.AggressivenessSquadCap = Random.Range(1f, 3f); } else if (!PlayerSquadFSM.Instance.IsAlive && m_EMFSM.CurrentAggressiveness > 0f) { m_EMFSM.AggressivenessSquadCap = 0f; } } if (PlayerSquadFSM.Instance != null) { // Reset Aggressiveness factor (Squad Child) if (PlayerSquadFSM.Instance.IsAlive && m_EMFSM.AggressivenessSquadChild != Mathf.Sqrt(Mathf.Sqrt((float)PlayerSquadFSM.Instance.AliveChildCount())) * 2.0f) { float fAggressivenessSquadChild = Mathf.Sqrt(Mathf.Sqrt((float)PlayerSquadFSM.Instance.AliveChildCount())) * 2.0f; if (fAggressivenessSquadChild > 4f) { fAggressivenessSquadChild = 4f; } m_EMFSM.AggressivenessSquadChild = fAggressivenessSquadChild; } else if (!PlayerSquadFSM.Instance.IsAlive && m_EMFSM.AggressivenessSquadChild > 0f) { m_EMFSM.AggressivenessSquadChild = 0f; } } // Update Aggressiveness if (m_EMFSM.CurrentAggressiveness != (m_EMFSM.InitialAggressiveness + m_EMFSM.AggressivenessEnemyChild + m_EMFSM.AggressivenessSquadCap + m_EMFSM.AggressivenessSquadChild) / m_EMFSM.AggressivenessDistance) { m_EMFSM.CurrentAggressiveness = (m_EMFSM.InitialAggressiveness + m_EMFSM.AggressivenessEnemyChild + m_EMFSM.AggressivenessSquadCap + m_EMFSM.AggressivenessSquadChild) / m_EMFSM.AggressivenessDistance; } // Make sure aggressiveness is not below 1 if (m_EMFSM.CurrentAggressiveness < 1.0f) { m_EMFSM.CurrentAggressiveness = 1.0f; } }
// Add enemy child cells to enemy main cell void AddChildCells(int num) { for (int i = 0; i < num; i++) { EMHelper.Instance().ECPool.SpawnFromPool(EMHelper.Instance().Position, true); } }
// Immediate transition to WinState void WinTransition() { if (EMHelper.Instance().IsEnemyWin&& m_EMFSM.CurrentStateIndex != EMState.Win) { m_EMFSM.ChangeState(EMState.Win); } }
private bool CheckGameEnd() { if (EnemyMainFSM.Instance() == null || PlayerMain.Instance == null || EMHelper.Instance() == null) { return(false); } if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.Die) { bPlayerWon = true; bGameIsOver = true; return(true); } else if (PlayerMain.Instance.IsAlive == false) { bPlayerWon = false; bGameIsOver = true; return(true); } else if (EMHelper.Instance().IsEnemyWin == true) { bPlayerWon = false; bGameIsOver = true; return(true); } return(false); }
public void CheckOtherOverlapADRange() { float YLimit = PlayerMain.Instance.transform.position.y + PlayerMain.Instance.m_fDetectionRadius; List <int> Keys = new List <int>(m_FPositionDatabase.Keys); float YAdjustment = 0f; float DistanceFromFirstToMain = EMHelper.Instance().Position.y - m_EMainBound.size.y / 2; Vector2 EMPosition = EMHelper.Instance().Position; foreach (int FIndex in Keys) { if (EMPosition.y + m_FPositionDatabase[FIndex].y <YLimit && YLimit - (EMPosition.y + m_FPositionDatabase[FIndex].y)> YAdjustment) { YAdjustment = YLimit - (EMPosition.y + m_FPositionDatabase[FIndex].y); } } if (YAdjustment == 0f) { return; } Vector2 UpdatedPosition = Vector2.zero; foreach (int FIndex in Keys) { if (m_FPositionDatabase[FIndex].y < YLimit) { UpdatedPosition = m_FPositionDatabase[FIndex]; UpdatedPosition.y += YAdjustment; m_FPositionDatabase[FIndex] = UpdatedPosition; } } }
// Checking whether the enemy main cell goes out of the screen or runs out of health // Make it invisible if it does private void LoseCheck() { if (transform.position.y - EMHelper.Instance().Radius / 2f > EMHelper.topLimit) { Visibility(false); bIsEnemyWin = true; } }
void Update() { // Update enemy main cell's position if (EMHelper.Instance() != null) { targetPosition = EMHelper.Instance().Position; } // Update seek weight if (fSeekWeight != .24f / Mathf.Sqrt(Mathf.Sqrt(Mathf.Sqrt(EMController.Instance().NutrientNum + 50f)))) { fSeekWeight = .24f / Mathf.Sqrt(Mathf.Sqrt(Mathf.Sqrt(EMController.Instance().NutrientNum + 50f))); } }
public Vector2 GetTargetFormationPosition(Formation _Formation, GameObject _EnemyCell) { if (!m_FIndexDatabase.ContainsKey(_EnemyCell)) { MessageDispatcher.Instance.DispatchMessage(_EnemyCell, _EnemyCell, MessageType.Idle, 0); return(Vector2.zero); } int TargetFIndex = m_FIndexDatabase[_EnemyCell]; Vector2 PosDifference = m_FPositionDatabase[TargetFIndex]; Vector2 EMPosition = EMHelper.Instance().Position; if (_Formation == Formation.Ladder) { return(new Vector2(PosDifference.x, EMPosition.y + PosDifference.y)); } return(new Vector2(EMPosition.x + PosDifference.x, EMPosition.y + PosDifference.y)); }
// Push back the enemy main cell when received attack IEnumerator ForceBack() { // One push at a time bCanPush = false; bPushed = true; // Push forward first if (EMHelper.Instance().Position.y > Constants.s_fEnemyMainMinY && !bStunned) { thisRB.velocity = pushForwardVel; } // Wait for 0.1 second yield return(new WaitForSeconds(.1f)); // Temporary velocity for enemy main cell when being pushed if (EMHelper.Instance().Position.y > Constants.s_fEnemyMainMinY && !bStunned) { thisRB.velocity = pushBackVel; } // Wait for 0.1 second yield return(new WaitForSeconds(.1f)); // Reduce damage count by 1 nDamageNum--; // Reset velocity if the enemy main cell is not stunned if (!bStunned) { ResetVelocity(); } bPushed = false; // Wait for 3 second yield return(new WaitForSeconds(3f)); // Set back the push status bCanPush = true; if (!bStunned) { nDamageNum = 0; } }
// Pause the spawning process so that there are not too many mini cells taking too much computing power at the same time IEnumerator PauseSpawn() { bCanSpawn = false; // Pause for random amount of time depending on the size of the agent yield return(new WaitForSeconds(Random.Range(Mathf.Sqrt(Mathf.Pow(nSize, 1f)), Mathf.Sqrt(Mathf.Pow(nSize, 3f))))); // Double check if the main nutrient is in the map and not too close to the enemy main cell if (MapManager.Instance.IsInBounds((Vector2)(position * 1.1f)) && Vector2.Distance(EMHelper.Instance().Position, transform.position) > fInitialRadius * nSize + EMHelper.Instance().Radius) { // Instantiate a mini nutrient object Instantiate(miniNutrient, position, Quaternion.identity); // Calling the Animate class for spawn animation Animate mAnimate; mAnimate = new Animate(this.transform); mAnimate.ExpandContract(0.1f, 1, 1.1f); // Reduce the size of the current main nutrient by 1 nSize--; } bCanSpawn = true; }
void Update() { // Destroy the nutrient if enemy main cell is invisible if (!EMHelper.Instance().IsEnemyVisible) { Destroy(this.gameObject); } // Absorb behaviour if (bIsAbsorbed) { Absorb(); } else { // A* pathfinding if (bCanFindPath) { // Set the target to the next node if (pathfindingManager.pathArray != null) { if (pathfindingManager.pathArray.Count > 1) { nextNode = (EnemyNutrientNode)pathfindingManager.pathArray [1]; currentNode = nextNode; } } else { nextNode = null; } // Move to the target node if (currentNode != null) { thisRB.velocity = (currentNode.position - (Vector2)this.gameObject.transform.position) * fSpeed; } } } }
void Update() { playerHurtTintCanvasGrp.alpha -= s_UIHurtTintFadeSpeed * Time.deltaTime; if (EMHelper.Instance().MinToMaxYRatio > 0.85f) { FlickerWarningTint(0.5f, 2 * s_UITintFlickerSpeed); } else if (EMHelper.Instance().MinToMaxYRatio > 0.7f) { FlickerWarningTint(0f, s_UITintFlickerSpeed); } else { enemyWarningTintCanvasGrp.alpha -= s_UIHurtTintFadeSpeed * Time.deltaTime; s_UITintFlickerAlpha = enemyWarningTintCanvasGrp.alpha; } if (PlayerSquadFSM.Instance == null) { return; } if (PlayerSquadFSM.Instance.bIsAlive == true) { if (spwnCptBtnGO.activeSelf == true) { spwnCptBtnGO.SetActive(false); } } else { if (spwnCptBtnGO.activeSelf == false) { spwnCptBtnGO.SetActive(true); } } if (m_bIsHoldingDownSpawnBtn) { m_fHoldTime += Time.deltaTime; if (m_fHoldTime > (m_fInitialHoldSpawnDelay + m_fHoldSpawnInterval)) { m_fHoldTime -= m_fHoldSpawnInterval; ActionSpawn((int)activeNode); // Update tutorial state if (Tutorial.Instance() != null && Tutorial.Instance().tutorialState == TutorialState.PlayerNodeHoldWaiting) { Tutorial.Instance().tutorialState = TutorialState.PlayerNodeHoldCompleted; } } } else { m_fHoldTime = 0f; } UpdateUI_nodeChildCountPos(); }
public override void Execute() { controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; // Produce enemy mini cell if has nutrient and can spawn if (controller.NutrientNum > 0 && helper.CanSpawn && Level_Manager.LevelID < 4) { helper.ECPool.SpawnFromPool(EMHelper.Instance().Position, false); } else if (controller.NutrientNum > 0 && helper.CanSpawn && Level_Manager.LevelID > 3) { for (int i = 0; i < 2; i++) { helper.ECPool.SpawnFromPool(EMHelper.Instance().Position, false); } } else if (controller.NutrientNum == 0 && EMTransition.Instance().CanTransit) { m_EMFSM.ChangeState(EMState.Maintain); } // Start checking transition only when there are more than 10 available enemy mini cells, transition is allowed and has nutrient if (m_EMFSM.AvailableChildNum > 10 && EMTransition.Instance().CanTransit&& controller.NutrientNum > 0) { // If there are more than 10 and less than 25 available enemy mini cells if (m_EMFSM.AvailableChildNum > 0 && m_EMFSM.AvailableChildNum <= 15) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // Transition to Defend if (EMHelper.Instance().MinToMaxYRatio > 0.5f) { EMTransition.Instance().Transition(1000f / (nPlayerChildFactor * 10f + EMLeraningAgent.Instance().RealScore(EMState.Defend)) * (1f + EMHelper.Instance().MinToMaxYRatio), EMState.Defend); } // Transition to EMAggressiveAttack if (nEnemyChildFactor > nPlayerChildFactor * 1.5f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nEnemyChildFactor * 1f / nPlayerChildFactor, 2f) * 3f) + m_EMFSM.CurrentAggressiveness * 3.5f + EMLeraningAgent.Instance().RealScore(EMState.AggressiveAttack)), EMState.AggressiveAttack); } // Transition to EMCautiousAttack if (nEnemyChildFactor > nPlayerChildFactor) { EMTransition.Instance().Transition(1000f / (helper.Pow(nEnemyChildFactor * 1.5f / nPlayerChildFactor, 2f) * 5f + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.CautiousAttack)), EMState.CautiousAttack); } // Transition to Landmine if (nPlayerChildFactor > 1.5f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nPlayerChildFactor, 2f) / helper.Sqrt(nPlayerChildFactor) * 1f) + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.Landmine)), EMState.Landmine); } // Transition to Maintain if (nPlayerChildFactor <= 5f && helper.Abs((nEnemyChildFactor - nPlayerChildFactor)) <= 1f) { EMTransition.Instance().Transition(1000f / (helper.Pow(5f - helper.Pow(nEnemyChildFactor - nPlayerChildFactor, 1.5f), 2f) + EMLeraningAgent.Instance().RealScore(EMState.Maintain)), EMState.Maintain); } } else if (m_EMFSM.AvailableChildNum > 15 && m_EMFSM.AvailableChildNum <= 30) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // Transition to Defend if (EMHelper.Instance().MinToMaxYRatio > 0.5f) { EMTransition.Instance().Transition(1000f / (nPlayerChildFactor * 10f + EMLeraningAgent.Instance().RealScore(EMState.Defend)) * (1f + EMHelper.Instance().MinToMaxYRatio), EMState.Defend); } // Transition to EMAggressiveAttack if (nEnemyChildFactor > nPlayerChildFactor * 1.5f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nEnemyChildFactor * 1.5f / nPlayerChildFactor, 2f) * 3f + m_EMFSM.CurrentAggressiveness * 3.75f) + EMLeraningAgent.Instance().RealScore(EMState.AggressiveAttack)), EMState.AggressiveAttack); } // Transition to EMCautiousAttack if (nEnemyChildFactor > nPlayerChildFactor) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nEnemyChildFactor * 2f / nPlayerChildFactor, 2f) * 5f + m_EMFSM.CurrentAggressiveness * 2.25f) + EMLeraningAgent.Instance().RealScore(EMState.CautiousAttack)), EMState.CautiousAttack); } // Transition to Landmine if (nPlayerChildFactor > 1f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nPlayerChildFactor, 2f) / helper.Sqrt(nPlayerChildFactor) * 1f) + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.Landmine)), EMState.Landmine); } // Transition to Maintain if (nPlayerChildFactor <= 5f && helper.Abs((nEnemyChildFactor - nPlayerChildFactor)) <= 1f) { EMTransition.Instance().Transition(1000f / (helper.Pow(5f - helper.Pow(nEnemyChildFactor - nPlayerChildFactor, 1.25f), 2f) + EMLeraningAgent.Instance().RealScore(EMState.Maintain)), EMState.Maintain); } } else if (m_EMFSM.AvailableChildNum > 30) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // Transition to EMAggressiveAttack if (nEnemyChildFactor > nPlayerChildFactor * 1.5f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nEnemyChildFactor * 1.75f / nPlayerChildFactor, 2f) * 5f + m_EMFSM.CurrentAggressiveness * 4f) + EMLeraningAgent.Instance().RealScore(EMState.AggressiveAttack)), EMState.AggressiveAttack); } // Transition to EMCautiousAttack if (nEnemyChildFactor > nPlayerChildFactor) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nEnemyChildFactor * 2f / nPlayerChildFactor, 2f) * 7.5f + m_EMFSM.CurrentAggressiveness * 2.5f) + EMLeraningAgent.Instance().RealScore(EMState.CautiousAttack)), EMState.CautiousAttack); } // Transition to Landmine if (nPlayerChildFactor > 1f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nPlayerChildFactor, 2f) / helper.Sqrt(nPlayerChildFactor) * 1f) + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.Landmine)), EMState.Landmine); } // Transition to Maintain if (nPlayerChildFactor <= 5f && helper.Abs((nEnemyChildFactor - nPlayerChildFactor)) <= 1f) { EMTransition.Instance().Transition(1000f / (helper.Pow(5f - helper.Pow(nEnemyChildFactor - nPlayerChildFactor, 1f), 2f) + EMLeraningAgent.Instance().RealScore(EMState.Maintain)), EMState.Maintain); } } // Check transition every 0.2 to 0.4 second to save computing power // With the value given by learning element increases, the transition check will be less frequent // Thus transition probability will decline if (EMTransition.Instance().CanTransit) { helper.StartPauseTransition(.2f * (1f + EnemyMainFSM.Instance().LearningDictionary[EMState.Production] / 100f)); } } }
void Update() { // Remove destroyed from the list if (AgentList != null) { AgentList.RemoveAll(item => item == null); } // Deactivate the nutrient if enemy main cell is invisible if (!EMHelper.Instance().IsEnemyVisible) { ActivateOrDeactivate(false); } // Deactivate the nutrient if it is empty if (nSize == 0) { ActivateOrDeactivate(false); } // Different behavior depends on whether the agent is sucked if (bSucked) { Sucking(); } else { // Reset acceleration Vector2 acceleration = Vector2.zero; // Modify acceleration based on the velocity returned by each behavior attached on the agent and its weight foreach (EMNutrientMainFlock behaviour in behaviours) { if (behaviour.enabled) { acceleration += behaviour.GetVelocity() * behaviour.FlockWeight; acceleration += behaviour.GetTargetVelocity() * behaviour.SeekWeight; } } // Lower acceleratin for heavier agent if (fMass != 0f) { currentVelocity += acceleration / Mathf.Sqrt(Mathf.Pow(fMass, 3)); } // Reduce based on the friction currentVelocity *= (1f - fFriction); if (currentVelocity.magnitude > fMaxVelocity) { currentVelocity = currentVelocity.normalized * fMaxVelocity; } if (currentVelocity.magnitude > 0f) { float angle = Mathf.Atan2(currentVelocity.y, currentVelocity.x) * Mathf.Rad2Deg; transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, angle); } if (currentVelocity.magnitude > 0f) { GetComponent <Rigidbody2D> ().velocity = currentVelocity; } } // Instantiate mini nutrient if (bCanSpawn && MapManager.Instance.IsInBounds((Vector2)(position * 1.5f)) && gameObject.activeSelf) { StartCoroutine(PauseSpawn()); } }
// Initial movement of enemy mini nutrient void InitialMovement() { thisRB.AddForce((EMHelper.Instance().Position - (Vector2)this.gameObject.transform.position) * Random.Range(5f, 10f)); }
public override void Execute() { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; // Start checking transition only when there are more than 10 available enemy mini cells and transition is allowed if (m_EMFSM.AvailableChildNum > 10 && transition.CanTransit) { // If there are more than 10 and less than 25 available enemy mini cells if (m_EMFSM.AvailableChildNum > 0 && m_EMFSM.AvailableChildNum <= 15) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // Transition to Defend if (EMHelper.Instance().MinToMaxYRatio > 0.5f) { transition.Transition(1000f / (nPlayerChildFactor * 10f + EMLeraningAgent.Instance().RealScore(EMState.Defend)) * (1f + EMHelper.Instance().MinToMaxYRatio), EMState.Defend); } // Transition to EMAggressiveAttack if (nEnemyChildFactor > nPlayerChildFactor * 1.5f) { transition.Transition(1000f / (helper.Pow(nEnemyChildFactor * 1f / nPlayerChildFactor, 2f) * 3f + m_EMFSM.CurrentAggressiveness * 3.5f + EMLeraningAgent.Instance().RealScore(EMState.AggressiveAttack)), EMState.AggressiveAttack); } // Transition to EMCautiousAttack if (nEnemyChildFactor > nPlayerChildFactor) { transition.Transition(1000f / (helper.Pow(nEnemyChildFactor * 1.5f / nPlayerChildFactor, 2f) * 5f + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.CautiousAttack)), EMState.CautiousAttack); } // Transition to Landmine if (nPlayerChildFactor > 1.5f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nPlayerChildFactor, 2f) / helper.Sqrt(nPlayerChildFactor) * 1f) + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.Landmine)), EMState.Landmine); } // Transition to Production if (controller.NutrientNum > 0) { transition.Transition(1000f / (helper.Pow(Mathf.Sqrt(controller.NutrientNum), 2f) + EMLeraningAgent.Instance().RealScore(EMState.Production)), EMState.Production); } } else if (m_EMFSM.AvailableChildNum > 15 && m_EMFSM.AvailableChildNum <= 30) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // Transition to Defend if (EMHelper.Instance().MinToMaxYRatio > 0.5f) { transition.Transition(1000f / (nPlayerChildFactor * 10f + EMLeraningAgent.Instance().RealScore(EMState.Defend)) * (1f + EMHelper.Instance().MinToMaxYRatio), EMState.Defend); } // Transition to EMAggressiveAttack if (nEnemyChildFactor > nPlayerChildFactor * 1.5f) { transition.Transition(1000f / ((helper.Pow(nEnemyChildFactor * 1.5f / nPlayerChildFactor, 2f) * 3f + m_EMFSM.CurrentAggressiveness * 3.75f) + EMLeraningAgent.Instance().RealScore(EMState.AggressiveAttack)), EMState.AggressiveAttack); } // Transition to EMCautiousAttack if (nEnemyChildFactor > nPlayerChildFactor) { transition.Transition(1000f / ((helper.Pow(nEnemyChildFactor * 2f / nPlayerChildFactor, 2f) * 5f + m_EMFSM.CurrentAggressiveness * 2.25f) + EMLeraningAgent.Instance().RealScore(EMState.CautiousAttack)), EMState.CautiousAttack); } // Transition to Landmine if (nPlayerChildFactor > 1f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nPlayerChildFactor, 2f) / helper.Sqrt(nPlayerChildFactor) * 1f) + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.Landmine)), EMState.Landmine); } // Transition to Production if (controller.NutrientNum > 0) { transition.Transition(1000f / (helper.Pow(Mathf.Sqrt(controller.NutrientNum), 2f) + EMLeraningAgent.Instance().RealScore(EMState.Production)), EMState.Production); } } else if (m_EMFSM.AvailableChildNum > 30) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // Transition to EMAggressiveAttack if (nEnemyChildFactor > nPlayerChildFactor * 1.5f) { transition.Transition(1000f / ((helper.Pow(nEnemyChildFactor * 1.75f / nPlayerChildFactor, 2f) * 5f + m_EMFSM.CurrentAggressiveness * 4f) + EMLeraningAgent.Instance().RealScore(EMState.AggressiveAttack)), EMState.AggressiveAttack); } // Transition to EMCautiousAttack if (nEnemyChildFactor > nPlayerChildFactor) { transition.Transition(1000f / ((helper.Pow(nEnemyChildFactor * 2f / nPlayerChildFactor, 2f) * 7.5f + m_EMFSM.CurrentAggressiveness * 2.5f) + EMLeraningAgent.Instance().RealScore(EMState.CautiousAttack)), EMState.CautiousAttack); } // Transition to Landmine if (nPlayerChildFactor > 1f) { EMTransition.Instance().Transition(1000f / ((helper.Pow(nPlayerChildFactor, 2f) / helper.Sqrt(nPlayerChildFactor) * 1f) + m_EMFSM.CurrentAggressiveness * 2f + EMLeraningAgent.Instance().RealScore(EMState.Landmine)), EMState.Landmine); } // Transition to Production if (controller.NutrientNum > 0) { transition.Transition(1000f / (helper.Pow(Mathf.Sqrt(controller.NutrientNum), 2f) + EMLeraningAgent.Instance().RealScore(EMState.Production)), EMState.Production); } } // Check transition every 0.1 second to save computing power if (transition.CanTransit) { helper.StartPauseTransition(.1f); } } }
public override void Execute() { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; #region Command child cells to transit to Defend state only when there are more player mini cells than enemy mini cells if (m_EMFSM.AvailableChildNum < PlayerChildFSM.GetActiveChildCount() && helper.CanAddDefend) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount() / 10f + 1f; // If there are more than 10 and less than 25 available enemy mini cells if (m_EMFSM.AvailableChildNum > 10 && m_EMFSM.AvailableChildNum <= 25 && helper.CanAddDefend) { for (int nAmount = 0; nAmount < Random.Range(5, 11); nAmount++) { int nIndex = Random.Range(0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.EnemyMainObject, m_EMFSM.ECList[nIndex].gameObject, MessageType.Defend, 0.0); helper.CanAddDefend = false; } } if (helper.CanAddDefend) { EMHelper.Instance().StartPauseAddDefend(0.25f); } } else if (m_EMFSM.AvailableChildNum > 25 && m_EMFSM.AvailableChildNum <= 50 && helper.CanAddDefend) { for (int nAmount = 0; nAmount < Random.Range(3, 9); nAmount++) { int nIndex = Random.Range(0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.EnemyMainObject, m_EMFSM.ECList[nIndex].gameObject, MessageType.Defend, 0.0); helper.CanAddDefend = false; } } } // Pause commanding enemy mini cells to defend state helper.StartPauseTransition(2f); } #endregion //Start checking transition only when transition is allowed if (transition.CanTransit) { // Transit to other states only when there are more player mini cells than enemy mini cells if (m_EMFSM.AvailableChildNum >= PlayerChildFSM.GetActiveChildCount()) { m_EMFSM.ChangeState(EMState.Production); } // If not transit to Production state, then transit to Maintain state m_EMFSM.ChangeState(EMState.Maintain); } // Check transition every 0.2 second to save computing power if (transition.CanTransit) { helper.StartPauseTransition(.2f); } }
// Die animation called in the Die state private void DieAnimation() { if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.Die) { if (nDieAniPhase == 1 && transform.localScale.x > initialScale.x / 2f) { bIsExpanding = false; bIsShrinking = true; currentScale.x -= fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(currentScale.x - initialScale.x / 2f)); currentScale.y -= fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(currentScale.y - initialScale.y / 2f)); } else if (nDieAniPhase == 1) { nDieAniPhase = 2; } if (nDieAniPhase == 2 && transform.localScale.x <= initialScale.x) { bIsExpanding = true; bIsShrinking = false; currentScale.x += fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(initialScale.x - currentScale.x)); currentScale.y += fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(initialScale.y - currentScale.y)); } else if (nDieAniPhase == 2) { nDieAniPhase = 3; } if (nDieAniPhase == 3 && transform.localScale.x > initialScale.x / 2.5f) { bIsExpanding = false; bIsShrinking = true; currentScale.x -= fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(currentScale.x - initialScale.x / 2f)); currentScale.y -= fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(currentScale.y - initialScale.y / 2f)); } else if (nDieAniPhase == 3) { nDieAniPhase = 4; } if (nDieAniPhase == 4 && transform.localScale.x <= initialScale.x / 1.25f) { bIsExpanding = true; bIsShrinking = false; currentScale.x += fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(initialScale.x / 1.25f - currentScale.x)); currentScale.y += fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(initialScale.y / 1.25f - currentScale.y)); } else if (nDieAniPhase == 4) { nDieAniPhase = 5; } if (nDieAniPhase == 5 && transform.localScale.x > initialScale.x / 20f) { bIsExpanding = false; bIsShrinking = true; currentScale.x -= fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(currentScale.x / 3f)); currentScale.y -= fDefaultExpandRate * Mathf.Sqrt(Mathf.Abs(currentScale.y / 3f)); } else if (nDieAniPhase == 5) { nDieAniPhase = 0; // Prevent the animation from showing again before the next death EMHelper.Instance().Visibility(false); // Make the enemy main cell invisible } transform.localScale = (Vector3)currentScale; } else { if (nDieAniPhase != 1) { nDieAniPhase = 1; // Reset the animation phase after exit Die state } } // Another set of code for color change if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.Die) { thisRend.material.color = dieColor * currentScale.x; } }