added name input as seperate functions
This commit is contained in:
203
web/game.js
203
web/game.js
@@ -2,9 +2,9 @@
|
||||
// Game variables
|
||||
const width = 1260;
|
||||
const height = 620;
|
||||
let requesteddata = [];
|
||||
let buttonWidth = width/ 6
|
||||
let buttonHeight = buttonWidth/5
|
||||
let requesteddata = [];
|
||||
let buttonWidth = width / 6
|
||||
let buttonHeight = buttonWidth / 5
|
||||
// Menu variables
|
||||
let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
|
||||
let currentIndex = [0, 0, 0];
|
||||
@@ -81,11 +81,9 @@ function setup() {
|
||||
// disable the outline of shapes
|
||||
noStroke();
|
||||
}
|
||||
|
||||
function score() {
|
||||
time += 3 / framerate;
|
||||
}
|
||||
|
||||
function reset() {
|
||||
lives = 1;
|
||||
time = 0;
|
||||
@@ -106,7 +104,6 @@ function reset() {
|
||||
submitted = false;
|
||||
escaped = false;
|
||||
}
|
||||
|
||||
function gameOver() {
|
||||
if (lives == 0) {
|
||||
//game over screen
|
||||
@@ -134,14 +131,12 @@ function gameOver() {
|
||||
pop();
|
||||
}
|
||||
}
|
||||
|
||||
function movementCheck() {
|
||||
//check if the player has moved
|
||||
if (playerPosX != initialPlayerPosX || playerPosY != initialPlayerPosY) {
|
||||
hasMoved = true;
|
||||
}
|
||||
}
|
||||
|
||||
async function keyPressed() {
|
||||
if (!isDead && !homescreenOn && !pausescreenOn) {
|
||||
if (keyIsDown(LEFT_ARROW) && playerPosX > 0 + radius) {
|
||||
@@ -158,32 +153,7 @@ async function keyPressed() {
|
||||
}
|
||||
}
|
||||
if (isDead && keyReleasedFlag && !entered) {
|
||||
if ((!(iIndex > 2)) || (!(iIndex < 0))) {
|
||||
if (keyCode == LEFT_ARROW) {
|
||||
iIndex -= 1;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == RIGHT_ARROW) {
|
||||
iIndex += 1;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
}
|
||||
if (keyCode == UP_ARROW) {
|
||||
currentIndex[iIndex] = (currentIndex[iIndex] + 1) % letters.length;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == DOWN_ARROW) {
|
||||
currentIndex[iIndex] = (currentIndex[iIndex] - 1 + letters.length) % letters.length;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == ENTER) {
|
||||
nameHS += letters[currentIndex[0]];
|
||||
nameHS += letters[currentIndex[1]];
|
||||
nameHS += letters[currentIndex[2]];
|
||||
entered = true;
|
||||
submit();
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
nameSubmit();
|
||||
}
|
||||
if (isDead && keyReleasedFlag && entered) {
|
||||
if (keyCode == UP_ARROW) {
|
||||
@@ -194,16 +164,6 @@ async function keyPressed() {
|
||||
buttonSelectDead += 1;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (buttonSelectDead == 0) {
|
||||
if (keyCode == RIGHT_ARROW && !submitted) {
|
||||
entered = false;
|
||||
nameHS = '';
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == ENTER) {
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
}
|
||||
if (buttonSelectDead == 1) {
|
||||
if (keyCode == ENTER) {
|
||||
//reset all the variables so the game can be played again
|
||||
@@ -284,7 +244,6 @@ async function keyPressed() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function submit() {
|
||||
if (entered == true) {
|
||||
sendData(nameHS, int(time));
|
||||
@@ -293,11 +252,9 @@ function submit() {
|
||||
submitted = true;
|
||||
}
|
||||
}
|
||||
|
||||
function keyReleased() {
|
||||
keyReleasedFlag = true; // Set the flag to true when a key is released
|
||||
}
|
||||
|
||||
function wait(waitTime) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
@@ -305,7 +262,93 @@ function wait(waitTime) {
|
||||
}, waitTime);
|
||||
});
|
||||
}
|
||||
function nameSubmit() {
|
||||
if ((!(iIndex > 2)) || (!(iIndex < 0))) {
|
||||
if (keyCode == LEFT_ARROW) {
|
||||
iIndex -= 1;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == RIGHT_ARROW) {
|
||||
iIndex += 1;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
}
|
||||
if (keyCode == UP_ARROW) {
|
||||
currentIndex[iIndex] = (currentIndex[iIndex] + 1) % letters.length;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == DOWN_ARROW) {
|
||||
currentIndex[iIndex] = (currentIndex[iIndex] - 1 + letters.length) % letters.length;
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
if (keyCode == ENTER) {
|
||||
nameHS += letters[currentIndex[0]];
|
||||
nameHS += letters[currentIndex[1]];
|
||||
nameHS += letters[currentIndex[2]];
|
||||
entered = true;
|
||||
if (isDead) {
|
||||
submit();
|
||||
} else {
|
||||
// do nothing
|
||||
}
|
||||
keyReleasedFlag = false;
|
||||
}
|
||||
}
|
||||
function nameDraw() {
|
||||
if (entered) {
|
||||
if (buttonSelectDead == 0) {
|
||||
push()
|
||||
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 - 15, 90, 40)
|
||||
pop()
|
||||
}
|
||||
if (buttonSelectDead == 1) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 + 30, 110, 40)
|
||||
pop()
|
||||
}
|
||||
if (buttonSelectDead == 2) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 + 75, 160, 40)
|
||||
pop()
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (iIndex == 0) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2 - 30, height / 2 - 15, 30, 40)
|
||||
pop()
|
||||
}
|
||||
if (iIndex == 1) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 - 15, 30, 40)
|
||||
pop()
|
||||
}
|
||||
if (iIndex == 2) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2 + 30, height / 2 - 15, 30, 40)
|
||||
pop()
|
||||
}
|
||||
}
|
||||
}
|
||||
// the function draw() is called every frame
|
||||
function draw() {
|
||||
keyPressed();
|
||||
@@ -331,7 +374,7 @@ function draw() {
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 + height/12, buttonWidth, buttonHeight)
|
||||
rect(width / 2, height / 2 + height / 12, buttonWidth, buttonHeight)
|
||||
pop()
|
||||
}
|
||||
}
|
||||
@@ -360,64 +403,12 @@ function draw() {
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rectMode(CENTER);
|
||||
rect(width / 2, height / 2 + height/12, buttonWidth, buttonHeight)
|
||||
rect(width / 2, height / 2 + height / 12, buttonWidth, buttonHeight)
|
||||
pop()
|
||||
}
|
||||
}
|
||||
if (isDead) {
|
||||
if (entered) {
|
||||
if (buttonSelectDead == 0) {
|
||||
push()
|
||||
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 - 15, 90, 40)
|
||||
pop()
|
||||
}
|
||||
if (buttonSelectDead == 1) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 + 30, 110, 40)
|
||||
pop()
|
||||
}
|
||||
if (buttonSelectDead == 2) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 + 75, 160, 40)
|
||||
pop()
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (iIndex == 0) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2 - 30, height / 2 - 15, 30, 40)
|
||||
pop()
|
||||
}
|
||||
if (iIndex == 1) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2, height / 2 - 15, 30, 40)
|
||||
pop()
|
||||
}
|
||||
if (iIndex == 2) {
|
||||
push()
|
||||
stroke(205, 205, 205)
|
||||
strokeWeight(2)
|
||||
fill(0, 0, 0, 0)
|
||||
rect(width / 2 + 30, height / 2 - 15, 30, 40)
|
||||
pop()
|
||||
}
|
||||
}
|
||||
nameDraw();
|
||||
}
|
||||
}
|
||||
async function randPatern() {
|
||||
@@ -448,7 +439,6 @@ async function randPatern() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function randomAttackPattern() {
|
||||
if (shot == false || hit == true) {
|
||||
for (i = 0; i < bulletAmount; i++) {
|
||||
@@ -456,12 +446,11 @@ function randomAttackPattern() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function phase() {
|
||||
switch (true) {
|
||||
case (time < 20):
|
||||
phases[0] = true;
|
||||
paternArray = [5];
|
||||
paternArray = [1];
|
||||
shotSpeedAdj = 2;
|
||||
push();
|
||||
fill(255, 0, 255);
|
||||
@@ -518,7 +507,6 @@ function phase() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function spawnRandomBullet() {
|
||||
if (chosen == false) {
|
||||
randPatern();
|
||||
@@ -640,7 +628,6 @@ function spawnRandomBullet() {
|
||||
bulletAmount = 5;
|
||||
}
|
||||
}
|
||||
|
||||
function game() {
|
||||
if (!(lives == 0)) {
|
||||
// draw player
|
||||
@@ -720,4 +707,4 @@ function game() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user