예제 #1
0
        public override List <CelestialBody> Collide(CelestialBody celestialBody)
        {
            List <CelestialBody> explodedRemnants = new List <CelestialBody>();

            if (!celestialBody.ShouldDissapear)
            {
                celestialBody.ShouldDissapear = true;
                explodedRemnants.Add(celestialBody);
                int i = 0;

                while (i < 5)
                {
                    //random number between 0.5 and 2.5
                    double newX = random.NextDouble() * 2 + 0.5;
                    double newY = random.NextDouble() * 2 + 0.5;

                    Astroid remnant = new Astroid()
                    {
                        Neighbours = new List <string>(),
                        Radius     = celestialBody.Radius / 5 + 1,
                        X          = celestialBody.X + i * 5.5,
                        Y          = celestialBody.Y + i * 5.5,
                        VX         = (celestialBody.VX + -i) * newX,
                        VY         = (celestialBody.VY + -i) * newY,
                        Colour     = celestialBody.Colour,
                        collision  = new Bounce()
                    };

                    explodedRemnants.Add(remnant);
                    i++;
                }
            }
            return(explodedRemnants);
        }
예제 #2
0
파일: Global.cs 프로젝트: epc2101/Astroids
    void addNewAsteroids()
    {
        //Add new asteroids at the start of each scene
        timer += Time.deltaTime;
        if (timer > spawnPeriod && newLevel)
        {
            timer = 0;
            float width  = Camera.main.GetScreenWidth();
            float height = Camera.main.GetScreenHeight();
            addedRoids = true;
            for (int i = 0; i < numberAstrSpawnedEachPeriod; i++)
            {
                float      horizontalPos = Random.Range(0.0f, width);
                float      verticalPos   = Random.Range(0.0f, height);
                GameObject obj           = Instantiate(astrToSpawn,
                                                       Camera.main.ScreenToWorldPoint(
                                                           new Vector3(horizontalPos, verticalPos, originInScreenCoords.z)),
                                                       Quaternion.identity) as GameObject;

                //Get the asteroid component & set a random rotation & speed
                float x = Random.Range(-20.0f, 20.0f);
                //float y = Random.Range(0.0f, 10.0f); //TODO - use this for 3D
                float   z = Random.Range(-20.0f, 20.0f);
                Astroid a = obj.GetComponent <Astroid>();
                //float rotation = Random.Range(0.0f,1.0f);
                //Quaternion rot = Quaternion.Euler(new Vector3(0,rotation,0));
                a.gameObject.rigidbody.AddRelativeForce(x, 0, z);
                a.lifeSpan = 2;
                currNumAstr++;
            }
        }
    }
예제 #3
0
    void OnCollisionEnter(Collision collision)
    {
        // the Collision contains a lot of info, but it’s the colliding
        // object we’re most interested in.
        Collider collider = collision.collider;

        if (collider.CompareTag("Astroids"))
        {
            Astroid roid = collider.gameObject.GetComponent <Astroid>();
            // let the other object handle its own death throes
            roid.Die();
            // Destroy the Bullet which collided with the Asteroid
            Destroy(gameObject);
        }
        else if (collider.CompareTag("UFO"))
        {
            UFO foe = collider.gameObject.GetComponent <UFO>();
            foe.Die();
            Destroy(gameObject);
        }
        else
        {
            //TODO
            // if we collided with something else, print to console
            // what the other thing was
            Debug.Log("Collided with " + collider.tag);
        }
    }
예제 #4
0
파일: Astroid.cs 프로젝트: tvl20/Uniturd
    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.name == "Bullet(Clone)")
        {
            print("A bullet has hit an astroid");

            if (Size > 0)
            {
                int rnd = Random.Range(1, 3);
                for (int i = 0; i < rnd; i++)
                {
                    float      rotation     = Random.Range(0f, 360f);
                    GameObject spawnAstroid = (GameObject)Instantiate(this.transform, this.transform.position, Quaternion.Euler(0f, 0f, rotation)).gameObject;
                    spawnAstroid.transform.localScale += new Vector3(-0.1f, -0.1f, 0);

                    Astroid spawnAstroidScript = spawnAstroid.GetComponent <Astroid>();
                    spawnAstroidScript.Size  = Size - 1;
                    spawnAstroidScript.speed = 1.5f;
                    //for (int i = 0; i < spawnAstroidScript.Size; i++)
                    //{
                    //    spawnAstroidScript.speed += 0.5f;
                    //}

                    Rigidbody2D spawnAstroidRigidbody2D = spawnAstroid.GetComponent <Rigidbody2D>();
                    spawnAstroidRigidbody2D.velocity = spawnAstroid.transform.position * spawnAstroidScript.speed * -1;
                }
            }

            Destroy(this.gameObject);
        }
    }
