I am trying to make an obstacle game where you are supposedly driving and there are trees you need to avoid (for now those are just rectangles). Since I am pretty new to processing, I tried some stuff out, and it all seemed to work (kinda), but when I added more that one obstacle (obst1), the translation I do at the beginning of the setup gets changed to a value that is lower that what I want (the translation should be -2000 on the z axis, but when I add obst2, it suddenly is only 1000). I really can’t find the problem.
If you have any other tips on how I should actually handle the overall game, in terms of how it functions, I would really appreciate that.
Code:
boolean moving = false;
boolean restart = false;
int amount = 4;
int numberWaves = 10;
int wave = 0;
int size = 100;
//Obstacle obst[][] = new Obstacle[numberWaves][amount];
Obstacle obst1, obst2, obst3;
void setup() {
size(1000, 1000, P3D);
background(200);
rectMode(CENTER);
//for (int w = 0; w < numberWaves; w++) {
// for (int i = 0; i < amount; i++) {
// //obst[w][i] = new Obstacle(size, ((int) random((width*5) / size)) * (2 * size));
// obst[w][i] = new Obstacle(size, -width + (i * (5*size)));
// }
//}
obst1 = new Obstacle(size, 100);
obst2 = new Obstacle(size, 900);
obst3 = new Obstacle(size, 1300);
//translate(0, 0, -2000);
pushMatrix();
}
void draw() {
background(200);
popMatrix();
//for (int i = 0; i < amount; i++) {
// obst[wave][i].moveCar(8);
// obst[wave][i].obstacleInteract();
// obst[wave][i].nextWave();
//}
obst1.moveCar(8);
obst1.obstacleInteract();
obst1.nextWave();
obst2.moveCar(8);
obst2.obstacleInteract();
obst2.nextWave();
obst3.moveCar(8);
obst3.obstacleInteract();
obst3.nextWave();
pushMatrix();
//println(obst[0].xCar , obst[1].xCar, obst[2].xCar, obst[3].xCar, obst[4].xCar);
}
class Obstacle {
float size, speed, xCar;
float distance = 0;
float z = 0;
boolean wavePassed = false;
Obstacle(float s, float x) {
size = s;
xCar = x;
}
void moveCar(float speed) {
if (mousePressed && mousePressed == (mouseButton == LEFT) && restart == false) { //Let's you drive when you hold a mousebutton
moving = true;
} else {
moving = false;
}
if (moving == true && restart == false) { //This part gives you the ability to move left and right while driving
if (keyPressed && keyPressed == (keyCode == LEFT)) {
xCar += speed;
}
if (keyPressed && keyPressed == (keyCode == RIGHT)) {
xCar -= speed;
}
}
if (moving == true && restart == false) { //Moves the objects towards you with translate, as if you are driving towards them
translate(0, 0, z);
z += 0.1;
} else {
z = 0;
}
}
void obstacleInteract() {
//for (int i = 0; i < amount; i++) {
distance += z;
translate(0, 0, -2000);
rect(xCar, -500, size, height*3); //draws the obstacles
if (distance > 2000 && moving == true) { //checks if the obstacle is hit (when it is still in screen) and gives a notification that you died or survived
if (dist(width/2, 0, xCar, 0) < 500) {
moving = false;
restart = true;
println("you are dead, not big suprise");
} else {
wavePassed = true;
translate(0,0,-distance);
//}
}
}
}
void nextWave() {
if (wavePassed == true && wave < numberWaves -1) {
wave += 1;
wavePassed = false;
distance = 0;
}
println("distance = ", distance );
println("z = ", z );
}
}