In my code, I have a rocket ship and have added a firing function. I would like to do two things:
- be able to fire multiple times
- be able to have the bullets continue in the direction fired (when rotated)
I am guessing I need to somehow separate the rocket animation from the firing animation? Currently, I have the firing animation as a method to the rocket ship object. And the rotation of the rocket ship is connected to the firing animation.
Thank you!
var rocket;
var baseLine;
var rotateValue = 0;
function setup() {
createCanvas(1000, 600);
baseLine = height - 50
rocket = {
x: width / 2,
y: baseLine,
thrust: false,
fire: false,
fireX: width / 2,
fireY: baseLine,
moveLeft: false,
moveRight: false,
rCW: false,
rCCW: false,
rotateAngle: 0,
drawRocket: function() {
fill(80)
push();
if (this.rCW) {
translate(this.x, this.y);
rotate(this.rotateAngle);
rect(10, -90, 10, 100);
} else if (this.rCCW) {
translate(this.x, this.y);
rotate(this.rotateAngle);
rect(10, -90, 10, 100);
} else {
translate(this.x, this.y);
rotate(this.rotateAngle);
rect(10, -90, 10, 100);
}
pop();
fill(150, 0, 0)
push();
translate(this.x, this.y);
rotate(this.rotateAngle);
triangle(10, -90, 15, -120, 20, -90);
pop();
fill(150, 0, 0);
noStroke();
push();
translate(this.x, this.y);
rotate(this.rotateAngle);
triangle(-10, 10, 10, -20, 10, 10);
pop();
push();
translate(this.x, this.y);
rotate(this.rotateAngle);
triangle(20, 10, 20, -20, 40, 10);
pop();
if (this.thrust) {
fill(255, 150, 0);
noStroke();
push();
translate(this.x, this.y);
rotate(this.rotateAngle);
triangle(0, 10, 20, 10, 10, 30);
triangle(5, 10, 25, 10, 15, 35);
triangle(10, 10, 30, 10, 20, 30);
pop();
}
},
moveRocket: function() {
if (this.thrust && this.y > 0 && this.rCW === true) {
this.y -= 2;
this.x += 1;
} else if (this.thrust && this.y > 0 && this.rCCW === true) {
this.y -= 2;
this.x -= 1;
} else if (this.thrust && this.y > 0) {
this.y -= 2;
} else if (this.y < baseLine) {
this.y += 3;
}
if (this.moveLeft && this.x > 0 && this.y != baseLine) {
this.x -= 2;
}
if (this.moveRight && this.x < width && this.y != baseLine) {
this.x += 2;
}
},
fireRocket: function() {
if (this.fire && this.fireY >= 0) {
fill(255, 140, 0);
noStroke();
ellipse(this.fireX + 5, this.fireY - 10, random(3, 6), random(8, 11));
ellipse(this.fireX + 25, this.fireY - 10, random(3, 6), random(8, 11));
if (this.rCW === true) {
this.fireY -= 3;
this.fireX += 2;
} else if (this.rCCW === true) {
this.fireY -= 3;
this.fireX -= 2;
} else {
this.fireY -= 3;
}
if (this.fireY <= 1) {
this.fireY = baseLine;
this.fire = false;
}
}
}
}
}
function draw() {
background(10);
rocket.drawRocket();
rocket.moveRocket();
rocket.fireRocket();
}
function keyPressed() {
if (key == "W") {
rocket.thrust = true;
}
if (key == "A") {
rocket.moveLeft = true;
}
if (key == "D") {
rocket.moveRight = true;
}
if (key == "F") {
rocket.fireX = rocket.x;
rocket.fireY = rocket.y;
rocket.fire = true;
}
if (key == "R") {
rocket.rCW = true;
rocket.rotateAngle = PI / 4;
}
if (key == "E") {
rocket.rCCW = true;
rocket.rotateAngle = -PI / 4;
}
}
function keyReleased() {
if (key == "W") {
rocket.thrust = false;
}
if (key == "A") {
rocket.moveLeft = false;
}
if (key == "D") {
rocket.moveRight = false;
}
if (key == "R") {
rocket.rCW = false;
rocket.rotateAngle = 0;
}
if (key == "E") {
rocket.rCCW = false;
rocket.rotateAngle = 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.min.js"></script>