Files
J1B2-Game-controller/web/game.js

263 lines
7.0 KiB
JavaScript

//--------------------Game--------------------
// Game variables
const width = 1000;
const height = 600;
// Player variables
const playerSize = 10;
let radius = playerSize / 2;
let playerPosX = 500
let playerPosY = 300;
let playerSpeed = 4;
let booleanArray = window.booleanArray;
let lives = 1;
let bossPosX = 500;
let bossPosY = 100;
let shotSpeed = 9;
let projectile;
let projSize = 5;
let shot = false;
let hit = false;
let shotPosX;
let shotPosY;
let initialPlayerPosX = playerPosX;
let initialPlayerPosY = playerPosY;
let nextAttack;
let bounceX = bossPosX;
let bounceY = bossPosY;
let predictiveBounceX;
let predictiveBounceY;
let bullets = [];
let direction;
let framerate = 120;
let hasMoved = false;
let time = 0;
let shotPoint = 0;
let angle = 0;
//let myBullet = new bullet();
// 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(framerate);
angleMode(DEGREES);
// disable the outline of shapes
Movementloop()
noStroke();
}
function score() {
time += 1 / framerate;
}
function life() {
textAlign(CENTER);
// text(lives, 500, 20);
if (lives == 0) {
fill(255, 0, 0);
textSize(40);
textAlign(CENTER);
text("Game Over", 500, 300);
}
if (key == ' ') {
lives = 1;
time = 0;
bounceX = bossPosX;
bounceY = bossPosY;
initialPlayerPosX = playerPosX;
initialPlayerPosY = playerPosY;
hasMoved = false;
bullets = [];
shot = false;
}
}
function movementCheck() {
if (playerPosX != initialPlayerPosX || playerPosY != initialPlayerPosY) {
hasMoved = true;
}
}
function keyPressed() {
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
playerPosX -= playerSpeed;
}
if (keyIsDown(RIGHT_ARROW) && playerPosX < width - radius) {
playerPosX += playerSpeed;
}
if (keyIsDown(UP_ARROW) && playerPosY > 0 + radius) {
playerPosY -= playerSpeed;
}
if (keyIsDown(DOWN_ARROW) && playerPosY < height - radius) {
playerPosY += playerSpeed;
}
}
async function Movementloop() {
window.addEventListener('booleanArrayUpdated', function (event) {
// event.detail contains the booleanArray
let booleanArray = event.detail;
// Use booleanArray here...
if (booleanArray[1]) {
playerPosX += playerSpeed;
}
if (booleanArray[3]) {
playerPosX -= playerSpeed;
}
if (booleanArray[2]) {
playerPosY += playerSpeed;
}
if (booleanArray[0]) {
playerPosY -= playerSpeed;
}
});
}
// 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;
// // 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);
// // 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;
// }
// }
// the function draw() is called every frame
function draw() {
textSize(10);
keyPressed();
life();
// draw background
//myBullet.draw();
background(0, 0, 0, 100);
if (!(lives == 0)) {
// draw player
fill(0, 255, 255)
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
movementCheck()
// draw boss
fill(255, 165, 0)
circle(500, 100, 50);
score();
textAlign(LEFT);
text(int(time), 10, 20);
randomAttackPattern()
bullets.forEach(myBullet => {
//zolang mybullet bestaat blijft hij drawen en updaten
({ hit, shot, isOffScreen } = myBullet.update(playerPosX, playerPosY));
myBullet.draw();
if (isOffScreen == true) {
myBullet.hit = true;
shot = false;
}
});
//blijf de bullet tekenen zolang hit false is
bullets = bullets.filter(bullet => !bullet.hit);
}
}
function randomAttackPattern() {
if (shot == false || hit == true) {
spawnRandomBullet()
spawnRandomBullet()
spawnRandomBullet()
spawnRandomBullet()
}
}
function spawnRandomBullet() {
let patern = random([1,2,3,4]);
if (patern == 1) {
nextAttack = random([1, 2, 3, 4]);
text(nextAttack, 30, 50);
if (nextAttack == 1) {
shotPosX = 500;
shotPosY = 100;
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved, 0));
}
if (nextAttack == 2) {
shotPosX = random(0, 1000);
shotPosY = 610;
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved, 0));
}
if (nextAttack == 3) {
shotPosX = -10;
shotPosY = random(0, 600);
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved, 0));
}
if (nextAttack == 4) {
shotPosX = 1010;
shotPosY = random(0, 600);
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved, 0));
}
}
if (patern == 2) {
shotPosX = bounceX;
shotPosY = bounceY;
bullets.push(new bullet(playerPosX, playerPosY, radius, shotSpeed, shotPosX, shotPosY, hasMoved, 0));
}
if (patern == 3) {
shotPosX = 500;
shotPosY = 100;
angle = 0;
// changing the angle of the bullets
for (let i = 0; i < 86; i++) {
setTimeout(function() {
bullets.push(new bullet(0, 100, radius, shotSpeed/4, shotPosX, shotPosY, hasMoved, angle));
angle -= 360 / 1;
}, i * 100);
}
angle = 0;
}
if (patern == 4) {
shotPosX = 500;
shotPosY = 100;
angle = 0;
// changing the angle of the bullets
for (let i = 0; i < 86; i++) {
bullets.push(new bullet(0, 100, radius, shotSpeed/4, shotPosX, shotPosY, hasMoved, angle));
angle -= 360 / 3;
}
angle = 0;
}
}