public void CheckWallCollisions(Map map, Character c)
        {
            Rectangle charRect = new Rectangle((int)c.WorldPosition.X, (int)c.WorldPosition.Y, 32, 32);

            for (int y = 0; y < map.Height; y++)
            {
                for (int x = 0; x < map.Width; x++)
                {
                    Tile tile = map.getTile(x,y);

                    if (tile.type == 2 && c.Collision.Intersects(tile.GetTileBounds()))
                    {
                        if (c.WorldPosition.X < tile.worldPosition.X + 32)
                        {
                            c.worldPosition.X -= c.velocity.X;
                            c.velocity.X = 0;
                        }
                        if (c.WorldPosition.X + 32 > tile.worldPosition.X)
                        {
                            c.worldPosition.X += c.velocity.X;
                            c.velocity.X = 0;
                        }

                        if (c.WorldPosition.Y < tile.worldPosition.Y + 32)
                        {
                            c.worldPosition.Y -= c.velocity.Y;
                            c.velocity.Y = 0;
                        }
                        if (c.WorldPosition.Y + 32 > tile.worldPosition.Y)
                        {
                            c.worldPosition.Y += c.velocity.Y;
                            c.velocity.Y = 0;
                        }
                    }
                }
            }
        }
예제 #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);
            colMan = new CollisionManager();

            currKeyState = Keyboard.GetState();
            prevKeyState = currKeyState;

            currMouseState = Mouse.GetState();
            prevMouseState = currMouseState;

            font = Content.Load<SpriteFont>("font");

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

            c = new Character(new Vector2(0, 0), new Vector2(520,520), Content.Load<Texture2D>(@"hero"), Content.Load<Texture2D>("testProjectile"));
            c.position = new Vector2((graphics.PreferredBackBufferWidth / 2), (graphics.PreferredBackBufferHeight / 2));

            cam = new Camera(c.worldPosition);
            cam.camOffset = c.position;
            cam.width = 800;
            cam.height = 480;

            map = FileManager.LoadMap("overworld", mapWidth, mapHeight, Content);
            /*
            string mapName = "temp";

            int width = 10;
            int height = 10;

            Tile[,] tiles = new Tile[width, height];

            Random rand = new Random();

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int num = rand.Next(0, 3);

                    Tile t = new Tile();
                    t.worldPosition = new Vector2(x * 32, y * 32);

                    if (num == 0)
                    {
                        t.type = 0;
                        t.sprite = Content.Load<Texture2D>("water");
                    }
                    if (num == 1)
                    {
                        t.type = 1;
                        t.sprite = Content.Load<Texture2D>("grass");
                    }
                    if (num == 2)
                    {
                        t.type = 2;
                        t.sprite = Content.Load<Texture2D>("rock");
                    }

                    tiles[x, y] = t;
                }

            }

            map = new Map(mapName, tiles, width, height);
            */
            map.Events = events;

            npcs = new List<NPC>();
            cam = new Camera(c.worldPosition);

            List<Tile> paletteTiles = new List<Tile>();
            Tile grassTile = new Tile(0, new Vector2(0, 0), Content.Load<Texture2D>("grass"));
            Tile waterTile = new Tile(0, new Vector2(0, 32), Content.Load<Texture2D>("water"));
            Tile rockTile = new Tile(0, new Vector2(0, 64), Content.Load<Texture2D>("rock"));

            tilePalette = new List<Texture2D>();
            tilePalette.Add(Content.Load<Texture2D>("water"));
            tilePalette.Add(Content.Load<Texture2D>("grass"));
            tilePalette.Add(Content.Load<Texture2D>("rock"));

            tile = Content.Load<Texture2D>("selectedTile");
            mapMode = false;
            selectedTile = 0;
        }