Conversion help! URGENT!

edited August 2014 in JavaScript Mode

Hi! I need help with something very quickly. I'm trying to convert my Processing code into Proccessing Javascript, but its not working. I think its variable name confliction, but I'm not sure. Also, its compressed, so that might be a problem.

int A=0, B=0, C=10, D=100, E, fM=100, fl=1, fT, J=-1, K;
float N=0;
float O=6.5;
float X;
float Y, V=1;
boolean y=true, x=false, F=y, I=x, R=x, L;
ArrayList<Pl>Ps=new ArrayList<Pl>();
ArrayList<Br>Bs=new ArrayList<Br>();
ArrayList<Bt>Bu=new ArrayList<Bt>();
class Pl {
  String M;
  float Z, yC, lX, wY;
  int U, S;
  boolean nB, gL, gR;
  void d() {
    if (M.equals("b")) {
      fill(113, 44, 0);
    }
    if (M.equals("g")) {
      fill(144, 144, 144);
    }
    if (M.equals("a")) {
      fill(3, 244, 250);
    }
    if (M.equals("w")) {
      fill(252, 252, 252);
    }
    if (M.equals("r")) {
      fill(252, 0, 0);
    }
    if (M.equals("s")) {
      fill(11, 255, 0);
    }
    if (M.equals("y")) {
      fill(226, 227, 16);
    }
    if (gL==y) {
      triangle(Z-10, yC-7.5, Z, yC-15, Z+10, yC-7.5);
      triangle(Z+22, yC, Z+32, yC-7.5, Z+32, yC+7.5);
      rectMode(CENTER);
      rect(Z, yC, lX, wY);
      rectMode(CORNER);
      fill(255, 255, 255);
      rect(Z-15, yC-4, 5, 3);
      fill(0, 0, 0);
      rect(Z-15, yC-4, 3, 2);
    }
    if (gR==y) {
      triangle(Z+10, yC-7.5, Z, yC-15, Z-10, yC-7.5);
      triangle(Z-22, yC, Z-32, yC-7.5, Z-32, yC+7.5);
      rectMode(CENTER);
      rect(Z, yC, lX, wY);
      rectMode(CORNER);
      fill(255, 255, 255);
      rect(Z+15, yC-4, 5, 3);
      fill(0, 0, 0);
      rect(Z+15, yC-4, 3, 2);
    }
  }
  Pl() {
    Z=random(0, 500);
    yC=Y;
    lX=50;
    wY=15;
    nB=y;
    gL=x;
    gR=y;
    S=int(random(1, 3));
  }
  void c() {
    if ((abs(mouseX-Z)<(10+25)) &&(abs(150-(A+yC))<(10+7.5))) {
      if (E<C) {
        if (F==y) {
          F=x;
          I=y;
        }
        if (F==x) {
          I=y;
          E++;
          B+=U*1;
        }
      }
    }
  }
  void mC() {
    X=random(N, O);
    if (X<3) {
      M="b";
      U=2;
    }
    else if (X<5.5) {
      M="g";
      U=3;
    }
    else if (X<7.5) {
      M="a";
      U=5;
    }
    else if (X<9.5) {
      M= "w";
      U=7;
    }
    else if (X<11) {
      M="r";
      U=10;
    }
    else if (X<15) {
      M="s";
      U=15;
    }
    else if (X>=15) {
      M="y";
      U=20;
    }
    N+=0.05;
    O+=0.05;
  }
}
class Br {
  float lX, wY, Y;
  boolean W;
  void d() {
    rectMode(CENTER);
    rect(250, this.Y, lX, wY);
  }
  Br(float y, boolean u) {
    lX=500;
    wY=20;
    Y=y;
    W=u;
  }
  void c() {
    if (abs(150-A-Y)<10+10) {
      if (W==y) {
        if (F==x) {
          J=1;
          F=y;
          I=x;
          Ps.clear();
          newPl();
          N=0;
          O=6.5;
          for (int i=0; i<Ps.size(); i++) {
            Ps.get(i).mC();
          }
          E=0;
          fT=fl;
        }
      }
      if (W != y) {
        F=x;
      }
    }
  }
}
class Bt {
  float Z, Y, lX, wY;
  int H, G, T;
  boolean X;
  void d() {
    fill(252, 252, 252);
    rectMode(CORNER);
    rect(Z, Y, lX, wY);
    rectMode(CENTER);
    fill(0, 0, 0);
  }
  Bt(float x, float y, float l, float w, int c, int cI, int pD) {
    Z=x;
    Y=y;
    lX=l;
    wY=w;
    H=c;
    G=cI;
    T=pD;
  }
  void o() {
    if ((mouseX>Z) &&(mouseX<Z+lX) &&(mouseY>Y) &&(mouseY<Y+wY)) {
      if (J==-1) {
        if ((Z==100) &&(Y==100)) {
          K=1;
          J+=T;
        }
        if ((Z==300) &&(Y==100)) {
          K=2;
          J+=T;
        }
        if ((Z==100) &&(Y==300)) {
          K=3;
          J+=T;
        }
        if ((Z==300) &&(Y==300)) {
          K=4;
          J+=T;
        }
      }
      if (J==1) {
        if (B>=H) {
          if ((Z==150) &&(Y==400)) {
            J+=T;
          }
          if ((Z==0) &&(Y==0)) {
            if (Bs.size()>2) {
              Bs.remove(1);
              B-=H;
              H+=G;
            }
            else X=y;
          }
          if ((Z==0) &&(Y==70)) {
            if (C<D) {
              C+=5;
              B-=H;
              H+=G;
            }
            else X=y;
          }
          if ((Z==0) &&(Y==140)) {
            if (fl<fM) {
              fl+=25;
              fT=fl;
              B-=H;
              H+=G;
              R=y;
            }
            else {
              X=y;
            }
          }
          if ((Z==349) &&(Y==0)) {
            if (X==x) {
              V=1.2;
              B-=H;
              X=y;
            }
          }
        }
      }
    }
  }
}
void newPl() {
  for (int i=0; i<300; i++) {
    Y=200+50*i;
    for (int n=0; n<3; n++) {
      Ps.add(new Pl());
    }
  }
}
void mousePressed() {
  for (int i=0; i<Bu.size(); i++) {
    Bu.get(i).o();
  }
}
void setup() {
  size(500, 500);
  newPl();
  Bs.add(new Br(0, y));
  Bs.add(new Br(500, x));
  Bs.add(new Br(700, x));
  Bs.add(new Br(1000, x));
  Bs.add(new Br(1250, x));
  Bs.add(new Br(5000, x));
  Bu.add(new Bt(100, 100, 100, 100, 0, 0, 2));
  Bu.add(new Bt(300, 100, 100, 100, 0, 0, 2));
  Bu.add(new Bt(100, 300, 100, 100, 0, 0, 2));
  Bu.add(new Bt(300, 300, 100, 100, 0, 0, 2));
  Bu.add(new Bt(150, 400, 200, 100, 0, 0, -1));
  Bu.add(new Bt(0, 0, 150, 70, 100, 50, 0));
  Bu.add(new Bt(0, 70, 150, 70, 75, 25, 0));
  Bu.add(new Bt(0, 140, 150, 70, 50, 75, 0));
  Bu.add(new Bt(349, 0, 150, 70, 175, 0, 0));
  for (int i=0; i<Ps.size(); i++) {
    Ps.get(i).mC();
  }
}
void draw() {
  frameRate(35);
  if (J==-1) {
    background(0, 0, 0);
    textAlign(CENTER);
    fill(0, 255, 0);
    rect(100, 100, 100, 100);
    textSize(20);
    text("Easy", 150, 80);
    fill(0, 0, 255);
    rect(300, 100, 100, 100);
    text("Medium", 350, 80);
    fill(255, 191, 26);
    rect(100, 300, 100, 100);
    text("Hard", 150, 280);
    fill(255, 0, 0);
    rect(300, 300, 100, 100);
    text("Insane", 350, 280);
    textAlign(CORNER);
  }
  if (J==0) {
    if (R=y) {
      if (mousePressed) {
        if (fT>0) {
          fT-=1;
          L=y;
        }
        else L=x;
      }
      else L=x;
    }
    else L=x;
    noCursor();
    background(0, 0, 252);
    fill(0, 0, 0);
    textSize(30);
    text(E, 20, 100);
    text(fT, 425, 450);
    fill(252, 252, 252);
    pushMatrix();
    if (F==y) {
      A-=K*2;
    }
    if (F==x) {
      if (E>=C) {
        A+=12;
      }
      else {
        A+=K*2 +1;
      }
    }
    translate(0, A);
    for (int i=0; i<Bs.size(); i++) {
      Bs.get(i).d();
    }
    for (int i=0; i<Bs.size(); i++) {
      Bs.get(i).c();
    }
    for (int i=0 ; i<Ps.size(); i++) {
      Ps.get(i).d();
    }
    popMatrix();
    for (int i=0; i<Ps.size(); i++) {
      Pl p=Ps.get(i);
      if (p.nB==y) {
        if (p.S==1) {
          p.Z+=K;
          p.gR=y;
          p.gL=x;
        }
        if (p.S==2) {
          p.Z-=K;
          p.gR=x;
          p.gL=y;
        }
        if (p.Z>500||p.Z<0) {
          p.nB=x;
        }
      }
      if ((p.Z>500||p.gL==y) && p.nB==x) {
        p.gL=y;
        p.gR=x;
        p.nB=x;
        p.Z-=K;
      }
      if ((p.Z<0||p.gR==y) && p.nB==x) {
        p.gR=y;
        p.gL=x;
        p.nB=x;
        p.Z+=K;
      }
    }
    if (L==x) {
      fill(252, 252, 252);
    }
    else fill(30, 247, 62);
    ellipse(mouseX, 150, 20, 20);
    if (L==x) {
      for (int i=0; i<Ps.size(); i++) {
        Ps.get(i).c();
        if (I==y) {
          Ps.remove(i);
          I=x;
        }
      }
    }
  }
  if (J==1) {
    noCursor();
    background(0, 252, 0);
    for (int i=4; i<Bu.size(); i++) {
      Bu.get(i).d();
    }
    text("Go down Further!", 5, 20);
    if (Bu.get(5).X==x) {
      text("$"+Bu.get(5).H, 30, 50);
    }
    else {
      text("Maxed", 50, 50);
    }
    textSize(30);
    text(B, 200, 150);
    text("Return", 205, 435);
    textSize(15);
    text("Catch more fish!", 5, 90);
    if (Bu.get(6).X==x) {
      text("$"+Bu.get(6).H, 30, 120);
    }
    else {
      text("Maxed", 50, 120);
    }
    text("Click to use.", 10, 180);
    if (Bu.get(7).X==x) {
      text("$"+Bu.get(7).H, 30, 200);
    }
    else text("Maxed", 50, 200);
    textSize(10);
    if (R==x) {
      text("Buy a shield to help go further", 3, 160);
    }
    else {
      text("Upgrade your shield.", 5, 160);
    }
    textSize(13);
    text("Increase income by 20%!", 350, 20);
    if (Bu.get(8).X==x) {
      text("$"+Bu.get(8).H, 390, 50);
    }
    else {
      text("Maxed", 400, 50);
    }
    fill(0, 0, 0);
    ellipse(mouseX, mouseY, 20, 20);
  }
}

Answers

  • Answer ✓

    Found the bug! Somehow the processing.js's parser fails to recognize the variable names if they are glued to the diamond braces:

    ArrayList<Pl>Ps=new ArrayList<Pl>(); -> ArrayList<Pl> Ps = new ArrayList<Pl>();

    Actually, you should make more effort to tidy up your code! :@)

  • I'm trying to make it as small as possible ;) <3 you GoToLoop

  • although, another problem... when I convert it into javascript and run the server, how come it only shows part of the screen and I have to scroll?

  • edited September 2014

    Lack of spaces doesn't count for performance prowess! 3:-O

    Dunno! I could see the whole 550x550 canvas! @-)

  • Answer ✓

    Removing characters can save a few milliseconds when downloading the script. But honestly, you would have to remove all spaces to hope gaining real time, and by "real", I mean something that can be measured, and that probably won't be perceived by the user anyway.
    This technique is mostly used on scripts weighting dozens or hundreds of kilobytes, or more...
    And there are tools for that, no need to make your script unreadable for this non-goal.

  • I believe there are programs that remove any unnecessary spaces from a JS script.
    But it's for final deployment and not when coding! b-(

Sign In or Register to comment.