コード例 #1
0
 public Node(Node parent, float runningCost, HashSet <KeyValuePair <string, object> > state, AbstractGOAPAction action)
 {
     this.parent      = parent;
     this.runningCost = runningCost;
     this.state       = state;
     this.action      = action;
 }
コード例 #2
0
ファイル: Tank.cs プロジェクト: sondrex76/aitanks
    public bool moveAgent(AbstractGOAPAction nextAction)
    {


        if (path != null && path.Count > 0)
        {
            currentTargetNode = path[0];
            if (Vector3.SqrMagnitude(transform.position - currentTargetNode.transform.position) <= 1.0f)
            {
                path.RemoveAt(0);
            }

        }
        if (currentTargetNode != null)
        {
            transform.position = Vector3.MoveTowards(transform.position, new Vector3(currentTargetNode.transform.position.x, transform.position.y,
                currentTargetNode.transform.position.z), Time.deltaTime * 2);

            // Gets vector direction of movement, normalize and multiply it with speed after setting the y direction to 
            Vector3 posValue = currentTargetNode.transform.position - transform.position;

            // Move towards goal
            if (currentTargetNode != null && posValue.magnitude > minDist)
            {
                posValue.y = 0;

                Vector3 newDir = Vector3.RotateTowards(transform.forward, posValue, 0.1f, 0.0f);
                transform.rotation = Quaternion.LookRotation(newDir);
            }

        }
        // Sends a raytrace to check if the enemy tank is in view
        RaycastHit hit;
        bool tankVisible = !Physics.Linecast(transform.position, EnemyTank.transform.position, out hit, ~(1 << gameObject.layer));

        canSeeEnemy = tankVisible;
        Debug.DrawLine(transform.position, EnemyTank.transform.position, Color.green);
        Debug.DrawRay(barrelDirection.position, barrelDirection.forward * 100, Color.blue);    // DEBUG

        if (tankVisible || hit.collider.transform == EnemyTank.transform)
        {
            knownEnemyPosition = new Vector3(EnemyTank.transform.position.x, 0, EnemyTank.transform.position.z);
            // check if barrel points towards target
            RaycastHit barrelHit;
            // Only shoots if the enemy is in sights from the turret
            if (Physics.Linecast(transform.position, transform.position + barrelDirection.forward * 100, out barrelHit, (1 << gameObject.layer))) //|| barrelHit.collider.transform == EnemyTank.transform)
            {
                shoot.Fire();
            }

        }
     

        // Points the turrent towards the last known position of the tank as long as one such position is known
        if (knownEnemyPosition != null)
        {
            PointTurretAtTarget();
        }
        return true;
    }
コード例 #3
0
ファイル: AgentGOAP.cs プロジェクト: sondrex76/aitanks
    private void createPerformActionState()
    {
        performActionState = (fsm, obj) => {
            if (!hasActionPlan())
            {
                //Go find new plan and tell the agent it is finished
                fsm.popState();
                fsm.pushState(idleState);
                dataProvider.actionsFinished();
                return;
            }

            AbstractGOAPAction action = currentActions.Peek();
            if (action.isDone())
            {
                //Take action out of action queue
                currentActions.Dequeue();
            }

            if (hasActionPlan())
            {
                //Set action to the action on top of the queue
                action = currentActions.Peek();
                //Check if you need to be in range
                bool inRange = action.requiresInRange() ? action.isInRange() : true;


                if (inRange)
                {
                    //Check if we could perfrom action if not go to idle state and find a new plan
                    bool success = action.perform(obj);
                    if (!success)
                    {
                        fsm.popState();
                        fsm.pushState(idleState);
                        //ABORT
                        dataProvider.planAborted(action);
                    }
                }
                else
                {
                    fsm.pushState(moveToState);
                }
            }
            else
            {
                //I don't have a plan and need to find one
                fsm.popState();
                fsm.pushState(idleState);
                dataProvider.actionsFinished();
            }
        };
    }
コード例 #4
0
ファイル: AgentGOAP.cs プロジェクト: sondrex76/aitanks
    private void createMoveToState()
    {
        moveToState = (fsm, gameObject) => {
            AbstractGOAPAction action = currentActions.Peek();
            if (action.requiresInRange() && action.target == null)
            {
                fsm.popState();
                fsm.popState();
                fsm.pushState(idleState);
                return;
            }

            if (dataProvider.moveAgent(action))
            {
                fsm.popState();
            }
        };
    }
コード例 #5
0
    //Create a subset of the actions excluding the removeMe one. Creates a new set.
    protected HashSet <AbstractGOAPAction> actionSubset(HashSet <AbstractGOAPAction> actions, AbstractGOAPAction removeMe)
    {
        HashSet <AbstractGOAPAction> subset = new HashSet <AbstractGOAPAction>();

        foreach (AbstractGOAPAction a in actions)
        {
            if (!a.Equals(removeMe))
            {
                subset.Add(a);
            }
        }
        return(subset);
    }
コード例 #6
0
ファイル: AgentGOAP.cs プロジェクト: sondrex76/aitanks
 public void removeAction(AbstractGOAPAction action)
 {
     availableActions.Remove(action);
 }
コード例 #7
0
ファイル: AgentGOAP.cs プロジェクト: sondrex76/aitanks
 public void addAction(AbstractGOAPAction action)
 {
     availableActions.Add(action);
 }
コード例 #8
0
ファイル: Tank.cs プロジェクト: sondrex76/aitanks
 public void planAborted(AbstractGOAPAction aborter)
 {
     Debug.Log("Aborted current plan");
 }