/* Things that bounce on the bow string-trampoline thing. */ abstract class Bouncy implements Entity, Stringable { float x, y, vX, vY; float mass, collisionRadius; boolean dead = false; int hitWall; boolean onString; int temp; boolean wasOnString() { return onString; } void setOnString(boolean isOn) { this.onString = isOn; } void setTemp(int t) { temp = t; } int getTemp() { return temp; } boolean shouldPlay() { float speed = sqrt(vY*vY+vX*vX); float vol = 1.0; int minspd = 4, maxspd = 9; if (speed < maxspd) { vol = (speed-minspd) / (maxspd-minspd); } //chirp.setGain(-30+30*vol); return (speed > minspd); } Bouncy(float x, float y, float mass, float collisionRadius) { this.x = x; this.y = y; vX = 0; vY = 0; this.mass = mass; this.collisionRadius = collisionRadius; } void doPhysics0() { if (dead) return; // Move. x += vX; y += vY; if (x < leftBorder + collisionRadius) { x = leftBorder + collisionRadius; hitWall = -1; } else if (x > arenaWidth - rightBorder - collisionRadius) { x = arenaWidth - rightBorder - collisionRadius; hitWall = 1; } else hitWall = 0; } void doPhysics1() { if (dead) return; // Apply friction. vX *= 0.99; vY *= 0.99; // Apply gravity. vY += gravity; // Bounce off the left and right walls. if (hitWall * vX > 0) { vX *= -1; if (abs(vX) >= 3) { playSound(wallhit); } else { playSound(wallhitsoft); } } } void push(float dX, float dY) { vX += dX / mass; vY += dY / mass; } float getX() { return x; } float getY() { return y; } abstract void collideWith(Bouncy b); void genericCollision(Bouncy b) { float []momEx = elasticCollision(b.x - x, b.y - y, b.mass * (b.vX - vX), b.mass * (b.vY - vY), 0); push(-momEx[0], -momEx[1]); b.push(momEx[0], momEx[1]); } void levelStart() { x += leftBorder; } }