예제 #5
0
    public void SpawnAstroid() //TODO determen position and rotation, then spawn astroid
    {
        float xPos;
        float yPos;
        float rotation;

        int side = Random.Range(1, 5);

        Vector2 spawnPos  = new Vector2(0, 0);
        Vector2 targetPos = new Vector2(Random.Range(-7.5f, 7.5f), Random.Range(-10f, 10f));

        switch (side)
        {
        case 1:     // left
            spawnPos.x = -10.5f;
            spawnPos.y = Random.Range(-8f, 8f);
            break;

        case 2:     // top
            spawnPos.x = Random.Range(-10.5f, 10.5f);
            spawnPos.y = 8.0f;
            break;

        case 3:     // right
            spawnPos.x = 10.5f;
            spawnPos.y = Random.Range(-8f, 8f);
            break;

        case 4:     // bottom
            spawnPos.x = Random.Range(-10.5f, 10.5f);
            spawnPos.y = -8.0f;
            break;
        }

        Vector3 vectorToTarget = targetPos - spawnPos;

        rotation = (Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg) - 180;

        if (side == 2 || side == 4)
        {
            rotation = rotation * -1;
        }

        GameObject spawnAstroid = (GameObject)Instantiate(BigAstroid, new Vector3(spawnPos.x, spawnPos.y, 0f), Quaternion.Euler(0f, 0f, rotation)).gameObject;

        Astroid spawnAstroidScript = spawnAstroid.GetComponent <Astroid>();

        spawnAstroidScript.Size  = Random.Range(1, 4);
        spawnAstroidScript.speed = 1.5f;
        //for (int i = 0; i < spawnAstroidScript.Size; i++)
        //{
        //    spawnAstroidScript.speed += 0.5f;
        //}

        Rigidbody2D spawnAstroidRigidbody2D = spawnAstroid.GetComponent <Rigidbody2D>();

        spawnAstroidRigidbody2D.velocity = spawnAstroid.transform.position * spawnAstroidScript.speed * -1;
    }
예제 #6
0
 void Start()
 {
     astroidInfo = AstroidLibrary.GetAstroid(id);
     health      = Random.Range(astroidInfo.healthRange.x, astroidInfo.healthRange.y);
     mass        = health * astroidInfo.massScalar;
     GetComponent <Rigidbody2D>().mass      = mass;
     GetComponent <SpriteRenderer>().sprite = astroidInfo.sprites[0];
     astroidDestroyedFactor = Convert.ToInt32(health / astroidInfo.sprites.Length);
     render = GetComponent <SpriteRenderer>();
 }
    // spawing Astroid
    public GameObject Spawn(Direction Dposition, Vector3 Spoint)
    {
        GameObject Rock   = Instantiate(AstroidPreFap) as GameObject;
        Astroid    script = Rock.GetComponent <Astroid>();

        script.Initilize(Dposition, Spoint);
        Animator anim = Rock.GetComponent <Animator>();

        anim.SetInteger("Animation_Selector", Random.Range(1, 4));
        return(Rock);
    }
예제 #8
0
    public Astroid[] genWorld()
    {
        Astroid[] a = new Astroid[numberOfObjects];
        Random r = new Random();

        for (int i= 0; i < a.Length; i++)
        {
            a[i] = new Astroid("Gold",new Vector2 ( Random.Range(-1000,1000), Random.Range(-1000,1000)));
        }
        return a;
    }
예제 #9
0
    void OnTriggerEnter2D(Collider2D other)
    {
        Astroid astroid = other.GetComponent <Astroid>();

        if (astroid)
        {
            AudioManager.instance.PlaySFX(breakAsteroid[Random.Range(0, breakAsteroid.Length - 1)]);
            astroid.StartExploding();
            Destroy(gameObject);
        }
    }
