Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using GrafikaRuchoma_00.CustomClasses;
- namespace GrafikaRuchoma_00
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D background;
- List<List<Tile>> map;
- List<Tile> tools;
- Tile selectedTile = null;
- bool clickLock = false;
- int mapX = 10, mapY = 10;
- int mapLeftOffset = 150;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- this.Window.AllowUserResizing = true;
- this.IsMouseVisible = true;
- }
- protected override void Initialize()
- {
- int tileScaleX = (GraphicsDevice.Viewport.Width - mapLeftOffset) / mapX;
- int tileScaleY = GraphicsDevice.Viewport.Height / mapY;
- #region Mapa Gry
- map = new List<List<Tile>>();
- for (int x = 0; x < mapX; x++)
- {
- map.Add(new List<Tile>());
- for (int y = 0; y < mapY; y++)
- {
- map[x].Add(new Tile(Content.Load<Texture2D>("puste"), new Rectangle((x * tileScaleX) + mapLeftOffset, y * tileScaleY, tileScaleX, tileScaleY), 0));
- }
- }
- #endregion
- #region Tool box
- tools = new List<Tile>();
- Texture2D gameSprites = Content.Load<Texture2D>("kafelki"); // 12 => 100x100
- Texture2D gameSpritesQ = Content.Load<Texture2D>("puste"); // 200x200
- tools.Add(new Tile(gameSpritesQ, new Rectangle(50, 150, 100, 100), 0));
- int i = 0;
- for (int sy = 0; sy < 2; sy++)
- {
- for (int sx = 0; sx < 4; sx++)
- {
- tools.Add(new Tile(gameSprites, new Rectangle(50, 100 + (50 * i), 50, 50), new Rectangle(sx * 100, sy * 100, 100, 100), 0));
- i++;
- }
- }
- #endregion
- base.Initialize();
- }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- background = Content.Load<Texture2D>("background");
- // TODO: use this.Content to load your game content here
- }
- protected override void UnloadContent()
- {
- // TODO: Unload any non ContentManager content here
- }
- protected override void Update(GameTime gameTime)
- {
- // Allows the game to exit
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- #region Mapa Gry
- int tileScaleX = (GraphicsDevice.Viewport.Width - mapLeftOffset) / mapX;
- int tileScaleY = GraphicsDevice.Viewport.Height / mapY;
- for (int x = 0; x < mapX; x++)
- {
- for (int y = 0; y < mapY; y++)
- {
- map[x][y].Position = new Rectangle((x * tileScaleX) + mapLeftOffset, y * tileScaleY, tileScaleX, tileScaleY);
- }
- }
- #endregion
- var mouseState = Mouse.GetState();
- var mousePosition = new Point(mouseState.X, mouseState.Y);
- if (mouseState.LeftButton == ButtonState.Pressed && !clickLock)
- {
- clickLock = true;
- foreach (var item in tools)
- {
- if (item.Position.Contains(mousePosition))
- {
- selectedTile = item;
- selectedTile.Rotation += 1.57F;
- break;
- }
- }
- }
- else if (mouseState.LeftButton == ButtonState.Released)
- {
- clickLock = false;
- }
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- spriteBatch.Draw(background, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.Wheat); // Wyrysowanie tła
- #region Mapa Gry
- for (int x = 0; x < mapX; x++)
- {
- for (int y = 0; y < mapY; y++)
- {
- if (map[x][y].IsPart)
- spriteBatch.Draw(map[x][y].Sprite, map[x][y].Position, map[x][y].CutPart, Color.Wheat, map[x][y].Rotation,
- new Vector2(map[x][y].Position.Width / 2, map[x][y].Position.Height / 2), new SpriteEffects(), 0);
- else
- spriteBatch.Draw(map[x][y].Sprite, map[x][y].Position,
- new Rectangle(0, 0, map[x][y].Sprite.Width, map[x][y].Sprite.Height), Color.Wheat,
- map[x][y].Rotation,
- new Vector2(map[x][y].CutPart.X, map[x][y].CutPart.Y/2), new SpriteEffects(), 0);
- }
- }
- #endregion
- #region Tool box
- for (int i = 0; i < tools.Count; i++)
- {
- if (tools[i].IsPart)
- spriteBatch.Draw(tools[i].Sprite, tools[i].Position, tools[i].CutPart, Color.Wheat, tools[i].Rotation,
- new Vector2(tools[i].Position.Width / 2, tools[i].Position.Height/2), new SpriteEffects(), 0);
- else
- spriteBatch.Draw(tools[i].Sprite, tools[i].Position,
- new Rectangle(0, 0, tools[i].Sprite.Width, tools[i].Sprite.Height), Color.Wheat,
- tools[i].Rotation,
- new Vector2(tools[i].CutPart.X, tools[i].CutPart.Y / 2), new SpriteEffects(), 0);
- }
- #endregion
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- public class Tile
- {
- public Texture2D Sprite { get; set; }
- public Rectangle Position { get; set; }
- public float Rotation { get; set; }
- public bool IsPart { get; set; }
- public Rectangle CutPart { get; set; }
- public Tile(Texture2D sprite, Rectangle position, int rotation)
- {
- Sprite = sprite;
- Position = position;
- Rotation = rotation;
- IsPart = false;
- CutPart = new Rectangle(0, 0, 0, 0);
- }
- public Tile(Texture2D sprite, Rectangle position, Rectangle cutPart, int rotation)
- {
- Sprite = sprite;
- Position = position;
- Rotation = rotation;
- IsPart = true;
- CutPart = cutPart;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement