xxxxxxxxxx
328
let me;
let guests;
let sharedBombId;
let moveSpeed = 5; // Speed at which players move
let colors = ['#FD558F', '#D24154', '#C390F1', '#AB5FB7', '#90B7F1', '#3F71BC', '#87E17D', '#3DAE5F', '#F0E748', '#E6A013', '#F0B273']; // Color array
const names = [
"MICKEY", "MINNIE", "JERRY", "REMY", "PINKY", "ALVIN", "SIMON",
"THEODORE"
];
let stars = []; // Array to hold star objects
let score = 0; // Player's score
const numStars = 5; // Number of stars to generate
const WAITING_DURATION = 10 * 1000;
const PLAYING_DURATION = 60 * 1000;
const requiredNumberOfPlayers = 2;
let sharedTimer;
function preload() {
partyConnect("wss://demoserver.p5party.org", "bomb_game_editbyfern");
let randomColor = random(colors);
let startX = random(0, 400);
let startY = random(0, 400);
let nameIndex = floor(random(names.length)); // Random index for name
let assignedName = names.splice(nameIndex, 1)[0]; // Assign and remove the name from the array
me = partyLoadMyShared({ x: startX, y: startY, id: random(1000000), color: randomColor, score: 0, name: assignedName ,isReady: false});
guests = partyLoadGuestShareds();
sharedBombId = partyLoadShared("bombId", null);
sharedTimer = partyLoadShared("globals", {
gameState: "start",
startTime: Date.now(),
displayTime: null,
});
}
function setup() {
createCanvas(400, 400);
fill(me.color)
noStroke();
if (partyIsHost()) {
sharedBombId.value = me.id; // Host has the // Generate stars
}
for (let i = 0; i < numStars; i++) {
stars.push(createVector(random(width), random(height)));
}
}
function draw() {
background("#F5F1EA");
manageTimer(); // Ensure the timer is managed correctly based on game state
switch (sharedTimer.gameState) {
case "start":
drawStartPage();
break;
case "waiting":
drawWaitingPage();
break;
case "playing":
drawPlayingPage(); // Now handles both playing and waiting logic
break;
case "result":
drawResultPage(); // Handle the result display
break;
// Optionally, you could handle other states here if needed
}
}
function drawStartPage() {
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text("Click to Start Game", width / 2, height / 2);
}
function drawWaitingPage(){
handleMovementAndInteraction()
// Draw and manage stars
for (let i = stars.length - 1; i >= 0; i--) {
fill(255, 255, 0); // Yellow color for stars
ellipse(stars[i].x, stars[i].y, 20, 20); // Draw star
// Check if player collects the star
if (dist(me.x, me.y, stars[i].x, stars[i].y) < 20) {
stars.splice(i, 1); // Remove the collected star
stars.push(createVector(random(width), random(height))); // Add a new star
}
}
// Check if the game is in the playing state and waiting for more players
if (sharedTimer.gameState === "waiting") {
let requiredPlayers = 2;
let currentPlayers = guests.length ; // Including 'me' in the count
if (currentPlayers < requiredPlayers) {
// Display waiting message
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text("Waiting for " + (requiredPlayers - currentPlayers) + " more players...", width / 2, height / 3);
}
else if (currentPlayers => requiredPlayers) {
// Display waiting message
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text("Your party is ready click to start", width / 2, height / 3);
}
}
// Now, call the newly defined function to draw players and the bomb
drawPlayersAndBomb();
}
function drawPlayingPage() {
handleMovementAndInteraction(); // Handle player movement
// Draw and manage stars
for (let i = stars.length - 1; i >= 0; i--) {
fill(255, 255, 0); // Yellow color for stars
ellipse(stars[i].x, stars[i].y, 20, 20); // Draw star
// Check if player collects the star
if (dist(me.x, me.y, stars[i].x, stars[i].y) < 20) {
me.score++; // Increase score
stars.splice(i, 1); // Remove the collected star
stars.push(createVector(random(width), random(height))); // Add a new star
}
}
// Check if the game is in the playing state and waiting for more players
if (sharedTimer.gameState === "playing") {
let requiredPlayers = 3;
let currentPlayers = guests.length ; // Including 'me' in the count
if (currentPlayers < requiredPlayers) {
// Transition back to waiting state
sharedTimer.gameState = "waiting";
sharedTimer.startTime = Date.now();
}
}
// Now, call the newly defined function to draw players and the bomb
drawPlayersAndBomb();
// Draw the countdown timer during gameplay
if (sharedTimer.displayTime !== null) {
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text("Time: " + sharedTimer.displayTime, width / 2, height - 20);
}
}
function handleMovementAndInteraction() {
// Arrow key controls for movement
if (keyIsDown(LEFT_ARROW)) {
me.x -= moveSpeed;
}
if (keyIsDown(RIGHT_ARROW)) {
me.x += moveSpeed;
}
if (keyIsDown(UP_ARROW)) {
me.y -= moveSpeed;
}
if (keyIsDown(DOWN_ARROW)) {
me.y += moveSpeed;
}
// Constrain player movement within the canvas boundaries
me.x = constrain(me.x, 0, width);
me.y = constrain(me.y, 0, height);
}
const BOMB_PASS_DISTANCE = 20; // The distance used for passing the bomb (try adjusting this value as needed)
function drawPlayersAndBomb() {
guests = partyLoadGuestShareds(); // Refresh the guest list
// Draw each guest
for (const guest of guests) {
fill(guest.color);
ellipse(guest.x, guest.y, 30, 30);
// Draw guest's name and score above them
fill("#000000"); // White color for text to stand out
textSize(11);
textAlign(CENTER, CENTER);
text(guest.name + " " + guest.score, guest.x, guest.y - 25); // Adjust Y position as needed
// Draw the bomb if this guest has it
if (guest.id === sharedBombId.value) {
drawBomb(guest.x, guest.y);
}
}
// Check bomb passing if the player is 'me' and the current guest is not 'me'
if (me.id === sharedBombId.value) {
for (const guest of guests) {
if (guest.id !== me.id && dist(me.x, me.y, guest.x, guest.y) < BOMB_PASS_DISTANCE) {
console.log(`Passing bomb from ${me.id} to ${guest.id}`);
// Before changing the bomb owner, reduce the score of the receiver
guest.score -= 1; // Deduct score for receiving the bomb
sharedBombId.value = guest.id; // Pass the bomb to another player
break; // Exit the loop after passing the bomb
}
}
}
}
function checkAndStartGame() {
if (partyIsHost() && guests.length >= requiredNumberOfPlayers - 1) {
let playerIds = [me.id].concat(guests.map(guest => guest.id));
let randomIndex = floor(random(playerIds.length));
sharedBombId.value = playerIds[randomIndex]; // Assign bomb to a random player
// Reduce the score of the initial bomb receiver
guests.forEach(guest => {
if (guest.id === sharedBombId.value) guest.score -= 1;
});
if (me.id === sharedBombId.value) me.score -= 1; // Check if 'me' is the initial bomb receiver
guests.forEach(guest => guest.score = 0); // Initialize scores to 0 for all guests
me.score = 0; // Initialize 'me' score to 0
sharedTimer.gameState = "playing";
sharedTimer.startTime = Date.now();
}
}
function drawBomb(x, y) {
fill('#000000');
ellipse(x, y, 15, 15); // Bomb representation
stroke("#000000");
strokeWeight(2);
line(x, y, x + 8, y - 12); // Fuse
noStroke();
}
function drawWaiting() {
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text("Waiting for players...", width / 2, height / 2);
}
function manageTimer() {
if (sharedTimer.gameState === "playing") {
const currentTime = Date.now();
const elapsed = currentTime - sharedTimer.startTime;
sharedTimer.displayTime = floor((PLAYING_DURATION - elapsed) / 1000);
if (elapsed >= PLAYING_DURATION) {
sharedTimer.gameState = "result";
sharedTimer.displayTime = null;
}
}
}
function drawResultPage() {
background("#F5F1EA");
fill(0);
textSize(16);
textAlign(CENTER, CENTER);
text("Game Over! Final Scores:", width / 2, 30); // Adjusted for better layout
// Calculate start Y position for listing scores
let startY = 60; // Start a bit lower to fit the title
let spacingY = 20;
// Display score and name for 'me'
let myFinalScore = me.id === sharedBombId.value ? 0 : me.score;
fill(me.id === sharedBombId.value ? 'red' : 'green');
text(`${me.name} - Score: ${myFinalScore}`, width / 2, startY);
// Display scores and names for guests
guests.forEach((guest, index) => {
let guestFinalScore = guest.id === sharedBombId.value ? 0 : guest.score;
fill(guest.id === sharedBombId.value ? 'red' : 'green');
text(`${guest.name} - Score: ${guestFinalScore}`, width / 2, startY + (index + 1) * spacingY);
});
// Prompt to restart game
fill(0);
text("Click to play again!", width / 2, height - 30);
}
function mousePressed() {
// Start the game when in the start state
if (sharedTimer.gameState === "start"){
// Assuming conditions are met to start the game (e.g., enough players)
sharedTimer.gameState = "waiting"; //
}
else if (sharedTimer.gameState === "waiting"){
// Assuming conditions are met to start the game (e.g., enough players)
checkAndStartGame();
}
else if (sharedTimer.gameState === "result"){
sharedTimer.gameState = "start";
sharedTimer.startTime = Date.now(); // Reset timer for game duration tracking
sharedTimer.displayTime = floor(PLAYING_DURATION / 1000) + 1; // Initialize display time if needed
} else if (sharedTimer.gameState === "result") {
// Reset the game from the result state
sharedTimer.gameState = "waiting"; // Or "waiting" if you have a waiting state before the game officially starts
sharedTimer.startTime = Date.now();
sharedTimer.displayTime = null;
// Reset scores and positions
resetGameForAllPlayers();
// Optionally, reassign the bomb randomly if that's part of your game logic
assignBombRandomly();
}
}
function assignBombRandomly() {
let playerIds = [me.id].concat(guests.map(guest => guest.id));
let randomIndex = floor(random(playerIds.length));
sharedBombId.value = playerIds[randomIndex];
// Ensure the initial bomb holder's score is adjusted if necessary
if (me.id === sharedBombId.value) me.score -= 1; // Adjust if holding the bomb has a score penalty
guests.forEach(guest => {
if (guest.id === sharedBombId.value) guest.score -= 1; // Adjust for guests
});
}