Пример #1
0
    void Align()
    {
        GameObject[] mBirds;
        mBirds = fManager.allBirds;

        float mDist;

        foreach (GameObject mBird in mBirds)
        {
            if (mBird != this.gameObject)
            {
                mDist = Vector3.Distance(this.transform.position, mBird.transform.position);
                float alignPower = mDist;

                if (mDist <= alignmentDist)
                {
                    FlockBehaviour mOtherFlockBehaviour = mBird.GetComponent <FlockBehaviour>();

                    this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                                               mBird.transform.rotation,
                                                               rotVel * alignPower * Time.deltaTime);

                    desiredDirection = mBird.transform.TransformVector(.0f, .0f, Time.deltaTime * transVel).normalized;
                }
            }
        }

        currentDirection = this.transform.TransformVector(.0f, .0f, Time.deltaTime * transVel).normalized *alignmentDist;
        Debug.DrawLine(transform.localPosition, transform.localPosition + currentDirection, Color.magenta);
    }
    private void RemoveBehavior(CompositBehaviour cb)
    {
        int oldCount = cb.behaviors.Length;

        if (oldCount == 1)
        {
            cb.behaviors = null;
            cb.weights   = null;
            return;
        }

        else
        {
            FlockBehaviour[] newBehaviors = new FlockBehaviour[oldCount - 1];
            float[]          newWeights   = new float[oldCount - 1];

            for (int i = 0; i < oldCount - 1; i++)
            {
                newBehaviors[i] = cb.behaviors[i];
                newWeights[i]   = cb.weights[i];
            }
            cb.behaviors = newBehaviors;
            cb.weights   = newWeights;
        }
    }
Пример #3
0
 public WanderState(NPCManager npcManager, CollisionAvoidance avoidObstacles, FlockBehaviour avoidNpcs)
 {
     NpcManager     = npcManager;
     _numberOfMoves = (int)Random.Range(1.0f, 10.0f);
     AvoidObstacles = avoidObstacles;
     AvoidNpcs      = avoidNpcs;
 }
Пример #4
0
    // This method is called when the user clicks the remove behaviour button
    void RemoveBehaviour(CompositeBehaviour cb)
    {
        // We know the array wont be null, as the button would not appear
        int oldCount = cb.behaviours.Length;

        // If there is only one behaviour, we can just clear it out:
        if (oldCount == 1)
        {
            cb.behaviours = null;
            cb.weights    = null;
            return;
        }

        // If array is empty we will start with 1 item
        FlockBehaviour[] newBehaviours = new FlockBehaviour[oldCount - 1];
        // Create new array
        float[] newWeights = new float[oldCount - 1];
        // Iterate through and add the original values
        for (int i = 0; i < oldCount - 1; i++)
        {
            newBehaviours[i] = cb.behaviours[i];
            newWeights[i]    = cb.weights[i];
        }
        // We do not need to assign anything to newWeights here because it has already been assigned
        cb.behaviours = newBehaviours;
        cb.weights    = newWeights;
    }
Пример #5
0
        public Goblin(string name, Vector2D pos, World w, MovingEntity Target) : base(name, pos, w)
        {
            // State.
            hunting    = new Hunting(this);
            retreating = new Retreating(this);
            guarding   = new Guarding(this);
            wandering  = new Wandering(this);
            regroup    = new Regroup(this);
            obey       = new Obeying(this);
            equip      = new Equip(this);
            setState(guarding); // Starting state.

            FollowingOrder = false;

            Key = _lastKey + 1;
            _lastKey++;
            debugText       = new List <string>();
            this.Target     = Target;
            Mass            = 50;
            MaxSpeed        = 5;
            MaxForce        = 25;
            DamagePerAttack = 10;
            AttackRange     = 10;
            AttackSpeed     = 15; // Lower is faster.

            GroupValue     = 10;
            NeighborsRange = 100;

            SeparationValue = 8;
            CohesionValue   = 1;
            AlignmentValue  = 16;

            FollowValue = 20;

            _SB     = new ArrivalBehaviour(me: this, target: Target, slowingRadius: SlowingRadius);
            _FleeB  = new FleeBehaviour(me: this, target: Target, panicDistance: PanicDistance);
            _FlockB = new FlockBehaviour(me: this, groupValue: GroupValue, cohesionValue: CohesionValue, alignmentValue: AlignmentValue, separationValue: SeparationValue);
            _LFB    = new LeaderFollowingBehaviour(me: this, leader: Leader, slowingRadius: SlowingRadius, leaderBehindDist: 30, groupValue: GroupValue, followValue: FollowValue, separationValue: SeparationValue);
            _OA     = new ObstacleAvoidance(this);
            _WA     = new WallAvoidance(this);
            _WB     = new WanderBehaviour(this, 100, 200);

            Velocity        = new Vector2D(0, 0);
            SlowingRadius   = 100;
            PanicDistance   = 200;  // Distance at which goblin starts fleeing.
            PassiveDistance = 1000; // Distance at which goblin goes to guard.
            BraveryDistance = 100;
            WanderRadius    = 10;
            WanderDistance  = 1;
            Scale           = 4;
            VColor          = Color.Black;

            AddDebugText("Current state: " + currentState, 0);
            AddDebugText("Previous state: " + previousState, 1);
        }
