public static void LoadScene(int _sceneId)
    {
        Application.LoadLevel(2);

        // Call All Reset Statics
        PlayerChildFSM.ResetStatics();
        PlayerMain.ResetStatics();
        player_control.ResetStatics();
        GameManager.ResetStatics();
        EndGamePanel.ResetStatics();

        PlayerSquadFSM.ResetStatics();
        SquadChildFSM.ResetStatics();

        Wall.ResetStatics();
        WallRenderer.ResetStatics();
        Nutrients.ResetStatics();

        ECPoolManager.ResetStatics();
        ECIdleState.ResetStatics();
        DirectionDatabase.ResetStatics();
        FormationDatabase.ResetStatics();
        PathQuery.ResetStatics();
        PointDatabase.ResetStatics();
        PositionQuery.ResetStatics();
        ECTracker.ResetStatics();

        EnemyMainFSM.ResetStatics();

        Application.LoadLevel(_sceneId);
    }
Example #2
0
    public override void Enter()
    {
        // Update active Child Count.
        PlayerChildFSM.DecrementActiveChildCount();

        // Hide the object and reset all variables.
        m_pcFSM.rigidbody2D.isKinematic = true;
        m_pcFSM.collider2D.enabled      = false;
        m_pcFSM.spriteRen.enabled       = false;

        // Teleport far away.
        m_pcFSM.transform.position = Constants.s_farfarAwayVector;

        // Remove from NodeList if any assignedNode.
        if (m_pcFSM.m_assignedNode != null)
        {
            if (PlayerChildFSM.s_playerChildStatus[m_pcFSM.poolIndex] != pcStatus.Attacking)
            {
                m_pcFSM.m_assignedNode.RemoveChildFromNode(m_pcFSM.poolIndex);
                m_pcFSM.m_assignedNode = null;
            }
            else
            {
                PlayerChildFSM.s_playerChildStatus[m_pcFSM.poolIndex] = pcStatus.DeadState;
            }

            player_control.Instance.UpdateUI_nodeChildCountText();
        }
    }
    // Critic Tells the learning element how well the agent is doing with respect to a fixed performance measure
    private void Critic()
    {
        EMState currentStateEnum     = m_EMFSM.CurrentStateIndex;
        int     nCurrentEnemyChild   = m_EMFSM.AvailableChildNum;
        int     nCurrentPlayerChild  = PlayerChildFSM.GetActiveChildCount();
        int     nCurrentSquadChild   = 0;
        bool    bSquadCaptainIsAlive = false;

        if (PlayerSquadFSM.Instance != null)
        {
            nCurrentSquadChild   = PlayerSquadFSM.Instance.AliveChildCount();
            bSquadCaptainIsAlive = PlayerSquadFSM.Instance.IsAlive;
        }
        int nCurrentEnemyHealth  = m_EMFSM.Health;
        int nCurrentPlayerHealth = 0;

        if (PlayerMain.Instance != null)
        {
            nCurrentPlayerHealth = PlayerMain.Instance.Health;
        }
        // Pause calling the function for checking and wait for the result
        fCheckFreq = Random.Range(0.25f, .5f);
        StartCoroutine(PauseCheck(fCheckFreq, currentStateEnum, nCurrentEnemyChild, nCurrentPlayerChild, nCurrentSquadChild, bSquadCaptainIsAlive,
                                  nCurrentEnemyHealth, nCurrentPlayerHealth));
    }
