ArrayList<entityRot> entCollection;
final int members = 128;
final int rows = 8;
void setup() {
size( 1280, 720 );
entCollection = new ArrayList<entityRot>();
// push instance
for ( int i = 0; i < rows; i++ ) {
for ( int j = 0; j < members/rows; j++ ) {
// protector
if ( i * (members/rows) + j < members ) {
entCollection.add( new entityRot(40 + j * 70, 90 + i * 80, 30));
}
}
}
randomSeed(millis()+second());
}
void draw() {
background(0);
textSize(32);
fill(255, 255, 255);
text("Left click to randomize, Right click to stop all", 20, 30);
for ( int i = 0; i < entCollection.size(); i++ ) { // display
entCollection.get(i).updateGraphics();
}
}
void mousePressed(MouseEvent event) {
if (mouseButton == LEFT ) {
for ( int i = 0; i < entCollection.size(); i++ ) {
int mv_stop = floor(random(65536));
if ( mv_stop % 2 == 1 ) { // move
int mv_dir = floor(random(65536));
if ( mv_dir % 2 == 1 ) {
entCollection.get(i).move(1);
} else {
entCollection.get(i).move(-1);
}
} else {
entCollection.get(i).stop();
}
}
}
if (mouseButton == RIGHT ) {
for ( int i = 0; i < entCollection.size(); i++ ) { // stop all
entCollection.get(i).stop();
}
}
}
class entityRot {
public entityRot(float x, float y, float r) {
this.m_x = x;
this.m_y = y;
this.m_r = r;
this.m_ang = 0.0; // radian
this.m_dir = 0; // stop
this.m_ang_speed = 0;
}
private void updateStat() {
m_ang += m_ang_speed;
switch( m_dir ) {
case 0: // move towards speed
if ( abs(m_ang_speed) > s_speed_step ) {
if (m_ang_speed > 0 ) {
m_ang_speed -= s_speed_step;
} else {
m_ang_speed += s_speed_step;
}
} else {
m_ang_speed = 0.0;
}
break;
case 1: // move towards max speed
if ( s_max_speed - m_ang_speed > s_speed_step) {
m_ang_speed += s_speed_step;
} else {
m_ang_speed = s_max_speed;
}
break;
case -1: // move towards the negative max speed
if ( m_ang_speed + s_max_speed > s_speed_step) {
m_ang_speed -= s_speed_step;
} else {
m_ang_speed = -s_max_speed;
}
break;
}
}
public void updateGraphics() {
this.updateStat();
// draw
pushMatrix();
fill(0);
stroke(255, 255, 255);
strokeWeight(1);
translate(m_x, m_y - (m_r * 1.2));
if ( m_dir == -1 ) {
line(0, 0, -m_r / 2, 0);
line(-m_r / 2, 0, -m_r / 2 + m_r / 4, -m_r / 4);
}
if ( m_dir == 1){
line(0, 0, m_r / 2, 0);
line(m_r / 2, 0, m_r / 2 - m_r / 4, -m_r / 4);
}
if ( m_dir == 0){
ellipse( 0, 0, m_r / 8, m_r / 8 );
}
popMatrix();
pushMatrix();
translate(m_x, m_y);
fill(0);
stroke(255, 255, 255);
strokeWeight(3);
rotate( m_ang );
ellipse( 0, 0, m_r * 2, m_r * 2 );
line( 0, 0, 0, -m_r);
popMatrix();
}
public void stop() {
this.m_dir = 0;
}
public void move(int dir) {
if ( dir != -1 && dir != 1) {
return;
}
this.m_dir = dir;
}
private entityRot() { // disallow no parameter
}
private float m_x, m_y, m_r;
private float m_ang;
private int m_dir;
private static final float s_max_speed = PI/20;
private static final float s_speed_step = PI/4000;
private float m_ang_speed;
}