Пример #6
0
    private void CreateParticle()
    {
        FlockBehaviour particle = Instantiate(prefab, transform.position, transform.rotation) as FlockBehaviour;
        Collider       collider = GetComponent <Collider>();

        particle.transform.parent        = transform;
        particle.transform.localPosition = new Vector3(
            Random.value * collider.bounds.size.x,
            Random.value * collider.bounds.size.y,
            Random.value * collider.bounds.size.z) - collider.bounds.extents;
        particle.controller = this;
        particles.Add(particle);
    }
    void AddBehavior(CompositeBehaviour cb)
    {
        int oldCount = (cb.behaviours != null) ? cb.behaviours.Length : 0;

        FlockBehaviour[] newBehaviors = new FlockBehaviour[oldCount + 1];
        float[]          newWeights   = new float[oldCount + 1];
        for (int i = 0; i < oldCount; i++)
        {
            newBehaviors[i] = cb.behaviours[i];
            newWeights[i]   = cb.weights[i];
        }
        newWeights[oldCount] = 1f;
        cb.behaviours        = newBehaviors;
        cb.weights           = newWeights;
    }
    private FlockBehaviour adding;            // a new behaviour

    private FlockBehaviour[] Remove(int index, FlockBehaviour[] old)
    {
        // Remove this behaviour
        var current = new FlockBehaviour[old.Length - 1];

        for (int y = 0, x = 0; y < old.Length; y++)
        {
            if (y != index)
            {
                current[x] = old[y];
                x++;
            }
        }
        return(current);
    }
Пример #9
0
    void Cohese()
    {
        GameObject[] mBirds;
        mBirds = fManager.allBirds;

        float mDist;

        foreach (GameObject mBird in mBirds)
        {
            if (mBird != this.gameObject)
            {
                mDist = Vector3.Distance(this.transform.position, mBird.transform.position);
                if (mDist <= cohesionDist)
                {
                    isFollowing = true;
                    FlockBehaviour mOtherFlockBehaviour = mBird.GetComponent <FlockBehaviour>();

                    Vector3 mDirection   = mBird.transform.position - this.transform.position;
                    float   attractPower = mDist;

                    if (this.transVel < mOtherFlockBehaviour.transVel)
                    {
                        this.transVel = Mathf.Lerp(this.transVel,
                                                   mOtherFlockBehaviour.transVel,
                                                   Time.deltaTime);

                        if (mDirection != Vector3.zero)
                        {
                            this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                                                       Quaternion.LookRotation(mDirection),
                                                                       rotVel * attractPower * Time.deltaTime);
                        }
                    }

                    desiredDirection = (desiredDirection + mDirection).normalized;
                    Debug.DrawLine(this.transform.position, mBird.transform.position, Color.red);
                }
                else
                {
                    isFollowing = false;
                }
            }
        }
    }
Пример #10
0
    // This method is called when the user clicks the add behaviour button
    void AddBehaviour(CompositeBehaviour cb)
    {
        // Get original size of the array
        int oldCount = (cb.behaviours != null) ? cb.behaviours.Length : 0;

        // If array is empty we will start with 1 item
        FlockBehaviour[] newBehaviours = new FlockBehaviour[oldCount + 1];
        // Create new array
        float[] newWeights = new float[oldCount + 1];
        // Iterate through and add the original values
        for (int i = 0; i < oldCount; i++)
        {
            newBehaviours[i] = cb.behaviours[i];
            newWeights[i]    = cb.weights[i];
        }
        // newWeights can't be zero otherwise new behaviours can't take effect
        // The user would think something has gone wrong if we don't set it to 1f:
        newWeights[oldCount] = 1f;
        cb.behaviours        = newBehaviours;
        cb.weights           = newWeights;
    }