Example #4
0
    public void ActionSpawn(int _nodeIndex)
    {
        Node _selectedNode = (Node)_nodeIndex;

        if (activeNode != Node.None && !m_bIsHoldingDownSpawnBtn)
        {
            return;
        }

        if (PlayerChildFSM.GetActiveChildCount() >= Constants.s_nPlayerMaxChildCount)
        {
            infoText.text = "Reached\nMaximum\nChild Cell\nCount";
            PresentInfoPanel();
            Node_Manager.GetNode(_selectedNode).CalculateChildCount();             // Force calcualtion check.
            return;
        }

        if (s_nResources >= Settings.s_nPlayerChildSpawnCost)
        {
            // Call a child cell from object pool and set its m_assignedNode to assigned node.
            PlayerChildFSM currentChild = PlayerChildFSM.Spawn(PlayerMain.Instance.transform.position + (Vector3)Random.insideUnitCircle * 0.25f);
            currentChild.m_assignedNode = Node_Manager.GetNode(_selectedNode);
            currentChild.m_bIsDefending = Node_Manager.GetNode(_selectedNode).m_bIsDefending;
            currentChild.m_assignedNode.AddChildToNode(currentChild.poolIndex);

            s_nResources -= Settings.s_nPlayerChildSpawnCost;
            UpdateUI_nutrients();
            UpdateUI_nodeChildCountText();

            // Animations
            PlayerMain.Instance.animate.ExpandContract(0.5f, 1, 1.2f, true, 0.2f);
            switch (_selectedNode)
            {
            case Node.LeftNode:
                leftNodeChildText.transform.localPosition = new Vector3(leftNodeChildText.transform.localPosition.x, childCountTextOriginY + childCountTextPopOffsetY);
                m_LeftNodeChildTextAnimate.ExpandContract(1.1f, 1, 4.0f, true, 0.4f);

                break;

            case Node.RightNode:
                rightNodeChildText.transform.localPosition = new Vector3(rightNodeChildText.transform.localPosition.x, childCountTextOriginY + childCountTextPopOffsetY);
                m_RightNodeChildTextAnimate.ExpandContract(1.1f, 1, 4.0f, true, 0.4f);
                break;
            }

            // Update tutorial state
            if (Tutorial.Instance() != null && Tutorial.Instance().tutorialState == TutorialState.PlayerNodeTapWaiting)
            {
                Tutorial.Instance().tutorialState = TutorialState.PlayerNodeTapCompleted;
            }

            AudioManager.PlayPMSoundEffect(PlayerMainSFX.SpawnCell);
        }
        else
        {
            infoText.text = "Not enough\nnutrients\n\nNeeded:\n" + Settings.s_nPlayerChildSpawnCost + " units";
            PresentInfoPanel();
        }
    }
Example #5
0
    public override void Exit()
    {
        // Update active Child Count.
        PlayerChildFSM.IncrementActiveChildCount();

        // Unhide the object, enabling the sprite and colliders, etc.
        m_pcFSM.rigidbody2D.isKinematic = false;
        m_pcFSM.collider2D.enabled      = true;
        m_pcFSM.spriteRen.enabled       = true;

        // Reset State.
        m_bIsCalledFromPool = false;
    }
Example #6
0
    public void ActionBurstShot(Node _selectedNode)
    {
        Node_Manager selectedNode = Node_Manager.GetNode(_selectedNode);

        if (selectedNode.activeChildCount < Settings.s_nPlayerActionBurstShotChildCost)
        {
            infoText.text = "Not enough\nchild cells\n\nNeeded:\n" + Settings.s_nPlayerActionBurstShotChildCost + " cells";
            PresentInfoPanel();
        }
        else
        {
            int[] childrenInNode = new int[] { -1 };
            switch (_selectedNode)
            {
            case Node.LeftNode: childrenInNode = PlayerChildFSM.childrenInLeftNode; break;

            case Node.RightNode: childrenInNode = PlayerChildFSM.childrenInRightNode; break;
            }

            PlayerChildFSM[] formationCells = new PlayerChildFSM[Settings.s_nPlayerActionBurstShotChildCost];
            int fcIndex = 0;

            // Assumes that there will be at least the required amount of cells
            for (int i = 0; i < childrenInNode.Length; i++)
            {
                int poolId = childrenInNode[i];

                formationCells[fcIndex] = PlayerChildFSM.playerChildPool[poolId];
                formationCells[fcIndex].m_formationCells = formationCells;                 // arrays are reference types.
                formationCells[fcIndex].attackMode       = PlayerAttackMode.BurstShot;

                formationCells[fcIndex].m_assignedNode.SendChildToAttack(poolId);
                formationCells[fcIndex].DeferredChangeState(PCState.ChargeMain);

                fcIndex++;
                if (fcIndex == formationCells.Length)
                {
                    break;
                }
            }

            infoText.text = "BurstShot";
            PresentInfoPanel();
        }

        UpdateUI_nodeChildCountText();
    }
    public override void Execute()
    {
        //Check for any nearby player attacking cells and obtain the closest attacking cell, store it in the "ClosestAttacker" Gameobject
        UpdateAttackerList();
        m_ClosestAttacker = GetClosestAttacker();
        if(m_ClosestAttacker.attackMode == PlayerAttackMode.SwarmTarget){MessageDispatcher.Instance.DispatchMessage(m_Child,m_Child,MessageType.Idle,0.0f);}

        //If there is no attackers nearby, increase the avoid timer. Once it reach a limit, transition the child cell back to idle state
        if(m_AttackersNearby.Count <= 0)
        {
            m_fAvoidTimer += Time.deltaTime;
        }

        if(m_fAvoidTimer >= m_fTimerLimit)
        {
            m_bReturnToMain = true;
        }
    }
