コード例 #1
0
 protected virtual void OnEnemyCollision()
 {
     EnemyCollision?.Invoke(this, EventArgs.Empty);
 }
コード例 #2
0
ファイル: Snake.cs プロジェクト: DanielGaull/MazeSnake
        public void Update(GameTime gameTime, List <PowerUp> powerUps, List <Wall> walls, Random rand, int windowWidth, int windowHeight, ref User player, List <Enemy> enemies)
        {
            this.windowWidth  = windowWidth;
            this.windowHeight = windowHeight;

            #region Handle Wall Collisions & Moving

            //if (!shouldMove)
            //{
            //    HandleWallCollisions(direction, walls, ref player);
            //}
            if (shouldMove)
            {
                Move(direction);
            }
            if (!ShouldMove(direction, walls, ref player))
            {
                HandleWallCollisions(direction, walls, ref player);
            }

            #endregion

            #region Updating With Enemies

            for (int i = 0; i < enemies.Count; i++)
            {
                if (enemies[i].Intersects(drawRectangle))
                {
                    if (SnakeEffect == Effect.Forcefield)
                    {
                        if (!enemiesCollided.Contains(enemies[i].Id))
                        {
                            player.EnemiesAvoided++;
                            enemiesCollided.Add(enemies[i].Id);
                        }
                    }
                    else
                    {
                        EnemyCollision?.Invoke();
                    }
                }
            }

            #endregion

            #region Handling Powerups

            CheckPowerUpPickup(powerUps, rand, walls, windowWidth, windowHeight, ref player);
            secondsTimer.Update(gameTime);

            if (SnakeEffect == Effect.Speed && secondsTimer.QueryWaitTime(gameTime))
            {
                // We've had speed for one second
                player.AddToStat(Stat.SpeedSeconds, 1);
            }

            if (SnakeEffect == Effect.Frozen && secondsTimer.QueryWaitTime(gameTime))
            {
                // We've been frozen for one second
                player.AddToStat(Stat.TimeFrozen, 1);
            }
            if (SnakeEffect == Effect.Frozen)
            {
                frozenTimer.Update(gameTime);
                if (frozenTimer.QueryWaitTime(gameTime))
                {
                    SnakeEffect = Effect.None;
                }
            }

            if (SnakeEffect == Effect.WallBreaker)
            {
                wallBreakTimer.Update(gameTime);
                if (wallBreakTimer.QueryWaitTime(gameTime))
                {
                    SnakeEffect = Effect.None;
                    if (Sound.IsPlaying(Sounds.Buzzing))
                    {
                        Sound.Stop(Sounds.Buzzing);
                    }
                }
                else
                {
                    if (!Sound.IsPlaying(Sounds.Buzzing))
                    {
                        Sound.PlaySound(Sounds.Buzzing);
                    }
                }
            }
            if (SnakeEffect != Effect.WallBreaker && Sound.IsPlaying(Sounds.Buzzing))
            {
                Sound.Stop(Sounds.Buzzing);
            }

            if (SnakeEffect == Effect.Forcefield)
            {
                forcefieldTimer.Update(gameTime);
                if (forcefieldTimer.QueryWaitTime(gameTime))
                {
                    SnakeEffect = Effect.None;
                }

                forcefieldRect.X = drawRectangle.X;
                forcefieldRect.Y = drawRectangle.Y - (forcefieldRect.Height / 2 - drawRectangle.Height / 2);
            }

            speedTimer.Update(gameTime);
            if (SnakeEffect == Effect.Speed && speedTimer.QueryWaitTime(gameTime))
            {
                SnakeEffect = Effect.None;
                speedTimer.Reset();
            }
            if (SnakeEffect == Effect.Speed)
            {
                speed = PW_SPD;
            }
            else
            {
                speed = REG_SPD;
            }

            if (lightningStrip != null)
            {
                animationTimer.Update(gameTime);
                if (SnakeEffect == Effect.WallBreaker && animationTimer.QueryWaitTime(gameTime))
                {
                    if (currentFrame + 1 >= L_FRAMES_PER_ROW)
                    {
                        currentFrame = 0;
                        sourceRect.X = 0;
                    }
                    else
                    {
                        currentFrame++;
                        sourceRect.X += L_ANIMATION_WIDTH;
                    }
                }
                lightningLocationRect.X      = drawRectangle.X;
                lightningLocationRect.Y      = drawRectangle.Y;
                lightningLocationRect.Width  = drawRectangle.Width;
                lightningLocationRect.Height = drawRectangle.Height;
            }

            hatRect.X = drawRectangle.X + (2 * hatSpacingX);
            hatRect.Y = drawRectangle.Y - hatRect.Height;

            #endregion

            #region PC Controls
            keyState = Keyboard.GetState();
            GamePadState gamePad = GamePad.GetState(PlayerIndex.One);

            shouldMove = false;

            // Check if any control keys are pressed.
            // PC controls
            if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W) && keyState.IsKeyUp(Keys.LeftControl) && keyState.IsKeyUp(Keys.RightControl) &&
                keyState.IsKeyUp(Keys.LeftAlt) && keyState.IsKeyUp(Keys.RightAlt))
            {
                direction = Direction.Up;
                //shouldMove = ShouldMove(direction, walls, ref player);
                shouldMove = true;
            }
            else if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S) && keyState.IsKeyUp(Keys.LeftAlt) && keyState.IsKeyUp(Keys.RightAlt))
            {
                direction = Direction.Down;
                //shouldMove = ShouldMove(direction, walls, ref player);
                shouldMove = true;
            }
            else if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
            {
                direction = Direction.Right;
                //shouldMove = ShouldMove(direction, walls, ref player);
                shouldMove = true;
                effect     = SpriteEffects.None;
            }
            else if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
            {
                direction = Direction.Left;
                //shouldMove = ShouldMove(direction, walls, ref player);
                shouldMove = true;
                effect     = SpriteEffects.FlipHorizontally;
            }
            #endregion

            #region Clamping

            // Clamp the snake so the player cannot navigate it out of the window.
            if (drawRectangle.X <= minX)
            {
                drawRectangle.X = minX;
            }
            if (drawRectangle.X >= windowWidth - drawRectangle.Width)
            {
                drawRectangle.X = windowWidth - drawRectangle.Width;
            }
            if (drawRectangle.Y <= minY)
            {
                drawRectangle.Y = minY;
            }
            if (drawRectangle.Y >= windowHeight - drawRectangle.Height)
            {
                drawRectangle.Y = windowHeight - drawRectangle.Height;
            }

            #endregion

            #region Hat Updating

            if (HasHat)
            {
                SetHatRectValues();
            }

            #endregion
        }