xxxxxxxxxx
229
// require https://cdn.jsdelivr.net/npm/p5@1.4.0/lib/p5.js
// require /microgames/sketches/p5.play.js
// require https://cdn.jsdelivr.net/npm/p5.party@latest/dist/p5.party.js
const IS_DEV = false;
const BG_COLOR = "#ffcccc";
const FILL_COLOR = "#000066";
const SCREEN_WIDTH = 240;
const SCREEN_HEIGHT = 160;
const PIXEL_FACTOR = 2;
// p5 party
let shared, players, me;
// p5 play
let box, trampoline;
const TRAMPOLINE_SPEED = 10;
const BOX_START_SPEED = 5;
const BOX_SPEED_INCREMENT = 0.03;
function preload() {
partyConnect(
"wss://deepstream-server-1.herokuapp.com",
"olivier_non-couch-coop",
"main"
);
shared = partyLoadShared("shared");
players = partyLoadParticipantShareds();
me = partyLoadMyShared();
}
function setup() {
createCanvas(SCREEN_WIDTH * PIXEL_FACTOR, SCREEN_HEIGHT * PIXEL_FACTOR);
box = createSprite(width / 2, 0, 20, 20);
trampoline = createSprite(width / 2, height - 20, 60, 10);
trampoline.immovable = true;
setupSubscribes();
getSharedState();
}
function draw() {
background(BG_COLOR);
fill(FILL_COLOR);
if (!shared.gameIsStarted) {
push();
textAlign(CENTER);
text("Start Micro Game\n Press Space", width / 2, height / 2);
pop();
} else {
if (shared.gameIsDone) {
push();
textAlign(CENTER);
text("You got " + shared.score + " packages", width / 2, height / 2);
text("Restart: Press Space", width / 2, height / 2 + 40);
pop();
} else {
checkGame();
}
}
}
const checkGame = () => {
background(BG_COLOR);
drawSprites();
fill(FILL_COLOR);
noStroke();
trampoline.position.x = shared.x;
text(players.length + " Players", 10, 20);
text("Score: " + shared.score, 10, 40);
text("Lifes: " + shared.lives, 10, 60);
if (IS_DEV) {
text("Box Speed: " + nfc(shared.boxSpeed, 2), 10, 80);
text("x Position: " + shared.x, 10, 100);
}
if (keyIsDown(LEFT_ARROW)) {
shared.x -= TRAMPOLINE_SPEED / players.length;
if (shared.x <= 0) {
shared.x = 0;
}
} else if (keyIsDown(RIGHT_ARROW)) {
shared.x += TRAMPOLINE_SPEED / players.length;
if (shared.x >= width) {
shared.x = width;
}
}
if (partyIsHost()) {
// only the host needs to check the data
trampoline.collide(box, boxIsCaught);
checkBox();
if (shared.lives <= 0) {
endGame();
}
}
};
const checkBox = () => {
if (box.position.y > height) {
shared.lives--;
spawnBox();
}
};
const boxIsCaught = () => {
console.log("📦");
shared.score++;
shared.boxSpeed += BOX_SPEED_INCREMENT;
spawnBox();
};
const spawnBox = () => {
/*if( partyIsHost() ){
shared.boxPosition.y = 0;
shared.boxPosition.x = random(30, width - 30);
console.log( "new box.x", shared.boxPosition.x )
}else{
console.log( "received", shared.boxPosition.x )
}
box.position.y = shared.boxPosition.y;
box.position.x = shared.boxPosition.x;
box.setSpeed(shared.boxSpeed, 90);
if( partyIsHost() ){
partyEmit("Spawn");
}*/
if (partyIsHost()) {
shared.boxPosition.y = 0;
shared.boxPosition.x = random(30, width - 30);
}
/*
box.position.y = shared.boxPosition.y;
box.position.x = shared.boxPosition.x;
box.setSpeed(shared.boxSpeed, 90);
if( partyIsHost() ){
partyEmit("Spawn");
}
*/
};
const setupSubscribes = () => {
/*
partySubscribe("Spawn", ()=>{
partyIsHost() ? null : spawnBox();
} );
partySubscribe("HostSpawnBox",()=>{
partyIsHost() ? spawnBox() : null;
});
*/
partySubscribe("fetchState",getSharedState);
partyWatchShared(shared, "boxPosition", () => {
box.position.y = shared.boxPosition.y;
box.position.x = shared.boxPosition.x;
box.setSpeed(shared.boxSpeed, 90);
});
};
const getSharedState = () => {
shared.x = shared.x || width / 2;
shared.score = shared.score || 0;
shared.lives = shared.lives || 3;
shared.boxSpeed = shared.boxSpeed || BOX_START_SPEED;
shared.boxPosition = shared.boxPosition || { x: 0, y: 0 };
shared.gameIsStarted = shared.gameIsStarted || false;
shared.gameIsDone = shared.gameIsDone || false;
if( shared.gameIsStarted && partyIsHost() ){
spawnBox();
}
};
const startGame = () => {
shared.gameIsStarted = true;
shared.x = width / 2;
shared.score = 0;
shared.lives = 3;
shared.boxSpeed = BOX_START_SPEED;
partyEmit("fetchState");
if( partyIsHost() ){
spawnBox();
}
};
const resetGame = () => {
shared.gameIsStarted = false;
shared.gameIsDone = false;
partyEmit("fetchState");
};
const endGame = () => {
shared.gameIsDone = true;
partyEmit("fetchState");
};
function keyPressed() {
if (key === " ") {
// start game
if (!shared.gameIsStarted) {
startGame();
// restart game if game has ended
} else if (shared.gameIsDone) {
resetGame();
}
}
if (key === "E" || key === "e") {
endGame();
}
}