예제 #10
0
    void Start()
    {
        mainCam           = Camera.main.transform;
        persistantObjects = GameObject.Find("PersistantObjects").transform;
        curAstroid        = astroids [curAstroidIndex];
        baseDifficulty    = curAstroid.baseDifficulty;
        maxDepth          = curAstroid.depth;

        float bottomSpawnY = -maxDepth + (maxDepth % sidePieceSpawnIntervals);

        Instantiate(bottom, new Vector3(0f, bottomSpawnY, 0f), Quaternion.identity);
        // spawn initial chunks
        ResetTerrain();
    }
예제 #11
0
    public void SetTriggers()
    {
        Triggers = GetComponentsInChildren <Trigger>();

        foreach (Trigger trigger in Triggers)
        {
            if (trigger.Type == FPS.Enums.TriggerType.Gunner)
            {
                if (trigger is Astroid)
                {
                    AstroidTrigger = trigger as Astroid;
                }
            }
        }
    }
예제 #12
0
파일: Global.cs 프로젝트: epc2101/Astroids
    public void addBabyRoids(int lifeSpan, GameObject parentRoid)
    {
        Vector3 lastPos = new Vector3(0, 0, 0);

        for (int i = 0; i < lifeSpan; i++)
        {
            Vector3 pos   = parentRoid.transform.position;
            int     randX = Random.Range(4, 10);
            int     randZ = Random.Range(4, 10);
            pos = new Vector3(pos.x + (float)randX / 10.0f, 0, pos.z + (float)randZ / 10.0f);
            //Make sure 2 new asteroids aren't on top of eachother
            if (i == 1)
            {
                print("TESTING SPLIT: " + pos.x + " " + lastPos.x + " " + lastPos.z);
                float rSqr    = 0.95f * 0.95f;              //Middle asteroids are 1.9 scale
                float dist    = Vector3.Distance(pos, lastPos);
                float distSqr = dist * dist;
                if (distSqr < rSqr)
                {
                    print("TOO Close!!");
                    if (pos.x < lastPos.x)
                    {
                        pos.x -= 1;
                    }
                    else
                    {
                        pos.x += 1;
                    }
                    if (pos.z < lastPos.z)
                    {
                        pos.z -= 1;
                    }
                    else
                    {
                        pos.z += 1;
                    }
                }
            }
            GameObject newRoid = Instantiate(astrToSpawn, pos, Quaternion.identity) as GameObject;
            lastPos = pos;
            Astroid a = newRoid.GetComponent <Astroid>();
            randX = Random.Range(0, 10);
            randZ = Random.Range(0, 10);
            a.gameObject.rigidbody.AddRelativeForce(randX, 0, randZ);
            a.lifeSpan = lifeSpan - 1;
            currNumAstr++;
        }
    }
        private Astroid BuildAstroid(ParserData parserData)
        {
            Astroid returningAstroid = new Astroid()
            {
                Neighbours = new List <string>(),
                Radius     = parserData.Radius,
                Colour     = parserData.Colour,
                X          = parserData.X,
                Y          = parserData.Y,
                VX         = parserData.VX,
                VY         = parserData.VY,
                collision  = returnCollisionComponent(parserData.OnCollision)
            };

            return(returningAstroid);
        }
    //Spawns 2 small Astroids
    public void Spawn_Small(float Health, Vector2 Loc)
    {
        int i = 0;

        if (Health == 100)
        {
            while (i < 2)
            {
                GameObject Rock = Spawn(Dpoints[Random.Range(0, 5)], Loc);
                Rock.transform.localScale = transform.localScale / 2;
                Astroid S = Rock.GetComponent <Astroid>();
                S.Health            = 50;
                Rock.gameObject.tag = ("Rock");
                i++;
            }
        }
    }
예제 #15
0
 protected Astroid GetReadyAstroid()
 {
     lock (gameObject)
     {
         Astroid astroidScript = _astroidInstances.FirstOrDefault(x => x != null && !x.Instance.activeInHierarchy);
         if (astroidScript == null)
         {
             var astroidObject = Instantiate <GameObject>(_astrodiPrefabs[Random.Range(0, _astrodiPrefabs.Count)]);
             astroidScript = astroidObject.GetComponent <Astroid>();
             if (astroidScript == null)
             {
                 astroidScript = astroidObject.GetComponentInChildren <Astroid>();
             }
             _astroidInstances.Add(astroidScript);
             astroidScript.Instance.SetActive(false);
         }
         _currentSpawnCount++;
         return(astroidScript);
     }
 }
예제 #16
0
    private void SpawnAstroid(Vector3 position, Vector3 velocity, AstroidSize size)
    {
        // Astroid
        var         astroidPrefab    = Resources.Load <GameObject>("Prefabs/Astroid");
        var         astroidInstance  = Object.Instantiate(astroidPrefab, position, Quaternion.identity);
        var         astroidView      = astroidInstance.GetComponent <View>();
        var         movementSettings = Resources.Load <MovementSettings>("Settings/Astroid");
        IProjectile projectile       = new Projectile(astroidView, movementSettings);

        projectile.Type = ProjectileType.Astroid;
        projectile.Movement.Position = position;
        projectile.Movement.Velocity = velocity;

        IAstroid astroid = new Astroid(astroidView, projectile);

        astroid.Size   = size;
        astroid.OnHit += AstroidHit;

        astroidsCount++;
    }
예제 #17
0
    public override void OnInspectorGUI()
    {
        Astroid ThisCube = (Astroid)target;

        base.DrawDefaultInspector();

        if (GUILayout.Button("Update Flight Paths"))
        {
            ThisCube.UpdateCubes();
        }

        if (GUILayout.Button("Return Pathways"))
        {
            ThisCube.ReturnPathways();
        }

        if (GUILayout.Button("Create New Astroid Block"))
        {
            ThisCube.CreateNewAstroid();
        }
    }
예제 #18
0
        public void newGame()
        {
            // Set up the initial game environment

            astroidList.Clear();

            for (int i = 0; i < 6; i++)
            {
                gamePaused = false;
                gameWon    = false;
                gameOver   = false;

                Astroid a = new Astroid(astroidSprite0);
                a.Position = new Vector2((float)(random.NextDouble() * viewport.Width), (float)(random.NextDouble() * viewport.Height));
                a.Rotation = (float)(random.NextDouble() * 6.2);
                astroidList.Add(a);
            }

            ship.Velocity = new Vector2();
            ship.teleport(true);
            lives = 4;
        }
예제 #19
0
        public long Part2()
        {
            var grid = ParseInput(input);

            var rows    = grid.GetLength(0);
            var columns = grid.GetLength(1);

            var ir       = coorr;
            var ic       = coorc;
            int zapcount = 0;

            while (true)
            {
                var vectors = new Dictionary <double, Astroid>();

                // scan
                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < columns; c++)
                    {
                        if (!(ir == r && ic == c) && grid[r, c] > 0)
                        {
                            var vr      = r - ir;
                            var vc      = c - ic;
                            var astroid = new Astroid()
                            {
                                row      = r,
                                column   = c,
                                distance = Math.Sqrt((vr * vr) + (vc * vc)),
                                angle    = ((Math.Atan2(r - ir, c - ic) * (180 / Math.PI)) + 360 + 90) % 360
                            };

                            //Console.WriteLine($"{r},{c} {angle}");
                            if (vectors.TryGetValue(astroid.angle, out var existing))
                            {
                                if (existing.distance > astroid.distance)
                                {
                                    vectors[astroid.angle] = astroid;
                                }
                            }
                            else
                            {
                                vectors.Add(astroid.angle, astroid);
                            }
                        }
                    }
                }

                // vape
                Console.WriteLine($"Zapped {zapcount}");
                PrintGrid(grid);

                var sortedVectors = vectors.Values.OrderBy(v => (v.angle)).ToList();
                if (sortedVectors.Count == 0)
                {
                    break;
                }
                foreach (var vector in sortedVectors)
                {
                    grid[vector.row, vector.column] = 0;
                    zapcount++;
                    if (zapcount == 200)
                    {
                        return((vector.column * 100) + vector.row);
                    }
                }
            }
            return(0);
        }