Example #8
0
    void Awake()
    {
        // does the pool exist yet
        if (s_playerChildFSMPool == null)
        {
            // lazy initialize it
            s_playerChildFSMPool = new PlayerChildFSM[Constants.s_nPlayerMaxChildCount];
            s_playerChildStatus  = new pcStatus[Constants.s_nPlayerMaxChildCount];
            s_nPoolPointerIndex  = 0;
            s_nActiveChildCount  = 0;

            s_childrenInRightNode = new int[] { -1 };
            s_childrenInLeftNode  = new int[] { -1 };
            s_childrenInAttack    = new int[] { -1 };
        }
        // add myself
        s_playerChildFSMPool[s_nPoolPointerIndex] = this;
        s_playerChildStatus[s_nPoolPointerIndex]  = pcStatus.DeadState;
        m_nPoolIndex = s_nPoolPointerIndex;
        s_nPoolPointerIndex++;


        // Cache components
        rigidbody2D   = GetComponent <Rigidbody2D>();
        collider2D    = GetComponent <CircleCollider2D>();
        spriteRen     = GetComponent <SpriteRenderer>();
        m_AudioSource = GetComponent <AudioSource>();

        // Set up the m_statesDictionary.
        m_statesDictionary = new Dictionary <PCState, IPCState>();
        m_statesDictionary.Add(PCState.Avoid, new PC_AvoidState(this));
        m_statesDictionary.Add(PCState.ChargeChild, new PC_ChargeChildState(this));
        m_statesDictionary.Add(PCState.ChargeMain, new PC_ChargeMainState(this));
        m_statesDictionary.Add(PCState.Dead, new PC_DeadState(this));
        m_statesDictionary.Add(PCState.Defend, new PC_DefendState(this));
        m_statesDictionary.Add(PCState.Idle, new PC_IdleState(this));


        PlayerChildFSM.IncrementActiveChildCount();
        // Change to dead state.
        m_currentState = m_statesDictionary[PCState.Dead];
        m_currentState.Enter();
        m_currentEnumState = PCState.Dead;
    }
    public override void Execute()
    {
        //Check for any nearby player attacking cells and obtain the closest attacking cell, store it in the "ClosestAttacker" Gameobject
        UpdateAttackerList();
        m_ClosestAttacker = GetClosestAttacker();
        if (m_ClosestAttacker.attackMode == PlayerAttackMode.SwarmTarget)
        {
            MessageDispatcher.Instance.DispatchMessage(m_Child, m_Child, MessageType.Idle, 0.0f);
        }

        //If there is no attackers nearby, increase the avoid timer. Once it reach a limit, transition the child cell back to idle state
        if (m_AttackersNearby.Count <= 0)
        {
            m_fAvoidTimer += Time.deltaTime;
        }

        if (m_fAvoidTimer >= m_fTimerLimit)
        {
            m_bReturnToMain = true;
        }
    }
Example #10
0
    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);
    }
Example #11
0
    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);
            }
        }
    }
Example #12
0
    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));
            }
        }
    }
Example #13
0
 // Constructor.
 public PC_DeadState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
    public void ActionBurstShot(Node _selectedNode)
    {
        Node_Manager selectedNode = Node_Manager.GetNode(_selectedNode);

        if (selectedNode.activeChildCount < Settings.s_nPlayerActionBurstShotChildCost)
        {
            infoText.text = "Not enough\nchild cells\n\nNeeded:\n" + Settings.s_nPlayerActionBurstShotChildCost + " cells";
            PresentInfoPanel();
        }
        else
        {
            int[] childrenInNode = new int[] {-1};
            switch (_selectedNode)
            {
            case Node.LeftNode: childrenInNode = PlayerChildFSM.childrenInLeftNode; break;
            case Node.RightNode: childrenInNode = PlayerChildFSM.childrenInRightNode; break;
            }

            PlayerChildFSM[] formationCells = new PlayerChildFSM[Settings.s_nPlayerActionBurstShotChildCost];
            int fcIndex = 0;

            // Assumes that there will be at least the required amount of cells
            for (int i = 0; i < childrenInNode.Length; i++)
            {
                int poolId = childrenInNode[i];

                formationCells[fcIndex] = PlayerChildFSM.playerChildPool[poolId];
                formationCells[fcIndex].m_formationCells = formationCells; // arrays are reference types.
                formationCells[fcIndex].attackMode = PlayerAttackMode.BurstShot;

                formationCells[fcIndex].m_assignedNode.SendChildToAttack(poolId);
                formationCells[fcIndex].DeferredChangeState(PCState.ChargeMain);

                fcIndex++;
                if (fcIndex == formationCells.Length)
                    break;
            }

            infoText.text = "BurstShot";
            PresentInfoPanel();
        }

        UpdateUI_nodeChildCountText();
    }
