Пример #1
0
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Initialize level
            Texture2D[,] tiles = new Texture2D[2, 2];
            tiles[0, 0] = Content.Load<Texture2D>("t1");
            tiles[0, 1] = Content.Load<Texture2D>("t2");
            tiles[1, 0] = Content.Load<Texture2D>("t3");
            tiles[1, 1] = Content.Load<Texture2D>("t4");

            level = new LevelObs(tiles, LoadObstacles());

            //Initialize scene
            camera = new Camera(Vector2.Zero, new Size(800, 600));
            scene = new Scene(this, spriteBatch, level, camera);
            Components.Add(scene);

            //Temporary sprite shapeset
            Polygon shape =
                new Polygon(new Vector2[] { new Vector2(5, 38),
                                            new Vector2(24, 38),
                                            new Vector2(26, 48),
                                            new Vector2(1, 48)});

            List<Polygon> shapeSet = new List<Xebab.Helpers.Polygons.Polygon>(1);
            shapeSet.Add(shape);

            //Initialize sprite
            Texture2D spritesheet = Content.Load<Texture2D>("guy01");
            sprite = new AnimatedSprite(scene.ContentHandler, shapeSet,
                spritesheet, new Size(31, 48));
            scene.ContentHandler.SpriteHandler.SpawnSprite(sprite);
            sprite.Position = new Vector2(300);
            sprite.AddBehavior(new CameraFollowerBehavior(sprite, camera, level));
        }
Пример #2
0
        private void DrawPolygon(Polygon p, Color color, int thickness = 1)
        {
            int n = p.Vertexes.Length;

            for (int i = 0; i < n; i++)
            {
                Primitives2D.DrawLine(spriteBatch, p.Vertexes[i] - camera.ViewportPosition,
                    p.NextVertex(i) - camera.ViewportPosition, color, thickness);
            }
        }
Пример #3
0
        public bool Hits(Polygon other)
        {
            //First test myself against other polygon...
            for (int i = 0; i < this.VertexCount; i++)
            {
                Interval otherProjection = other.GetProjection(this.norms[i], this.verts[0]);
                if (!this.selfProjections[i].Intersects(otherProjection))
                {
                    //other polygon's projection is not intersecting mine
                    return false;
                }
            }

            //..then test other polygon against me
            for (int i = 0; i < other.VertexCount; i++)
            {
                Interval thisProjection = this.GetProjection(other.norms[i], other.verts[0]);
                if(!other.selfProjections[i].Intersects(thisProjection))
                {
                    //my projection is not intersecting other polygon's one
                    return false;
                }
            }

            //All projections are respectively intersecting along all axes:
            //polygons are overlapping
            return true;
        }
Пример #4
0
        private void LoadLevel()
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.Filter = "XML Document|*.xml";
            fileDialog.Title = "Load level";
            fileDialog.ShowDialog();

            if (fileDialog.FileName == "") return;

            XmlTextReader xmlReader = new XmlTextReader(fileDialog.FileName);

            List<Polygon> pols = new List<Polygon>();
            xmlReader.Read();
            xmlReader.Read();
            xmlReader.Read();
            while (xmlReader.Name == "Polygon") //Read polygon
            {
                List<Vector2> vertexes = new List<Vector2>();
                xmlReader.Read();	//Read Vertex
                while (xmlReader.Name == "Vertex")
                {
                    Vector2 v = new Vector2();
                    xmlReader.Read();	//Read X
                    xmlReader.Read();
                    //v.X = float.Parse(xmlReader.Value, System.Globalization.NumberStyles.);
                    v.X = (float)Convert.ToDouble(xmlReader.Value, CultureInfo.InvariantCulture.NumberFormat);
                    xmlReader.Read();	//Read Y
                    xmlReader.Read();
                    xmlReader.Read();
                    v.Y = (float)Convert.ToDouble(xmlReader.Value, CultureInfo.InvariantCulture.NumberFormat);
                    vertexes.Add(v);

                    xmlReader.Read();
                    xmlReader.Read();
                    xmlReader.Read();	//Read next node
                }

                //polygon ends here
                pols.Add(new Polygon(vertexes.ToArray()));
                xmlReader.Read();
            }

            //EOF
            xmlReader.Close();

            polys = pols;
            activePolygon = null;
            camera.SetViewportPosition(Vector2.Zero);
        }
Пример #5
0
        private void BuildPolys(Vector2 rCursor, KeyboardState keyb)
        {
            if (keyb.IsKeyDown(Keys.LeftControl))
            {
                //did user clicked over a pre-existent polygon?
                foreach (Polygon p in polys)
                {
                    if (p.GetBoundingBox().Contains(rCursor.ToPoint()))
                    {
                        activePolygon = p;
                        break;
                    }
                }
            }
            //are we building a polygon from scratch?
            else if (activePolygon == null)
            {
                activePolygon = new Polygon(new Vector2[1] { rCursor });
            }
            else if (activePolygon != null)
            {
                //.. add another vertex to the polygon vertex list...
                Vector2[] vertexes = new Vector2[activePolygon.VertexCount + 1];
                activePolygon.Vertexes.CopyTo(vertexes, 0);
                vertexes[vertexes.Length - 1] = rCursor;

                //.. and recreate it
                polys.Remove(activePolygon);
                activePolygon = Polygon.ConvexHull(vertexes);
                polys.Add(activePolygon);
            }
        }
Пример #6
0
        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyb = Keyboard.GetState();
            MouseState mouse = Mouse.GetState();
            cursor = new Vector2(mouse.X, mouse.Y);

            // Allows the game to exit
            if (keyb.IsKeyDown(Keys.Escape))
                this.Exit();

            //reset camera position
            if (keyb.IsKeyDown(Keys.Space))
                camera.SetViewportPosition(Vector2.Zero);

            //Handle camera dragging
            MoveCamera(mouse);

            Vector2 relativeCursor = cursor + camera.ViewportPosition;
            //If user clicked the canvas...
            if (mouse.LeftButton == ButtonState.Pressed)
            {
                if ((keyb.IsKeyDown(Keys.LeftShift) || keyb.IsKeyDown(Keys.RightShift))
                    && activePolygon != null)
                {
                    // move the entire polygon to mouse cursor
                    activePolygon.Translate(relativeCursor - activePolygon.GetTopLeftPosition());
                }
                else
                {
                    BuildPolys(relativeCursor, keyb);
                }
            }

            //If user is inserting poly and clicked enter, finish drawing poly (if it's valid)
            if (keyb.IsKeyDown(Keys.Enter) && activePolygon != null
                && activePolygon.VertexCount >= 3)
            {
                activePolygon = null;
            }

            //If user has an active poly and presses cancel, remove the selecte polygon
            if (keyb.IsKeyDown(Keys.Delete) && activePolygon != null)
            {
                polys.Remove(activePolygon);
                activePolygon = null;
            }

            if (keyb.IsKeyDown(Keys.Back))
            {
                activePolygon = null;
            }

            //Save work when user presses F8
            if (keyb.IsKeyDown(Keys.F8))
            {
                SaveLevel();
            }

            //Load level when user presses F6
            if (keyb.IsKeyDown(Keys.F6))
            {
                LoadLevel();
            }

            base.Update(gameTime);
        }