Exemple #1
0
        public GameEngine()
        {
            #region GameWindow properties and init
            GameWindow.FormBorderStyle = FormBorderStyle.Fixed3D;
            GameWindow.ClientSize = new Size(Width, Height);
            GameWindow.Text = Title;
            GameWindow.MaximizeBox = false;
            GameWindow.StartPosition = FormStartPosition.CenterScreen;
            GameWindow.KeyPreview = false;

            //Add the GameScreen to the GameWindow
            GameWindow.Controls.Add(GameScreen);

            //Pull out the Draw event in the gamescreen
            GameScreen.Paint += GameDraw;

            //Pull out the FormClosed event so we can terminate the game thread
            GameWindow.FormClosed += GameWindow_FormClosed;
            #endregion

            //Other game objects
            GameTimer = new HiResTimer();
            GameInput = new Input(GameWindow, GameScreen);

            //Create the game
            game = new Game(this);

            //Start the game thread
            StartGameThread();

            //Start the form application
            Application.Run(GameWindow);
        }
Exemple #2
0
        public void Update(TimeSpan gameTime, Input input)
        {
            //Logic to toggle debug mode if F3 is pressed
            if (input.IsKeyDown(Keys.F3) && isF3Released == true)
            {
                Game.DebugView = !Game.DebugView;
                isF3Released = false;
            }
            isF3Released = input.IsKeyUp(Keys.F3);

            CurrentGameState.Update(gameTime, input);
        }
        public override void Update(TimeSpan gameTime, Input input)
        {
            if (input.Left)
            {
                Game.CurrentState = State.game;
            }

            //update the background asteroids
            foreach (Asteroid asteroid in BackgroundAsteroids)
                asteroid.Update(gameTime, input);

            base.Update(gameTime, input);
        }
Exemple #4
0
        public override void Update(TimeSpan GameTime, Input input)
        {
            if (Alive)
            {
                //stops the bullet from updating and being drawn after so long(kills the bullet)
                if (GameTime.TotalMilliseconds > (startTime + BulletLifeSpan))
                {
                    Alive = false;
                }

                base.Update(GameTime, input);
            }
        }
Exemple #5
0
 public override void Update(TimeSpan GameTime, Input input)
 {
     if (Alive)
     {
         base.Update(GameTime, input);
     }
 }
