Example #1
0
    private void CreateMoveToState(GOAPComponent aic)
    {
        aic.moveToState = (fsm, gameObject) =>
        {
            //aic.AnnounceMoveToState();

            GOAPAction action = aic.currentActions.Peek();
            if (action.requiresInRange() && action.target == null)
            {
                fsm.popState();
                fsm.popState();
                fsm.pushState(aic.idleState);
                return;
            }

            if (aic.IsAgentInRange(action))
            {
                fsm.popState();
            }
            else
            {
                if (aic.IsActionInterrupted(action))
                {
                    fsm.popState();
                    fsm.pushState(aic.idleState);
                }
                aic.gameObject.GetComponent <NavMeshAgent>()?.SetDestination(action.target.transform.position);
            }
        };
    }
Example #2
0
    private void CreateIdleState(GOAPComponent aic)
    {
        aic.idleState = (fsm, obj) =>
        {
            //aic.AnnounceIdleState();

            HashSet <KeyValuePair <string, object> > worldState = worldStateComponent.worldState;
            HashSet <KeyValuePair <string, object> > goal       = aic.CreateGoalState();

            Queue <GOAPAction> plan = planner.plan(aic.gameObject, aic.availableActions, worldState, goal);

            /*int index = 1;
             * foreach (GOAPAction a in aic.availableActions)
             * {
             *  Debug.Log("action no: " + index + " " + a);
             *  index++;
             * }*/

            if (plan != null)
            {
                aic.currentActions = plan;
                aic.PlanFound(goal, plan);

                fsm.popState();
                fsm.pushState(aic.performActionState);
            }
            else
            {
                aic.PlanFailed(goal);
                fsm.popState();
                fsm.pushState(aic.idleState);
            }
        };
    }
Example #3
0
    public override void Initialize(Transform[] objects)
    {
        worldStateComponent = GetComponentInChildren <WorldStateComponent>();

        // list because I don't know size here
        List <Filter> tmpFilters = new List <Filter>();
        int           index      = 0;

        for (int i = 0; i < objects.Length; i++)
        {
            // check performance
            EnemyComponent  ec  = objects[i].GetComponent <EnemyComponent>();
            CombatComponent cc  = objects[i].GetComponent <CombatComponent>();
            HealthComponent hc  = objects[i].GetComponent <HealthComponent>();
            GOAPComponent   aic = objects[i].GetComponent <GOAPComponent>();

            if (cc && hc && ec && aic)
            {
                tmpFilters.Add(new Filter(index, objects[i].gameObject, ec, cc, hc, aic));

                aic.stateMachine     = new FSM();
                aic.availableActions = new HashSet <GOAPAction>();
                aic.currentActions   = new Queue <GOAPAction>();

                GOAPAction[] actions = aic.gameObject.GetComponents <GOAPAction>();
                foreach (GOAPAction action in actions)
                {
                    aic.availableActions.Add(action);
                    action.constantTarget = worldStateComponent.gameObject;
                }

                CreateIdleState(aic);
                CreateMoveToState(aic);
                CreatePerformActionState(aic);
                aic.stateMachine.pushState(aic.idleState);
            }
        }

        filters = tmpFilters.ToArray();

        planner = new GOAPPlanner();
        //currentWorldState = new HashSet<KeyValuePair<string, object>>();

        KeyValuePair <string, object> playerDmgState   = new KeyValuePair <string, object>("damagePlayer", false);
        KeyValuePair <string, object> playerStalkState = new KeyValuePair <string, object>("stalkPlayer", false);

        worldStateComponent.worldState.Add(playerLitState);
        worldStateComponent.worldState.Add(playerDmgState);
        worldStateComponent.worldState.Add(playerStalkState);

        /*currentWorldState.Add(playerLitState);
         * currentWorldState.Add(playerDmgState);
         * currentWorldState.Add(playerStalkState);*/
    }
Example #4
0
    private void CreatePerformActionState(GOAPComponent aic)
    {
        aic.performActionState = (fsm, obj) =>
        {
            //aic.AnnouncePerformActionState();

            if (!aic.HasActionPlan())
            {
                fsm.popState();
                fsm.pushState(aic.idleState);
                aic.ActionsFinished();
                return;
            }

            GOAPAction action = aic.currentActions.Peek();
            if (action.isDone())
            {
                aic.currentActions.Dequeue();
            }

            if (aic.HasActionPlan())
            {
                action = aic.currentActions.Peek();
                bool inRange = action.requiresInRange() ? action.isInRange() : true;

                if (inRange)
                {
                    bool success = action.perform(obj);
                    if (!success)
                    {
                        fsm.popState();
                        fsm.pushState(aic.idleState);
                        CreateIdleState(aic);
                        aic.PlanAborted(action);
                    }
                }
                else
                {
                    fsm.pushState(aic.moveToState);
                }
            }
            else
            {
                fsm.popState();
                fsm.pushState(aic.idleState);
                aic.ActionsFinished();
            }
        };
    }
Example #5
0
    public override bool perform(GameObject agent)
    {
        GOAPComponent gc = agent.GetComponent <GOAPComponent>();

        if (gc)
        {
            if (gc.CanAttack(attackCost))
            {
                hasAttacked = true;
                agent.GetComponent <GOAPComponent>()?.ConsumeEnergy(cost);
                target.GetComponent <HealthComponent>()?.TakeGranularDamageOverTime(damage * 50f, 1f, true);
            }
        }
        return(hasAttacked);
    }
Example #6
0
        public Filter(int id,
                      GameObject go,
                      EnemyComponent ec,
                      CombatComponent cc,
                      HealthComponent hc,
                      GOAPComponent aic
                      )
        {
            this.id = id;

            gameObject      = go;
            enemyComponent  = ec;
            combatComponent = cc;
            healthComponent = hc;
            aiComponent     = aic;
        }
Example #7
0
    public override void Tick(float deltaTime)
    {
        //ToggleLightState();

        for (int i = 0; i < filters.Length; i++)
        {
            Filter filter = filters[i];

            EnemyComponent  enemyComp  = filter.enemyComponent;
            CombatComponent combComp   = filter.combatComponent;
            HealthComponent healthComp = filter.healthComponent;
            GOAPComponent   aiComp     = filter.aiComponent;

            aiComp.stateMachine.Update(aiComp.gameObject);

            if (!aiComp.HasMaxEnergy())
            {
                aiComp.RegenerateEnergy(deltaTime);
            }
        }
    }