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