Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package elec0.proceduralCity;
- import java.util.Random;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Image;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.opengl.Texture;
- public class TextureGenerator
- {
- private Image tTex;
- private Graphics g;
- private Random rand;
- private Color cBaseColor;
- private int iHorizMaxWindows; // Horizontal and vertical maximum windows without a window-sized break
- private int iVertMaxWindows;
- public int WIND_WIDTH = 5;
- public int WIND_HEIGHT = 5;
- public int TEX_HEIGHT = 256;
- public int TEX_WIDTH = 256;
- private final int BUFFER_X = WIND_WIDTH * 1;
- private final int BUFFER_Y = WIND_WIDTH * 1;
- private final int BUFFER_MAX_X = TEX_WIDTH - WIND_WIDTH * 2;
- private final int BUFFER_MAX_Y = TEX_HEIGHT - WIND_HEIGHT * 2;
- public TextureGenerator()
- { }
- public Texture generate()
- {
- rand = new Random();
- try {
- tTex = new Image(TEX_WIDTH, TEX_HEIGHT);
- g = tTex.getGraphics();
- } catch (SlickException e) {
- e.printStackTrace();
- }
- iHorizMaxWindows = 10 + rand.nextInt(15);
- iVertMaxWindows = 10 + rand.nextInt(25);
- int colors = 3;
- int pick = rand.nextInt(colors);
- // Pick default color, either blue, white, or orange
- if(pick == 0) // White
- {
- cBaseColor = Color.white;
- }
- else if(pick == 1) // Orange
- {
- cBaseColor = new Color(200 + rand.nextInt(55), 125 + rand.nextInt(155), 0);
- }
- else if(pick == 2)
- {
- cBaseColor = new Color(0, 125 + rand.nextInt(75), 125 + rand.nextInt(75));
- }
- g.setColor(Color.black);
- g.fillRect(0, 0, TEX_WIDTH, TEX_HEIGHT);
- g.flush();
- for(int y = BUFFER_Y; y < BUFFER_MAX_Y; y += WIND_HEIGHT)
- {
- for(int x = BUFFER_X; x < BUFFER_MAX_X; x += WIND_WIDTH)
- {
- if(y % (iVertMaxWindows*WIND_HEIGHT) == 0 || x % (iHorizMaxWindows*WIND_WIDTH) == 0)
- {
- // Skip window for break
- g.setColor(new Color(0, 0, 5));
- g.fillRect(x, y, WIND_WIDTH, WIND_HEIGHT);
- g.flush();
- }
- else
- {
- float fPercent = randomInRange(0, .1f);
- if(rand.nextFloat() < fPercent)
- {
- genWindow(x, y, true);
- }
- else
- genWindow(x, y, false);
- }
- }
- }
- return tTex.getTexture();
- }
- private void genWindow(int x, int y, boolean lit)
- {
- if(lit)
- {
- float percent = randomInRange(0, 0.3f);
- if(rand.nextBoolean()) // Subtract or add
- percent *= -1;
- float fR = cBaseColor.r*percent + cBaseColor.r;
- float fG = cBaseColor.g*percent + cBaseColor.g;
- float fB = cBaseColor.b*percent + cBaseColor.b;
- if(fR > 255)
- fR = 255;
- if(fG > 255)
- fG = 255;
- if(fB > 255)
- fB = 255;
- Color col = new Color(fR, fG, fB);
- g.setColor(col);
- g.fillRect(x, y, WIND_WIDTH, WIND_HEIGHT);
- g.flush();
- }
- else
- {
- int col = 0;
- if(rand.nextBoolean())
- col = rand.nextInt(10);
- g.setColor(new Color(col, col, col));
- g.fillRect(x, y, WIND_WIDTH, WIND_HEIGHT);
- g.flush();
- }
- }
- private float randomInRange(float min, float max)
- {
- return (float)Math.random() * (max-min) + min;
- }
- // public Texture getTexture()
- // {
- // return tTex.getTexture();
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement