hotwire-js/game.js-global

210 lines
6.5 KiB
Plaintext

if(process.platform !== 'darwin') var gpio = require('rpi-gpio');
const player = require('node-wav-player');
// ist an wenn im kind mode (erw LED is aus)
const childLED = 37;
// ist and wenn im erwachsenen Mode (kind LED ist aus)
const erwLED = 38;
// status : ist an wenn in ruhe position, blinkt, wenn gestartet, ist aus bei fail
const statusLED = 40;
// setze auf HIGH wenn fail (klingel relais)
const failSwitch = 35;
// warte auf spielstart wenn auf HIGH
const startButton = 32;
// beende spiel wenn auf HIGH
const finishButton = 31;
// faile spiel wenn auf HIGH
const failButton = 29;
// wechsle kind/erwachen Modus + LED
const toggleButton = 33;
// wird je nach modus gesetzt und abgespielt wenn start
const backgroundGrown = "jeopardy.wav";
const backgroundChild = "northsouth.wav";
// wird zufallig zu zufaelligen momenten abgespielt (erw mode)
const erschreckSounds = [ "ziege.wav", "scream.wav", "lough.wav" ];
// wird nach fester zeit + random oder spaeter nach der besten zeit abgespielt (erw mode)
const stresserSound = "sharks.wav";
// wird bei fail abgespielt (zonk)
const failSound = "zonk.wav";
// hurra wird bei finish abgespielt
const finishSound = "kidscheering.wav";
const GROWN=1;
const CHILD=2;
global.STATES = {
STARTED: 'started',
FINISH: 'finshed',
FAIL: 'failed'
};
const bounce = {};
global.STATE = STATES.FINISH;
global.MODE = GROWN;
global.GAME = {};
function setup() {
console.log('Setting up GPIOs');
if(process.platform === 'darwin') {
setTimeout(start, 500);
setTimeout(fail, 3500);
setTimeout(start, 4000);
setTimeout(finish, 9000);
setTimeout(fail, 10000);
setTimeout(finish, 11000);
return;
}
gpio.setup(failSwitch, gpio.OUT, () => {
gpio.output(failSwitch, false);
});
gpio.setup(statusLED, gpio.OUT, () => {
gpio.output(statusLED, true);
});
gpio.setup(childLED, gpio.OUT, () => {
gpio.output(childLED, true);
});
gpio.setup(erwLED, gpio.OUT, () => {
gpio.output(erwLED, false);
});
gpio.setup(finishButton, gpio.DIR_IN, gpio.EDGE_RISING);
gpio.setup(toggleButton, gpio.DIR_IN, gpio.EDGE_RISING);
gpio.setup(startButton, gpio.DIR_IN, gpio.EDGE_RISING);
gpio.setup(failButton, gpio.DIR_IN, gpio.EDGE_RISING);
gpio.on('change', (channel, value) => {
if(bounce[value] === true) return;
console.log('Change',channel, value);
bounce[value] = true;
setTimeout(() => { bounce[value] = false; console.log('Debounced', value); }, 1000);
switch(channel) {
case startButton:
start(value);
break;
case finishButton:
finish(value);
break;
case failButton:
fail(value);
break;
case toggleButton:
toggle(value);
break;
default:
console.log('Alert! Unknown button change',channel);
}
});
console.log('Done. Game logic started. Waiting for input');
}
function gameTimer() {
console.log('game timer called');
global.GAME.events.forEach((el) => {
if(el.start === global.GAME.runTime) {
console.log('Event :', el);
playSound(el.sound);
}
});
global.GAME.runTime++;
}
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
function start(value) {
global.GAME = {
events: [],
runTime: 0
};
if(global.STATE === global.STATES.FINISH || global.STATE === STATES.FAIL) {
global.STATE = global.STATES.STARTED;
// set all params for next game, then start counter and event timer
if(global.MODE === GROWN) {
global.GAME.events.push({ sound: backgroundGrown, start: 0});
global.GAME.events.push({ sound: stresserSound, start: 30+getRandomInt(10) });
global.GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: getRandomInt(10) });
global.GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: 10+getRandomInt(10) });
global.GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: 20+getRandomInt(10) });
global.GAME.mode = GROWN;
global.GAME.fail = failSound;
global.GAME.finish = finishSound;
global.GAME.runtime = 0;
}
if(global.MODE === CHILD) {
global.GAME.events.push({ sound: backgroundChild, start: 0});
global.GAME.events.push({ sound: stresserSound, start: 40+getRandomInt(10)});
global.GAME.events.push({ sound: erschreckSounds[getRandomInt(2)], start: 10+getRandomInt(10)});
global.GAME.mode = CHILD;
global.GAME.fail = failSound;
global.GAME.finish = finishSound;
global.GAME.runtime = 0;
}
console.log('Starting new game', JSON.stringify(global.GAME, 2, null));
console.log(global.GAME);
global.GAME.countTimer = setInterval(gameTimer, 1000);
} else {
console.log('START not in state', global.STATE);
}
}
function finish(value) {
console.log('Finish');
if(global.STATE === global.STATES.STARTED) {
console.log('You made it in', global.GAME.runTime, 'seconds');
playSound(global.GAME.finish);
global.STATE = global.STATES.FINISH;
clearInterval(global.GAME.countTimer)
GAME.events = [];
} else {
console.log('FINISH not in state', global.STATE);
}
}
function fail(value) {
console.log('Fail');
if(global.STATE === global.STATES.STARTED) {
console.log('You failed after', global.GAME.runTime, 'seconds');
playSound(global.GAME.fail);
global.STATE = global.STATES.FAIL;
clearInterval(global.GAME.countTimer)
global.GAME.events = [];
} else {
console.log('FAIL not in state', global.STATE);
}
}
function toggle(value) {
if(global.STATE === global.STATES.FINISH || global.STATE === global.STATES.FAIL) {
console.log('Toggle');
if(global.MODE === CHILD) {
global.MODE = GROWN;
// set all values for GROWN mode
}
if(global.MODE === GROWN) {
global.MODE = CHILD;
// set all values for CHILD mode
}
} else {
console.log('Toggle not in state', global.STATE);
}
}
function playSound(file){
console.log("Play sound : ", file);
if(global.GAME.playing) {
console.log("Stopping old soundfile", global.GAME.playing);
player.stop();
}
global.GAME.playing = file;
player.play({ path: `wav/${file}`, sync: true }).then(() => { delete global.GAME.playing })
}
setup();