japanese-drum-game/public/src/js/controller.js

226 lines
5.4 KiB
JavaScript
Raw Normal View History

2018-08-28 07:56:31 +08:00
function Controller(selectedSong, songData, autoPlayEnabled){
2015-07-17 16:22:46 +08:00
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);
2018-08-23 03:31:37 +08:00
var _view = new View(this, _backgroundURL, selectedSong.title, selectedSong.difficulty);
2018-08-28 07:56:31 +08:00
var _mekadon = new Mekadon(this, _game);
2015-07-17 16:22:46 +08:00
var _keyboard = new Keyboard(this);
var _mainLoop;
var _pauseMenu = false;
2018-09-11 06:17:13 +08:00
var _mainAsset
assets.songs.forEach(song => {
if(song.id == selectedSong.folder){
_mainAsset = song.sound
}
})
2015-07-17 16:22:46 +08:00
2018-08-29 13:58:03 +08:00
this.autoPlayEnabled = autoPlayEnabled
2015-07-17 16:22:46 +08:00
this.run = function(){
_this.loadUIEvents();
_game.run();
_view.run();
_this.startMainLoop();
}
this.loadUIEvents = function(){
$("#song-selection-butt").click(function(){
2018-09-11 06:17:13 +08:00
assets.sounds["don"].play();
2015-07-17 16:22:46 +08:00
_this.songSelection();
});
$("#restart-butt").click(function(){
2018-09-11 06:17:13 +08:00
assets.sounds["don"].play();
2015-07-17 16:22:46 +08:00
_this.restartSong();
});
2018-08-06 19:04:02 +08:00
$("#continue-butt").click(function(){
_this.togglePauseMenu();
});
2015-07-17 16:22:46 +08:00
}
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();
}
2015-07-18 13:07:34 +08:00
else if(ms>=0 && !started){ //when music shall starts
2018-09-11 06:17:13 +08:00
_mainAsset.play(_songData.generalInfo.audioWait)
2015-07-17 16:22:46 +08:00
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();
}
this.togglePauseMenu = function(){
_this.togglePause();
_view.togglePauseMenu();
}
this.displayResults = function(){
clearInterval(_mainLoop);
2018-08-23 03:31:37 +08:00
var score = _this.getGlobalScore();
if (score.fail == 0) {
vp = 'fullcombo';
2018-09-11 22:50:10 +08:00
_this.playSoundMeka('fullcombo', 1.350);
2018-08-23 03:31:37 +08:00
} else if (score.hp >= 50) {
vp = 'clear';
} else {
vp = 'fail';
}
2018-09-11 06:17:13 +08:00
assets.sounds['game' + vp].play();
2018-08-23 03:31:37 +08:00
setTimeout(function(){
2018-09-11 06:17:13 +08:00
new Scoresheet(_this, _this.getGlobalScore());
2018-08-23 03:31:37 +08:00
}, 7000);
2015-07-17 16:22:46 +08:00
}
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(){
2018-09-11 06:17:13 +08:00
_mainAsset.stop()
2015-07-17 16:22:46 +08:00
clearInterval(_mainLoop);
$("#screen").load("/src/views/game.html", function(){
var taikoGame = new Controller(selectedSong, songData, autoPlayEnabled);
2015-07-17 16:22:46 +08:00
taikoGame.run();
});
}
2018-09-02 20:05:08 +08:00
2018-09-11 06:17:13 +08:00
this.playSoundMeka = function(soundID, time){
assets.sounds[soundID + (autoPlayEnabled ? '-meka' : '')].play(time)
2015-07-17 16:22:46 +08:00
}
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();
}
2018-08-28 07:56:31 +08:00
this.setKey = function(keyCode, down){
return _keyboard.setKey(keyCode, down);
}
2018-08-29 13:58:03 +08:00
this.getBindings = function(){
return _keyboard.getBindings();
}
2015-07-17 16:22:46 +08:00
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);
}
2018-09-06 00:46:26 +08:00
this.getKeyTime = function(){
return _keyboard.getKeyTime();
}
2015-07-17 16:22:46 +08:00
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);
}
2018-08-28 07:56:31 +08:00
this.autoPlay = function(circle){
2018-08-29 13:58:03 +08:00
_mekadon.play(circle)
2018-08-28 07:56:31 +08:00
}
2015-07-17 16:22:46 +08:00
}