예제 #1
0
    //void Update() {
    //    agent.nextPosition = transform.position;
    //}

    void FixedUpdate()
    {
        //Commands
        if (currentCommand == null)
        {
            NextCommand();
            return;
        }
        else
        {
            if (currentCommand.targetType == Command.targetTypes.Unit)
            {
                if (target == null)
                {
                    NextCommand();
                    return;
                }
                targetPoint = target.position;
                //check if target is moving and do path recalculation
                if (GameLogic.Instance.GetController(currentCommand.target).isMoving())
                {
                    RecalcPath();
                }
            }

            switch (currentCommand.commandType)   //do stuff every frame while having command
            {
            case Command.commandTypes.Stay:
                if (commandQueue.Count > 0)
                {
                    NextCommand();
                }
                break;

            case Command.commandTypes.AttackTarget:
                break;

            case Command.commandTypes.AttackMove:
                Vehicle newTarget;
                if (GetNearestObject(out newTarget, sightRange))
                {
                    Debug.Log("attackmove new target");
                    Command attackCommand = new Command(Command.commandTypes.AttackTarget, newTarget);
                    Command oldCommand    = new Command(currentCommand);
                    AddCommand(attackCommand, commandPriority.SetAsCurrent);
                    AddCommand(oldCommand, commandPriority.FrontOfQueue);
                }
                break;

            case Command.commandTypes.Move:
                break;

            case Command.commandTypes.Follow:
                if (commandQueue.Count > 0)
                {
                    NextCommand();
                }
                break;

            default:
                break;
            }
        }

        if (currentCommand.targetType == Command.targetTypes.Unit && (target == null || target == transform))
        {
            NextCommand();
        }

        moreInfo    = "";
        changeSpeed = Time.deltaTime * 5;

        if (targetPoint == noTarget)
        {
            switch (aiMovementMode)
            {
            case moveMode.Closest:
                Vehicle newTarget;
                GetNearestObject(out newTarget);
                target = newTarget.vehicleObject.transform;
                break;

            case moveMode.ToPlayer:
                target = GameLogic.Instance.player.transform;
                break;

            case moveMode.Command:
                break;

            default:
                break;
            }
            agent.enabled = false;
            //controller.vertical = -1;
        }

        if (targetPoint != noTarget)
        {
            //pathfinding stuff
            agent.enabled = true;
            if (!agent.hasPath && !agent.pathPending)
            {
                RecalcPath();
            }
            if (agent.path.corners.Length > 1)
            {
                nextNode = agent.path.corners[1];
            }
            else if (!targetReached && currentCommand.commandType != Command.commandTypes.Stay)
            {
                Debug.LogWarning("NO PATH");
            }

            //Stuck detection
            if (Vector3.Distance(oldPos, transform.position) > stuckRotThreshold)
            {
                moving = true;
            }
            else
            {
                moving = false;
            }
            if (!targetReached && currentCommand.commandType != Command.commandTypes.Stay)
            {
                float AngularVelY = rigidbody.angularVelocity.y;
                if (!moving && AngularVelY < stuckRotThreshold)
                {
                    stuckTimer += Time.deltaTime;
                    if (stuckTimer > stuckTime)
                    {
                        Debug.Log(gameObject.name + " was stuck");
                        stuckTimer    = 0;
                        agent.enabled = false;
                        return;
                    }
                }
                else
                {
                    stuckTimer = 0;
                }
            }



            oldPos = transform.position;
        }

        if (currentCommand.commandType != Command.commandTypes.Stay)
        {
            if (!facingNextNode && targetPoint != noTarget)
            {
                TurnToTarget();
            }

            if (facingNextNode && targetPoint != noTarget)
            {
                CheckRotation();
                MoveToTarget();
            }
        }

        if (targetReached)
        {
            if (currentCommand.commandType == Command.commandTypes.AttackTarget)
            {
                controller.vertical = -1;
            }
            else
            {
                NextCommand();
            }
        }
        if ((!targetReached || currentCommand.commandType == Command.commandTypes.Stay) && currentCommand.commandType != Command.commandTypes.AttackTarget)
        {
            Vehicle aggroVehicle = null;
            switch (aggroLevel)
            {
            case aggroLevels.Passive:
                break;

            case aggroLevels.Hold:
                if (aggroVehicle != null || GetNearestObject(out aggroVehicle, sightRange))
                {
                    aggroTarget = aggroVehicle.vehicleObject.transform;
                }
                break;

            case aggroLevels.Defensive:     //TODO: new command when receiving hit
                break;

            case aggroLevels.Aggressive:
                if (aggroVehicle != null || GetNearestObject(out aggroVehicle, sightRange))
                {
                    if (currentCommand.commandType == Command.commandTypes.Stay)
                    {
                        Command newcmd = new Command(Command.commandTypes.AttackTarget, aggroVehicle);
                        AddCommand(newcmd, commandPriority.SetAsCurrent);
                    }
                    else
                    {
                        aggroTarget = aggroVehicle.vehicleObject.transform;
                    }
                }

                break;

            default:
                break;
            }
        }

        if (aggroTarget != null)
        {
            float distance = Vector3.Distance(aggroTarget.position, transform.position);
            if (distance < sightRange && attackController.facingTarget && !attackController.attacking)
            {
                if (distance < attackRange)
                {
                    attackController.StartAttacking(aggroTarget);
                }
            }
            else if (distance < sightRange && !attackController.facingTarget)
            {
                attackController.StartTurning(aggroTarget);
            }
        }


        for (int i = 0; i < agent.path.corners.Length - 1; i++)
        {
            Debug.DrawLine(agent.path.corners[i], agent.path.corners[i + 1], Color.red);
        }



        agent.nextPosition = transform.position;

        moreInfo += "target reached: " + targetReached + " LOS to target: " + checkTargetLOS() + ", stucktimer:" + stuckTimer + ", path node count: " + agent.path.corners.Length;
    }