Ejemplo n.º 1
0
    private void Awake()
    {
        fishBehaviour = GetComponent <FishBehaviour>();

        currentNutrientsAmount = initialNutrients;

        currentAge = 0;
        ageToDie   = Random.Range(ageToDieMin, ageToDieMax);

        if (Random.value < 0.5f)
        {
            male   = true;
            female = false;
        }

        else
        {
            male   = false;
            female = true;
        }

        if (male)
        {
            maleFish.SetActive(true);
            femaleFish.SetActive(false);
        }

        if (female)
        {
            maleFish.SetActive(false);
            femaleFish.SetActive(true);
        }
    }
    void Awake()
    {
        preysLayerMask = huntFor.value;

        nose = ((Nose)GetComponent(typeof(Nose))).position;
        children = new FishBehaviour[2] {hunting, biting};
    }
Ejemplo n.º 3
0
 protected void CollisionFish(FishBehaviour fish)
 {
     if (!caughtFish)
     {
         EventManager.Single.TriggerFirstCatch();
     }
     // fish-specific collision stuff
 }
    protected virtual void Awake()
    {
        children = new FishBehaviour[2]{velocityMatcher, orientationMatcher};
        noseRelative = ((Nose)GetComponent(typeof(Nose))).position;
        _transform = transform;

        originalMaxSpeed = cachedComputedMaxSpeed = maxSpeed;
    }
Ejemplo n.º 5
0
    // Apply the rules for boids to each fish to enact the relevant behaviour
    void ApplyBoidsRules()
    {
        if (!turning) // Only applies the rules so long as the fish is not turning away from the outer wall
        {
            List <GameObject> fishList;
            fishList = manager.allFish;

            Vector3 averageCentre  = Vector3.zero; // Average centre of the group calculated from each member of the group
            Vector3 averageAvoid   = Vector3.zero; // Average avoidance vector of each member of the group
            float   globalSpeed    = 0.01f;        // Average group speed
            int     localGroupSize = 0;            // Size of the local group of this particular fish

            foreach (GameObject fish in fishList)
            {
                if (fish != this.gameObject)
                {
                    neighbourDistance = Vector3.Distance(fish.transform.position + (transform.localScale / 2), this.transform.position);

                    if (neighbourDistance <= manager.distanceToNeighbours)
                    {
                        averageCentre += fish.transform.position;
                        localGroupSize++;

                        if (neighbourDistance < 1.5f) // How close each fish should be before they should begin avoiding
                        {
                            averageAvoid = averageAvoid + (this.transform.position - fish.transform.position);
                        }

                        FishBehaviour fishManager = fish.GetComponent <FishBehaviour>();
                        globalSpeed += fishManager.speed;
                    }
                }
            }

            if (localGroupSize > 0)
            {
                averageCentre = averageCentre / localGroupSize + (/*manager.*/ fishDestinationTarget.transform.position - this.transform.position); // Gets the centre of the local group and moves it towards the target
                speed         = globalSpeed / localGroupSize;                                                                                       // Matches fish speed with global speed
                // speed = Random.Range(manager.minSpeed, manager.maxSpeed);

                Vector3 direction = (averageCentre + averageAvoid) - transform.position;

                if (direction != Vector3.zero)
                {
                    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), manager.rotationSpeed * Time.deltaTime);
                }
            }

            else if (localGroupSize <= 0)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), manager.rotationSpeed * Time.deltaTime);
            }
        }
    }
Ejemplo n.º 6
0
 public void Release()
 {
     fish = null;
     if (joint2D != null)
     {
         Destroy(joint2D);
     }
     joint2D      = null;
     line.enabled = false;
     ball.drag    = 0;
     ball.AddForce(ball.velocity * 0.5f, ForceMode2D.Impulse);
     arrow.SetActive(false);
 }