Пример #11
0
    void Separate()
    {
        GameObject[] mBirds;
        mBirds = fManager.allBirds;

        float mDist;

        foreach (GameObject mBird in mBirds)
        {
            if (mBird != this.gameObject)
            {
                mDist = Vector3.Distance(this.transform.position, mBird.transform.position);
                if (mDist <= separationDist)
                {
                    isAvoiding = true;
                    FlockBehaviour mOtherFlockBehaviour = mBird.GetComponent <FlockBehaviour>();

                    Vector3 mDirection = this.transform.position - mBird.transform.position;
                    float   avoidPower = 10 / mDist;

                    if (mDirection != Vector3.zero)
                    {
                        this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                                                   Quaternion.LookRotation(mDirection),
                                                                   rotVel * avoidPower * Time.deltaTime);
                    }


                    desiredDirection = (desiredDirection + mDirection).normalized;
                    Debug.DrawLine(transform.localPosition, transform.localPosition + mDirection, Color.white);
                }
                else
                {
                    isAvoiding = false;
                }
            }
        }
    }
Пример #12
0
        void RemoveBehaviour(CompositeBehaviour compositeBehaviour)
        {
            int oldCount = compositeBehaviour.behaviour.Length;

            if (oldCount == 1)
            {
                compositeBehaviour.behaviour = null;
                compositeBehaviour.weights   = null;
                return;
            }

            FlockBehaviour[] newBehaviours = new FlockBehaviour[oldCount - 1];
            float[]          newWeights    = new float[oldCount - 1];

            for (int i = 0; i < oldCount - 1; i++)
            {
                newBehaviours[i] = compositeBehaviour.behaviour[i];
                newWeights[i]    = compositeBehaviour.weights[i];
            }

            compositeBehaviour.behaviour = newBehaviours;
            compositeBehaviour.weights   = newWeights;
        }
 private float DistanceTo(FlockBehaviour boid)
 {
     return(Vector3.Distance(boid.transform.position, Position));
 }
    public override void OnInspectorGUI()
    {
        // Setup
        var current = (CompositeBehaviour)target;

        EditorGUILayout.BeginHorizontal();

        // Draw
        if (current.behaviours == null || current.behaviours.Length == 0)
        {
            EditorGUILayout.HelpBox("No behaviours attached.", MessageType.Warning);
            EditorGUILayout.EndHorizontal();
        }
        else
        {
            EditorGUILayout.LabelField("Behaviours");
            EditorGUILayout.LabelField("Weights");
            EditorGUILayout.EndHorizontal();

            for (int i = 0; i < current.behaviours.Length; i++)
            {
                // Draw index
                EditorGUILayout.BeginHorizontal();

                if (GUILayout.Button("Remove") || current.behaviours[i] == null)
                {
                    // Remove this behaviour and its weight
                    current.behaviours = Remove(i, current.behaviours);
                    // Remove this behaviour
                    var newWeights = new float[current.weights.Length - 1];
                    for (int y = 0, x = 0; y < current.weights.Length; y++)
                    {
                        if (y != i)
                        {
                            newWeights[x] = current.weights[y];
                            x++;
                        }
                    }
                    current.weights = newWeights;
                    break;
                }

                current.behaviours[i] = (FlockBehaviour)EditorGUILayout.ObjectField(current.behaviours[i], typeof(FlockBehaviour), false);
                EditorGUILayout.Space(30);
                current.weights[i] = EditorGUILayout.Slider(current.weights[i], 0, 5);

                EditorGUILayout.EndHorizontal();
            }
        }
        EditorGUILayout.EndHorizontal();


        // possibility to add new behaviour
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Add behaviour...");
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();

        adding = (FlockBehaviour)EditorGUILayout.ObjectField(adding, typeof(FlockBehaviour), false);

        //if (adding != null && (current.behaviours != null || current.behaviours.Length == 0))
        if (GUILayout.Button("Add"))
        {
            if (current.behaviours == null)
            {
                current.behaviours = new FlockBehaviour[0];
                current.weights    = new float[0];
            }

            // add this item to the array
            var oldBehaviours = current.behaviours;
            current.behaviours = new FlockBehaviour[oldBehaviours.Length + 1];
            var oldWeights = current.weights;
            current.weights = new float[oldWeights.Length + 1];

            for (int i = 0; i < oldBehaviours.Length; i++)
            {
                current.behaviours[i] = oldBehaviours[i];
                current.weights[i]    = oldWeights[i];
            }

            current.behaviours[oldBehaviours.Length] = adding;
            current.weights[oldWeights.Length]       = 1;

            adding = null;
        }

        EditorUtility.SetDirty(current);
    }
Пример #15
0
 public void RemoveFromFlock(FlockBehaviour remove)
 {
     squad.Remove(remove.gameObject.GetComponent <Ship>());
 }