Exemple #6
0
        public void Update(TimeSpan GameTime, Input input, List<Entity> entityList)
        {
            #region Player Death
            if (!Alive)
            {
                if (canRespawn == false)
                {
                    //Player fadeout animation
                    DeathDeltaTime = GameTime.TotalMilliseconds - DeathStartTimer;

                    //stops the DeathDeltaTimer from going over 1800, to stop errors with percentage
                    if (DeathDeltaTime > 1800) { DeathDeltaTime = 1800; }

                    //Calculate a percentage oh time till the death animation stops so that we can color the line accordingly
                    double percentage = (1800 - DeathDeltaTime) / 1800;

                    //Sets the color of the player depending on the delta time
                    Model.ModelColor = Color.FromArgb(255, (int)(percentage * 255), (int)(percentage * 255), (int)(percentage * 255));

                    if (DeathDeltaTime >= 1800)
                    {
                        canRespawn = true;
                    }

                    //Stops the flame from being drawn when the player is dead;
                    DrawFlame = false;
                }
            }
            #endregion

            #region Player Movement
            //Moving and rotating the player depending of different input
            if (Alive)
            {
                if (input.IsKeyDown(Keys.Right))
                {
                    AddToAngle(PlayerTurnSpeed);
                }
                if (input.IsKeyDown(Keys.Left))
                {
                    AddToAngle(-PlayerTurnSpeed);
                }
                if (input.IsKeyDown(Keys.Up))
                {
                    Velocity = Direction * PlayerSpeed;
                }

                //Displaying the flickering flame if the player is moving and
                if (input.IsKeyDown(Keys.Up))
                {
                    if (GameTime.TotalMilliseconds > (FlameStartTimer + PlayerFlameInterval))
                    {
                        FlameStartTimer = GameTime.TotalMilliseconds;
                        DrawFlame = !DrawFlame;
                    }
                }

                if (input.IsKeyUp(Keys.Up))
                {
                    DrawFlame = false;
                    FlameStartTimer = 0;
                }
            }
            #endregion

            #region Player Shooting
            if (input.IsKeyDown(Keys.Space) && Alive)
            {
                if (GameTime.TotalMilliseconds > (BulletStartTimer + PlayerBulletInterval))
                {
                    //Fire a new bullet and reset the start time

                    //add a rotated vector of -25 in the Y direction to have the bullet shoot from the nose of the ship
                    Bullet bullet = new Bullet(Game, Position + new Vector2(0, -25).Rotate(Angle), Direction, GameTime.TotalMilliseconds);
                    bulletList.Add(bullet);

                    //reset the bullet timer to stop the ship from firing a constant stream of bullets
                    BulletStartTimer = GameTime.TotalMilliseconds;
                }
            }

            //Update every bullet in the list
            foreach (Bullet bullet in bulletList)
            {
                bullet.Update(GameTime, input);
            }
            #endregion

            #region Asteroids and Bullet Collision
            foreach (Bullet bullet in bulletList)
            {
                if (bullet.Alive)
                {
                    foreach (Entity entity in entityList.ToList())
                    {
                        if (entity is Asteroid)
                        {
                            Asteroid asteroid = (Asteroid)entity;
                            if (asteroid.Alive)
                            {
                                if (Hitbox.Collision(bullet.BulletHitbox, asteroid.AsteroidHitbox))
                                {
                                    asteroid.BulletHitPosition = bullet.Position;
                                    asteroid.Kill();
                                    bullet.Kill();

                                    if (AsteroidKill != null) { AsteroidKill(asteroid.Size); }
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            #region Player and Asteroid Collision
            if (Alive && !IsImmortal)
            {
                foreach (Entity entity in entityList.ToList())
                {
                    if (entity is Asteroid)
                    {
                        Asteroid asteroid = (Asteroid)entity;
                        if (asteroid.Alive)
                        {
                            if (Hitbox.Collision(asteroid.AsteroidHitbox, PlayerHitbox))
                            {
                                //kill the asteroid that collided with the player (causing it to split)
                                asteroid.Kill();

                                //call the on plater death event
                                if (Death != null) { Death(); }

                                //call the asteroid killed event
                                if (AsteroidKill != null) { AsteroidKill(asteroid.Size); }

                                //Set alive to false and set the death animation start timer
                                Alive = false;
                                DeathStartTimer = GameTime.TotalMilliseconds;
                            }
                        }
                    }
                }
            }
            #endregion

            #region Immortality
            if (IsImmortal)
            {
                double TimePassed = GameTime.TotalMilliseconds - ImmortalityStartTime;

                if (TimePassed > ImmortalityTime) //stop the immortality
                {
                    Hide = false;
                    IsImmortal = false;
                }
                else
                {
                    int ModResult = ((int)TimePassed) / ImmortalityFlashInterval;
                    Hide = MathUtil.IsEven(ModResult);
                }
            }

            #endregion

            //Move the player by its velocity
            base.Update(GameTime, input);

            //Lower the velocity to add the effect of friction to the player
            //If the keys are still being pressed, then this will be discounted
            //because the velocity is reset
            Velocity = Velocity - (Velocity * PlayerFrictionFactor);
        }
Exemple #7
0
        public virtual void Update(TimeSpan GameTime, Input input)
        {
            //Increase the position by the velocity
            Position += Velocity;

            //Keep the entity within the 800, 600 viewport by looping it around the edges
            if (loop)
            {
                if (position.X < -model.BoundingBoxSize.Width/2) { position += new Vector2(800, 0); }
                if (position.X > 800 + model.BoundingBoxSize.Width/2) { position -= new Vector2(800, 0); }
                if (position.Y < -model.BoundingBoxSize.Height/2) { position += new Vector2(0, 600); }
                if (position.Y > 600 + model.BoundingBoxSize.Height/2) { position -= new Vector2(0, 600); }
            }
        }
Exemple #8
0
 public virtual void Update(TimeSpan gameTime, Input input)
 {
     MousePosition = new Vector2(input.MousePosition);
 }