void OnCollisionEnter(Collision coll)
        {
            if (isServer)
            {
                return; // hosting client, server path will handle collision
            }
            //if not, we are on a client, so just disable the spaceship (& play destruction aprticle).
            //This way client won't see it's destruction delayed (time for it to happen on server & message to get back)
            NetworkAsteroid asteroid = coll.gameObject.GetComponent <NetworkAsteroid>();

            if (asteroid != null)
            {
                LocalDestroy();
            }
        }
        IEnumerator AsteroidCoroutine()
        {
            const float MIN_TIME = 5.0f;
            const float MAX_TIME = 10.0f;

            while (_spawningAsteroid)
            {
                yield return(new WaitForSeconds(Random.Range(MIN_TIME, MAX_TIME)));

                Vector2 dir      = Random.insideUnitCircle;
                Vector3 position = Vector3.zero;

                if (Mathf.Abs(dir.x) > Mathf.Abs(dir.y))
                {//make it appear on the side
                    position = new Vector3(Mathf.Sign(dir.x) * Camera.main.orthographicSize * Camera.main.aspect,
                                           0,
                                           dir.y * Camera.main.orthographicSize);
                }
                else
                {//make it appear on the top/bottom
                    position = new Vector3(dir.x * Camera.main.orthographicSize * Camera.main.aspect,
                                           0,
                                           Mathf.Sign(dir.y) * Camera.main.orthographicSize);
                }

                //offset slightly so we are not out of screen at creation time (as it would destroy the asteroid right away)
                position -= position.normalized * 0.1f;


                GameObject ast = Instantiate(asteroidPrefabs[asteroidPrefabs.Length - 1], position, Quaternion.Euler(Random.value * 360.0f, Random.value * 360.0f, Random.value * 360.0f)) as GameObject;

                NetworkAsteroid asteroid = ast.GetComponent <NetworkAsteroid>();
                asteroid.SetupStartParameters(-position.normalized * 1000.0f, Random.insideUnitSphere * Random.Range(500.0f, 1500.0f));

                NetworkServer.Spawn(ast);
            }
        }
Exemplo n.º 3
0
        public void Explode()
        {
            //if the 2 bullet touch the asteroid the same frame, they will both generate the callback before the asteroid get destroyed
            //but we want to destroy it only once.
            if (_isDestroyed)
            {
                return;
            }

            _isDestroyed = true;

            int targetLevel = level - 1;

            if (targetLevel > 0)
            {
                //pick a number between 3 & 5 small asteroids to spawn
                int numberToSpawn = Random.Range(3, 6);

                for (int i = 0; i < numberToSpawn; ++i)
                {
                    Vector3 force = Quaternion.Euler(0, i * 360.0f / numberToSpawn, 0) * Vector3.forward * Random.Range(0.5f, 1.5f) * 300.0f;;

                    GameObject      newGO    = Instantiate(NetworkGameManager.sInstance.asteroidPrefabs[targetLevel - 1], transform.position + force.normalized * 5.0f, Quaternion.Euler(0, Random.value * 180.0f, 0)) as GameObject;
                    NetworkAsteroid asteroid = newGO.GetComponent <NetworkAsteroid>();

                    //we slice a 360 angle in numberToSpawn part & send an asteroid for each
                    asteroid.originalForce  = force;
                    asteroid.originalTorque = Random.insideUnitSphere * Random.Range(500.0f, 1500.0f);

                    NetworkServer.Spawn(newGO);
                }
            }

            //destroy that asteroid too
            NetworkServer.Destroy(gameObject);
        }