Beispiel #1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            IsMouseVisible = true;

            graphics.PreferredBackBufferHeight = 600;
            graphics.PreferredBackBufferWidth  = 800;
            graphics.ApplyChanges();

            world = new Quadtree <Polygon>(new RectangleF(0, 0, 800, 600));

            poly1 = new Polygon(Vector2.Zero, new List <Vector2>()
            {
                new Vector2(25, 0),
                new Vector2(38, 12),
                new Vector2(25, 38),
                new Vector2(0, 25)
            });

            world.Insert(new Polygon(new Vector2(80, 80), new List <Vector2>()
            {
                new Vector2(50, 50),
                new Vector2(100, 0),
                new Vector2(150, 150)
            }));

            world.Insert(new Polygon(new Vector2(400, 200), new List <Vector2>()
            {
                new Vector2(0, 50),
                new Vector2(50, 0),
                new Vector2(150, 80),
                new Vector2(160, 200),
                new Vector2(-10, 190)
            }));

            world.Insert(ShapePrimitives.Circle(new Vector2(600, 200), 60, 10));
            world.Insert(ShapePrimitives.BezelRectangle(new Vector2(60, 60), new Vector2(160, 220), 15));


            world.Insert(ShapePrimitives.Rectangle(new Vector2(620, 200), new Vector2(660, 230)));

            world.Insert(ShapePrimitives.Rectangle(new Vector2(0, 400), new Vector2(30, 430)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(30, 400), new Vector2(60, 430)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(60, 400), new Vector2(90, 430)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(90, 400), new Vector2(120, 430)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(120, 400), new Vector2(150, 430)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(150, 400), new Vector2(180, 430)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(0, 430), new Vector2(30, 460)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(30, 430), new Vector2(60, 460)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(60, 430), new Vector2(90, 460)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(90, 430), new Vector2(120, 460)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(120, 430), new Vector2(150, 460)));
            world.Insert(ShapePrimitives.Rectangle(new Vector2(150, 430), new Vector2(180, 460)));

            base.Initialize();
        }
Beispiel #2
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)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            MouseState mouse = Mouse.GetState();

            if (mouse.LeftButton == ButtonState.Pressed && lastMouse.LeftButton == ButtonState.Released)
            {
                world.Insert(ShapePrimitives.BezelRectangle(new Vector2(mouse.X, mouse.Y), new Vector2(mouse.X + 20, mouse.Y + 20), 5));
            }
            lastMouse = mouse;

            float         deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            KeyboardState ks        = Keyboard.GetState();
            Vector2       velocity  = Vector2.Zero;
            float         speed     = 90; // a link to the past link moves at 90 pixels per second!

            if (ks.IsKeyDown(Keys.W))
            {
                velocity += new Vector2(0, -speed * deltaTime);
            }
            if (ks.IsKeyDown(Keys.S))
            {
                velocity += new Vector2(0, speed * deltaTime);
            }
            if (ks.IsKeyDown(Keys.A))
            {
                velocity += new Vector2(-speed * deltaTime, 0);
            }
            if (ks.IsKeyDown(Keys.D))
            {
                velocity += new Vector2(speed * deltaTime, 0);
            }

            world.ClearDebugTag();
            List <Polygon> polys = world.Retrieve(new RectangleF(poly1.Origin.X + poly1.BoundingBox.Left, poly1.Origin.Y + poly1.BoundingBox.Top, poly1.BoundingBox.Width, poly1.BoundingBox.Height));

            foreach (IPolygon poly in polys)
            {
                poly.AddTag("debug");
            }

            Window.Title = polys.Count.ToString();
            poly1.Move(polys, velocity);


            base.Update(gameTime);
        }