Search code examples
javaprocessing

How to perform an operation in x seconds given a certain attribute in processing


enter image description here I am doing an exercise in processing in which I need to create an intersection between two streets and in each of them will have a traffic light (totaling 4 traffic lights in total), I will have two types of traffic lights those that are in the horizontal street and those that are in the vertical street. When the traffic lights on the vertical street are yellow or red, the ones on the horizontal street will be green, etc. My problem is that I can't create the function that does this, my idea was to create the traffic lights giving opacity to the "lights" and then through a function decrease the opacity to have the sensation that they are turning on and off. To identify which traffic lights are on the horizontal street and which are on the vertical street I created a variable called type where 1 means they are on the vertical street and 0 means they are on the horizontal street. At this moment in my function I am doing as if each "light" stays on for 2 seconds but my intention is to make the red light stay on for 4 seconds and the green and yellow light stay on for 2 seconds each so that the red light can stay on while the other lights (yellow and green) are on at the other traffic lights and then they keep turning on and off. This is my code: First tab:

final color WHITE = color(255,255,255);
final color GREEN = color(0,255,0);
final color DARK_GREEN = color(0,55,0);
final color RED = color(255,0,0);
final color YELLOW = color(255,255,0);
final color DARK_GREY = color(100,100,100);
final color GREY = color(200,200,200);
final color BLACK = color(0,0,0);
final int W_WIDTH = 1000;
final int W_HEIGHT = 700;

carro c1 = new carro(W_WIDTH/2 - 150,W_HEIGHT/2,150,100,0);
carro c2 = new carro(W_WIDTH/2 + 300,W_HEIGHT/2 + 150,150,100,3.15);
carro c3 = new carro(W_WIDTH/2 + 75,W_HEIGHT/2 + 300,150,100,4.7);
carro c4 = new carro(W_WIDTH/2 + 75, W_HEIGHT/2 - 150,150,100,1.58);
carro carros[] = {c1,c2,c3,c4};
lane s1 = new lane(350, W_HEIGHT/2 + 150,0, RED,W_WIDTH/2 - 150,W_HEIGHT/2,150,100,0,1,150);
lane s2 = new lane(500,W_HEIGHT/2 - 150,-300,RED,W_WIDTH/2 + 300,W_HEIGHT/2 + 150,150,100,3.15,0,150);
lane s3 = new lane(650,W_HEIGHT/2 + 300,300,GREEN,W_WIDTH/2 + 75,W_HEIGHT/2 + 300,150,100,4.7,0,150);
lane s4 = new lane(800,W_HEIGHT/2,3.15,GREEN,W_WIDTH/2 + 75, W_HEIGHT/2 - 150,150,100,1.58,1,150);
lane semaforos[] = {s1,s2,s3,s4};

void settings()
{
  size(W_WIDTH, W_HEIGHT);
}

void setup()
{
}

void update()
{
  for(int i = 0;i < semaforos.length;i++)
  {
    semaforos[i].update();
  }
}

void draw()
{
  update();
  noStroke();
  background(DARK_GREEN);
  fill(DARK_GREY);
  rect(0, W_HEIGHT/2, 1000, 150);
  fill(DARK_GREY);
  rect(W_WIDTH/2,0,150,700);
  for(int i = 0; i<carros.length; i++)
  {
    carros[i].draw();
  }
  for(int i = 0; i < semaforos.length;i++)
  {
    semaforos[i].draw();
  }
}

My second tab

class carro
{
  float x;
  float y;
  float velocidade_max;
  float velocidade_atual;
  float angle;
  
  carro(float x,float y, float velocidade_max,float velocidade_atual,float angle)
  {
    this.x = x;
    this.y = y;
    this.velocidade_max = velocidade_max;
    this.velocidade_atual = velocidade_atual;
    this.angle = angle;
  }
  
void draw() {
  pushMatrix();
  translate(x, y);
  rotate(angle); // aplicar o ângulo em radianos
  fill(RED);
  rect(0, 10, 100, 50);
  fill(BLACK);
  fill(RED);
  rect(101,15,25,40);
  fill(BLACK);
  rect(70,60,20,5);
  fill(BLACK);
  rect(15,60,20,5);
  fill(BLACK);
  rect(70,5,20,5);
  fill(BLACK);
  rect(15,5,20,5);
  popMatrix();
}
}

My third tab where i try to turn on the lights:

class lane extends carro {
  float positionx;
  float positiony;
  float angleS;
  color c; // cor inicial do semáforo
  float tipo; //vert ou hor, 1 horizontal o vertical
  float transparencia;
  int startTime;
  float alphav = 150; //transparencia vermelho
  float alphaa = 150; //transparencia amarelo
  float alphaver = 100; //transparencia verde
  int activeLight;

  lane(float positionx, float positiony, float angleS, color c, float tipo, float transparencia, float x, float y, float velocidade_max, float velocidade_atual, float angle) {
    super(x, y, velocidade_max, velocidade_atual, angle);
    this.positionx = positionx;
    this.positiony = positiony;
    this.angleS = angleS;
    this.c = c;
    this.tipo = tipo;
    this.transparencia = transparencia;
    this.activeLight = 0; // começa com o vermelho aceso
    this.startTime = millis(); // armazena o tempo de início
  }

