88 lines
2.8 KiB
Plaintext
88 lines
2.8 KiB
Plaintext
import java.util.ArrayDeque;
|
|
import pathfinder.*;
|
|
|
|
PImage img_houses;
|
|
PImage img_streets;
|
|
PImage img_spawnPoints;
|
|
int i_biggestHouse;
|
|
|
|
ArrayList<SpawnArea> spawnAreas = new ArrayList<SpawnArea>();
|
|
|
|
void initHouses() {
|
|
img_houses.loadPixels();
|
|
for (int x = 0; x < img_houses.width; x++) {
|
|
for (int y = 0; y < img_houses.height; y++) {
|
|
if (int(red(img_houses.pixels[y*img_houses.width+x])) == 187) { //red must be 187 for a pixel to be part of a house
|
|
spawnAreas.add(new SpawnArea(x, y));
|
|
img_houses.updatePixels();
|
|
img_houses.loadPixels();
|
|
}
|
|
}
|
|
}
|
|
img_houses.updatePixels();
|
|
for(SpawnArea spawn : spawnAreas){
|
|
spawn.i_size = int(map(spawn.i_size,0,i_biggestHouse,0,100));
|
|
}
|
|
}
|
|
|
|
class SpawnArea {
|
|
PVector v2_center;
|
|
int i_size;
|
|
int i_ID;
|
|
|
|
void display(){
|
|
//text(i_size,v2_center.x,v2_center.y);
|
|
text(i_ID,v2_center.x,v2_center.y);
|
|
}
|
|
ArrayDeque<PVector> v2d_selected = new ArrayDeque<PVector>();
|
|
ArrayList<PVector> v2d_points = new ArrayList<PVector>();
|
|
boolean isToSelect(int px, int py, int[] pxl, int pw, int ph, int orgColor) {
|
|
if (px < 0 || px >= pw || py < 0 || py >= ph)
|
|
return false;
|
|
return pxl[px + py * pw] == orgColor;
|
|
}
|
|
PVector v2_randomSpawnPoint(){
|
|
return v2d_points.get(int(random(0,v2d_points.size())));
|
|
}
|
|
|
|
SpawnArea(int startX, int startY) {
|
|
i_ID = spawnAreas.size();
|
|
int [] pxl = img_houses.pixels;
|
|
int pw = img_houses.width;
|
|
int ph = img_houses.height;
|
|
int orgColor = pxl[startX + startY * pw];
|
|
PVector v2_p = new PVector(startX, startY);
|
|
//color randCol = color(int(random(0, 255)), int(random(0, 255)), int(random(0, 255)));
|
|
v2d_selected.add(v2_p);
|
|
int west, east;
|
|
while (!v2d_selected.isEmpty () ) { //&& q.size() < 500) {
|
|
v2_p = v2d_selected.removeFirst();
|
|
if (isToSelect(int(v2_p.x), int(v2_p.y), pxl, pw, ph, orgColor)) {
|
|
west = east = int(v2_p.x);
|
|
while (isToSelect(--west, int(v2_p.y), pxl, pw, ph, orgColor));
|
|
while (isToSelect(++east, int(v2_p.y), pxl, pw, ph, orgColor));
|
|
for (int x = west + 1; x < east; x++) {
|
|
v2d_points.add(new PVector(x, int(v2_p.y)));
|
|
pxl[x + int(v2_p.y) * pw] = color(188, 188, 188); //important for not selecting the same area more than once
|
|
if (isToSelect(x, int(v2_p.y) - 1, pxl, pw, ph, orgColor))
|
|
v2d_selected.add(new PVector(x, int(v2_p.y) - 1));
|
|
if (isToSelect(x, int(v2_p.y) + 1, pxl, pw, ph, orgColor))
|
|
v2d_selected.add(new PVector(x, v2_p.y + 1));
|
|
}
|
|
}
|
|
}
|
|
i_size = v2d_points.size();
|
|
PVector avgPoint = new PVector(0,0);
|
|
for (PVector point : v2d_points) {
|
|
avgPoint.add(point);
|
|
}
|
|
avgPoint.x = avgPoint.x/i_size;
|
|
avgPoint.y = avgPoint.y/i_size;
|
|
v2_center = avgPoint;
|
|
|
|
if(i_size > i_biggestHouse) i_biggestHouse = i_size;
|
|
|
|
}
|
|
|
|
}
|