framerate control and better coloring
This commit is contained in:
parent
6bd7dd94bc
commit
8d46466fdf
|
@ -16,22 +16,25 @@ cellsW and cellsH must be an uneven number because we always need to skip 1 (= t
|
|||
|
||||
*/
|
||||
|
||||
final int cellsW = 15 * 3; // cells in 1 row
|
||||
final int cellsH = 9 * 3; // cells in 1 col
|
||||
final int cellsW = 15 * 3 * 3; // cells in 1 row
|
||||
final int cellsH = 9 * 3 * 3; // cells in 1 col
|
||||
final int cellAmount = cellsW * cellsH; // total cells
|
||||
ArrayList<Cell> Cells = new ArrayList<Cell>();
|
||||
float scaleW = -1, scaleH = -1; // Width and Height of a Cell
|
||||
|
||||
ArrayList<Cell> path = new ArrayList<Cell>(); // Current path from Start to currentCell
|
||||
Cell currentCell; // Cell currently being processed
|
||||
Cell currentCell, inBetween; // Cell currently being processed
|
||||
|
||||
ArrayList<Cell> solved; // Solved Path from start to finish
|
||||
boolean isDone = false; // Finish declarator
|
||||
|
||||
Cell start, finish;
|
||||
|
||||
int normalFPS = 60;
|
||||
int slowmoFPS = 5;
|
||||
|
||||
void setup() {
|
||||
fullScreen();
|
||||
fullScreen(P3D);
|
||||
|
||||
// Initialize Cells in order
|
||||
for (int y = 0; y < cellsH; y++) {
|
||||
|
@ -50,7 +53,15 @@ void setup() {
|
|||
scaleW = (float) width / cellsW;
|
||||
scaleH = (float) height / cellsH;
|
||||
|
||||
frameRate(4);
|
||||
frameRate(normalFPS);
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
frameRate(slowmoFPS);
|
||||
}
|
||||
|
||||
void mouseReleased() {
|
||||
frameRate(normalFPS);
|
||||
}
|
||||
|
||||
void draw() {
|
||||
|
@ -64,6 +75,7 @@ void draw() {
|
|||
}
|
||||
solved.add(currentCell);
|
||||
isDone = true;
|
||||
frameRate(1000);
|
||||
}
|
||||
|
||||
// Save Cells for coloring
|
||||
|
@ -72,6 +84,8 @@ void draw() {
|
|||
|
||||
// The actual Algorithm step by step of the generation.
|
||||
currentCell.state = true;
|
||||
if (inBetween != null)
|
||||
inBetween.state = true;
|
||||
ArrayList<Cell> neighbors = currentCell.getNeighbors();
|
||||
if (neighbors.size() > 0) {
|
||||
// If Cell has Neighbors, choose a random one,
|
||||
|
@ -80,8 +94,7 @@ void draw() {
|
|||
|
||||
int randomIndex = floor(random(neighbors.size()));
|
||||
Cell next = neighbors.get(randomIndex);
|
||||
Cell inBetween = next.getBetween(currentCell);
|
||||
inBetween.state = true;
|
||||
inBetween = next.getBetween(currentCell);
|
||||
|
||||
path.add(currentCell);
|
||||
|
||||
|
@ -92,21 +105,15 @@ void draw() {
|
|||
}
|
||||
}
|
||||
savedCells.add(next);
|
||||
} else {
|
||||
// If no neighbors that are "dead" exist, backtrack to find some
|
||||
if (path.size() > 0) {
|
||||
currentCell = path.get(path.size()-1);
|
||||
path.remove(path.size()-1);
|
||||
}
|
||||
}
|
||||
|
||||
// For-Each loop to display all cells
|
||||
for (Cell c : Cells) {
|
||||
color col = (c.state) ? color(0) : color(255); // Set color according to state
|
||||
if (c == finish) { col = color(0,255,0); }; // Set Goal to Green
|
||||
if (c == start && currentCell != start) { col = color(255,0,0); }; // Set Goal to Green
|
||||
if (c == savedCells.get(0)) { col = color(0,0,255); }; // Set current Cell to Blue
|
||||
if (c == savedCells.get(savedCells.size()-1)) { col = color(255,0,0); }; // Set chosen Neighbor to Red
|
||||
if (c == finish) { col = color(0,255,0); }; // Set Goal to Green
|
||||
if (c == start) { col = color(255,0,0); }; // Set Start to Red
|
||||
if (c == savedCells.get(0) && !isDone) { col = color(0,0,255); }; // Set current Cell to Blue
|
||||
if (c == currentCell && savedCells.size() > 1) { col = color(255,0,0); }; // Set chosen Neighbor to Red
|
||||
// Set other Neighbors to Yellow
|
||||
for (int i = 1; i < savedCells.size()-1; i++) {
|
||||
if (c == savedCells.get(i)) {
|
||||
|
@ -138,12 +145,23 @@ void draw() {
|
|||
float px = path.get(i+1).x * scaleW + scaleW/2;
|
||||
float py = path.get(i+1).y * scaleH + scaleH/2;
|
||||
line(cx, cy, px, py);
|
||||
if (i==path.size()-2 && path.get(i+1)!=savedCells.get(0)) {
|
||||
float ccx = savedCells.get(0).x * scaleW + scaleW/2;
|
||||
float ccy = savedCells.get(0).y * scaleH + scaleH/2;
|
||||
line(ccx, ccy, px, py);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
|
||||
// If no neighbors that are "dead" exist, backtrack to find some
|
||||
if (path.size() > 0 && neighbors.size() == 0 && savedCells.size() == 1) {
|
||||
currentCell = path.get(path.size()-1);
|
||||
path.remove(path.size()-1);
|
||||
}
|
||||
|
||||
// Display Framerate
|
||||
noStroke();
|
||||
fill(255,0,0);
|
||||
textSize(24);
|
||||
text(nfs(frameRate, 2, 2),24,24);
|
||||
*/
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user