Here is the latest update on ball classes and ballgroup class.
Not changed the "ball b = (ball) balls.get(j); " case (JR) yet.
Anyway, it starts to be fun now...
Code:
class ball{
float x,y,tx,ty;
PVector target;
PVector pos;
PVector go;
int nob;
//constructor
ball(float x, float y, float tx, float ty){
this.x = x; this.y = y;
this.tx = tx; this.ty = ty;
nob = 0;
}
//methods
void update(){
pos.add(go);
if (pos.x > 500){
pos.x = 500;
go.x *= -1;
nob++;
}
if (pos.x < 0){
pos.x = 0;
go.x *= -1;
nob++;
}
if (pos.y > 500){
pos.y = 500;
go.y *= -1;
nob++;
}
if (pos.y < 0){
pos.y = 0;
go.y *= -1;
nob++;
}
}
void display(){
noStroke();
ellipseMode(CENTER);
ellipse(pos.x, pos.y, 5, 5);
}
}
class redball extends ball{
//fields
color cr;
float speed;
int nobmax;
//constructor
redball(float x, float y, float tx, float ty){
super(x,y,tx,ty);
pos = new PVector(x,y);
target = new PVector(tx,ty);
go = PVector.sub(target,pos);
go.normalize();
cr = color(255,0,0);
speed = 10;
go.mult(speed);
}
//methods
void update(){
super.update();
}
void display(){
fill(cr);
super.display();
}
}
class blueball extends ball{
//fields
color cb;
float speed;
int nobmax;
//constructor
blueball(float x, float y, float tx, float ty){
super(x,y,tx,ty);
pos = new PVector(tx,ty);
target = new PVector(x,y);
go = PVector.sub(target,pos);
go.normalize();
cb = color(0,0,255);
speed = 10;
go.mult(speed);
}
//methods
void update(){
super.update();
}
void display(){
fill(cb);
super.display();
}
}
class ballgroup{
//parameters
ArrayList redballs;
ArrayList blueballs;
//constructor
ballgroup(){
redballs = new ArrayList();
blueballs = new ArrayList();
}
// Private methods
private void update(ArrayList balls) {
for (int j = balls.size() - 1; j >= 0; j--) {
ball b = (ball) balls.get(j);
b.update();
if (b.nob > 3) balls.remove(j);
if (balls.size()>50) balls.remove(balls.size()-1);
//^^remove the last one (don't create any more)
}
}
void display(ArrayList balls) {
for (int j = balls.size() - 1; j >= 0; j--) {
ball b = (ball) balls.get(j);
b.display();
}
}
//methods
void update() {
if (mbleft /*&& !mbright*/) redballs.add(new redball(p.x,p.y,mouseX,mouseY));
update(redballs);
if (/*!mbleft &&*/ mbright) blueballs.add(new blueball(p.x,p.y,mouseX,mouseY));
update(blueballs);
}
void display() {
display(redballs);
display(blueballs);
}
}
Next step... something to shoot!