예제 #20
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            // TODO: Add your update logic here
            curKeyboardState = Keyboard.GetState();
            MouseState mouse = Mouse.GetState();


            // Input

            if (!gamePaused && !gameOver && !gameWon)
            {
                if (curKeyboardState.IsKeyDown(Keys.Left))
                {
                    ship.Rotation -= ship.RotationRate;
                }
                if (curKeyboardState.IsKeyDown(Keys.Right))
                {
                    ship.Rotation += ship.RotationRate;
                }
                if (curKeyboardState.IsKeyDown(Keys.Up))
                {
                    ship.thrust();
                }
                if (curKeyboardState.IsKeyUp(Keys.Space) && lastKeyBoardState.IsKeyDown(Keys.Space))
                {
                    fire(gameTime);
                }
                if (curKeyboardState.IsKeyUp(Keys.T) && lastKeyBoardState.IsKeyDown(Keys.T))
                {
                    ship.teleport(false);
                }
            }

            if (curKeyboardState.IsKeyUp(Keys.N) && lastKeyBoardState.IsKeyDown(Keys.N))
            {
                newGame();
            }

            if (curKeyboardState.IsKeyUp(Keys.P) && lastKeyBoardState.IsKeyDown(Keys.P))
            {
                gamePaused = !gamePaused;
            }


            // Do logic for each entity

            if (!gamePaused)
            {
                // Check for winning and losing state

                // Winning will have priority over losing
                // If the winning conditions are satisfied for both winning and losing, the player will win
                if (astroidList.Count < 1)
                {
                    gameWon = true;
                }

                if (!gameWon)
                {
                    if (lives < 1)
                    {
                        gameOver = true;
                    }
                }

                // Ship
                if (!gameWon && !gameOver)
                {
                    ship.Update();
                }

                // Projectiles
                foreach (Projectile p in projectileList)
                {
                    p.Update();
                    if (p.expired(gameTime.TotalGameTime))
                    {
                        deleteList.Add(p);
                    }
                }

                foreach (Projectile p in deleteList)
                {
                    if (projectileList.Contains(p))
                    {
                        projectileList.Remove(p);
                        //boomList.Add(new Boom(boomSprite, p));
                    }
                }

                // Astroids
                List <Astroid>    splitList            = new List <Astroid>();
                List <Projectile> projectileDeleteList = new List <Projectile>();

                foreach (Astroid a in astroidList)
                {
                    a.Update();

                    if (RotatedSpritesCollide((Entity)ship, (Entity)a))
                    {
                        if (IntersectPixels(ship.getTransform(), ship.Sprite.Width, ship.Sprite.Height, ship.SpriteData,
                                            a.getTransform(), a.Sprite.Width, a.Sprite.Height, a.SpriteData))
                        {
                            ship.Velocity += a.Velocity;
                            if ((lastHitTime.Seconds + 1) < (gameTime.TotalGameTime.Seconds))
                            {
                                lives--;
                                lastHitTime = gameTime.TotalGameTime;
                            }
                        }
                    }

                    foreach (Projectile p in projectileList)
                    {
                        if (RotatedSpritesCollide((Entity)p, (Entity)a))
                        {
                            if (IntersectPixels(p.getTransform(), p.Sprite.Width, p.Sprite.Height, p.SpriteData,
                                                a.getTransform(), a.Sprite.Width, a.Sprite.Height, a.SpriteData))
                            {
                                splitList.Add(a);
                                a.Velocity = p.Velocity / 4;
                                projectileDeleteList.Add(p);
                            }
                        }
                    }
                }

                // Splitting astroids that are found to have collided
                foreach (Astroid a in splitList)
                {
                    if (astroidList.Contains(a))
                    {
                        // Remove big astroid
                        astroidList.Remove(a);

                        // Add two little ones
                        for (int i = 0; i < 2; i++)
                        {
                            if (a.canBreak)
                            {
                                Astroid newAstriod = new Astroid(astroidSprite1, a, gameTime.TotalGameTime);
                                newAstriod.canBreak = false;

                                newAstriod.Velocity = new Vector2(
                                    (float)(newAstriod.Velocity.X + (newAstriod.Velocity.X * (random.NextDouble() * 2))),
                                    (float)(newAstriod.Velocity.Y + (newAstriod.Velocity.Y * (random.NextDouble() * 2))));
                                astroidList.Add(newAstriod);
                            }
                        }
                    }
                }

                foreach (Projectile p in projectileDeleteList)
                {
                    if (projectileList.Contains(p))
                    {
                        projectileList.Remove(p);
                    }
                }
            }

            message  = "";
            message += "CS3113 Astroids\n";
            message += "(N) New game\n";
            message += "(P) Pause\n";
            message += "(T) Teleport\n";

            lastKeyBoardState = curKeyboardState;

            base.Update(gameTime);
        }
예제 #21
0
 // Use this for initialization
 void Start()
 {
     GetComponent <Rigidbody2D>().AddTorque(rotationSpeed);
     Instance = this;
 }
예제 #22
0
 public void SetTrigger(Astroid trigger)
 {
     Trigger = trigger;
 }