fixed timer, cleanup junk and improved bullets

This commit is contained in:
Sam
2023-11-30 15:02:24 +01:00
parent d703a82a7f
commit 559621835f
2 changed files with 44 additions and 60 deletions

View File

@@ -22,6 +22,9 @@ let hit = false;
let shotPosX; let shotPosX;
let shotPosY; let shotPosY;
let initialPlayerPosX = playerPosX;
let initialPlayerPosY = playerPosY;
let nextAttack; let nextAttack;
let bounceX = bossPosX; let bounceX = bossPosX;
let bounceY = bossPosY; let bounceY = bossPosY;
@@ -30,7 +33,7 @@ let predictiveBounceY;
let bullets = []; let bullets = [];
let direction; let direction;
let framerate = 120; let framerate = 120;
let hasMoved = false;
let time = 0; let time = 0;
//let myBullet = new bullet(); //let myBullet = new bullet();
@@ -40,7 +43,6 @@ function setup() {
createCanvas(width, height); createCanvas(width, height);
frameRate(framerate); frameRate(framerate);
myBullet = new bullet(playerPosX, playerPosY, radius, shotSpeed);
// disable the outline of shapes // disable the outline of shapes
Movementloop() Movementloop()
@@ -48,21 +50,13 @@ function setup() {
} }
function score() {
async function score() {
while (true) {
time += 1 / framerate; time += 1 / framerate;
await sleep(1000);
}
} }
function life() { function life() {
textAlign(CENTER); textAlign(CENTER);
text(lives, 500, 20); text(lives, 500, 20);
if (hit == true) {
hit = false;
lives -= 1;
}
if (lives == 0) { if (lives == 0) {
fill(255, 0, 0); fill(255, 0, 0);
textSize(40); textSize(40);
@@ -79,6 +73,12 @@ function life() {
} }
} }
function movementCheck() {
if (playerPosX != initialPlayerPosX && playerPosY != initialPlayerPosY) {
hasMoved = true;
}
}
function keyPressed() { function keyPressed() {
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) { if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
playerPosX -= playerSpeed; playerPosX -= playerSpeed;
@@ -161,14 +161,14 @@ function draw() {
// draw player // draw player
fill(0, 255, 255) fill(0, 255, 255)
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize); circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
movementCheck()
// draw boss // draw boss
fill(255, 165, 0) fill(255, 165, 0)
circle(500, 100, 50); circle(500, 100, 50);
score(); score();
textAlign(LEFT); textAlign(LEFT);
text(time, 10, 20); text(int(time), 10, 20);
randomAttackPattern() randomAttackPattern()
@@ -186,6 +186,7 @@ function draw() {
function randomAttackPattern(){ function randomAttackPattern(){
if (shot == false || hit == true) { if (shot == false || hit == true) {
spawnRandomBullet() spawnRandomBullet()
spawnRandomBullet() spawnRandomBullet()
spawnRandomBullet() spawnRandomBullet()
@@ -202,29 +203,29 @@ function spawnRandomBullet(){
if (nextAttack == 1) { if (nextAttack == 1) {
shotPosX = 500; shotPosX = 500;
shotPosY = 100; shotPosY = 100;
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved));
} }
if (nextAttack == 2) { if (nextAttack == 2) {
shotPosX = random(0, 1000); shotPosX = random(0, 1000);
shotPosY = 600; shotPosY = 600;
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved));
} }
if (nextAttack == 3) { if (nextAttack == 3) {
shotPosX = 0; shotPosX = 0;
shotPosY = random(0, 600); shotPosY = random(0, 600);
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved));
} }
if (nextAttack == 4) { if (nextAttack == 4) {
shotPosX = 1000; shotPosX = 1000;
shotPosY = random(0, 600); shotPosY = random(0, 600);
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved));
} }
} }
if (patern == 2) { if (patern == 2) {
shotPosX = bounceX; shotPosX = bounceX;
shotPosY = bounceY; shotPosY = bounceY;
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY)); bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved));
} }
} }

View File

@@ -1,49 +1,32 @@
class bullet { class bullet {
//Een constructor voert eerst de code uit die er in staat voordat de rest van de class wordt uitgevoerd. constructor(targetx, targety, radius, speed, shotPosX, shotPosY, hasMoved) { // Add hasMoved parameter
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,
//maar ook in de rest van de class
this.targetx = targetx; this.targetx = targetx;
this.targety = targety; this.targety = targety;
this.x = shotPosX; this.x = shotPosX;
this.y = shotPosY; this.y = shotPosY;
this.radius = radius; this.radius = radius;
this.speed = speed; this.speed = speed;
this.bulletHit = false;
this.directionX = null; this.directionX = null;
this.directionY = null; this.directionY = null;
if ((this.directionX === null) || (this.directionY === null)) { if ((this.directionX === null) || (this.directionY === null)) {
this.directionX = this.targetx; this.directionX = this.targetx;
this.directionY = this.targety; this.directionY = this.targety;
} }
this.hasMoved = hasMoved; // Set this.hasMoved to the value of hasMoved
this.projectile = createVector(this.x, this.y); this.projectile = createVector(this.x, this.y);
// Calculate the initial direction
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y); this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
this.direction.normalize(); this.direction.normalize();
} }
draw() { 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(); push();
fill(0, 255, 0); fill(0, 255, 0);
circle(this.projectile.x, this.projectile.y, this.radius); circle(this.projectile.x, this.projectile.y, this.radius);
pop(); pop();
// Set the initial position of the projectile to the player's position
} }
// // Move the projectile towards the movable circle
update(targetx, targety) { update(targetx, targety) {
this.targetx = targetx; this.targetx = targetx;
this.targety = targety; this.targety = targety;
@@ -55,14 +38,14 @@ class bullet {
if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) { if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
hit = true; hit = true;
shot = false; shot = false;
if (this.hasMoved && lives != 0 && this.bulletHit == false) {
this.bulletHit = true;
lives -= 1;
} }
else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) { } 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; shot = false;
} }
return { hit, shot }; return { hit, shot };
} }
}
}