Ejemplo n.º 7
0
 public void Hook(Rigidbody2D body)
 {
     if (fish == null && joint2D == null)
     {
         grabSound.Play();
         fish    = body.transform.parent.GetComponentInChildren <FishBehaviour>();
         joint2D = ball.gameObject.AddComponent <SpringJoint2D>();
         joint2D.autoConfigureDistance = false;
         joint2D.distance     = 3.5f;
         joint2D.anchor       = Vector2.zero;
         joint2D.dampingRatio = 1;
         joint2D.frequency    = 3;
         joint2D.breakForce   = Mathf.Infinity;
         ball.drag            = 3f;
         arrow.SetActive(true);
     }
 }
    public void BeingEaten()
    {
        _food.Health--;
        transform.localScale = new Vector3(transform.localScale.x - _scalingFactor, transform.localScale.y - _scalingFactor, transform.localScale.z - _scalingFactor);
        if (_food.Health <= 0)
        {
            _dataManager.RemoveFood(_food);
            transform.gameObject.SetActive(false);

            foreach (Fish fish in _dataManager.fishList)
            {
                FishBehaviour fishBehav = fish.FishObject.GetComponent <FishBehaviour>();
                if (fishBehav != null)
                {
                    fishBehav.EatFood(this);
                }
            }
        }
    }
    private Vector3 SwimWithFriends()
    {
        Vector3 followClosest = new Vector3();

        if (_nearbyFish.Count > 0)
        {
            FishBehaviour closestFish = _nearbyFish[0];
            foreach (FishBehaviour fish in _nearbyFish)
            {
                if (MathTools.GetDistanceBetweenVectors(transform.position, fish.transform.position) <
                    MathTools.GetDistanceBetweenVectors(transform.position, closestFish.transform.position))
                {
                    closestFish   = fish;
                    followClosest = fish._fish.DesiredPoint;
                }
            }
        }
        return(followClosest);
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.tag.Equals("Food"))
        {
            if (MathTools.GetDistanceBetweenVectors(transform.position, other.ClosestPoint(transform.position)) > _innerCollider.radius)
            {
                FoodBehavior foodBehav = other.GetComponent <FoodBehavior>();
                if (foodBehav == null)
                {
                    return;
                }

                if (_nearbyFood.Contains(foodBehav))
                {
                    _nearbyFood.Remove(foodBehav);
                }
            }
        }
        else if (other.tag.Equals("Fish"))
        {
            if (MathTools.GetDistanceBetweenVectors(transform.position, other.gameObject.transform.position) >= _innerCollider.radius)
            {
                return;
            }

            FishBehaviour fishBehav = other.GetComponent <FishBehaviour>();
            if (fishBehav == null)
            {
                return;
            }

            if (_nearbyFish.Contains(fishBehav))
            {
                _nearbyFish.Remove(fishBehav);
            }
        }
    }
