> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.-2aTdM$qWHr/rev.1866
 * 
 * authors: 
 *   Sean J. Vaughan
 *   

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



// Pressing Control-R will render this sketch.

Swarm p1, p2;
Board board = new Board();

class Board {
    Bot[][] bLocation = new Bot[300][300];
    
    Board () {
        int x, y;
        for (x = 0; x < 300; x++) {
            for (y = 0; y < 300; y++) {
                bLocation[x][y] = null;
            }
        }
    }
    
    Bot occupier (int x, int y) {
        return bLocation[parseInt(x, 10)][parseInt(y, 10)];
    }
    
    void removeBotAt (int x, int y) {
        bLocation[parseInt(x, 10)][parseInt(y, 10)] = null;        
    }
    
    void locateBotAt (Bot b, int x, int y) {
        bLocation[parseInt(x, 10)][parseInt(y, 10)] = b;
    }
}

float damp = 0.995;
float clickDamp = 0.1;

class Bot {
    int x;
    int y;
    int xM;
    int yM;
    boolean player;
    
    Bot (int ix, int iy, boolean iPlayer) {
        x = ix;
        y = iy;
        player = iPlayer
        yM = random(2)-1;
        xM = random(2)-1;
        board.locateBotAt(this, x, y);
    }
    
    void update() {
        int nx, ny, bxM, byM, txM, tyM;
        Bot bot;
        
        if (mousePressed && player) {
            xM += (mouseX - x) / 300;
            yM += (mouseY - y) / 300;
            xM = xM * damp;
            yM = yM * damp;
        } else {

            xM = xM * damp;
            yM = yM * damp;
            
        }
        nx = x + xM;
        ny = y + yM;
        
        if (nx >= 300) {
            nx = 300 - (nx - 300);
            xM = -xM
        } else if (nx < 0) {
            nx = -nx;
            xM = -xM
        }
        
        if (ny >= 300) {
            ny = 300 - (ny - 300);
            yM = -yM
        } else if (ny < 0) {
            ny = -ny;
            yM = -yM
        }
            //bot = board.occupier(nx, ny);
            //if (bot != null) {
            //    
            //    txM = xM;
            //    tyM = yM;
            //    
            //    bxM = bot.getXM();
            //    byM = bot.getYM();
            //    
            //    xM -= (xM - bxM)/2;
            //    yM -= (yM - byM)/2;
            //    
            //    bot.setXM(bxM - (bxM - txM)/2);
            //    bot.setYM(byM - (byM - tyM)/2);
            //    
            //    //bot.setColor(bColor);
            //    //bot.setPlayer(player);
            //}
//        }
        
        point(x, y);
        //fill(bColor);
        //ellipse(nx, ny, 3, 3);
        //fill(bColor);
        board.removeBotAt(x, y);
        x = nx;
        y = ny;
        board.locateBotAt(this, x, y);
    }
    
    void setPlayer (boolean iPlayer) {
        player = iPlayer;
    }
    
    int getXM() {
        return xM;
    }
    
    void setXM(int iXM) {
        xM = iXM;
    }
    
    int getYM() {
        return yM;
    }
    
    void setYM(int iYM) {
        yM = iYM;
    }
}

int botCount = 100;
class Swarm { 
    Bot[] bots = new Bot[botCount];
    color sColor;

    Swarm (y, color isColor, boolean player) {
        int i;
        sColor = isColor;
        for (i = 0; i < botCount; i++) {
            bots[i] = new Bot(random(300), random(300), player);
        }
    }

    void update() {
        int i;

        stroke(sColor);

        for (i = 0; i < botCount; i++) {
            bots[i].update();
        }
    }

}

void setup() {  // this is run once.   

    colorMode(HSB, 255);
    
    // set the background color
    background(0, 0, 255);
    
    // canvas size (Variable aren't evaluated. Integers only, please.)
    size(300, 300); 
      
    // smooth edges
    smooth();
    strokeWeight(6.0);
    strokeCap(ROUND);
    // limit the number of frames per second
    frameRate(30);
    
    p1 = new Swarm(1, color(0, 255, 255), true);
    p2 = new Swarm(299, color(127, 255, 255), false);

} 

void draw() {  // this is run repeatedly.
    background(0, 0, 255);

    p1.update();
    p2.update();

}