Exemplo n.º 1
0
    private void Update()
    {
        // Add the time since Update was last called to the timer.
        nextFire += Time.deltaTime;

        // If the timer exceeds the time between attacks, the player is in range and this enemy is alive...
        if (nextFire >= timeBetweenAttacks && playerInRange && thealth.currentHealth > 0)
        {
            // ... attack.
            Attack();
        }



        //make the enemy tank go through the waypoints
        if (motor.RotateTowards(waypoints[currentWaypoint].position, data.turnLeftSpeed)) //we are already in the right direction
        {
            //Do nothing!
        }
        else
        {
            //move forward
            motor.Move(data.moveForwardSpeed);
        }

        //how close are we to the waypoint
        if (Vector3.SqrMagnitude(waypoints[currentWaypoint].position - tf.position) < (data.closeEnough * data.closeEnough))
        {
            if (loopType == LoopType.Stop)
            {
                //advance
                if (currentWaypoint < waypoints.Length - 1)
                {
                    currentWaypoint++;
                }
            }
            else if (loopType == LoopType.Loop)
            {
                //advance
                if (currentWaypoint < waypoints.Length - 1) //if we are in range keep going
                {
                    currentWaypoint++;
                }
                else
                {
                    currentWaypoint = 0; // go to first waypoint
                }
            }
            else if (loopType == LoopType.PingPong)
            {
                if (isPatrolForward)
                {
                    //advance
                    if (currentWaypoint < waypoints.Length - 1)
                    {
                        currentWaypoint++;
                    }//keep going if we are in range
                    else
                    {
                        isPatrolForward = false;
                        currentWaypoint--;//reverse direction and go back one
                    }
                }
                else
                {
                    //keep going
                    if (currentWaypoint > 0)
                    {
                        currentWaypoint--;
                    }
                    else
                    {
                        //reverse direction, go to next waypoint
                        isPatrolForward = true;
                        currentWaypoint++;
                    }
                }
            }
        }
        if (aiState == AIState.Chase)
        {
            // do we avoid or do we chase
            if (avoidanceStage != 0)
            {
                DoAvoidance();
            }
            else
            {
                DoChase();
            }

            // what is our health status
            if (thealth.currentHealth < data.maxHealth * 0.5f)
            {
                ChangeState(AIState.CheckForFlee);
            }
            else if (Vector3.SqrMagnitude(target.position - tf.position) <= (data.aiSenseRadius * data.aiSenseRadius))
            {
                ChangeState(AIState.ChaseAndFire);
            }
        }
        else if (aiState == AIState.ChaseAndFire)
        {
            if (avoidanceStage != 0)
            {
                DoAvoidance();  //run away
            }
            else
            {
                DoChase();

                // can we shoot yet?
                if (Time.time > data.lastShootTime + data.fireRate)
                {
                    firing.Fire();
                    data.lastShootTime = Time.time;
                }
            }
            // Check for Transitions
            if (thealth.currentHealth < data.maxHealth * 0.5f)
            {
                ChangeState(AIState.CheckForFlee);
            }
            else if (Vector3.SqrMagnitude(target.position - tf.position) <= (data.aiSenseRadius * data.aiSenseRadius))
            {
                ChangeState(AIState.Chase);
            }
        }
        else if (aiState == AIState.Flee)
        {
            // Perform Behaviors
            if (avoidanceStage != 0)
            {
                DoAvoidance();
            }
            else
            {
                DoFlee();
            }

            // Check for Transitions
            if (Time.time >= data.stateEnterTime + 30)
            {
                ChangeState(AIState.CheckForFlee);
            }
        }
        else if (aiState == AIState.CheckForFlee)
        {
            // Perform Behaviors
            CheckForFlee();

            // Check for Transitions
            if (Vector3.SqrMagnitude(target.position - tf.position) <= (data.aiSenseRadius * data.aiSenseRadius))
            {
                ChangeState(AIState.Flee);
            }
            else
            {
                ChangeState(AIState.Rest);
            }
        }
        else if (aiState == AIState.Rest)
        {
            // Perform Behaviors
            DoRest();

            // Check for Transitions
            if (Vector3.SqrMagnitude(target.position - tf.position) <= (data.aiSenseRadius * data.aiSenseRadius))
            {
                ChangeState(AIState.Flee);
            }
            else if (thealth.currentHealth >= data.maxHealth)
            {
                ChangeState(AIState.Chase);
            }
        }
    }
Exemplo n.º 2
0
    private void Update()
    {
        switch (input)
        {
        case InputScheme.WASD:     //case for using WASD for player 1
            if (Input.GetKey(KeyCode.W))
            {
                motor.Move(data.moveForwardSpeed);
            }
            if (Input.GetKey(KeyCode.S))
            {
                motor.Move(-data.moveBackwardSpeed);
            }
            if (Input.GetKey(KeyCode.A))
            {
                motor.Rotate(-data.turnLeftSpeed);
            }
            if (Input.GetKey(KeyCode.D))
            {
                motor.Rotate(data.turnRightSpeed);
            }
            break;
        }

        switch (input)
        {
        case InputScheme.ArrowKeys:      //case for using arrows for player 2
            if (Input.GetKey(KeyCode.UpArrow))
            {
                motor.Move(data.moveForwardSpeed);
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                motor.Move(-data.moveBackwardSpeed);
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                motor.Rotate(data.turnRightSpeed);
            }
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                motor.Rotate(-data.turnLeftSpeed);
            }
            break;
        }

        // Track the current state of the fire button and make decisions based on the current launch force.

        if (Time.time > data.fireRate)
        {
            if (firing.currentLaunchForce >= data.maxLaunchForce && !firing.fired)
            {
                // at max xharge not yet fired
                firing.currentLaunchForce = data.maxLaunchForce;
                firing.Fire();
            }
            else if (Input.GetButtonDown("Fire1"))
            {
                //has the button been pressed for the first time
                firing.fired = false;
                firing.currentLaunchForce = data.minLaunchForce; //sets back to minimum
            }
            else if (Input.GetButton("Fire1") && !firing.fired)
            {
                //holding the button
                firing.currentLaunchForce += firing.chargeSpeed * Time.deltaTime;  //increases force accordingly
            }
            else if (Input.GetButtonUp("Fire1") && !firing.fired)
            {
                //release the button
                firing.Fire();
            }
            firing.nextFire = Time.time + data.fireRate;
        }

        if (data.playerNumber == 2) //continuous shooting for player 2
        {
            if (Time.time > data.fireRate)
            {
                firing.nextFire = Time.time + data.fireRate;
                firing.Fire();
            }
        }
    }