  void update() {
  int elapsedTime = millis() - this.startTime; // calcula o tempo decorrido
  int currentSecond = elapsedTime / 1000; // converte para segundos

  float lightIndex = currentSecond % 6; // tempo total do ciclo é 6 segundos
  
  if (this.tipo == 1) {
    if (lightIndex < 2) {
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
    }
    else if (lightIndex < 4 && lightIndex > 2) {
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
    }
    else if (lightIndex <= 6 && lightIndex > 4) {
      this.alphav = 150;
      this.alphaa = 150;
      this.alphaver = 500;
    }
  }
  else if (this.tipo == 2) {
    if (lightIndex < 2) {
      this.alphav = 500;
      this.alphaa = 150;
      this.alphaver = 150;
    }
    else if (lightIndex < 4 && lightIndex > 2) {
      this.alphav = 150;
      this.alphaa = 500;
      this.alphaver = 150;
    }
    else if (lightIndex < 6 && lightIndex > 4) {
      this.alphav = 150;
      this.alphaa = 150;
      this.alphaver = 500;
    }
  }
}

  void draw() {
    pushMatrix();
    translate(positionx, positiony);
    rotate(angleS);
    fill(BLACK);
    rect(0, 0, 150, 50);
    fill(RED, alphav);
    circle(120, 25, 40);
    fill(GREEN, alphaver);
    circle(75, 25, 40);
    fill(YELLOW, alphaa);
    circle(30, 25, 40);
    fill(BLACK);
    rect(-40, 14, 40, 20);
    popMatrix();
  }
}

Sorry if the code doesn't follow the conventions but I have to try to do it with the "structure" that my teacher decides... And sorry if this is something too simple I started this last week and just did a processing exercise...Someone can help me?


Solution

  • I left everything the way it was counting time and to make the lights of the traffic lights different I added a variable that tells you what color the traffic light starts when you start the program and with a simple if else I was able to do what I wanted. Below is the code that was changed:

        void update()
    {
      float elapsedTime = millis() - this.startTime; // calcula o tempo decorrido
      float currentSecond = elapsedTime / 1000; // converte para segundos
    
      float cycleTime = timev + timea + timever; // tempo total do ciclo
      float lightIndex = currentSecond % cycleTime; // calcula o índice da luz acesa
    
      float distanciax = W_WIDTH/2 - 120;
      float distanciay = W_HEIGHT/2 + 300;
      if (cor_inicio == GREEN) {
        if (lightIndex < this.timever) {
          // vermelho
          this.alphav = 150;
          this.alphaa = 150;
          this.alphaver = 500;
          this.activeLight = 0;
          carros[0].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
          carros[0].wrapAroundScreenx();
          carros[3].x -= velocidade_atual;
          carros[3].wrapAroundScreenx();
        } else if (lightIndex < this.timever + this.timea) {
          // amarelo
          this.alphav = 150;
          this.alphaa = 500;
          this.alphaver = 150;
          this.activeLight = 1;
      if (abs(carros[0].x - distanciax) > 10) {
        carros[0].x += velocidade_atual;
        carros[0].wrapAroundScreenx();
        carros[3].x -= velocidade_atual;
        carros[3].wrapAroundScreenx();
      }
        } else {
          // verde
          this.alphav = 500;
          this.alphaa = 150;
          this.alphaver = 150;
          this.activeLight = 2;
          if(abs(carros[3].x - distanciax) < 10 && carros[3].x < distanciax)
          {
          carros[0].x += velocidade_atual; // move o primeiro carro da esquerda para a direita
          carros[0].wrapAroundScreenx();
          carros[3].x += velocidade_atual;
          carros[3].wrapAroundScreenx();
          }
        }
      } else if (cor_inicio == RED) {
        if (lightIndex < this.timev) {
          // vermelho
          this.alphav = 500;
          this.alphaa = 150;
          this.alphaver = 150;
          this.activeLight = 0;
          if (abs(carros[1].y - distanciay) > 10 && carros[1].y < distanciay) {
           carros[1].y -= velocidade_atual;
           carros[1].wrapAroundScreenx();
           carros[2].y += velocidade_atual;
           carros[2].wrapAroundScreenx();
          }
    
        } else if (lightIndex < this.timev + this.timea) {
          // amarelo
          this.alphav = 150;
          this.alphaa = 150;
          this.alphaver = 500;
          this.activeLight = 1;
          carros[1].y -= velocidade_atual;
          carros[1].wrapAroundScreenx();
          carros[2].y += velocidade_atual;
          carros[2].wrapAroundScreenx();
        } else {
          // verde
          this.alphav = 150;
          this.alphaa = 500;
          this.alphaver = 150;
          this.activeLight = 2;
      if (abs(carros[1].y - distanciay) > 10 && carros[1].y < distanciay) {
      carros[1].y -= velocidade_atual;
      carros[1].wrapAroundScreenx();
      carros[2].y += velocidade_atual;
      carros[2].wrapAroundScreenx();
    }
        }
      }
    }