Hi everyone, i hope everything is allright for all of you.
I’m looking for advices and/or help to improve a specific part of my code.
So here’s my code so you can run it:
first the main class:
import java.util.Iterator;
int locX, locY;
int offsetX, offsetY;
final int unit = 20;
byte[][] terrain = new byte[30][30]; //30
ArrayList<GrassL> grassl;
ArrayList<GrassD> grassd;
ArrayList<Water> water;
void setup() {
size(601, 601); //601 601
grassl = new ArrayList<GrassL>();
grassd = new ArrayList<GrassD>();
water = new ArrayList<Water>();
randomizeNoise();
}
void draw() {
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
offsetX = i;
offsetY = j;
PVector l = new PVector();
l.x = offsetX*unit;
l.y = offsetY*unit;
boolean TgrassL = false;
boolean TgrassD = false;
boolean Twater = false;
// Out of bounds
if (offsetX < 0 || offsetX > 29 || offsetY < 0 || offsetY > 29) fill(0xff808080); //grey
// Draw based on terrain
else {
if (terrain[offsetX][offsetY]>25){ //25 //22
TgrassD = true;
}
if (terrain[offsetX][offsetY]>22){ //25 //22
TgrassD = true;
}
else if (terrain[offsetX][offsetY]>18){ //18 //14
TgrassL = true;
}
else if (terrain[offsetX][offsetY]>14){ //18 //14
TgrassL = true;
}
else {
Twater = true;
}
}
if(TgrassL) grassl.add(new GrassL(l));
if(TgrassD) grassd.add(new GrassD(l));
if(Twater) water.add(new Water(l));
}
}
//collision detection
for(int a = 0; a < water.size(); a++){
Water w1 = water.get(a);
for(int b = 0; b < water.size(); b++){
Water w2 = water.get(b);
for(int c = 0; c < water.size(); c++){
Water w3 = water.get(c);
for(int d = 0; d < water.size(); d++){
Water w4 = water.get(d);
for(int e = 0; e < grassl.size(); e++){
GrassL gl1 = grassl.get(e);
for(int f = 0; f < grassl.size(); f++){
GrassL gl2 = grassl.get(f);
boolean left = false;
boolean right = false;
boolean top = false;
boolean bottom = false;
boolean leftBottom = false;
boolean rightBottom = false;
boolean leftTop = false;
boolean rightTop = false;
float dist = dist(w1.position.x,w1.position.y,gl1.position.x,gl1.position.y);
if(dist == 20 ){
gl1.skin = 1;
if(w1.position.x +20 == w2.position.x &&
w1.position.x -20 == gl1.position.x &&
w1.position.y +20 == w3.position.y &&
w1.position.y -20 == w4.position.y) left = true;
if(w1.position.x +20 == gl1.position.x &&
w1.position.x -20 == w2.position.x &&
w1.position.y +20 == w3.position.y &&
w1.position.y -20 == w4.position.y) right = true;
if(w1.position.x +20 == w2.position.x &&
w1.position.x -20 == w3.position.x &&
w1.position.y +20 == w4.position.y &&
w1.position.y -20 == gl1.position.y) top = true;
if(w1.position.x +20 == w2.position.x &&
w1.position.x -20 == w3.position.x &&
w1.position.y +20 == gl1.position.y &&
w1.position.y -20 == w4.position.y) bottom = true;
if(w1.position.x +20 == w2.position.x &&
w1.position.x -20 == gl1.position.x &&
w1.position.y +20 == gl2.position.y &&
w1.position.y -20 == w3.position.y) leftBottom = true;
if(w1.position.x +20 == w2.position.x &&
w1.position.x -20 == gl1.position.x &&
w1.position.y +20 == w3.position.y &&
w1.position.y -20 == gl2.position.y) rightBottom = true;
}
if(left == true ) w1.skinWater = 1;
if(right == true ) w1.skinWater = 2;
if(top == true ) w1.skinWater = 3;
if(bottom == true) w1.skinWater = 4;
if(leftBottom == true) w1.skinWater = 5;
if(rightBottom == true) w1.skinWater = 6;
if(leftTop == true) w1.skinWater = 7;
if(rightTop == true) w1.skinWater = 8;
}
}
}
}
}
}
Iterator<GrassL> GL = grassl.iterator();
while(GL.hasNext()){
GrassL gl = GL.next();
gl.display();
}
Iterator<GrassD> GD = grassd.iterator();
while(GD.hasNext()){
GrassD gd = GD.next();
gd.display();
}
Iterator<Water> WT = water.iterator();
while(WT.hasNext()){
Water wt = WT.next();
wt.display();
}
}
void keyPressed() {
// SPACE BAR or any key
randomizeNoise();
}
void randomizeNoise() {
int seed;
seed = int(random(11111));
//noiseSeed( seed );
//println(int(seed) );
noiseSeed( 179 ); //179
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 30; j++) {
terrain[i][j] = byte(ceil(noise(i*0.05,j*0.05)*60));
}
}
}
and the three tabs for the tiles:
class GrassD{
ArrayList<PVector> grassd;
public PVector position;
int r;
GrassD(PVector l){
position = l.get();
r = 20;
grassd = new ArrayList<PVector>();
}
void add(PVector l){
grassd.add(l.get());
}
void display(){
fill(0xff005000);
rect(position.x,position.y,r,r);
}
ArrayList<PVector> getGrassD(){
return grassd;
}
PVector position(){
return position;
}
}
class GrassL{
ArrayList<PVector> grassl;
public PVector position;
int r;
int skin;
GrassL(PVector l){
position = l.get();
r = 20;
grassl = new ArrayList<PVector>();
}
void add(PVector l){
grassl.add(l.get());
}
void display(){
fill(0xff008000);
rect(position.x,position.y,r,r);
switch(skin){
case 0:
break;
case 1:
fill(255,0,0);
text("X",position.x+5,position.y+15);
break;
}
}
ArrayList<PVector> getGrassL(){
return grassl;
}
PVector position(){
return position;
}
}
class Water{
ArrayList<PVector> water;
public PVector position;
int r;
int skinWater;
Water(PVector l){
position = l.get();
r = 20;
water = new ArrayList<PVector>();
}
void add(PVector l){
water.add(l.get());
}
void display2(){
fill(255,0,0);
text("O",position.x+5,position.y+15);
}
void display(){
fill(0xff0000c0);
rect(position.x,position.y,r,r);
switch(skinWater){
case 0: //<>//
fill(0,0,0);
text("X",position.x+5,position.y+15);
break;
case 1:
fill(0,0,0);
text("1",position.x+5,position.y+15);
break;
case 2:
fill(0,0,0);
text("2",position.x+5,position.y+15);
break;
case 3:
fill(0,0,0);
text("3",position.x+5,position.y+15);
break;
case 4:
fill(0,0,0);
text("4",position.x+5,position.y+15);
break;
case 5:
fill(0,0,0);
text("5",position.x+5,position.y+15);
break;
case 6:
fill(0,0,0);
text("6",position.x+5,position.y+15);
break;
case 7:
fill(0,0,0);
text("7",position.x+5,position.y+15);
break;
case 8:
fill(0,0,0);
text("8",position.x+5,position.y+15);
break;
case 9:
fill(000000);
text("9",position.x+5,position.y+15);
break;
case 10:
fill(0,0,0);
text("10",position.x+5,position.y+15);
break;
}
}
ArrayList<PVector> getWater(){
return water;
}
PVector position(){
return position;
}
}
So the part of my code i’m looking to improve is (i bet you allready seen it) in the collision detection:
It’s all the for(int…) i use.
I use them because i don’t know a better way to do it.
I need them to check the four adjacents tile for each water tile and then put the correct number on it (see the example)
.
But it’s clearly not effective.
I imagine that is a noob question but i didn’t find a better way to do it alone.
Thanks for your times.