Example #15
0
 // Constructor.
 public PC_IdleState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
Example #16
0
 // Constructor.
 public PC_AvoidState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
Example #17
0
 // Constructor.
 public PC_IdleState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
    // Check only once within a given perio of time
    IEnumerator PauseCheck(float checkFreq, EMState pastState, int pastEnemyChild, int pastPlayerChild, int pastSquadChild, bool squadCaptainWasAlive,
                           int pastEnemyHealth, int pastPlayerHealth)
    {
        bCanStartCheck = false;                 // One check at a time
        yield return(new WaitForSeconds(checkFreq));

        // If we are still in the same state then proceed
        if (EMLeraningAgent.instance != null && PlayerChildFSM.playerChildPool != null && PlayerSquadFSM.Instance != null)
        {
            if (pastState == m_EMFSM.CurrentStateIndex)
            {
                LearningElement(pastState, pastEnemyChild, m_EMFSM.AvailableChildNum, pastPlayerChild, PlayerChildFSM.GetActiveChildCount(),
                                pastSquadChild, PlayerSquadFSM.Instance.AliveChildCount(), squadCaptainWasAlive, PlayerSquadFSM.Instance.IsAlive,
                                pastEnemyHealth, pastPlayerHealth);
            }
        }

        bCanStartCheck = true;                  //Reset the checking availability
    }
 // Constructor.
 public PC_ChargeChildState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
Example #20
0
    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
    }
Example #21
0
    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);
        }
    }
 // Constructor.
 public PC_DefendState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
Example #23
0
 void FixedUpdate()
 {
     PlayerChildFSM.UpdateChildrenStatusArrays();
     m_surroundingEnemyCells = Physics2D.OverlapCircleAll(transform.position, m_fDetectionRadius, Constants.s_onlyEnemeyChildLayer);
 }
Example #24
0
    // Color blink animation in AggresiveAttack, CautiousAttack, Landmine and defend states
    private void ColorBlink()
    {
        // Reset elapsed time when blink animation is disabled
        if (!bCanBlink && fBlinkElapsedTime != 0.0f)
        {
            fBlinkElapsedTime = 0.0f;
        }

        // Update blink animation
        if (bCanBlink)
        {
            fBlinkElapsedTime += Time.deltaTime;
            if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.AggressiveAttack)
            {
                if (fBlinkElapsedTime >= 0.8f / Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().CurrentAggressiveness)))
                {
                    if (thisRend.material.color != aggressiveColor)
                    {
                        thisRend.material.color = aggressiveColor;
                    }
                    else
                    {
                        thisRend.material.color = defaultColor;
                    }

                    fBlinkElapsedTime = 0.0f;
                }
            }
            else if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.CautiousAttack)
            {
                if (fBlinkElapsedTime >= 1.2f / Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().CurrentAggressiveness)))
                {
                    if (thisRend.material.color != cautiousColor)
                    {
                        thisRend.material.color = cautiousColor;
                    }
                    else
                    {
                        thisRend.material.color = defaultColor;
                    }

                    fBlinkElapsedTime = 0.0f;
                }
            }
            else if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.Landmine)
            {
                if (fBlinkElapsedTime >= 1.2f / Mathf.Sqrt(Mathf.Sqrt(EnemyMainFSM.Instance().CurrentAggressiveness)))
                {
                    if (thisRend.material.color != landmineColor)
                    {
                        thisRend.material.color = landmineColor;
                    }
                    else
                    {
                        thisRend.material.color = defaultColor;
                    }

                    fBlinkElapsedTime = 0.0f;
                }
            }
            else if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.Defend)
            {
                if (fBlinkElapsedTime >= 1.2f /
                    (Mathf.Sqrt(Mathf.Sqrt(PlayerChildFSM.GetActiveChildCount() + PlayerSquadFSM.Instance.AliveChildCount() + 4f) / 2.0f)))
                {
                    if (thisRend.material.color != defendColor)
                    {
                        thisRend.material.color = defendColor;
                    }
                    else
                    {
                        thisRend.material.color = defaultColor;
                    }

                    fBlinkElapsedTime = 0.0f;
                }
            }
        }
        // Update color in stun state
        else if (EnemyMainFSM.Instance().CurrentStateIndex == EMState.Stunned)
        {
            if (thisRend.material.color != stunColor)
            {
                thisRend.material.color = stunColor;
            }
        }
        else
        {
            thisRend.material.color = defaultColor;
        }
    }
 // Constructor.
 public PC_ChargeChildState(PlayerChildFSM pcFSM)
 {
     m_pcFSM = pcFSM;
 }
Example #26
0
    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
    }