Пример #1
0
        private void CheckCollisionWithMushrooms(MushroomGrid mushroomGrid)
        {
            int[,] xy = new int[, ] {
                { -1, 0 }, { 0, -1 }, { 1, 0 }, { 0, 1 }
            };

            for (int i = 0; i < collisionRadar.Length; i++)
            {
                collisionRadar[i] = Vector2.Zero;
            }

            for (int i = 0; i < mushroomGrid.MushroomCount; i++)
            {
                Rectangle mushroom = mushroomGrid.Mushroom(i).Rectangle;

                // unnecessary to check for mushroom collisions above PlayerClampHeight
                if (mushroom.Bottom < GameConstants.PlayerClampHeight)
                {
                    continue;
                }

                for (int j = 0; j < xy.GetLength(0); j++)
                {
                    bool hasCollision = LineRectangle(
                        new Vector2(rectangle.Center.X, rectangle.Center.Y),
                        new Vector2(rectangle.Center.X + xy[j, 0] * collisionRadarDistance, rectangle.Center.Y + xy[j, 1] * collisionRadarDistance),
                        mushroom,
                        out Vector2 intersection, out Vector2 dummy);
                    if (hasCollision)
                    {
                        if (collisionRadar[j] == Vector2.Zero ||
                            Vector2.Distance(new Vector2(rectangle.Center.X, rectangle.Center.Y), intersection) <
                            Vector2.Distance(new Vector2(rectangle.Center.X, rectangle.Center.Y), collisionRadar[j]))
                        {
                            collisionRadar[j].X = intersection.X;
                            collisionRadar[j].Y = intersection.Y;
                        }
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // create a new SpriteBatch, which can be used to draw textures
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // load sprites
            pointSprite    = Content.Load <Texture2D>(@"graphics\point");
            bulletSprite   = Content.Load <Texture2D>(@"graphics\bullet");
            playerSprite   = Content.Load <Texture2D>(@"graphics\player");
            mushroomSprite = Content.Load <Texture2D>(@"graphics\mushroom");
            scoreSprite    = Content.Load <Texture2D>(@"graphics\score");
            wurmHead       = Content.Load <Texture2D>(@"graphics\head2");

            // add initial game objects
            player = new Player(spriteBatch, playerSprite, 24, 24,
                                new Vector2(GameConstants.WindowWidth / 2 - 12, GameConstants.WindowHeight - 12));

            wurmheads.Add(new Wurmhead(spriteBatch, wurmHead, 24, 24, new Vector2(random.Next(0, 31) * 24, 24), random.Next(1, 8)));
            mushroom     = new Mushroom(spriteBatch, mushroomSprite, 24, 24, Vector2.Zero);
            mushroomGrid = new MushroomGrid(spriteBatch, mushroom);
            score        = new Score(spriteBatch, scoreSprite, 21, 21, Vector2.Zero);

            Mouse.SetPosition(player.Rectangle.Center.X, player.Rectangle.Center.Y);
        }
Пример #3
0
        public void Update(GameTime gameTime, MouseState mouseState, MushroomGrid mushroomGrid)
        {
            if (active)
            {
                float dx = mouseState.X - rectangle.Center.X;
                float dy = mouseState.Y - rectangle.Center.Y;
                //Console.WriteLine(mouseState.Position + ";" + dx + ";" + dy);

                if (mouseState.LeftButton == ButtonState.Released)
                {
                    canShoot = true;
                }
                if (mouseState.LeftButton == ButtonState.Pressed && canShoot)
                {
                    canShoot = false;
                    Bullet bullet = new Bullet(spriteBatch, Game1.bulletSprite, 3, 15, new Vector2(rectangle.Center.X - 1, rectangle.Center.Y - 3));
                    Game1.AddBullet(bullet);
                }

                CheckCollisionWithMushrooms(mushroomGrid);

                if (dx < 0 || dx > 0)
                {
                    rectangle.X += (int)(dx * 0.3);
                    if (collisionRadar[LEFT] != Vector2.Zero && rectangle.X < collisionRadar[LEFT].X)
                    {
                        rectangle.X = (int)collisionRadar[LEFT].X;
                    }
                    if (collisionRadar[RIGHT] != Vector2.Zero && rectangle.Right > collisionRadar[RIGHT].X)
                    {
                        rectangle.X = (int)collisionRadar[RIGHT].X - rectangle.Width;
                    }
                }
                if (dy < 0 || dy > 0)
                {
                    rectangle.Y += (int)(dy * 0.3);
                    if (collisionRadar[TOP] != Vector2.Zero && rectangle.Y < collisionRadar[TOP].Y)
                    {
                        rectangle.Y = (int)collisionRadar[TOP].Y;
                    }
                    if (collisionRadar[BOTTOM] != Vector2.Zero && rectangle.Bottom > collisionRadar[BOTTOM].Y)
                    {
                        rectangle.Y = (int)collisionRadar[BOTTOM].Y - rectangle.Height;
                    }
                }

                // clamp player in window
                if (rectangle.Left < 0)
                {
                    rectangle.X = 0;
                }

                if (rectangle.Right > GameConstants.WindowWidth)
                {
                    rectangle.X = GameConstants.WindowWidth - rectangle.Width;
                }

                if (rectangle.Y < GameConstants.PlayerClampHeight)
                {
                    rectangle.Y = GameConstants.PlayerClampHeight;
                }

                if (rectangle.Bottom > GameConstants.WindowHeight)
                {
                    rectangle.Y = GameConstants.WindowHeight - rectangle.Height;
                }
            }
        }