public EMAggressiveAttackState (EnemyMainFSM EMFSM) { m_EMFSM = EMFSM; transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; }
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; } }
public override void Execute () { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; #region Attack only when there are more enemy mini cells than player's if (m_EMFSM.AvailableChildNum > PlayerChildFSM.GetActiveChildCount () && helper.CanAddAttack) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount () / 10f + 1f; if (m_EMFSM.AvailableChildNum > 0 && m_EMFSM.AvailableChildNum <= 15) { for (int nAmount = 0; nAmount < Random.Range (1, 2 + (int)Mathf.Sqrt(nEnemyChildFactor)); nAmount++) { int nIndex = Random.Range (0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Defend || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.gameObject,m_EMFSM.ECList[nIndex].gameObject,MessageType.Attack,0.0); } } } else if (m_EMFSM.AvailableChildNum > 15 && m_EMFSM.AvailableChildNum <= 30) { for (int nAmount = 0; nAmount < Random.Range (2, 4 + (int)Mathf.Sqrt(nEnemyChildFactor)); nAmount++) { int nIndex = Random.Range (0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Defend || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.gameObject,m_EMFSM.ECList[nIndex].gameObject,MessageType.Attack,0.0); } } } else if (m_EMFSM.AvailableChildNum > 30) { for (int nAmount = 0; nAmount < Random.Range (4, 6 + (int)Mathf.Sqrt(nEnemyChildFactor)); nAmount++) { int nIndex = Random.Range (0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Defend || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.gameObject,m_EMFSM.ECList[nIndex].gameObject,MessageType.Attack,0.0); } } } // Pause commanding enemy mini cells to Attack state float fPauseTime = 1.5f / EMDifficulty.Instance().CurrentDiff; if (fPauseTime > 0f) helper.StartPauseAddAttack (fPauseTime); } #endregion #region Transition if (transition.CanTransit && controller.NutrientNum > 0) m_EMFSM.ChangeState (EMState.Production); else if (transition.CanTransit && controller.NutrientNum == 0) m_EMFSM.ChangeState (EMState.Maintain); #endregion }
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; } } }
// 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); } }
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); }
// 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); } }
// Immediate transition to WinState void WinTransition() { if (EMHelper.Instance().IsEnemyWin&& m_EMFSM.CurrentStateIndex != EMState.Win) { m_EMFSM.ChangeState(EMState.Win); } }
public override void Enter() { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; // Turn blink animation on EMAnimation.Instance().CanBlink = true; // Reset availability to command mini cell to Landmine state if (!helper.CanAddLandmine) { helper.CanAddLandmine = true; } // Enable expand animation if (!EMAnimation.Instance().IsExpanding) { EMAnimation.Instance().IsExpanding = true; } // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time float fPauseTime = Random.Range((m_EMFSM.CurrentAggressiveness / 1.5f) / EMDifficulty.Instance().CurrentDiff, (m_EMFSM.CurrentAggressiveness) / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition(fPauseTime); }
public EMAggressiveAttackState(EnemyMainFSM EMFSM) { m_EMFSM = EMFSM; transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; }
//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); }
void Start() { if (instance == null) { instance = this; } // GetComponent m_EMFSM = GetComponent <EnemyMainFSM> (); width = GetComponent <CircleCollider2D> ().bounds.size.x; fRadius = GetComponent <CircleCollider2D> ().bounds.size.x; position = transform.position; // Find gameObject ECPool = GameObject.Find("Enemy Child Cell Pool").GetComponent <ECPoolManager>(); // Initialise status bIsEnemyVisible = true; bIsEnemyWin = false; bCanSpawn = true; // Able to command child cells to any state by default bCanAddDefend = true; bCanAddAttack = true; bCanAddLandmine = true; }
// 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); } }
// 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; } }
public override void Execute() { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; // If the enemy main cell is not stunned any more, transit to Production State if (!controller.Stunned) { m_EMFSM.ChangeState (EMState.Production); } }
public override void Execute() { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; // If the enemy main cell is not stunned any more, transit to Production State if (!controller.Stunned) { m_EMFSM.ChangeState(EMState.Production); } }
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 override void Enter() { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; transition = m_EMFSM.emTransition; // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time based on num of available child cells float fPauseTime = Random.Range (Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().AvailableChildNum) * 10f) / EMDifficulty.Instance().CurrentDiff, Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().AvailableChildNum) * 50f) / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition (fPauseTime); }
public override void Enter() { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; transition = m_EMFSM.emTransition; // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time based on num of available child cells float fPauseTime = Random.Range(Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().AvailableChildNum) * 10f) / EMDifficulty.Instance().CurrentDiff, Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().AvailableChildNum) * 50f) / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition(fPauseTime); }
public override void Exit() { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; // Reset animation status if (EMAnimation.Instance().CanBlink) { EMAnimation.Instance().CanBlink = false; } // Reset availability to command mini cell to Attack state if (!helper.CanAddAttack) { helper.CanAddAttack = true; } // Reset transition availability transition.CanTransit = true; }
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)); }
public override void Enter () { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; // Turn blink animation on EMAnimation.Instance ().CanBlink = true; // Reset availability to command mini cell to Attack state if (!helper.CanAddAttack) helper.CanAddAttack = true; // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time float fPauseTime = Random.Range (Mathf.Sqrt (m_EMFSM.CurrentAggressiveness) * 2.5f/ EMDifficulty.Instance().CurrentDiff, Mathf.Sqrt (m_EMFSM.CurrentAggressiveness) * 3f / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition (fPauseTime); }
// 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; }
public override void Exit() { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; // Reset animation status if (EMAnimation.Instance().CanBlink) { EMAnimation.Instance().CanBlink = false; } // Reset availability to command mini cell to Defend state if (!helper.CanAddDefend) { helper.CanAddDefend = true; } // Set status to not defending controller.bIsDefend = false; // Reset transition availability transition.CanTransit = true; }
public override void Enter() { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; // Turn blink animation on EMAnimation.Instance().CanBlink = true; // Reset availability to command mini cell to Attack state if (!helper.CanAddAttack) { helper.CanAddAttack = true; } // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time float fPauseTime = Random.Range(Mathf.Sqrt(m_EMFSM.CurrentAggressiveness) * 1.5f / EMDifficulty.Instance().CurrentDiff, Mathf.Sqrt(m_EMFSM.CurrentAggressiveness) * 2.5f / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition(fPauseTime); }
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; } } } }
public override void Enter() { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; // Turn blink animation on EMAnimation.Instance ().CanBlink = true; // Reset availability to command mini cell to Landmine state if (!helper.CanAddLandmine) helper.CanAddLandmine = true; // Enable expand animation if (!EMAnimation.Instance ().IsExpanding) EMAnimation.Instance ().IsExpanding = true; // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time float fPauseTime = Random.Range ((m_EMFSM.CurrentAggressiveness / 1.5f) / EMDifficulty.Instance().CurrentDiff, (m_EMFSM.CurrentAggressiveness) / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition (fPauseTime); }
public override void Enter() { transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; // Turn blink animation on EMAnimation.Instance().CanBlink = true; // Reset availability to command mini cell to Defend state if (!helper.CanAddDefend) { helper.CanAddDefend = true; } // Set status to defending controller.bIsDefend = true; // Reset transition availability transition.CanTransit = true; // Pause the transition for randomized time float fPauseTime = Random.Range(Mathf.Sqrt(Mathf.Sqrt((float)PlayerChildFSM.GetActiveChildCount() + 4f)) * 2f, Mathf.Sqrt((float)PlayerChildFSM.GetActiveChildCount() + 4f) * 2f); helper.StartPauseTransition(fPauseTime); }
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; // 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); } }
void Start() { enemyMainObject = this.gameObject; #region Initialize state dictionary m_statesDictionary = new Dictionary<EMState, IEMState>(); m_statesDictionary.Add (EMState.Production, new EMProductionState (this)); m_statesDictionary.Add (EMState.Maintain, new EMMaintainState (this)); m_statesDictionary.Add (EMState.Defend, new EMDefendState (this)); m_statesDictionary.Add (EMState.AggressiveAttack, new EMAggressiveAttackState (this)); m_statesDictionary.Add (EMState.CautiousAttack, new EMCautiousAttackState (this)); m_statesDictionary.Add (EMState.Landmine, new EMLandmineState (this)); m_statesDictionary.Add (EMState.Stunned, new EMStunnedState (this)); m_statesDictionary.Add (EMState.Die, new EMDieState (this)); m_statesDictionary.Add (EMState.Win, new EMWinState (this)); #endregion #region Initialize the Learning Element dictionary m_learningDictionary = new Dictionary<EMState, float>(); m_learningDictionary.Add (EMState.Production, 0f); m_learningDictionary.Add (EMState.Maintain, 0f); m_learningDictionary.Add (EMState.Defend, 0f); m_learningDictionary.Add (EMState.AggressiveAttack, 0f); m_learningDictionary.Add (EMState.CautiousAttack, 0f); m_learningDictionary.Add (EMState.Landmine, 0f); m_learningDictionary.Add (EMState.Stunned, 0f); m_learningDictionary.Add (EMState.Die, 0f); m_learningDictionary.Add (EMState.Win, 0f); #endregion // Initialize the default to Production m_CurrentState = m_statesDictionary [EMState.Production]; m_currentStateIndex = EMState.Production; // Get the enemy main controller, helper class and transition class emController = GetComponent<EMController> (); emHelper = GetComponent<EMHelper> (); emTransition = GetComponent<EMTransition> (); //Initialize and refresh the point database for Enemy Child Cells PointDatabase.Instance.InitializeDatabase(); PointDatabase.Instance.RefreshDatabase(); FormationDatabase.Instance.RefreshDatabases(ECList); // Initialise the enemy child list /* EnemyChildFSM[] ecClasses = (EnemyChildFSM[])GameObject.FindObjectsOfType (typeof(EnemyChildFSM)); foreach (EnemyChildFSM ecClass in ecClasses) { if (ecClass.CurrentStateEnum != ECState.Dead) ecList.Add (ecClass); } */ // ecList = GameObject.FindGameObjectsWithTag("EnemyChild").Select(gameObject => gameObject.GetComponent<EnemyChildFSM>()).ToList(); // Count the number of child cells in list /* for (int i = 0; i < ecList.Count; i++) { if(ecList[i].CurrentStateEnum != ECState.Dead) nAvailableChildNum++; } */ }
public override void Exit () { transition = m_EMFSM.emTransition; helper = m_EMFSM.emHelper; // Reset animation status if (EMAnimation.Instance ().CanBlink) EMAnimation.Instance ().CanBlink = false; // Reset availability to command mini cell to Attack state if (!helper.CanAddAttack) helper.CanAddAttack = true; // Reset transition availability transition.CanTransit = true; }
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(); }
void Start() { enemyMainObject = this.gameObject; #region Initialize state dictionary m_statesDictionary = new Dictionary <EMState, IEMState>(); m_statesDictionary.Add(EMState.Production, new EMProductionState(this)); m_statesDictionary.Add(EMState.Maintain, new EMMaintainState(this)); m_statesDictionary.Add(EMState.Defend, new EMDefendState(this)); m_statesDictionary.Add(EMState.AggressiveAttack, new EMAggressiveAttackState(this)); m_statesDictionary.Add(EMState.CautiousAttack, new EMCautiousAttackState(this)); m_statesDictionary.Add(EMState.Landmine, new EMLandmineState(this)); m_statesDictionary.Add(EMState.Stunned, new EMStunnedState(this)); m_statesDictionary.Add(EMState.Die, new EMDieState(this)); m_statesDictionary.Add(EMState.Win, new EMWinState(this)); #endregion #region Initialize the Learning Element dictionary m_learningDictionary = new Dictionary <EMState, float>(); m_learningDictionary.Add(EMState.Production, 0f); m_learningDictionary.Add(EMState.Maintain, 0f); m_learningDictionary.Add(EMState.Defend, 0f); m_learningDictionary.Add(EMState.AggressiveAttack, 0f); m_learningDictionary.Add(EMState.CautiousAttack, 0f); m_learningDictionary.Add(EMState.Landmine, 0f); m_learningDictionary.Add(EMState.Stunned, 0f); m_learningDictionary.Add(EMState.Die, 0f); m_learningDictionary.Add(EMState.Win, 0f); #endregion // Initialize the default to Production m_CurrentState = m_statesDictionary [EMState.Production]; m_currentStateIndex = EMState.Production; // Get the enemy main controller, helper class and transition class emController = GetComponent <EMController> (); emHelper = GetComponent <EMHelper> (); emTransition = GetComponent <EMTransition> (); //Initialize and refresh the point database for Enemy Child Cells PointDatabase.Instance.InitializeDatabase(); PointDatabase.Instance.RefreshDatabase(); FormationDatabase.Instance.RefreshDatabases(ECList); // Initialise the enemy child list /* * EnemyChildFSM[] ecClasses = (EnemyChildFSM[])GameObject.FindObjectsOfType (typeof(EnemyChildFSM)); * foreach (EnemyChildFSM ecClass in ecClasses) * { * if (ecClass.CurrentStateEnum != ECState.Dead) * ecList.Add (ecClass); * } */ // ecList = GameObject.FindGameObjectsWithTag("EnemyChild").Select(gameObject => gameObject.GetComponent<EnemyChildFSM>()).ToList(); // Count the number of child cells in list /* * for (int i = 0; i < ecList.Count; i++) { * if(ecList[i].CurrentStateEnum != ECState.Dead) * nAvailableChildNum++; * } */ }
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()); } }
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)); } } }
// 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; int nCommandNum = 0; #region Landmine Behaviour if (helper.CanAddLandmine) { float nEnemyChildFactor = (float)m_EMFSM.AvailableChildNum / 10f + 1f; float nPlayerChildFactor = (float)PlayerChildFSM.GetActiveChildCount () / 10f + 1f; if (m_EMFSM.AvailableChildNum > 0 && m_EMFSM.AvailableChildNum <= 15) { nCommandNum = Random.Range (1, 2 + (int)nPlayerChildFactor); for (int nAmount = 0; nAmount < nCommandNum; nAmount++) { int nIndex = Random.Range (0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Defend || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.EnemyMainObject,m_EMFSM.ECList[nIndex].gameObject,MessageType.Landmine,0.0); helper.CanAddLandmine = false; } } } else if (m_EMFSM.AvailableChildNum > 15 && m_EMFSM.AvailableChildNum <= 30) { nCommandNum = Random.Range (2, 3 + (int)nPlayerChildFactor); for (int nAmount = 0; nAmount < nCommandNum; nAmount++) { int nIndex = Random.Range (0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Defend || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.EnemyMainObject,m_EMFSM.ECList[nIndex].gameObject,MessageType.Landmine,0.0); helper.CanAddLandmine = false; } } } else if (m_EMFSM.AvailableChildNum > 30) { nCommandNum = Random.Range (3, 4 + (int)nPlayerChildFactor); for (int nAmount = 0; nAmount < nCommandNum; nAmount++) { int nIndex = Random.Range (0, m_EMFSM.ECList.Count); if (m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Idle || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Defend || m_EMFSM.ECList[nIndex].CurrentStateEnum == ECState.Avoid) { MessageDispatcher.Instance.DispatchMessage(m_EMFSM.EnemyMainObject,m_EMFSM.ECList[nIndex].gameObject,MessageType.Landmine,0.0); helper.CanAddLandmine = false; } } } // Pause commanding enemy mini cells to Attack state // Pause duration depends only on the number of enemy mini cell commanded //helper.StartPauseAddLandmine ((float)nCommandNum * 2f); } #endregion #region Transition if (transition.CanTransit && controller.NutrientNum > 0) m_EMFSM.ChangeState (EMState.Production); else if (transition.CanTransit && controller.NutrientNum == 0) m_EMFSM.ChangeState (EMState.Maintain); #endregion }
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); } }
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)); } }