133 lines
3.3 KiB
JavaScript
133 lines
3.3 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 = 3;
|
|
let booleanArray = window.booleanArray;
|
|
// let squareX = 100;
|
|
// let squareY = 100;
|
|
// const squareSize = 100;
|
|
|
|
let shotSpeed = 5;
|
|
let projectile;
|
|
let projSize = 5;
|
|
let shot;
|
|
|
|
// 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);
|
|
// disable the outline of shapes
|
|
Movementloop()
|
|
noStroke();
|
|
}
|
|
|
|
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 object_collision() {
|
|
// var squareCenterX = squareX + squareSize / 2;
|
|
// var squareCenterY = squareY + squareSize / 2;
|
|
|
|
// var distance = dist(playerPosX, playerPosY, squareCenterX, squareCenterY);
|
|
|
|
// if (distance < squareSize / 2 + radius) {
|
|
// squareX = random(0, width - squareSize);
|
|
// squareY = random(0, height - squareSize);
|
|
// }
|
|
// }
|
|
|
|
function shoot(directionX, directionY) {
|
|
shot = true;
|
|
|
|
if (!projectile) {
|
|
// Set the initial position of the projectile to the player's position
|
|
projectile = createVector(500, 100);
|
|
Xbuffer = directionX;
|
|
Ybuffer = directionY;
|
|
}
|
|
|
|
// Draw the small circle (projectile)
|
|
fill(255, 0, 0);
|
|
circle(projectile.x, projectile.y, projSize);
|
|
|
|
// Move the projectile towards the movable circle
|
|
let targetFormer = createVector(Xbuffer, Ybuffer);
|
|
let target = createVector(directionX, directionY);
|
|
let direction = targetFormer.copy().sub(projectile);
|
|
direction.normalize();
|
|
direction.mult(shotSpeed);
|
|
projectile.add(direction);
|
|
|
|
if (projectile.dist(target) <= radius) {
|
|
shot = false;
|
|
projectile = null;
|
|
}
|
|
}
|
|
|
|
// the function draw() is called every frame
|
|
function draw() {
|
|
keyPressed();
|
|
// draw background
|
|
background(0, 0, 0, 100);
|
|
|
|
// draw player
|
|
fill(0, 255, 255)
|
|
circle(constrain(playerPosX, 0 + radius, width - radius), constrain(playerPosY, 0 + radius, height - radius), playerSize);
|
|
|
|
// draw boss
|
|
fill(255,165,0)
|
|
circle(500, 100, 50);
|
|
|
|
// square(squareX,squareY,squareSize);
|
|
// object_collision();
|
|
|
|
if (shot == false) {
|
|
shoot(playerPosX, playerPosY);
|
|
|
|
} else {
|
|
shot = false;
|
|
}
|
|
|
|
} |