renaming fix and update class

This commit is contained in:
Sam
2024-01-08 19:26:37 +01:00
parent f4149d08b2
commit 55158def5c
2 changed files with 56 additions and 60 deletions

View File

@@ -1,70 +1,66 @@
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, angle) { // Add hasMoved parameter
constructor(targetx, targety, radius, speed, angle) { // add all incoming variables to the class
//"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.angle = radians(angle);
//maar ook in de rest van de class this.targetx = targetx;
this.angle = radians(angle); this.targety = targety;
this.targetx = targetx; this.x = shotPosX;
this.targety = targety; this.y = shotPosY;
this.x = 500; this.radius = radius;
this.y = 100; this.speed = speed;
this.radius = 10; this.bulletHit = false;
this.speed = 5; this.directionX = null;
this.directionY = null;
this.hasMoved = hasMoved;
this.directionX = null; // Set a variable that cant be changed
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; }
} // create a vector for the projectile and the direction
this.projectile = createVector(this.x, this.y);
this.projectile = createVector(600, 100); this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y);
// Calculate the initial direction this.direction.normalize();
this.direction = createVector(this.targetx - this.projectile.x, this.targety - this.projectile.y); // rotate the direction towards the player
this.direction.normalize(); this.direction.rotate(this.angle);
this.direction.rotate(this.angle);
} }
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. //draw the bullet
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; // keeps the projetile updated so hit collision can be checked
this.targety = targety; this.targetx = targetx;
this.projectile.add(p5.Vector.mult(this.direction, this.speed)); this.targety = targety;
// update the projectile
this.projectile.add(p5.Vector.mult(this.direction, this.speed));
let hit = false; let hit = false;
let shot = true; let shot = true;
// check if the projectile has hit the player
if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) {
hit = true;
shot = false;
// if the projectile has hit the player and the player has lives left, remove a life
if (this.hasMoved && lives != 0 && this.bulletHit == false) {
this.bulletHit = true;
lives -= 1;
}
//if the bullet hits one of the wall reset the shot variable so a new wave of bullets can be fired
} else if (this.projectile.x < 0 - 10 || this.projectile.x > width + 10 || this.projectile.y < 0 - 10 || this.projectile.y > height + 10) {
shot = false;
}
// check if the projectile is off screen so it can be removed
let isOffScreen = this.projectile.x < 0 || this.projectile.x > width || this.projectile.y < 0 || this.projectile.y > height;
//unused variable to check if the projetile passsed the original player position when it was fired
let originalPos = this.projectile.x == this.targetx || this.projectile.y == this.targety;
if (dist(this.projectile.x, this.projectile.y, this.targetx, this.targety) <= this.radius) { // return all the variables
hit = true; return { hit, shot, isOffScreen, originalPos };
}
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 };
} }
} }

View File

@@ -8,7 +8,7 @@
<!-- support p5--> <!-- support p5-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.1.9/addons/p5.sound.min.js"></script>
<script src="js/basicbullet.js"></script> <script src="basicbullet.js"></script>
<script src="game.js"></script> <script src="game.js"></script>
</head> </head>
<body> <body>