// Updates the behaviour
    public override BehaviourResult UpdateBehaviour(AgentActor agent)
    {
        Vector3 agentPos = agent.gameObject.transform.position;

        //distanceDetection = agent.maxSpeed / 2f;
        //Debug.Log("agent Pos: " + agentPos);
        if ((agentPos - m_waypoints[m_currentWaypointNumber]).sqrMagnitude <= distanceDetection && m_currentWaypointNumber < m_waypoints.Count - 1)
        {
            m_currentWaypointNumber++;
            m_currentTarget = m_waypoints[m_currentWaypointNumber];
            //Debug.Log("Current Target: " + m_waypoints[m_currentWaypointNumber]);
        }
        else if (m_currentWaypointNumber >= m_waypoints.Count - 1)
        {
            if (m_castle != null)
            {
                m_castle.castleTouched = false;
            }
            m_currentWaypointNumber = 0;
            return(BehaviourResult.FAILURE);
        }


        m_seekForce.SetTarget(m_waypoints[m_currentWaypointNumber]);
        agent.AddForce(m_seekForce.GetForce(agent));

        return(BehaviourResult.SUCCESS);
    }
    // Updates the behaviour
    public override BehaviourResult UpdateBehaviour(AgentActor agent)
    {
        if (agent.m_ballInteractTime <= 0)
        {
            initialised    = false;
            m_ball.touched = false;
            Debug.Log("Suppose to disengage");
            return(BehaviourResult.FAILURE);
        }

        m_seekForce.SetTarget(m_ball.gameObject.transform.position);
        agent.AddForce(m_seekForce.GetForce(agent));


        //currentTarget = -(m_ball.gameObject.transform.position - m_middlePoint).normalized * m_waypointDistance;
        //
        //Vector3 agentPos = agent.gameObject.transform.position;
        //
        //if (m_waypointTarget) {
        //	m_ball.gameObject.GetComponent<Collider> ().enabled = false;
        //	if ((agent.transform.position - currentTarget).sqrMagnitude < 100f)
        //	{
        //		m_waypointTarget = false;
        //		m_ball.gameObject.GetComponent<Collider> ().enabled = true;
        //	}
        //
        //	Debug.Log ("Waypoint Target");
        //
        //	m_seekForce.SetTarget(currentTarget);
        //	agent.AddForce(m_seekForce.GetForce(agent));
        //
        //}
        //
        //if (!m_waypointTarget)
        //{
        //	m_seekForce.SetTarget(m_ball.gameObject.transform.position);
        //	agent.AddForce(m_seekForce.GetForce(agent));
        //
        //	Debug.Log ("Ball Target");
        //
        //	if ((m_ball.gameObject.transform.position - m_middlePoint).sqrMagnitude < 10f) {
        //
        //		initialised = false;
        //		m_ball.touched = false;
        //		return BehaviourResult.FAILURE;
        //	}
        //}



//
//        if ((m_ball.transform.position - m_middlePoint).sqrMagnitude > m_radius)
//        {
//        }
//        else
//        {
//            Bop(agent);
//        }
        return(BehaviourResult.SUCCESS);
    }
    //Vector3 m_currentTarget;

    // Update is called once per frame
    void FixedUpdate()
    {
        UpdateBehaviours();
        speed  = m_body.velocity.magnitude;
        player = GameObject.FindGameObjectWithTag("Player Hand");
        if (player != null)
        {
            HandTransform = player.transform.position;
            m_seekForce.SetTarget(player.transform.position);
        }
        if (!m_castleScript.castleTouched)
        {
            m_body.AddForce(transform.forward * m_moveFowardForce);
        }
        else
        {
            m_body.AddForce(transform.forward * (m_moveFowardForce / 2));
        }
    }
