// Used to handle everything happens when spawns a child cell public IEnumerator ProduceChild() { if (bCanSpawn) { bCanSpawn = false; // Calling the Animate class for spawn animation Animate mAnimate; mAnimate = new Animate(this.transform); mAnimate.ExpandContract(0.1f, 1, 1.1f); if (Level_Manager.LevelID < 4) { EMController.Instance().ReduceNutrient(); } // Randomize the interval time between spawns of child cells in terms of current difficulty float intervalTime = UnityEngine.Random.Range( 1.25f / EMDifficulty.Instance().CurrentDiff, 1.75f / EMDifficulty.Instance().CurrentDiff ); if (Level_Manager.LevelID > 2) { intervalTime /= 1.2f; } yield return(new WaitForSeconds(intervalTime)); if (m_EMFSM.AvailableChildNum < 100) { bCanSpawn = true; } } }
public EMAggressiveAttackState(EnemyMainFSM EMFSM) { m_EMFSM = EMFSM; transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; }
void Start() { if (instance == null) { instance = this; } // Initialization of difficulty and factors fHealthDiff = 1f; fHealthWeight = 1f; fNutrientDiff = 1f; fNutrientWeight = 1f; fLevelDiff = 1f; fLevelWeight = 1f; fCurrentDiff = 1f; fMaxHealthInfluence = .5f; fMaxNutrientInfluence = .5f; // Initialization of current health and num of nutrient if (EnemyMainFSM.Instance().Health != 0) { nCurrentHealth = EnemyMainFSM.Instance().Health; } if (EMController.Instance().NutrientNum != 0) { nCurrentNutrient = EMController.Instance().NutrientNum; } }
//Constructor public ECIdleState(GameObject _childCell, EnemyChildFSM _ecFSM) { m_Child = _childCell; m_ecFSM = _ecFSM; m_Main = m_ecFSM.m_EMain; m_EMTransform = m_Main.transform; m_fMaxMagnitude = 7.5f; m_fTimerLimit = 1.5f; m_fSpreadRange = m_Child.GetComponent <SpriteRenderer>().bounds.size.x / 10; m_EMControl = m_Main.GetComponent <EMController>(); m_EMBounds = m_Main.GetComponent <SpriteRenderer>().bounds; m_EMFSM = m_Main.GetComponent <EnemyMainFSM>(); m_ChildRB = m_Child.GetComponent <Rigidbody2D>(); m_MainRB = m_Main.GetComponent <Rigidbody2D>(); m_Collisions = new Collider2D[m_EMFSM.ECList.Count]; m_Neighbours = new List <GameObject>(); m_ecFSM.rigidbody2D.drag = 0f; m_nIdleCount = 0; m_CurrentIdleState = IdleStatus.None; m_ShrinkRate = new Vector3(0.025f, -0.025f, 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 EMAggressiveAttackState (EnemyMainFSM EMFSM) { m_EMFSM = EMFSM; transition = m_EMFSM.emTransition; controller = m_EMFSM.emController; helper = m_EMFSM.emHelper; }
//Constructor public ECIdleState(GameObject _childCell, EnemyChildFSM _ecFSM) { m_Child = _childCell; m_ecFSM = _ecFSM; m_Main = m_ecFSM.m_EMain; m_EMTransform = m_Main.transform; m_fMaxMagnitude = 7.5f; m_fTimerLimit = 1.5f; m_fSpreadRange = m_Child.GetComponent<SpriteRenderer>().bounds.size.x/10; m_EMControl = m_Main.GetComponent<EMController>(); m_EMBounds = m_Main.GetComponent<SpriteRenderer>().bounds; m_EMFSM = m_Main.GetComponent<EnemyMainFSM>(); m_ChildRB = m_Child.GetComponent<Rigidbody2D>(); m_MainRB = m_Main.GetComponent<Rigidbody2D>(); m_Collisions = new Collider2D[m_EMFSM.ECList.Count]; m_Neighbours = new List<GameObject>(); m_ecFSM.rigidbody2D.drag = 0f; m_nIdleCount = 0; m_CurrentIdleState = IdleStatus.None; m_ShrinkRate = new Vector3(0.025f,-0.025f,0f); }
void OnCollisionEnter2D(Collision2D col) { // Ignore collisions if is being scrificed for squad captain. if (m_bSacrificeToSquadCpt == true) { return; } // Hit Enemy Child if (col.gameObject.tag == Constants.s_strEnemyChildTag) { // Kill Enemy. col.gameObject.GetComponent <EnemyChildFSM>().KillChildCell(); // Kill Self. KillPlayerChildCell(); AudioManager.PlayPMSoundEffect(PlayerMainSFX.KillChild); } // Hit Enemy Main. else if (col.gameObject.tag == Constants.s_strEnemyTag) { // Damage Enemy. EMController.Instance().CauseDamageOne(); MainCamera.CameraShake(); KillPlayerChildCell(); } }
// 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); } }
// Update nutrient factor void NutrientDiffUpdate() { // Max fNutrientDiff = 1f + fMaxNutrientInfluence if (EMController.Instance().NutrientNum != 0) { fNutrientDiff = 1f + fMaxNutrientInfluence / Mathf.Sqrt(Mathf.Sqrt((float)nCurrentNutrient) * 2f) / Mathf.Sqrt(Mathf.Sqrt((2f))); } }
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 Start() { if (instance == null) { instance = this; } m_EMFSM = GetComponent <EnemyMainFSM> (); controller = GetComponent <EMController> (); bCanTransit = true; }
public override void Enter() { // Reset transition availability EMTransition.Instance().CanTransit = true; // Reset spawn availability // Pause the transition for randomized time based on num of available nutrient float fPauseTime = Random.Range(Mathf.Sqrt(Mathf.Sqrt(EMController.Instance().NutrientNum + 1f) * 2f) / EMDifficulty.Instance().CurrentDiff, Mathf.Sqrt(Mathf.Sqrt(EMController.Instance().NutrientNum + 1f) * 10f) / EMDifficulty.Instance().CurrentDiff); helper.StartPauseTransition(fPauseTime); }
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))); } }
void Start() { // Initialization fFlockWeight = 1f; // Number behind makes sure the seek weight is so small that the nutrient never mve away from the main cell fSeekWeight = .24f / Mathf.Sqrt(Mathf.Sqrt(Mathf.Sqrt((float)EMController.Instance().NutrientNum + 50f))); fNeighbourRadius = 1f; fAlignmentWeight = .6f; fCohesionWeigth = .6f; fSeperationWeight = .8f; // Add this behavior to the agent agent = GetComponent <EMNutrientMainAgent>(); agent.AddBehaviour(this); }
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; }
// Make sure the enemy main cell do not go outside the screen void PositionLimit() { if (transform.position.x <= EMHelper.leftLimit + width || transform.position.x >= EMHelper.rightLimit - width) { // Make sure the enemy main cell is cannot go out of the camera if (transform.position.x < EMHelper.leftLimit + width) { transform.position = new Vector2(EMHelper.leftLimit + width, transform.position.y); } else if (transform.position.x > EMHelper.rightLimit - width) { transform.position = new Vector2(EMHelper.rightLimit - width, transform.position.y); } EMController.Instance().ChangeDirection(); } if (transform.position.y < Constants.s_fEnemyMainMinY) { transform.position = new Vector2(transform.position.x, Constants.s_fEnemyMainMinY); } }
void Update() { #region Update of health and num of nutrient, and difficulty factors if (EnemyMainFSM.Instance() != null) { // Health Update if (nCurrentHealth != EnemyMainFSM.Instance().Health) { nCurrentHealth = EnemyMainFSM.Instance().Health; } // Nutrient Update if (nCurrentNutrient != EMController.Instance().NutrientNum) { nCurrentNutrient = EMController.Instance().NutrientNum; } HealthDiffUpdate(); NutrientDiffUpdate(); LevelDiffUpdate(); } #endregion // Update current difficulty CurrentDiffUpdate(); }
// void Start() { //Initialize the variables and data structure m_fRotationTarget = Random.Range(0f, 360f); m_RandomRotateSpeed = new Vector3(0f, 0f, 0.85f); m_bRotateCW = false; m_bRotateACW = false; m_PMain = GameObject.Find("Player_Cell"); m_EMain = GameObject.Find("Enemy_Cell"); m_EMFSM = m_EMain.GetComponent <EnemyMainFSM>(); m_EMControl = m_EMain.GetComponent <EMController>(); m_Rigidbody2D = GetComponent <Rigidbody2D>(); m_ChildTransform = transform; m_ChargeTarget = null; m_StatesDictionary = new Dictionary <ECState, IECState>(); m_AudioSource = GetComponent <AudioSource>(); //Initialize the various states for the enemy child cell and added them into the dictionary m_StatesDictionary.Add(ECState.Idle, new ECIdleState(this.gameObject, this)); m_StatesDictionary.Add(ECState.Defend, new ECDefendState(this.gameObject, this)); m_StatesDictionary.Add(ECState.Avoid, new ECAvoidState(this.gameObject, this)); m_StatesDictionary.Add(ECState.Attack, new ECAttackState(this.gameObject, this)); m_StatesDictionary.Add(ECState.ChargeMain, new ECChargeMState(this.gameObject, this)); m_StatesDictionary.Add(ECState.ChargeChild, new ECChargeCState(this.gameObject, this)); m_StatesDictionary.Add(ECState.TrickAttack, new ECTrickAttackState(this.gameObject, this)); m_StatesDictionary.Add(ECState.Landmine, new ECMineState(this.gameObject, this)); m_StatesDictionary.Add(ECState.Dead, new ECDeadState(this.gameObject, this)); //initialize the current state for the enemy child cell m_CurrentState = m_StatesDictionary[ECState.Dead]; m_CurrentEnum = ECState.Dead; m_CurrentCommand = MessageType.Empty; CurrentState.Enter(); }
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; 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 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, 3 + (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(3, 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); } } } // Pause commanding enemy mini cells to Attack state float fPauseTime = 2f / 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 static void ResetStatics() { instance = null; }
void Awake() { if (instance == null) instance = this; // GetComponent m_EMFSM = GetComponent<EnemyMainFSM> (); thisRB = GetComponent<Rigidbody2D> (); PMain = GameObject.Find("Player_Cell").GetComponent<PlayerMain>(); // Size nInitialNutrientNum = Settings.s_nEnemyMainInitialNutrientNum; nCurrentNutrientNum = nInitialNutrientNum; // Speed fSpeed = Settings.s_fEnemyMainInitialVertSpeed; fSpeedFactor = 1f; fSpeedTemp = fSpeed; bIsDefend = false; fDefendFactor = 0.9f; // Initialization of horizontal movement // Randomize direction int nDirection = Random.Range (0, 2); if (nDirection == 0) bMovingLeft = true; else bMovingLeft = false; // Horizontal speed fMinHoriSpeed = Settings.s_fEnemyMainMinHiriSpeed; fHoriSpeed = Random.Range (fMinHoriSpeed, fMinHoriSpeed * 2f); bCanChangeHori = true; // Velocity velocity = new Vector2 (fHoriSpeed, fSpeed * fSpeedFactor); thisRB.velocity = velocity; pushBackVel = new Vector2 (0.0f, -5.0f); pushForwardVel = new Vector2 (0.0f, 1.0f); stunVel = new Vector2 (0.0f, -0.5f); // Damage nDamageNum = 0; // State bPushed = false; bStunned = false; bCanPush = true; bCanStun = true; bJustAttacked = false; fAttackElapsedTime = 0.1f; fDefaultStunTime = Settings.s_fDefaultStunTime; fCurrentStunTime = fDefaultStunTime; fStunCoolDown = fDefaultStunTime * 2.0f; fDefaultStunTolerance = Settings.s_fDefaultStunTolerance; fCurrentStunTolerance = fDefaultStunTolerance; bIsMainBeingAttacked = false; bIsAllChildWithinMain = false; fNumOfDefaultCells = Settings.s_nEnemyMainInitialChildCellNum; }
// void Start() { //Initialize the variables and data structure m_fRotationTarget = Random.Range(0f,360f); m_RandomRotateSpeed = new Vector3(0f,0f,0.85f); m_bRotateCW = false; m_bRotateACW = false; m_PMain = GameObject.Find("Player_Cell"); m_EMain = GameObject.Find("Enemy_Cell"); m_EMFSM = m_EMain.GetComponent<EnemyMainFSM>(); m_EMControl = m_EMain.GetComponent<EMController>(); m_Rigidbody2D = GetComponent<Rigidbody2D>(); m_ChildTransform = transform; m_ChargeTarget = null; m_StatesDictionary = new Dictionary<ECState,IECState>(); m_AudioSource = GetComponent<AudioSource>(); //Initialize the various states for the enemy child cell and added them into the dictionary m_StatesDictionary.Add(ECState.Idle, new ECIdleState(this.gameObject,this)); m_StatesDictionary.Add(ECState.Defend, new ECDefendState(this.gameObject,this)); m_StatesDictionary.Add(ECState.Avoid, new ECAvoidState(this.gameObject,this)); m_StatesDictionary.Add(ECState.Attack, new ECAttackState(this.gameObject,this)); m_StatesDictionary.Add(ECState.ChargeMain, new ECChargeMState(this.gameObject,this)); m_StatesDictionary.Add(ECState.ChargeChild, new ECChargeCState(this.gameObject,this)); m_StatesDictionary.Add(ECState.TrickAttack, new ECTrickAttackState(this.gameObject,this)); m_StatesDictionary.Add(ECState.Landmine, new ECMineState(this.gameObject,this)); m_StatesDictionary.Add(ECState.Dead, new ECDeadState(this.gameObject,this)); //initialize the current state for the enemy child cell m_CurrentState = m_StatesDictionary[ECState.Dead]; m_CurrentEnum = ECState.Dead; m_CurrentCommand = MessageType.Empty; CurrentState.Enter(); }
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 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 static void ResetStatics() { instance = null; }
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() { if (instance == null) instance = this; m_EMFSM = GetComponent<EnemyMainFSM> (); controller = GetComponent<EMController> (); bCanTransit = true; }
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; 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 }
void Awake() { if (instance == null) { instance = this; } // GetComponent m_EMFSM = GetComponent <EnemyMainFSM> (); thisRB = GetComponent <Rigidbody2D> (); PMain = GameObject.Find("Player_Cell").GetComponent <PlayerMain>(); // Size nInitialNutrientNum = Settings.s_nEnemyMainInitialNutrientNum; nCurrentNutrientNum = nInitialNutrientNum; // Speed fSpeed = Settings.s_fEnemyMainInitialVertSpeed; fSpeedFactor = 1f; fSpeedTemp = fSpeed; bIsDefend = false; fDefendFactor = 0.9f; // Initialization of horizontal movement // Randomize direction int nDirection = Random.Range(0, 2); if (nDirection == 0) { bMovingLeft = true; } else { bMovingLeft = false; } // Horizontal speed fMinHoriSpeed = Settings.s_fEnemyMainMinHiriSpeed; fHoriSpeed = Random.Range(fMinHoriSpeed, fMinHoriSpeed * 2f); bCanChangeHori = true; // Velocity velocity = new Vector2(fHoriSpeed, fSpeed * fSpeedFactor); thisRB.velocity = velocity; pushBackVel = new Vector2(0.0f, -5.0f); pushForwardVel = new Vector2(0.0f, 1.0f); stunVel = new Vector2(0.0f, -0.5f); // Damage nDamageNum = 0; // State bPushed = false; bStunned = false; bCanPush = true; bCanStun = true; bJustAttacked = false; fAttackElapsedTime = 0.1f; fDefaultStunTime = Settings.s_fDefaultStunTime; fCurrentStunTime = fDefaultStunTime; fStunCoolDown = fDefaultStunTime * 2.0f; fDefaultStunTolerance = Settings.s_fDefaultStunTolerance; fCurrentStunTolerance = fDefaultStunTolerance; bIsMainBeingAttacked = false; bIsAllChildWithinMain = false; fNumOfDefaultCells = Settings.s_nEnemyMainInitialChildCellNum; }
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 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)); } }