示例#1
0
        /// <summary>
        /// Spawn boids over time.
        /// </summary>
        /// <param name="delay">Amount of time between spawns.</param>
        private IEnumerator SpawnBoids(float delay)
        {
            for (int i = 0; i < boidCount; i++)
            {
                Vector3 spawnPoint = Random.insideUnitSphere;
                spawnPoint.z = Mathf.Abs(spawnPoint.z);
                GameObject g = Instantiate(boidPrefab, transform.TransformPoint(spawnPoint), Quaternion.LookRotation(spawnPoint, transform.up));

                if (goal)
                {
                    Boid b = g.GetComponent <Boid>();

                    if (b)
                    {
                        b.goal = goal;
                    }
                }

                yield return(new WaitForSeconds(delay));
            }
        }
示例#2
0
        /*public void Draw()
         * {
         *  _sb.Begin();
         *
         *  for (int i = 0; i < boids.Count; ++i)
         *      boids[i].Draw(_sb);
         *
         *  _sb.End();
         * }*/

        public Boid[] FindNeighbours(Boid _of)
        {
            List <Boid> neighbours = new List <Boid>();

            for (int i = 0; i < boids.Count; ++i)
            {
                if (boids[i] == _of)
                {
                    continue;
                }

                Vector2 vec  = _of.transform.position - boids[i].transform.position;
                float   dist = vec.magnitude;

                if (dist < _of.radius)
                {
                    neighbours.Add(boids[i]);
                }
            }

            return(neighbours.ToArray());
        }
示例#3
0
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        // * Class Function
        //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        public void Update()
        {
            spawnCount += Time.deltaTime;
            if (Input.GetMouseButtonDown(0) && spawnCount > spawnFrequency)
            {
                //Vector3 mousePos = Camera.current.ScreenToWorldPoint(new Vector3(100, 100, Camera.current.nearClipPlane));
                //boids.Add(ships[Random.Range(0, ships.Length -1)]);
                Vector3 position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
                Boid    boid     = (Boid)Instantiate(ships[Random.Range(0, ships.Length - 1)], position, Quaternion.identity);
                boids.Add(boid);
                spawnCount = 0;
            }

            for (int i = 0; i < boids.Count; ++i)
            {
                boids[i].FixedUpdate();
                if (!screenRect.Contains(new Vector2((int)boids[i].transform.position.x, (int)boids[i].transform.position.y)))
                {
                    boids.RemoveAt(i--);
                }
            }
        }
示例#4
0
        private void DrawBoid(Boid boid)
        {
            SceneObjectPart existing = m_scene.GetSceneObjectPart (boid.Id);

            SceneObjectGroup sog;
            if (existing == null) {
                SceneObjectGroup group = findByName (m_boidPrim);
                sog = CopyPrim (group, boid.Id);
                m_sogMap [boid.Id] = sog;
                m_scene.AddNewSceneObject (sog, false);
            } else {
                sog = existing.ParentGroup;
            }

            Quaternion rotation = CalcRotationToEndpoint (sog, sog.AbsolutePosition, boid.Location);
            sog.UpdateGroupRotationPR( boid.Location, rotation);
        }
示例#5
0
        void AddBoid(string name)
        {
            Boid boid = new Boid (name, this, m_flowMap);

            // find an initial random location for this Boid
            // somewhere not within an obstacle
            int xInit = m_rnd.Next(m_flowMap.LengthX);
            int yInit = m_rnd.Next(m_flowMap.LengthY);
            int zInit = m_rnd.Next(m_flowMap.LengthZ);

            while( m_flowMap.IsWithinObstacle( xInit, yInit, zInit ) ){
                xInit = m_rnd.Next(m_flowMap.LengthX);
                yInit = m_rnd.Next(m_flowMap.LengthY);
                zInit = m_rnd.Next(m_flowMap.LengthZ);
            }

            boid.Location = new Vector3 (Convert.ToSingle(xInit), Convert.ToSingle(yInit), Convert.ToSingle(zInit));
            m_flock.Add (boid);
        }