Esempio n. 4
0
    // Updates the behaviour
    public override BehaviourResult UpdateBehaviour(AgentActor agent)
    {
        Vector3 agentPos = agent.gameObject.transform.position;

        if ((agentPos - m_currentTarget).sqrMagnitude <= nearEnough)
        {
            Vector2 randomVector = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f));
            randomVector    = randomVector.normalized * Random.Range(0, m_maxRadius) + m_middlePoint;
            m_prevTarget    = m_currentTarget;
            m_aimPrev       = true;
            m_currentTarget = new Vector3(randomVector.x, randomVector.y, Random.Range(-m_maxDepth, m_maxDepth));
        }

        m_seekForce.SetTarget(m_currentTarget);
        agent.AddForce(m_seekForce.GetForce(agent));
        //Debug.Log ("Wander Current Target: " + m_currentTarget);

        //if (m_aimPrev)
        //{
        //    m_rotationalForce.SetTarget(m_prevTarget);
        //    agent.AddForce(m_rotationalForce.GetForce(agent));
        //    if (Vector3.Dot(agent.transform.forward, (agentPos - m_currentTarget)) > 0.8f)
        //        m_aimPrev = false;
        //}
        //else
        //{
        //    m_seekForce.SetTarget(m_currentTarget);
        //    agent.AddForce(m_seekForce.GetForce(agent));
        //    if (m_arrivalForce != null)
        //    {
        //        m_arrivalForce.SetTarget(m_currentTarget);
        //        agent.AddForce(m_arrivalForce.GetForce(agent));
        //    }
        //}


        //foreach (SteeringForce force in m_forces)
        //    agent.AddForce(force.GetForce(agent));

        return(BehaviourResult.SUCCESS);
    }
    // Use for initialising the companion
    void Start()
    {
        // Turns off companion hack script, change the emission texture of the companion and make it moveable
        CompanionHackScript companionHack = gameObject.GetComponent <CompanionHackScript>();

        companionHack.enabled = false;
        GetComponent <Renderer>().material.SetTexture("_EmissionMap", companionTexture);
        GetComponent <Rigidbody>().isKinematic = false;


        // Initialise behaviour list
        m_behaviours = new List <IBehaviour>();

        // Find the player game object
        player       = FindObjectOfType <FPSController>().gameObject;
        playerScript = player.GetComponent <FPSController>();

        //-----------------------------------------------------------------
        // The Flee Sequence

        // Set up the flee force and flee force parameter
        m_fleeForce = new HorizontalFleeForce();
        m_fleeForce.SetTarget(player /*.transform.position + new Vector3 (2, 2, 2)*/);

        // Set up the flee behaviour
        fleeBehaviour = new SteeringBehaviour();
        fleeBehaviour.Constructor();
        fleeBehaviour.AddNewForce(m_fleeForce);

        // Set up condition for flee sequence
        WithinRange fleeCondition = new WithinRange();

        fleeCondition.SetParameters(player, 2.0f);

        // Set up flee sequence
        Sequence fleeSequence = new Sequence();

        fleeSequence.addBehaviour(fleeCondition);
        fleeSequence.addBehaviour(fleeBehaviour);



        //-----------------------------------------------------------------
        // The Chase Sequence

        // Set up the seek force and seek force parameter
        m_seekForce = new SeekForce();
        m_seekForce.SetTarget(player /*.transform.position + new Vector3 (2, 2, 2)*/);

        // Set up the seek behaviour
        seekBehaviour = new SteeringBehaviour();
        seekBehaviour.Constructor();
        seekBehaviour.AddNewForce(m_seekForce);

        // Set up condition for chase sequence
        WithinRange chaseCondition = new WithinRange();

        chaseCondition.SetParameters(player, 3.5f);

        // Set up the reverse condition
        NotCondition notChase = new NotCondition();

        notChase.SetCondition(chaseCondition);

        // Set up chase sequence
        Sequence chaseSequence = new Sequence();

        chaseSequence.addBehaviour(notChase);
        chaseSequence.addBehaviour(seekBehaviour);


        //-----------------------------------------------------------------
        // The In Range Sequence

        // Companion in range behaviour

        CompanionInRange inRangeBehaviour = new CompanionInRange();

        inRangeBehaviour.SetPlayer(player);


        //----------------------------------------------------------------
        // The Main Selector

        // Set up main selector
        Selector mainSelector = new Selector();

        mainSelector.addBehaviour(fleeSequence);
        mainSelector.addBehaviour(chaseSequence);
        mainSelector.addBehaviour(inRangeBehaviour);


        // Add all sequences to behaviour list
        m_behaviours.Add(mainSelector);

        // Setting the forward direction
        transform.forward = new Vector3(0, 0, 1);
    }
    // Use this for initialization
    void Start()
    {
        // Initialise behaviour list
        m_behaviours = new List <IBehaviour>();

        // Find the player game object
        player       = FindObjectOfType <FPSController>().gameObject;
        playerScript = player.GetComponent <FPSController>();

        currentHealth = maxHealth;

        //-----------------------------------------------------------------
        // The Flee Sequence

        // Set up the flee force and flee force parameter
        m_fleeForce = new HorizontalFleeForce();
        m_fleeForce.SetTarget(player /*.transform.position + new Vector3 (2, 2, 2)*/);

        // Set up the flee behaviour
        fleeBehaviour = new SteeringBehaviour();
        fleeBehaviour.Constructor();
        fleeBehaviour.AddNewForce(m_fleeForce);

        // Set up condition for flee sequence
        WithinRange fleeCondition = new WithinRange();

        fleeCondition.SetParameters(player, 1.5f);

        // Set up flee sequence
        Sequence fleeSequence = new Sequence();

        fleeSequence.addBehaviour(fleeCondition);
        fleeSequence.addBehaviour(fleeBehaviour);

        //-----------------------------------------------------------------
        // The Attack Sequence

        // Set up the attack behaviour
        ShootBehaviour attackBehaviour = new ShootBehaviour();

        attackBehaviour.SetTarget(player);
        attackBehaviour.SetWeaponType(ShootBehaviour.WeaponType.HitScanType);
        List <GameObject> guns = new List <GameObject>();

        // Give a list of bullet spawners to the shoot behaviour
        foreach (Transform child in transform)
        {
            GunParticleSystem gun;
            gun = child.gameObject.GetComponent <GunParticleSystem>();
            if (gun != null)
            {
                guns.Add(gun.gameObject);
            }
        }
        attackBehaviour.SetGuns(guns);

        // Set up condition for the attack sequence
        WithinRange attackCondition = new WithinRange();

        attackCondition.SetParameters(player, attackRange);

        // Set up play sound behaviour
        PlaySound playAttackSound = new PlaySound();

        if (stateAudioSource != null && attackStateClip != null)
        {
            playAttackSound.SetAudioSource(stateAudioSource);
            playAttackSound.SetAudioClip(attackStateClip);
            playAttackSound.SetTimer(15.0f);
        }

        // Set up attack sequence
        Sequence attackSequence = new Sequence();

        attackSequence.addBehaviour(attackCondition);
        if (stateAudioSource != null && attackStateClip != null)
        {
            attackSequence.addBehaviour(playAttackSound);
        }
        attackSequence.addBehaviour(attackBehaviour);



        //-----------------------------------------------------------------
        // The Chase Sequence

        // Set up the seek force and seek force parameter
        m_seekForce = new SeekForce();
        m_seekForce.SetTarget(player);

        // Set up the seek behaviour
        seekBehaviour = new SteeringBehaviour();
        seekBehaviour.Constructor();
        seekBehaviour.AddNewForce(m_seekForce);

        // Set up condition for chase sequence
        WithinRange chaseCondition = new WithinRange();

        chaseCondition.SetParameters(player, chaseRange);

        // Set up play sound behaviour
        PlaySound playChaseSound = new PlaySound();

        if (stateAudioSource != null && chaseStateClip != null)
        {
            playChaseSound.SetAudioSource(stateAudioSource);
            playChaseSound.SetAudioClip(chaseStateClip);
            playChaseSound.SetTimer(15.0f);
        }

        // Set up chase sequence
        Sequence chaseSequence = new Sequence();

        chaseSequence.addBehaviour(chaseCondition);
        if (stateAudioSource != null && chaseStateClip != null)
        {
            chaseSequence.addBehaviour(playChaseSound);
        }
        chaseSequence.addBehaviour(seekBehaviour);



        //----------------------------------------------------------------
        // The Patrol Sequence

        // Set up patrol behaviour
        PatrolBehaviour patrol = new PatrolBehaviour();

        patrol.SetPatrolPoints(m_patrolPointA, m_patrolPointB);
        patrol.StartUp();

        // Set up play sound behaviour
        PlaySound playPatrolSound = new PlaySound();

        if (stateAudioSource != null && chaseStateClip != null)
        {
            playPatrolSound.SetAudioSource(stateAudioSource);
            playPatrolSound.SetAudioClip(patrolStateClip);
            playPatrolSound.SetTimer(15.0f);
        }

        // Set up patrol sequence
        Sequence patrolSequence = new Sequence();

        if (stateAudioSource != null && chaseStateClip != null)
        {
            chaseSequence.addBehaviour(playPatrolSound);
        }
        patrolSequence.addBehaviour(patrol);

        //----------------------------------------------------------------
        // The Main Selector

        // Set up main selector
        Selector mainSelector = new Selector();

        mainSelector.addBehaviour(attackSequence);
        mainSelector.addBehaviour(chaseSequence);
        mainSelector.addBehaviour(patrolSequence);



        // Add all sequences to behaviour list
        m_behaviours.Add(mainSelector);

        // Setting the forward direction
        transform.forward = new Vector3(0, 0, 1);
    }
