i have made a simple project where a have a player that shoots. i have coded an aimer that gets bigger when you help the mouse down. i need help so the bullets will pick a random direction in the size of my aimer. here is the code.
ArrayList <Bullet> bullets = new ArrayList <Bullet> ();
PVector player, playerSpeed;
float maxSpeed = 3;
float s = 2.0;
int l = 2;
void setup() {
size(600, 600);
player = new PVector(width/2, height/2);
playerSpeed = new PVector();
noCursor();
noStroke();
smooth();
}
void draw() {
text();
background(255);
fill(255);
player.add(playerSpeed);
fill(255, 0, 0);
ellipse(player.x, player.y, 20, 20);
fill(255);
ellipse(player.x, player.y, 10, 10);
PVector mouse = new PVector(mouseX, mouseY);
drawC();
if (frameCount%5==0 && mousePressed) {
PVector dir = PVector.sub(mouse, player);
dir.normalize();
dir.mult(maxSpeed*3);
Bullet b = new Bullet(player, dir);
bullets.add(b);
}
for (Bullet b : bullets) {
b.update();
b.display();
}
}
class Bullet extends PVector {
PVector vel;
Bullet(PVector loc, PVector vel) {
super(loc.x, loc.y);
this.vel = vel.get();
}
void update() {
add(vel);
}
void display() {
noStroke();
fill(0, 0, 255);
ellipse(x, y, 3, 3);
}
}
void keyPressed() {
if (key == 'w') { playerSpeed.y = -maxSpeed; }
if (key == 's') { playerSpeed.y = maxSpeed; }
if (key == 'a') { playerSpeed.x = -maxSpeed; }
if (key == 'd') { playerSpeed.x = maxSpeed; }
}
void keyReleased() {
if (key == 'w' || key == 's') { playerSpeed.y = 0; }
if (key == 'a' || key == 'd') { playerSpeed.x = 0; }
}
void drawC()
{
stroke(0);
line(mouseX, mouseY + s, mouseX, mouseY + s + l);
line(mouseX, mouseY - s, mouseX, mouseY - s - l);
line(mouseX + s, mouseY, mouseX + s + l, mouseY);
line(mouseX - s, mouseY, mouseX - s - l, mouseY);
if(mousePressed)
{
if(s < 30)
{
s = s + .1;
}
}
else
{
if(s > 2)
{
s = s - .5;
}
}
}
void text()
{
println(bullets);
println("FrameRate");
println(int(frameRate));
}