Add 2-player mode
This commit is contained in:
@@ -1,226 +1,220 @@
|
||||
function Controller(selectedSong, songData, autoPlayEnabled){
|
||||
|
||||
var _this = this;
|
||||
var _backgroundURL = "/songs/"+selectedSong.folder+"/bg.png";
|
||||
|
||||
var _songParser = new ParseSong(songData); //get file content
|
||||
var _songData = _songParser.getData();
|
||||
|
||||
var _game = new Game(this, selectedSong, _songData);
|
||||
var _view = new View(this, _backgroundURL, selectedSong.title, selectedSong.difficulty);
|
||||
var _mekadon = new Mekadon(this, _game);
|
||||
var _keyboard = new Keyboard(this);
|
||||
var _mainLoop;
|
||||
var _pauseMenu = false;
|
||||
var _mainAsset
|
||||
assets.songs.forEach(song => {
|
||||
if(song.id == selectedSong.folder){
|
||||
_mainAsset = song.sound
|
||||
}
|
||||
})
|
||||
|
||||
this.autoPlayEnabled = autoPlayEnabled
|
||||
|
||||
this.run = function(){
|
||||
class Controller{
|
||||
constructor(selectedSong, songData, autoPlayEnabled, multiplayer){
|
||||
this.selectedSong = selectedSong
|
||||
this.songData = songData
|
||||
this.autoPlayEnabled = autoPlayEnabled
|
||||
this.multiplayer = multiplayer
|
||||
this.pauseMenu = false
|
||||
|
||||
_this.loadUIEvents();
|
||||
_game.run();
|
||||
_view.run();
|
||||
_this.startMainLoop();
|
||||
}
|
||||
|
||||
this.loadUIEvents = function(){
|
||||
$("#song-selection-butt").click(function(){
|
||||
assets.sounds["don"].play();
|
||||
_this.songSelection();
|
||||
});
|
||||
$("#restart-butt").click(function(){
|
||||
assets.sounds["don"].play();
|
||||
_this.restartSong();
|
||||
});
|
||||
$("#continue-butt").click(function(){
|
||||
_this.togglePauseMenu();
|
||||
});
|
||||
var backgroundURL = "/songs/" + this.selectedSong.folder + "/bg.png"
|
||||
var songParser = new ParseSong(songData)
|
||||
this.parsedSongData = songParser.getData()
|
||||
|
||||
assets.songs.forEach(song => {
|
||||
if(song.id == this.selectedSong.folder){
|
||||
this.mainAsset = song.sound
|
||||
}
|
||||
})
|
||||
|
||||
this.game = new Game(this, this.selectedSong, this.parsedSongData)
|
||||
this.view = new View(this, backgroundURL, this.selectedSong.title, this.selectedSong.difficulty)
|
||||
this.mekadon = new Mekadon(this, this.game)
|
||||
this.keyboard = new Keyboard(this)
|
||||
}
|
||||
|
||||
this.startMainLoop = function(){
|
||||
|
||||
var started=false;
|
||||
_mainLoop = setInterval(function(){
|
||||
|
||||
var ms = _game.getEllapsedTime().ms;
|
||||
if(ms<0){ //before starting game, offseting the circles
|
||||
_game.updateTime();
|
||||
_view.refresh();
|
||||
}
|
||||
else if(ms>=0 && !started){ //when music shall starts
|
||||
_mainAsset.play(_songData.generalInfo.audioWait)
|
||||
started=true;
|
||||
}
|
||||
|
||||
if(started){ //Game start here
|
||||
if(!_game.isPaused()){
|
||||
_game.update();
|
||||
_view.refresh();
|
||||
_keyboard.checkGameKeys();
|
||||
}
|
||||
_keyboard.checkMenuKeys();
|
||||
}
|
||||
|
||||
}, 20);
|
||||
|
||||
}
|
||||
|
||||
this.getDistanceForCircle = function(){
|
||||
return _view.getDistanceForCircle();
|
||||
run(syncWith){
|
||||
this.loadUIEvents()
|
||||
this.game.run()
|
||||
this.view.run()
|
||||
this.startMainLoop()
|
||||
if(syncWith){
|
||||
syncWith.game.getElapsedTime = () => {
|
||||
return this.game.elapsedTime
|
||||
}
|
||||
this.game.setElapsedTime =
|
||||
syncWith.game.setElapsedTime = time => {
|
||||
this.game.elapsedTime.ms = time
|
||||
syncWith.game.elapsedTime.ms = time
|
||||
}
|
||||
syncWith.run()
|
||||
this.syncWith = syncWith
|
||||
}
|
||||
}
|
||||
|
||||
this.togglePauseMenu = function(){
|
||||
_this.togglePause();
|
||||
_view.togglePauseMenu();
|
||||
}
|
||||
|
||||
this.displayResults = function(){
|
||||
clearInterval(_mainLoop);
|
||||
|
||||
|
||||
var score = _this.getGlobalScore();
|
||||
|
||||
if (score.fail == 0) {
|
||||
vp = 'fullcombo';
|
||||
_this.playSoundMeka('fullcombo', 1.350);
|
||||
} else if (score.hp >= 50) {
|
||||
vp = 'clear';
|
||||
} else {
|
||||
vp = 'fail';
|
||||
}
|
||||
|
||||
assets.sounds['game' + vp].play();
|
||||
|
||||
setTimeout(function(){
|
||||
new Scoresheet(_this, _this.getGlobalScore());
|
||||
}, 7000);
|
||||
}
|
||||
|
||||
this.displayScore = function(score, notPlayed){
|
||||
_view.displayScore(score, notPlayed);
|
||||
}
|
||||
|
||||
this.fadeOutOver = function(){
|
||||
_game.fadeOutOver();
|
||||
_this.displayResults();
|
||||
}
|
||||
|
||||
this.getCurrentTimingPoint = function(){
|
||||
return _game.getCurrentTimingPoint();
|
||||
}
|
||||
|
||||
this.songSelection = function(){
|
||||
$("#main-music").remove();
|
||||
$("#music-bg").remove();
|
||||
clearInterval(_mainLoop);
|
||||
new SongSelect();
|
||||
}
|
||||
|
||||
this.restartSong = function(){
|
||||
_mainAsset.stop()
|
||||
clearInterval(_mainLoop);
|
||||
$("#screen").load("/src/views/game.html", function(){
|
||||
var taikoGame = new Controller(selectedSong, songData, autoPlayEnabled);
|
||||
taikoGame.run();
|
||||
});
|
||||
}
|
||||
|
||||
this.playSoundMeka = function(soundID, time){
|
||||
assets.sounds[soundID + (autoPlayEnabled ? '-meka' : '')].play(time)
|
||||
}
|
||||
|
||||
this.initTiming = function(){
|
||||
_game.initTiming();
|
||||
}
|
||||
|
||||
this.setHitcircleSpeed = function(speed){
|
||||
_view.setHitcircleSpeed(speed);
|
||||
}
|
||||
|
||||
this.getHitcircleSpeed = function(){
|
||||
return _game.getHitcircleSpeed();
|
||||
}
|
||||
|
||||
this.toggleMainMusic = function(){
|
||||
_game.toggleMainMusic();
|
||||
}
|
||||
|
||||
this.togglePause = function(){
|
||||
_game.togglePause();
|
||||
}
|
||||
|
||||
this.isPaused = function(){
|
||||
return _game.isPaused();
|
||||
}
|
||||
|
||||
this.getKeys = function(){
|
||||
return _keyboard.getKeys();
|
||||
}
|
||||
|
||||
this.setKey = function(keyCode, down){
|
||||
return _keyboard.setKey(keyCode, down);
|
||||
}
|
||||
|
||||
this.getBindings = function(){
|
||||
return _keyboard.getBindings();
|
||||
}
|
||||
|
||||
this.getSongData = function(){
|
||||
return _game.getSongData();
|
||||
}
|
||||
|
||||
this.getEllapsedTime = function(){
|
||||
return _game.getEllapsedTime();
|
||||
}
|
||||
|
||||
this.getCircles = function(){
|
||||
return _game.getCircles();
|
||||
}
|
||||
|
||||
this.getCurrentCircle = function(){
|
||||
return _game.getCurrentCircle();
|
||||
}
|
||||
|
||||
this.updateCurrentCircle = function(){
|
||||
_game.updateCurrentCircle();
|
||||
}
|
||||
|
||||
this.isWaitingForKeyup = function(key, type){
|
||||
return _keyboard.isWaitingForKeyup(key, type);
|
||||
}
|
||||
|
||||
this.waitForKeyup = function(key, type){
|
||||
_keyboard.waitForKeyup(key, type);
|
||||
}
|
||||
|
||||
this.getKeyTime = function(){
|
||||
return _keyboard.getKeyTime();
|
||||
}
|
||||
|
||||
this.updateCombo = function(score){
|
||||
_game.updateCombo(score);
|
||||
}
|
||||
|
||||
this.getCombo = function(){
|
||||
return _game.getCombo();
|
||||
}
|
||||
|
||||
this.getGlobalScore = function(){
|
||||
return _game.getGlobalScore();
|
||||
}
|
||||
|
||||
this.updateGlobalScore = function(score){
|
||||
_game.updateGlobalScore(score);
|
||||
}
|
||||
|
||||
this.autoPlay = function(circle){
|
||||
_mekadon.play(circle)
|
||||
}
|
||||
|
||||
}
|
||||
loadUIEvents(){
|
||||
$("#song-selection-butt").click(() => {
|
||||
assets.sounds["don"].play()
|
||||
this.songSelection()
|
||||
})
|
||||
$("#restart-butt").click(() => {
|
||||
assets.sounds["don"].play()
|
||||
this.restartSong()
|
||||
})
|
||||
$("#continue-butt").click(() => {
|
||||
this.togglePauseMenu()
|
||||
})
|
||||
}
|
||||
startMainLoop(){
|
||||
this.mainLoopStarted = false
|
||||
this.mainLoopRunning = true
|
||||
this.mainLoop()
|
||||
}
|
||||
mainLoop(){
|
||||
if(this.mainLoopRunning){
|
||||
if(this.multiplayer != 2){
|
||||
requestAnimationFrame(() => {
|
||||
if(this.syncWith){
|
||||
this.syncWith.game.elapsedTime.ms = this.game.elapsedTime.ms
|
||||
}
|
||||
this.mainLoop()
|
||||
if(this.syncWith){
|
||||
this.syncWith.mainLoop()
|
||||
}
|
||||
})
|
||||
}
|
||||
var ms = this.game.getElapsedTime().ms
|
||||
if(!this.game.isPaused()){
|
||||
if(ms >= 0 && !this.mainLoopStarted){
|
||||
this.mainLoopStarted = true
|
||||
}
|
||||
if(ms < 0){
|
||||
this.game.updateTime()
|
||||
}
|
||||
if(this.mainLoopStarted){
|
||||
this.game.update()
|
||||
this.game.playMainMusic()
|
||||
}
|
||||
this.view.refresh()
|
||||
this.keyboard.checkGameKeys()
|
||||
}
|
||||
this.keyboard.checkMenuKeys()
|
||||
}
|
||||
}
|
||||
getDistanceForCircle(){
|
||||
return this.view.getDistanceForCircle()
|
||||
}
|
||||
togglePauseMenu(){
|
||||
this.togglePause()
|
||||
this.view.togglePauseMenu()
|
||||
}
|
||||
displayResults(){
|
||||
var score = this.getGlobalScore()
|
||||
var vp
|
||||
if (score.fail == 0) {
|
||||
vp = "fullcombo"
|
||||
this.playSoundMeka("fullcombo", 1.350)
|
||||
} else if (score.hp >= 50) {
|
||||
vp = "clear"
|
||||
} else {
|
||||
vp = "fail"
|
||||
}
|
||||
assets.sounds["game" + vp].play()
|
||||
setTimeout(() => {
|
||||
this.mainLoopRunning = false
|
||||
if(this.multiplayer != 2){
|
||||
new Scoresheet(this, this.getGlobalScore())
|
||||
}
|
||||
}, 7000)
|
||||
}
|
||||
displayScore(score, notPlayed){
|
||||
this.view.displayScore(score, notPlayed)
|
||||
}
|
||||
fadeOutOver(){
|
||||
this.game.fadeOutOver()
|
||||
this.displayResults()
|
||||
}
|
||||
getCurrentTimingPoint(){
|
||||
return this.game.getCurrentTimingPoint()
|
||||
}
|
||||
songSelection(){
|
||||
$("#music-bg").remove()
|
||||
this.mainLoopRunning = false
|
||||
new SongSelect()
|
||||
}
|
||||
restartSong(){
|
||||
this.mainAsset.stop()
|
||||
this.mainLoopRunning = false
|
||||
$("#screen").load("/src/views/game.html", () => {
|
||||
var taikoGame = new Controller(this.selectedSong, this.songData, this.autoPlayEnabled)
|
||||
taikoGame.run()
|
||||
})
|
||||
}
|
||||
playSoundMeka(soundID, time){
|
||||
var meka = ""
|
||||
if(this.autoPlayEnabled && !this.multiplayer){
|
||||
meka = "-meka"
|
||||
}
|
||||
assets.sounds[soundID + meka].play(time)
|
||||
}
|
||||
initTiming(){
|
||||
this.game.initTiming()
|
||||
}
|
||||
setHitcircleSpeed(speed){
|
||||
this.view.setHitcircleSpeed(speed)
|
||||
}
|
||||
getHitcircleSpeed(){
|
||||
return this.game.getHitcircleSpeed()
|
||||
}
|
||||
toggleMainMusic(){
|
||||
this.game.toggleMainMusic()
|
||||
}
|
||||
togglePause(){
|
||||
if(this.syncWith){
|
||||
this.syncWith.game.togglePause()
|
||||
}
|
||||
this.game.togglePause()
|
||||
}
|
||||
isPaused(){
|
||||
return this.game.isPaused()
|
||||
}
|
||||
getKeys(){
|
||||
return this.keyboard.getKeys()
|
||||
}
|
||||
setKey(keyCode, down){
|
||||
return this.keyboard.setKey(keyCode, down)
|
||||
}
|
||||
getBindings(){
|
||||
return this.keyboard.getBindings()
|
||||
}
|
||||
getSongData(){
|
||||
return this.game.getSongData()
|
||||
}
|
||||
getElapsedTime(){
|
||||
return this.game.getElapsedTime()
|
||||
}
|
||||
getCircles(){
|
||||
return this.game.getCircles()
|
||||
}
|
||||
getCurrentCircle(){
|
||||
return this.game.getCurrentCircle()
|
||||
}
|
||||
updateCurrentCircle(){
|
||||
this.game.updateCurrentCircle()
|
||||
}
|
||||
isWaitingForKeyup(key, type){
|
||||
return this.keyboard.isWaitingForKeyup(key, type)
|
||||
}
|
||||
waitForKeyup(key, type){
|
||||
this.keyboard.waitForKeyup(key, type)
|
||||
}
|
||||
getKeyTime(){
|
||||
return this.keyboard.getKeyTime()
|
||||
}
|
||||
updateCombo(score){
|
||||
this.game.updateCombo(score)
|
||||
}
|
||||
getCombo(){
|
||||
return this.game.getCombo()
|
||||
}
|
||||
getGlobalScore(){
|
||||
return this.game.getGlobalScore()
|
||||
}
|
||||
updateGlobalScore(score){
|
||||
this.game.updateGlobalScore(score)
|
||||
}
|
||||
autoPlay(circle){
|
||||
if(this.multiplayer){
|
||||
p2.play(circle, this.mekadon)
|
||||
}else{
|
||||
this.mekadon.play(circle)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user