Esempio n. 7
0
    // Use this for initialization
    void Start()
    {
        // Initialise behaviour list
        m_behaviours = new List <IBehaviour>();

        // Find the player game object
        player       = FindObjectOfType <FPSController>().gameObject;
        playerScript = player.GetComponent <FPSController>();


        //-----------------------------------------------------------------
        // The Collision Avoidance Sequence

        // Set up the collsion avoidance force



        ////-----------------------------------------------------------------
        //// The Attack Sequence

        //// Set up the attack behaviour
        //ShootBehaviour m_attackBehaviour = new ShootBehaviour();


        //// Set up condition for the attack sequence
        //WithinRange attackCondition = new WithinRange();
        //attackCondition.SetParameters(player, 5);

        //// Set up attack sequence
        //Sequence attackSequence = new Sequence();
        //attackSequence.addBehaviour(attackCondition);
        //attackSequence.addBehaviour(m_attackBehaviour);



//-----------------------------------------------------------------
// The Chase Sequence

        // Set up the seek force and seek force parameter
        m_seekForce = new SeekForce();
        m_seekForce.SetTarget(player);

        // Set up the seek behaviour
        seekBehaviour = new SteeringBehaviour();
        seekBehaviour.Constructor();
        seekBehaviour.AddNewForce(m_seekForce);

        // Set up condition for chase sequence
        WithinRange chaseCondition = new WithinRange();

        chaseCondition.SetParameters(player, 20);

        // Set up chase sequence
        Sequence chaseSequence = new Sequence();

        chaseSequence.addBehaviour(chaseCondition);
        chaseSequence.addBehaviour(seekBehaviour);



//----------------------------------------------------------------
// The Patrol Sequence

        // Set up patrol behaviour
        PatrolBehaviour patrol = new PatrolBehaviour();

        patrol.StartUp();
        patrol.SetPatrolPoints(new Vector3(20, 3, 12), new Vector3(-20, 3, 12));



//----------------------------------------------------------------
// The Main Selector

        // Set up main selector
        Selector mainSelector = new Selector();

        mainSelector.addBehaviour(chaseSequence);
        mainSelector.addBehaviour(patrol);



        // Add all sequences to behaviour list
        m_behaviours.Add(mainSelector);

        // Setting the forward direction
        transform.forward = new Vector3(0, 0, 1);
    }
    // Use this for initialization
    void Start()
    {
        // Initialise behaviour list
        m_castleScript = FindObjectOfType <CastleScript>();
        m_behaviours   = new List <IBehaviour>();

        m_body = GetComponent <Rigidbody>();
        if (!m_animator)
        {
            m_animator = GetComponentInChildren <Animator> ();
        }


        //-----------------------------------------------------------------
        // The Castle Interaction Sequence

        // Set up the seek force and seek force parameter
        SeekForce m_castleSeekForce = new SeekForce();

        m_castleSeekForce.m_rotationSpeed = turnSpeed;

        // Set waypoints
        foreach (Transform waypoint in waypoints)
        {
            waypointVectors.Add(waypoint.position);
        }

        // Set up the castle interaction behaviour
        CastleScript            castle         = FindObjectOfType <CastleScript>();
        CastleInteractBehaviour castleInteract = new CastleInteractBehaviour();

        castleInteract.SetParameters(m_castleSeekForce, castle);

        // Set up condition for castle interaction sequence
        CastleTouchedCondition castleCondition = new CastleTouchedCondition();

        castleCondition.castleScript = castle;

        //Set up chase sequence
        Sequence castleSequence = new Sequence();

        castleSequence.addBehaviour(castleCondition);
        castleSequence.addBehaviour(castleInteract);

        //-----------------------------------------------------------------
        // The Ball Interaction Sequence

        // Set up the seek force and seek force parameter
        SeekForce m_ballSeekForce = new SeekForce();

        m_ballSeekForce.m_rotationSpeed = turnSpeed;

        // Set up the ball interaction behaviour
        if (!ball)
        {
            ball = FindObjectOfType <BallScript> ();
        }
        BallInteractBehaviour ballInteract = new BallInteractBehaviour();

        ballInteract.SetParameters(ball, m_ballSeekForce, 10.0f, middleBallTarget);

        // Set up condition for ball interaction sequence
        BallTouchedCondition ballCondition = new BallTouchedCondition();

        ballCondition.m_ball        = ball;
        ballCondition.ballBehaviour = ballInteract;

        //Set up chase sequence
        Sequence ballSequence = new Sequence();

        ballSequence.addBehaviour(ballCondition);
        ballSequence.addBehaviour(ballInteract);



        //-----------------------------------------------------------------
        // The Chase Sequence

        // Set up the seek force and seek force parameter
        /*SeekForce seekChase*/ m_seekForce = new SeekForce();
        m_seekForce.SetTarget(player);

        // Set up the seek behaviour
        seekBehaviour = new SteeringBehaviour();
        seekBehaviour.Constructor();
        seekBehaviour.AddNewForce(m_seekForce);

        // Set up condition for chase sequence
        WaveCondition waveCondition = new WaveCondition();
        //waveCondition.m_target = player;

        // Set up chase sequence
        Sequence chaseSequence = new Sequence();

        chaseSequence.addBehaviour(waveCondition);
        chaseSequence.addBehaviour(seekBehaviour);


        //----------------------------------------------------------------
        // The Wander Sequence

        // Set up seek force and seek parameter
        SeekForce seekWander = new SeekForce();

        seekWander.m_rotationSpeed = turnSpeed;

        // Set up rotational force and arrival parameter
        RotationalForce rotationWander = new RotationalForce();

        // Set up arrival force and arrival parameter
        ArrivalForce arrivalWander = new ArrivalForce();

        arrivalWander.SetParameter();

        // Set up wander behaviour
        LimitedWanderBehaviour wanderBehaviour = new LimitedWanderBehaviour();

        wanderBehaviour.SetLimit(middlePoint, maxRadius, maxDepth);
        wanderBehaviour.SetForce(seekWander, rotationWander, arrivalWander);
        wanderBehaviour.nearEnough = m_waypointTolerance;

        //----------------------------------------------------------------
        // The Main Selector

        // Set up main selector
        Selector mainSelector = new Selector();

        mainSelector.addBehaviour(castleSequence);
        mainSelector.addBehaviour(ballSequence);
        mainSelector.addBehaviour(chaseSequence);
        mainSelector.addBehaviour(wanderBehaviour);

        // Add all sequences to behaviour list
        m_behaviours.Add(mainSelector);

        // Setting the forward direction
        transform.forward = new Vector3(0, 0, 1);
    }