Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace Asteroids
  9. {
  10.    public class TileGen
  11.     {
  12.        
  13.        private Random random;
  14.        int sX;
  15.        int sY;
  16.        int sH = 60;
  17.        int sW = 60;
  18.        public List<Rectangle> Squares;
  19.        public List<Texture2D> Textures;
  20.        private Texture2D texture;
  21.        bool inter = false;
  22.  
  23.        private Rectangle GenSquare()
  24.        {
  25.            sX = (int)random.Next(1920);
  26.            sY = (int)random.Next(1080);
  27.  
  28.            Rectangle rect = new Rectangle(sX, sY, sW, sH);
  29.            
  30.            return rect;
  31.        }
  32.  
  33.        public void Init()
  34.        {
  35.            Squares = new List<Rectangle>();
  36.            Textures = new List<Texture2D>();
  37.            random = new Random();
  38.        }
  39.  
  40.  
  41.        private Texture2D GenTexture(GraphicsDevice graphics)
  42.        {
  43.  
  44.            Texture2D Texture;
  45.  
  46.            Color color = new Color(
  47.                 (float)random.NextDouble(),
  48.                 (float)random.NextDouble(),
  49.                 (float)random.NextDouble());
  50.  
  51.            Color[] colorData = new Color[100 * 100];
  52.  
  53.            for (int i = 0; i < 10000; i++)
  54.                colorData[i] = color;
  55.  
  56.            Texture = new Texture2D(graphics, sW, sH);
  57.  
  58.            Texture.SetData<Color>(colorData);
  59.  
  60.            return Texture;
  61.  
  62.  
  63.  
  64.        }
  65.  
  66.  
  67.        public void Update(GraphicsDevice graphics)
  68.        {
  69.            for (int i = 0; i <= Squares.Count() - 1; i++)
  70.            {
  71.                if (Squares.Count() <= 150)
  72.                {
  73.                    if (!inter)
  74.                    {
  75.  
  76.                        Squares.Add(GenSquare());
  77.                    }
  78.  
  79.                    inter = checkInter(i);
  80.                }
  81.            }
  82.  
  83.            if (Textures.Count() <= 150)
  84.            {
  85.                Textures.Add(GenTexture(graphics));
  86.            }
  87.  
  88.            texture = GenTexture(graphics);
  89.  
  90.        }
  91.  
  92.        public void Draw(SpriteBatch spriteBatch)
  93.        {
  94.            spriteBatch.Begin();
  95.            for (int i = 0; i < Squares.Count(); i++)
  96.            {
  97.                    spriteBatch.Draw(Textures[i], Squares[i], Color.White);
  98.            }
  99.            spriteBatch.End();
  100.        }
  101.  
  102.        private bool checkInter(int Index)
  103.        {
  104.  
  105.            bool intersected = false;
  106.  
  107.            for (int i = 1; i <= Squares.Count() - 1; i++)
  108.            {
  109.                if (Squares[Index].Intersects(Squares[i - 1]))
  110.                {
  111.                    intersected = true;
  112.                }
  113.  
  114.                else intersected = false;
  115.            }
  116.  
  117.            return intersected;
  118.        }
  119.  
  120.     }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement