GalaxyInPixels/src/com/annimon/gipgame/BackgroundCreator.java

94 lines
2.6 KiB
Java
Raw Normal View History

package com.annimon.gipgame;
import java.util.ArrayList;
public class BackgroundCreator {
private static final boolean ANTI_ALIAS = true;
private static final int WIDTH = 5;
private int height = 12;
private ArrayList<int[]> background;
public BackgroundCreator() {
background = new ArrayList<int[]>();
createBackground();
}
public void setHeight(int height) {
this.height = height;
}
public int[][] getBackground() {
int[][] array = background.toArray(new int[0][]);
if (!ANTI_ALIAS) return array;
// Smooth background colors.
int[][] antialias = new int[height][WIDTH];
for(int y = 0; y < height; y++) {
for (int x = 0; x < WIDTH; x++) {
int top = (y > 0) ? array[y - 1][x] : 0;
int bottom = (y < (height - 1)) ? array[y + 1][x] : 0;
int left = (x > 0) ? array[y][x - 1] : 0;
int right = (x < (WIDTH - 1)) ? array[y][x + 1] : 0;
int center = array[y][x];
antialias[y][x] = getAntiAliasColor(new int[] {top, bottom, left, right, center});
}
}
return antialias;
}
private int getAntiAliasColor(int[] colors) {
int red = 0;
int green = 0;
int blue = 0;
for (int i = 0; i < 4; i++) {
int color = colors[i];
red += (color >> 16) & 0xFF;
green += (color >> 8) & 0xFF;
blue += color & 0xFF;
}
red /= 4;
green /= 4;
blue /= 4;
red += (colors[4] >> 16) & 0xFF;
green += (colors[4] >> 8) & 0xFF;
blue += colors[4] & 0xFF;
return 0xFF000000 | (red << 16) | (green << 8) | blue;
}
public void updateBackground(int addValue) {
background.remove(height - 1);
background.add(0, createRow(addValue));
}
private void createBackground() {
for (int i = 0; i < height; i++) {
background.add(createRow(0));
}
}
private int[] createRow(int addValue) {
int[] row = new int[WIDTH];
for (int i = 0; i < WIDTH; i++) {
row[i] = Util.randomSpaceColor(15 + (addValue / 2));
}
if (Util.rand(8) == 5) {
// Create star.
int x = Util.rand(WIDTH);
int add = (addValue / 2);
row[x] = Util.randomColor(10 + add, 60 + add);
}
return row;
}
}