ReflectingProjectiles/reflectingProjectiles.pde

139 lines
3.9 KiB
Plaintext

final int SHOOTING_COOLDOWN = 2;
final float PROJECTILE_RADIUS = 10;
final float VELOCITY_MAGNITUDE = 2;
final float HEADING_LINE_LENGTH = 50;
final float USER_TERMINAL_VELOCITY = 6;
final float USER_DECELERATION_MAGNITUDE = 0.1;
final float USER_ACCELERATION_MAGNITUDE = 0.25;
ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
ArrayList<Wall> walls = new ArrayList<Wall>();
PVector mouse;
int dragIndex = -1;
boolean dragType = false;
PVector userPosition, userVelocity;
void setup() {
fullScreen();
mouse = new PVector(width, height/2);
userPosition = new PVector(width/2, height/2);
userVelocity = new PVector(0, 0);
// walls.add(new Wall(
// new PVector(width/2, 0),
// new PVector(width, height/2)
// ));
// walls.add(new Wall(
// new PVector(width, height/2),
// new PVector(width/2, height)
// ));
// walls.add(new Wall(
// new PVector(width/2, height),
// new PVector(0, height/2)
// ));
// walls.add(new Wall(
// new PVector(0, height/2),
// new PVector(width/2, 0)
// ));
walls.add(new Wall(
new PVector(100, 100),
new PVector(width-100, 100)
));
walls.add(new Wall(
new PVector(width-100, 100),
new PVector(width-100, height-100)
));
walls.add(new Wall(
new PVector(width-100, height-100),
new PVector(100, height-100)
));
walls.add(new Wall(
new PVector(100, height-100),
new PVector(100, 100)
));
}
void draw() {
background(0);
for (Wall w : walls)
w.show();
for (int i = projectiles.size()-1; i >= 0; i--) {
Projectile p = projectiles.get(i);
p.show();
p.move();
}
mouse = new PVector(mouseX, mouseY);
stroke(255,0,0);
fill(255,0,0);
ellipse(userPosition.x, userPosition.y, PROJECTILE_RADIUS*2, PROJECTILE_RADIUS*2);
// Handle 'mouseDrag' when pressing LMB
if (dragIndex != -1) {
Wall w = walls.get(dragIndex);
if (dragType)
w.end = mouse.copy();
else
w.start = mouse.copy();
}
// Handle 'shooting' when pressing RMB
if (mousePressed && mouseButton == RIGHT && frameCount % SHOOTING_COOLDOWN == 0 && !mouse.equals(userPosition)) {
PVector baseVelocity = PVector.sub(mouse, userPosition);
baseVelocity.setMag(VELOCITY_MAGNITUDE);
baseVelocity.add(userVelocity);
projectiles.add(new Projectile(
userPosition,
baseVelocity
));
}
// Display user heading
PVector userHeading = PVector.sub(mouse, userPosition);
userHeading.setMag(HEADING_LINE_LENGTH);
PVector temp = PVector.add(userPosition, userHeading);
float headingFromOrigin = userHeading.heading();
PVector arrowLeft = PVector.fromAngle(headingFromOrigin-(4*PI)/5);
PVector arrowRight = PVector.fromAngle(headingFromOrigin+(4*PI)/5);
arrowLeft.setMag(HEADING_LINE_LENGTH / 3);
arrowRight.setMag(HEADING_LINE_LENGTH / 3);
arrowLeft.add(temp);
arrowRight.add(temp);
if (!mouse.equals(userPosition)) {
stroke(255);
noFill();
line(userPosition.x, userPosition.y, temp.x, temp.y);
line(temp.x, temp.y, arrowLeft.x, arrowLeft.y);
line(temp.x, temp.y, arrowRight.x, arrowRight.y);
}
// Handle user movement
float decel = userVelocity.mag()-USER_DECELERATION_MAGNITUDE;
if (decel < 0)
decel = 0;
userVelocity.setMag(decel);
PVector nPos = PVector.add(userPosition, userVelocity);
boolean xZero = nPos.x < 0;
if (xZero || nPos.x > width) {
nPos.x = (xZero) ? 0 : width;
userVelocity.x = 0;
}
boolean yZero = nPos.y < 0;
if (yZero || nPos.y > height) {
nPos.y = (yZero) ? 0 : height;
userVelocity.y = 0;
}
userPosition = nPos.copy();
if (keyPressed) {
userMovement(key, headingFromOrigin);
}
}