Ejemplo n.º 11
0
    /// <summary>
    /// This function receives the current Fish Behaviour that is being looped and modify it's values.
    /// </summary>
    /// <param name="behaviour">FishBehaviour struct containing the fish's data.</param>
    /// <returns>The modified FishBehaviour.</returns>
    public FishBehaviour OnUpdateFish(FishBehaviour behaviour)
    {
        FishBehaviour modifiedBehaviour = behaviour;

        modifiedBehaviour.acceleration -= Random.Range(valueToAffect - (valueToAffect / 2), valueToAffect);
        if (modifiedBehaviour.acceleration <= minAccelValue)
        {
            modifiedBehaviour.acceleration = minAccelValue;
        }
        else if (modifiedBehaviour.acceleration >= controller.maxAcceleration * 2)
        {
            modifiedBehaviour.acceleration = controller.maxAcceleration;
        }

        modifiedBehaviour.speed -= Random.Range(valueToAffect - (valueToAffect / 2), valueToAffect);
        if (modifiedBehaviour.speed <= minSpeedValue)
        {
            modifiedBehaviour.speed = minSpeedValue;
        }
        else if (modifiedBehaviour.speed >= controller.maxSpeed * 2)
        {
            modifiedBehaviour.speed = controller.maxSpeed;
        }

        modifiedBehaviour.turnSpeed -= Random.Range(valueToAffect - (valueToAffect / 2), valueToAffect);
        if (modifiedBehaviour.turnSpeed <= minTurnSpeedValue)
        {
            modifiedBehaviour.turnSpeed = minTurnSpeedValue;
        }
        else if (modifiedBehaviour.turnSpeed >= controller.maxTurnSpeed * 2)
        {
            modifiedBehaviour.turnSpeed = controller.maxTurnSpeed;
        }

        return(modifiedBehaviour);
    }
    private void HandleSpottedObject(Collider other)
    {
        // Check if the object detected is another fish, or an obstacle.
        if (other.tag.Equals("Fish"))
        {
            if (_innerCollider == null)
            {
                return;
            }

            if (MathTools.GetDistanceBetweenVectors(transform.position, other.gameObject.transform.position) <= _innerCollider.radius)
            {
                return;
            }

            FishBehaviour fishBehav = other.GetComponent <FishBehaviour>();
            if (fishBehav == null)
            {
                return;
            }


            if (!_nearbyFish.Contains(fishBehav))
            {
                _nearbyFish.Add(fishBehav);
            }
        }
        else if (other.tag.Equals("Obstacle") || (other.tag.Equals("Cage")))
        {
            Vector3 closestPos = other.ClosestPoint(transform.position);

            if (MathTools.GetDistanceBetweenVectors(transform.position, closestPos) <= _innerCollider.radius)
            {
                if (_isObstacleDetected = IsHeadingTowardsPoint(closestPos))
                {
                    int offset = 1;
                    directions.DodgeCollisionDirection = FindFreeDir(closestPos, ref offset);
                }
            }
        }
        else if (other.tag.Equals("Food"))
        {
            FoodBehavior othersFoodBehavior = other.GetComponent <FoodBehavior>();
            if (othersFoodBehavior == null)
            {
                return;
            }

            if (MathTools.GetDistanceBetweenVectors(transform.position, other.ClosestPoint(transform.position)) <= _innerCollider.radius)
            {
                _fish.Hunger = Fish.maxHunger;
                othersFoodBehavior.BeingEaten();
            }
            else
            {
                if (!_nearbyFood.Contains(othersFoodBehavior))
                {
                    _nearbyFood.Add(othersFoodBehavior);
                }
                if (!lastKnownFoodSpotsVec2.Contains(new Vector2(other.transform.position.x, other.transform.position.z)))
                {
                    lastKnownFoodSpotsVec2.Add(new Vector2(other.transform.position.x, other.transform.position.z));
                }
            }
        }
    }
 void Awake()
 {
     children = new FishBehaviour[1]{seeking};
 }
Ejemplo n.º 14
0
 public void TriggerCollisionFish(FishBehaviour fish)
 {
     ONTriggerCollisionFish?.Invoke(fish);
 }
 void Awake()
 {
     children = new FishBehaviour[1]{flee};
 }
 void Awake()
 {
     escapeeLayerMask = escapeFrom.value;
     children = new FishBehaviour[1] {escape};
 }
    void Awake()
    {
        children = new FishBehaviour[1]{orientationMatcher};

        _transform = transform;
    }
 void Awake()
 {
     Nose _nose  = (Nose)GetComponent(typeof(Nose));
     nose = _nose.position;
     children = new FishBehaviour[1]{seeking};
     _transform = transform;
     seekingTarget = new GameObject("collision avoidance target");
     seekingTargetTransform = seekingTarget.transform;
     seeking.target = seekingTarget;
 }
Ejemplo n.º 19
0
 private void AddActiveBehaviour(FishBehaviour beh)
 {
     // print("behaviours: " + rawActiveBehaviours.Length + "; trying to push: " + activeBehaviourCount + 1);
     rawActiveBehaviours[activeBehaviourCount++] = beh;
 }