Intergrated bullet class into game.js

This commit is contained in:
Sam
2023-11-29 15:46:04 +01:00
parent 77ba652a69
commit 2e9c22dee2
2 changed files with 101 additions and 70 deletions

View File

@@ -36,8 +36,11 @@ let time = 0;
// the function setup() is called once when the page is loaded // the function setup() is called once when the page is loaded
function setup() { function setup() {
// create a canvas element and append it to the body // create a canvas element and append it to the body
createCanvas(width, height); createCanvas(width, height);
frameRate(120); frameRate(120);
myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed);
// disable the outline of shapes // disable the outline of shapes
Movementloop() Movementloop()
noStroke(); noStroke();
@@ -107,43 +110,42 @@ async function Movementloop() {
}); });
} }
function shoot(directionX, directionY) { // function shoot(directionX, directionY) {
shot = true; // shot = true;
hit = false; // hit = false;
if (!projectile) { // if (!projectile) {
// Set the initial position of the projectile to the player's position // // Set the initial position of the projectile to the player's position
projectile = createVector(shotPosX, shotPosY); // projectile = createVector(shotPosX, shotPosY);
targetX = directionX; // targetX = directionX;
targetY = directionY; // targetY = directionY;
// Calculate the initial direction // // Calculate the initial direction
direction = createVector(targetX - projectile.x, targetY - projectile.y); // direction = createVector(targetX - projectile.x, targetY - projectile.y);
direction.normalize(); // direction.normalize();
} // }
// Draw the small circle (projectile) // // Draw the small circle (projectile)
fill(255, 0, 0); // fill(255, 0, 0);
circle(projectile.x, projectile.y, projSize); // circle(projectile.x, projectile.y, projSize);
// Move the projectile towards the movable circle // // Move the projectile towards the movable circle
projectile.add(p5.Vector.mult(direction, shotSpeed)); // projectile.add(p5.Vector.mult(direction, shotSpeed));
if (dist(projectile.x, projectile.y, directionX, directionY) <= radius) { // if (dist(projectile.x, projectile.y, directionX, directionY) <= radius) {
projectile = null; // projectile = null;
hit = true; // hit = true;
} // }
else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) { // else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) {
bounceX = projectile.x; // bounceX = projectile.x;
bounceY = projectile.y; // bounceY = projectile.y;
projectile = null; // projectile = null;
shot = false; // shot = false;
} // }
} // }
// the function draw() is called every frame // the function draw() is called every frame
function draw() { function draw() {
shot = false;
textSize(10); textSize(10);
keyPressed(); keyPressed();
life(); life();
@@ -171,30 +173,33 @@ function draw() {
if (nextAttack == 1) { if (nextAttack == 1) {
shotPosX = 500; shotPosX = 500;
shotPosY = 100; shotPosY = 100;
shoot(playerPosX, playerPosY); myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY);
} }
if (nextAttack == 2) { if (nextAttack == 2) {
shotPosX = random(0, 1000); shotPosX = random(0, 1000);
shotPosY = 600; shotPosY = 600;
shoot(playerPosX, playerPosY); myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY);
} }
if (nextAttack == 3) { if (nextAttack == 3) {
shotPosX = 0; shotPosX = 0;
shotPosY = random(0, 600); shotPosY = random(0, 600);
shoot(playerPosX, playerPosY); myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY);
} }
if (nextAttack == 4) { if (nextAttack == 4) {
shotPosX = 1000; shotPosX = 1000;
shotPosY = random(0, 600); shotPosY = random(0, 600);
shoot(playerPosX, playerPosY);
} }
} }
if (patern == 2) { if (patern == 2) {
shotPosX = bounceX; shotPosX = bounceX;
shotPosY = bounceY; shotPosY = bounceY;
shoot(playerPosX, playerPosY); myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY);
} }
} }
if (myBullet) {
myBullet.draw();
({ hit, shot } = myBullet.update(playerPosX, playerPosY));
}
} }
} }

View File

@@ -1,42 +1,68 @@
class bullet { class bullet {
//Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd. //Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd.
constructor(targetx, targety, radius, speed) { constructor(targetx, targety, radius, speed, shotPosX, shotPosY) {
//"This" moet gebruikt worden om de variabelen aan te maken in de class en het zorgt er voor dat de variabelen niet alleen in de constructor gebruikt kunnen worden, //"This" moet gebruikt worden om de variabelen aan te maken in de class en het zorgt er voor dat de variabelen niet alleen in de constructor gebruikt kunnen worden,
//maar ook in de rest van de class //maar ook in de rest van de class
this.x = targetx; this.targetx = targetx;
this.y = targety; this.targety = targety;
this.radius = radius; this.x = shotPosX;
this.speed = speed; this.y = shotPosY;
this.radius = radius;
// Set the initial position of the projectile to the player's position this.speed = speed;
let projectile = createVector(this.x, this.y);
this.x = directionX;
this.y = directionY;
// Calculate the initial direction
direction = createVector(targetX - projectile.x, targetY - projectile.y); this.directionX = null;
direction.normalize(); this.directionY = null;
if ((this.directionX === null) || (this.directionY === null)) {
} this.directionX = this.targetx;
this.directionY = this.targety;
}
this.projectile = createVector(this.x, this.y);
draw() { // Calculate the initial direction
//push en pop zorgen er voor dat de code tussen push en pop een eigen canvas heeft. Dus dat er meerdere projectielen tegelijk kunnen zijn. this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
fill(255, 0, 0); this.direction.normalize();
circle(500, 500, 5);
projectile.add(p5.Vector.mult(direction, shotSpeed)); }
if (dist(projectile.x, projectile.y, directionX, directionY) <= radius) {
draw() {
//push en pop zorgen er voor dat de code tussen push en pop een eigen canvas heeft. Dus dat er meerdere projectielen tegelijk kunnen zijn.
push();
fill(0, 255, 0);
circle(this.projectile.x, this.projectile.y, this.radius);
pop();
// Set the initial position of the projectile to the player's position
}
// // Move the projectile towards the movable circle
update(targetx, targety) {
this.targetx = targetx;
this.targety = targety;
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
let hit = false;
let shot = true;
if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
hit = true; hit = true;
}
else if (projectile.x < 0 - 10 || projectile.x > width + 10 || projectile.y < 0 - 10 || projectile.y > height + 10) {
bounceX = projectile.x;
bounceY = projectile.y;
shot = false; shot = false;
}
else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
// bounceX = this.projectile.x;
// bounceY = this.projectile.y;
shot = false;
}
return { hit, shot };
} }
}
}
}