ArrayList spaces = new ArrayList(); void setup(){ size(500,750); spaces.add(new Cans(0,0)); spaces.add(new Attention(1,0)); spaces.add(new Gears(0,1)); spaces.add(new Tris(1,1)); spaces.add(new Spiral(0,2)); spaces.add(new Break(1,2)); } void draw(){ for(PlaySpace ps : spaces){ ps.draw(); } } PlaySpace currentSpace; void keyPressed(){ if(currentSpace != null){ currentSpace.keyPressed(key); } } void mousePressed(){ PlaySpace newSpace = findSpace(); if(newSpace == null) return; newSpace.mousePressed(mouseX,mouseY); currentSpace = newSpace; } void mouseReleased(){ PlaySpace newSpace = findSpace(); if(newSpace == null) return; newSpace.mouseReleased(mouseX,mouseY); } void mouseDragged(){ PlaySpace newSpace = findSpace(); if(newSpace == null) return; newSpace.mouseDragged(mouseX,mouseY); } void mouseMoved(){ PlaySpace newSpace = findSpace(); if(newSpace == null) return; newSpace.mouseMoved(mouseX,mouseY); } PlaySpace findSpace(){ for(PlaySpace ps : spaces){ if(ps.isIn(mouseX,mouseY)){ return ps; } } return null; } class Pair{ float x,y; Pair(float px,float py){ x = px; y = py; } } int GRIDSIZE = 2; abstract class PlaySpace{ boolean isIn(float mx, float my){ if( mx >= x * w && mx < (x+1)* w){ if(my >= y * h && my < (y+1)*h){ return true; } } return false; } color bgc; int x, y; float w,h; PlaySpace(int px, int py){ bgc = color(random(32,128)); x = px; y = py; w = width/2; h = height/3; } void draw(){ pushMatrix(); noStroke(); translate(x*w,y*w); run(); popMatrix(); } abstract void run(); void mousePressed(float mx, float my){ doMousePressed(mx - (x*w),my-(y*h)); } void doMousePressed(float x, float y){} void mouseReleased(float mx, float my){ doMouseReleased(mx - (x*w),my-(y*h)); } void doMouseReleased(float x, float y){} void mouseDragged(float mx, float my){ doMouseDragged(mx - (x*w),my-(y*h)); } void doMouseDragged(float x, float y){} void mouseMoved(float mx, float my){ doMouseMoved(mx - (x*w),my-(y*h)); } void doMouseMoved(float x, float y){} void keyPressed(char k){ if(k == ' '){ reset(); } } void reset(){ } } class Attention extends PlaySpace{ Attention(int x,int y){ super(x,y); } float mx, my; void run(){ fill(bgc); rect(0,0,w,h); stroke(0); strokeWeight(2); noStroke(); fill(222); for(int x = 1; x < 10; x++){ for(int y = 1; y < 10; y++){ float cx = x * (w/10); float cy = y * (h/10); float d = dist(cx,cy,mx,my)/6; rect(cx - d/2, cy - d/2 , d,d); } } } void doMouseMoved(float x, float y){ mx = x; my = y; } } class Break extends PlaySpace { boolean clear = true; void reset() { clear = true; } Break(int x, int y) { super(x, y); } void run() { if(clear){ noStroke(); fill(255); rect(0,0,w,h); clear = false; } strokeWeight(1); stroke(0); if(doit) { line(0,0,a,b); line(w,0,a,b); line(0,h,a,b); line(w,h,a,b); } doit = false; } boolean doit; float a,b; void doMousePressed(float mx, float my) { doit = true; a = mx; b = my; } } class Cans extends PlaySpace { ArrayList gearthings = new ArrayList(); void reset() { gearthings = new ArrayList(); } Cans(int x, int y) { super(x, y); } void run() { fill(bgc); rect(0,0,w,h); noStroke(); for (GearThing gt : gearthings) { gt.move(); gt.draw(); } } void doMousePressed(float mx, float my) { gearthings.add(new GearThing(this, mx, my)); } } class GearThing { Pair place; Pair speed; Cans parent; GearThing(Cans g, float px, float py) { parent = g; place = new Pair(parent.w/8, py); speed = new Pair(0, 0); } void move() { if (place.x < parent.w/2) { speed.x += .2; } else { speed.x -= .2; } place.x += speed.x; } void draw() { fill(222); noStroke(); strokeWeight(2); ellipse(place.x, place.y, 10, 10); } } class Gears extends PlaySpace{ Gears(int x,int y){ super(x,y); } void reset() { currentGear = null; lastGear = null; gears = new ArrayList(); } Gear currentGear; Gear lastGear; ArrayList gears = new ArrayList(); void run(){ fill(bgc); rect(0,0,w,h); for(Gear g : gears){ g.draw(); } if(currentGear != null){ currentGear.sz+= .3; } } void doMousePressed(float x, float y){ currentGear = new Gear(x,y,lastGear); gears.add(currentGear); } void doMouseDragged(float x, float y){ if(currentGear != null){ currentGear.x = x; currentGear.y = y; } } void doMouseReleased(float x, float y){ lastGear = currentGear; currentGear = null; } } class Gear{ float x,y,sz,a; Gear p; Gear(float px, float py, Gear prevGear){ x = px; y = py; sz = 5; p = prevGear; }; void draw(){ stroke(255); noFill(); ellipse(x,y,sz,sz); a += .1; if(p != null){ line(x + cos(a)*sz/2,y + sin(a)*sz/2 ,p.x + cos(p.a)*p.sz/2,p.y + sin(p.a)*p.sz/2); } } } class Spiral extends PlaySpace { ArrayList gearthings = new ArrayList(); boolean clear = true; void reset() { gearthings = new ArrayList(); clear = true; } Spiral(int x, int y) { super(x, y); } void run() { if(clear){ noStroke(); fill(222); rect(0,0,w,h); clear = false; } noStroke(); ArrayList linToKill = new ArrayList(); for (Lin gt : gearthings) { if(!gt.move()){ linToKill.add(gt); } else gt.draw(); } gearthings.removeAll(linToKill); } void doMousePressed(float mx, float my) { gearthings.add(new Lin(this, mx, my)); } } class Lin { float speed ; Spiral parent; color c; Lin(Spiral g, float px, float py) { parent = g; speed = dist( g.w/2,g.h/2,px,py)/100.0; c = color(random(0,255)); x = lastx = g.w/2; y = lasty = g.h/2; } float lastx,lasty,x,y, a,d; boolean move() { a += speed; d += speed; lastx = x; lasty = y; x = parent.w/2+ cos(a)*d; y = parent.h/2 + sin(a)*d; if(dist(parent.w/2,parent.h/2,x,y) > parent.w/2) { // println("DEATH"); return false; } return true; } void draw() { stroke(c); strokeWeight(3); line(lastx,lasty,x,y); } } class Tris extends PlaySpace { ArrayList gearthings = new ArrayList(); boolean clear = true; void reset() { gearthings = new ArrayList(); clear = true; } Tris(int x, int y) { super(x, y); } void run() { if(clear){ noStroke(); fill(222); rect(0,0,w,h); clear = false; } noStroke(); for (Tri gt : gearthings) { gt.move(); gt.draw(); } } void doMousePressed(float mx, float my) { gearthings.add(new Tri(this, mx, my)); } } class Tri { Pair place; Pair speed; Tris parent; Tri(Tris g, float px, float py) { parent = g; place = new Pair(px, py); speed = new Pair(0, 0); } void move() { if (place.x < parent.w/2) { speed.x += .2; } else { speed.x -= .2; } place.x += speed.x; if (place.y < parent.h/2) { speed.y += .2; } else { speed.y -= .2; } place.y += speed.y; } void draw() { fill(255); stroke(0); strokeWeight(2); //triangle(place.x,place.y,place.x+10,place.y+10,place.x-10,place.y+10); ellipse(place.x,place.y,10,10); } }