コード例 #1
0
        /// <summary>
        /// Creates the child asteroid.
        /// </summary>
        /// <param name="dir">Dir.</param>
        /// <param name="asteroidPos">Asteroid position.</param>
        /// <param name="newSize">New size.</param>
        private void CreateChildAsteroid(Vector2 dir, Vector2 asteroidPos, AsteroidSize newSize)
        {
            var pos = new Vector3(asteroidPos.x + dir.x, asteroidPos.y + dir.y);
            var a   = _creator.CreateAsteroid(newSize, pos);

            var multiplier = GetForceMultiplier(newSize);
            var rb         = a.GetComponent <Rigidbody2D>();
            var force      = new Vector2(dir.x, dir.y) * multiplier;

            rb.AddForce(force, ForceMode2D.Impulse);
            rb.AddTorque(force.x * multiplier * multiplier, ForceMode2D.Impulse);
        }
コード例 #2
0
        private void SplitAsteroid(Asteroid asteroid)
        {
            if (asteroid.size != AsteroidSize.Tiny)
            {
                var newSize = AsteroidSize.Medium;
                var scale   = 0.5f;
                if (asteroid.size == AsteroidSize.Medium)
                {
                    newSize = AsteroidSize.Small;
                    scale   = 0.2f;
                }
                if (asteroid.size == AsteroidSize.Small)
                {
                    newSize = AsteroidSize.Tiny;
                    scale   = 0.1f;
                }

                var vel       = asteroid.rigidbody2D.velocity;
                var velNormal = vel.normalized;

                var perp         = new Vector2(-velNormal.y, velNormal.x);
                var newVelNormal = (velNormal + perp).normalized;
                var pos          = new Vector3(asteroid.transform.position.x + perp.x * scale, asteroid.transform.position.y + perp.y * scale);
                var a            = creator.CreateAsteroid(newSize, pos);
                var rigidbody    = a.Get <Rigidbody2D>(typeof(Rigidbody2D));
                rigidbody.AddRelativeForce(new Vector2(newVelNormal.x * 100f * scale, newVelNormal.y * 100f * scale));
                rigidbody.AddTorque(UnityEngine.Random.Range(-200f, 200f) * scale);

                perp         = new Vector2(velNormal.y, -velNormal.x);
                newVelNormal = (velNormal + perp).normalized;
                pos          = new Vector3(asteroid.transform.position.x + perp.x * scale, asteroid.transform.position.y + perp.y * scale);
                a            = creator.CreateAsteroid(newSize, pos);
                rigidbody    = a.Get <Rigidbody2D>(typeof(Rigidbody2D));
                rigidbody.AddRelativeForce(new Vector2(newVelNormal.x * 100f * scale, newVelNormal.y * 100f * scale));
                rigidbody.AddTorque(UnityEngine.Random.Range(-200f, 200f) * scale);
            }
        }
コード例 #3
0
        /// <summary>
        /// Spawns the asteroid.
        /// </summary>
        private void SpawnAsteroid()
        {
            var spaceship = spaceships.First();
            var position  = FindPositionForNewAsteroid(spaceship);
            var asteroid  = _creator.CreateAsteroid(AsteroidSize.Large, position);

            // Give it a kick
            var vel    = UnityEngine.Random.insideUnitCircle.normalized;
            var torque = vel.x / 2;

            var rigidBody = asteroid.GetComponent <Rigidbody2D>();

            rigidBody.AddForce(vel, ForceMode2D.Impulse);
            rigidBody.AddTorque(torque, ForceMode2D.Impulse);
        }
コード例 #4
0
        override public void Update(float time)
        {
            var node = (GameNode)gameNodes.Head;

            if (node != null && node.State.playing)
            {
                if (spaceships.Empty)
                {
                    if (node.State.lives > 0)
                    {
                        var newSpaceshipPosition = Vector2.zero;
                        var clearToAddSpaceship  = true;
                        for (var asteroid = (AsteroidCollisionNode)asteroids.Head; asteroid != null; asteroid = (AsteroidCollisionNode)asteroid.Next)
                        {
                            if (Vector2.Distance(asteroid.Transform.position, newSpaceshipPosition) <= 1f)
                            {
                                clearToAddSpaceship = false;
                                break;
                            }
                        }
                        if (clearToAddSpaceship)
                        {
                            creator.CreateSpaceship();
                        }
                    }
                    else
                    {
                        Debug.Log("IN HERE");
                        node.State.playing = false;
                        creator.CreateWaitForClick();
                    }
                }
            }

            if (asteroids.Empty && bullets.Empty && !spaceships.Empty)
            {
                // next level
                var spaceship = (SpaceshipNode)spaceships.Head;
                node.State.level++;

                var asteroidCount = 2 + node.State.level;
                for (int i = 0; i < asteroidCount; i++)
                {
                    // check not on top of spaceship
                    Vector2 position;
                    do
                    {
                        position = new Vector2(UnityEngine.Random.Range(config.Bounds.min.x, config.Bounds.max.x),
                                               UnityEngine.Random.Range(config.Bounds.min.y, config.Bounds.max.y));
                    }while (Vector2.Distance(position, spaceship.Transform.position) <= 1);

                    var asteroid = creator.CreateAsteroid(AsteroidSize.Large, position);

                    // Give it a kick
                    var xVel   = UnityEngine.Random.Range(-100f, 100f);
                    var yVel   = UnityEngine.Random.Range(-100f, 100f);
                    var torque = UnityEngine.Random.Range(-100f, 100f);

                    var rigidBody = asteroid.Get <Rigidbody2D>(typeof(Rigidbody2D));
                    rigidBody.AddForce(new Vector2(xVel, yVel));
                    rigidBody.AddTorque(torque);
                }
            }
        }