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

444 lines
13 KiB
JavaScript
Raw Normal View History

2015-07-17 16:22:46 +08:00
function Game(controller, selectedSong, songData){
var _this = this;
var _selectedSong = selectedSong;
2018-09-13 01:10:00 +08:00
this.elapsedTime = {} //current time in ms from the beginning of the song
2015-07-17 16:22:46 +08:00
var _offsetDate; //date when the chrono is started (before the game begins)
var _startDate; //real start date (when the chrono will be 0)
var _currentDate; // refreshed date
var _songData=songData;
var _currentCircle=0;
var _currentScore=0;
var _combo=0;
2018-08-06 20:41:56 +08:00
var _globalScore={points:0, great:0, good:0, fail:0, maxCombo:0, hp:0, song:selectedSong.title};
2015-07-17 16:22:46 +08:00
var _HPGain= 100/_songData.circles.length;
var _paused=false;
var _started=false;
2018-09-13 01:10:00 +08:00
var _mainMusicPlaying=false;
2015-07-17 16:22:46 +08:00
var _latestDate;
2018-09-13 01:10:00 +08:00
var _elapsedTimeSincePause=0;
2015-07-17 16:22:46 +08:00
var _musicFadeOut=0;
var _fadeOutStarted=false;
var _currentTimingPoint=0;
var _offsetTime=0;
2018-09-13 01:10:00 +08:00
var _hitcircleSpeed=_songData.difficulty.sliderMultiplier*8;
var _timeForDistanceCircle;
var _mainAsset
assets.songs.forEach(song => {
if(song.id == selectedSong.folder){
_mainAsset = song.sound
}
})
2015-07-17 16:22:46 +08:00
this.run = function(){
2018-09-13 01:10:00 +08:00
_timeForDistanceCircle=2500
_this.initTiming();
2015-07-17 16:22:46 +08:00
}
this.initTiming = function(){
_offsetDate = new Date();
2018-09-13 01:10:00 +08:00
_this.setElapsedTime(-_timeForDistanceCircle |0)
_offsetTime = _timeForDistanceCircle |0
2015-07-17 16:22:46 +08:00
_startDate = new Date();
2018-09-13 01:10:00 +08:00
// The real start for the game will start when chrono will reach 0
_startDate.setMilliseconds(_startDate.getMilliseconds()+_offsetTime);
2015-07-17 16:22:46 +08:00
}
this.update = function(){
2018-09-13 01:10:00 +08:00
// Main operations
2015-07-17 16:22:46 +08:00
_this.updateTime();
_this.checkTiming();
_this.updateCirclesStatus();
_this.checkPlays();
2018-09-13 01:10:00 +08:00
// Event operations
2015-07-17 16:22:46 +08:00
_this.whenFadeoutMusic();
_this.whenLastCirclePlayed();
2018-09-13 01:10:00 +08:00
}
this.getCircles = function(){
return _songData.circles;
2015-07-17 16:22:46 +08:00
}
this.updateCirclesStatus = function(){
var circles = _songData.circles;
circles.forEach(function(circle){
if(!circle.getPlayed()){
2018-09-13 01:10:00 +08:00
var currentTime = _this.getElapsedTime().ms;
2015-07-17 16:22:46 +08:00
var startingTime = circle.getMS()-_timeForDistanceCircle;
2018-09-13 01:10:00 +08:00
// At circle.getMS(), the circle fits the slot
var finishTime = circle.getMS();
2015-07-17 16:22:46 +08:00
if( currentTime >= startingTime && currentTime <= finishTime+200){
2018-09-13 01:10:00 +08:00
if(currentTime>= finishTime-50 && currentTime < finishTime-30){
2015-07-17 16:22:46 +08:00
circle.updateStatus(0);
}
else if(currentTime>= finishTime-30 && currentTime < finishTime){
circle.updateStatus(230);
}
else if(currentTime >= finishTime && currentTime < finishTime+200){
circle.updateStatus(450);
}
}
else if(currentTime>finishTime+200 && currentTime<=finishTime+300){
2018-09-13 01:10:00 +08:00
if(controller.multiplayer != 2){
circle.updateStatus(-1);
_currentScore=0;
circle.played(_currentScore);
controller.displayScore(_currentScore, true);
_this.updateCurrentCircle();
_this.updateCombo(_currentScore);
_this.updateGlobalScore(_currentScore);
}
if(controller.multiplayer == 1){
p2.send("note", {
score: -1
})
}
2015-07-17 16:22:46 +08:00
}
}
});
}
this.setHPGain = function(gain){
_HPGain=gain;
}
this.checkPlays = function(){
2018-09-13 01:10:00 +08:00
var circles = _songData.circles;
2015-07-17 16:22:46 +08:00
var circle = circles[_currentCircle];
if(circle){
2018-08-29 13:58:03 +08:00
if(controller.autoPlayEnabled){
return controller.autoPlay(circle)
2018-08-28 07:56:31 +08:00
}
2018-08-29 13:58:03 +08:00
var keys = controller.getKeys()
var kbd = controller.getBindings()
if(keys[kbd["don_l"]]){
_this.checkKey(kbd["don_l"], circle)
2015-07-17 16:22:46 +08:00
}
2018-08-29 13:58:03 +08:00
if(keys[kbd["don_r"]]){
_this.checkKey(kbd["don_r"], circle)
2015-07-17 16:22:46 +08:00
}
2018-08-29 13:58:03 +08:00
if(keys[kbd["ka_l"]]){
_this.checkKey(kbd["ka_l"], circle)
2015-07-17 16:22:46 +08:00
}
2018-08-29 13:58:03 +08:00
if(keys[kbd["ka_r"]]){
_this.checkKey(kbd["ka_r"], circle)
2015-07-17 16:22:46 +08:00
}
}
}
2018-08-29 13:58:03 +08:00
this.checkKey = function(keyCode, circle){
if(!circle.getPlayed() && !controller.isWaitingForKeyup(keyCode, "score") && circle.getStatus()!=-1){
var score = _this.checkScore(circle);
circle.played(score);
_this.updateCurrentCircle();
controller.waitForKeyup(keyCode, "score");
2018-09-13 01:10:00 +08:00
if(controller.multiplayer == 1){
p2.send("note", {
score: score,
ms: circle.getMS() - _this.getElapsedTime().ms
})
}
2018-08-29 13:58:03 +08:00
}
}
2015-07-17 16:22:46 +08:00
this.checkScore = function(circle){
2018-09-06 00:46:26 +08:00
var keys = controller.getKeys()
var kbd = controller.getBindings()
2015-07-17 16:22:46 +08:00
if(
2018-09-13 01:10:00 +08:00
((keys[kbd["don_l"]] || keys[kbd["don_r"]]) && (circle.getType()=="don" || circle.getType()=="daiDon")) ||
2018-09-06 00:46:26 +08:00
((keys[kbd["ka_l"]] || keys[kbd["ka_r"]]) && (circle.getType()=="ka" || circle.getType()=="daiKa"))
2015-07-17 16:22:46 +08:00
){
switch(circle.getStatus()){
case 230:
_currentScore=230;
break;
case 450:
_currentScore=450;
break;
}
2018-09-13 01:10:00 +08:00
controller.displayScore(_currentScore);
2015-07-17 16:22:46 +08:00
}
else{
_currentScore=0;
2018-09-13 01:10:00 +08:00
controller.displayScore(_currentScore, true);
2015-07-17 16:22:46 +08:00
}
_this.updateCombo(_currentScore);
_this.updateGlobalScore(_currentScore);
2018-09-13 01:10:00 +08:00
return _currentScore;
2015-07-17 16:22:46 +08:00
}
this.whenLastCirclePlayed = function(){
var circles = _songData.circles;
var lastCircle = circles[_songData.circles.length-1];
2018-09-13 01:10:00 +08:00
if(!_fadeOutStarted && _this.getElapsedTime().ms>=lastCircle.getMS()+2000){
_fadeOutStarted=_this.getElapsedTime().ms
2015-07-17 16:22:46 +08:00
}
}
2018-08-27 00:14:56 +08:00
2015-07-17 16:22:46 +08:00
this.whenFadeoutMusic = function(){
if(_fadeOutStarted){
2018-09-11 06:17:13 +08:00
if(_musicFadeOut==0){
snd.musicGain.fadeOut(1.6)
2018-09-13 01:10:00 +08:00
_musicFadeOut++
if(controller.multiplayer == 1){
p2.send("gameend")
}
2015-07-17 16:22:46 +08:00
}
2018-09-13 01:10:00 +08:00
if(_musicFadeOut==1 && _this.getElapsedTime().ms>=_fadeOutStarted+1600){
2018-09-11 06:17:13 +08:00
controller.fadeOutOver()
2018-09-11 22:50:10 +08:00
_mainAsset.stop()
2018-09-13 01:10:00 +08:00
_musicFadeOut++
2018-09-11 22:50:10 +08:00
setTimeout(() => {
snd.musicGain.fadeIn()
snd.musicGain.unmute()
}, 1000)
2015-07-17 16:22:46 +08:00
}
}
}
this.checkTiming = function(){
if(_songData.timingPoints[_currentTimingPoint+1]){
2018-09-13 01:10:00 +08:00
if(_this.getElapsedTime().ms>=_songData.timingPoints[_currentTimingPoint+1].start){
2015-07-17 16:22:46 +08:00
_currentTimingPoint++;
}
}
}
this.getCurrentTimingPoint = function(){
return _songData.timingPoints[_currentTimingPoint];
}
2018-09-13 01:10:00 +08:00
this.playMainMusic = function(){
var ms = _this.getElapsedTime().ms
if(!_mainMusicPlaying && (!_fadeOutStarted || ms<_fadeOutStarted+1600)){
if(controller.multiplayer != 2){
_mainAsset.play((ms < 0 ? -ms : 0) / 1000, false, Math.max(0, ms / 1000));
}
2015-07-17 16:22:46 +08:00
_mainMusicPlaying=true;
}
}
this.fadeOutOver = function(){
2018-09-13 01:10:00 +08:00
2015-07-17 16:22:46 +08:00
}
this.getHitcircleSpeed = function(){
return _hitcircleSpeed;
}
this.togglePause = function(){
if(!_paused){
2018-09-11 06:17:13 +08:00
assets.sounds["pause"].play();
2015-07-17 16:22:46 +08:00
_paused=true;
_latestDate = new Date();
2018-09-13 01:10:00 +08:00
_mainAsset.stop();
_mainMusicPlaying=false;
2015-07-17 16:22:46 +08:00
}
else{
2018-09-11 06:17:13 +08:00
assets.sounds["cancel"].play();
2018-09-13 01:10:00 +08:00
_paused=false;
2015-07-17 16:22:46 +08:00
var currentDate = new Date();
2018-09-13 01:10:00 +08:00
_elapsedTimeSincePause = _elapsedTimeSincePause + currentDate.getTime() - _latestDate.getTime();
2015-07-17 16:22:46 +08:00
}
}
this.isPaused = function(){
return _paused;
}
2018-09-13 01:10:00 +08:00
this.getElapsedTime = function(){
return this.elapsedTime;
}
this.setElapsedTime = function(time){
this.elapsedTime.ms = time
this.elapsedTime.sec = (this.elapsedTime.ms / 1000 |0) % 60
this.elapsedTime.min = (this.elapsedTime.ms / 1000 / 60 |0) % 60
this.elapsedTime.hour = (this.elapsedTime.ms / 1000 / 60 / 60 |0) % 60
2015-07-17 16:22:46 +08:00
}
this.updateTime = function(){
_currentDate = new Date();
2018-09-13 01:10:00 +08:00
var time = _this.getElapsedTime()
2015-07-17 16:22:46 +08:00
2018-09-13 01:10:00 +08:00
if(time.ms<0){
_this.setElapsedTime(_currentDate.getTime() - _startDate.getTime() - _elapsedTimeSincePause)
2015-07-17 16:22:46 +08:00
}
2018-09-13 01:10:00 +08:00
else if(time.ms>=0 && !_started){
2015-07-17 16:22:46 +08:00
_startDate = new Date();
2018-09-13 01:10:00 +08:00
_elapsedTimeSincePause = 0;
_this.setElapsedTime(_currentDate.getTime() - _startDate.getTime())
2015-07-17 16:22:46 +08:00
_started=true;
}
2018-09-13 01:10:00 +08:00
else if(time.ms>=0 && _started){
_this.setElapsedTime(_currentDate.getTime() - _startDate.getTime() - _elapsedTimeSincePause)
2015-07-17 16:22:46 +08:00
}
}
this.getCircles = function(){
return _songData.circles;
}
this.getSongData = function(){
return _songData;
}
this.updateCurrentCircle = function(){
2018-09-13 01:10:00 +08:00
_currentCircle++;
2015-07-17 16:22:46 +08:00
}
this.getCurrentCircle = function(){
return _currentCircle;
}
this.updateCombo = function(score){
(score!=0) ? _combo++ : _combo=0;
if(_combo>_globalScore.maxCombo) _globalScore.maxCombo = _combo;
switch(_combo){
case 50:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-50");
2015-07-17 16:22:46 +08:00
break;
case 100:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-100");
2015-07-17 16:22:46 +08:00
break;
case 200:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-200");
2015-07-17 16:22:46 +08:00
break;
case 300:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-300");
2015-07-17 16:22:46 +08:00
break;
case 400:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-400");
2015-07-17 16:22:46 +08:00
break;
case 500:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-500");
2015-07-17 16:22:46 +08:00
break;
case 600:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-600");
2015-07-17 16:22:46 +08:00
break;
case 700:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-700");
2015-07-17 16:22:46 +08:00
break;
2018-08-28 03:34:40 +08:00
case 800:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-800");
2018-08-28 03:34:40 +08:00
break;
case 900:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-900");
2018-08-28 03:34:40 +08:00
break;
case 1000:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-1000");
2018-08-28 03:34:40 +08:00
break;
case 1100:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-1100");
2018-08-28 03:34:40 +08:00
break;
case 1200:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-1200");
2018-08-28 03:34:40 +08:00
break;
case 1300:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-1300");
2018-08-28 03:34:40 +08:00
break;
case 1400:
2018-09-02 20:05:08 +08:00
controller.playSoundMeka("combo-1400");
2018-08-28 03:34:40 +08:00
break;
2015-07-17 16:22:46 +08:00
}
}
this.getCombo = function(){
return _combo;
}
this.getGlobalScore = function(){
return _globalScore;
}
this.updateGlobalScore = function(score){
/* Circle score */
switch(score){
case 450:
_globalScore.great++;
break;
case 230:
_globalScore.good++;
break;
case 0:
_globalScore.fail++;
break;
}
/* HP Update */
if(score!=0){
_globalScore.hp+=_HPGain;
}
else{
if(_globalScore.hp-_HPGain>0)
_globalScore.hp-=_HPGain;
else
_globalScore.hp=0;
}
/* Points update */
if(_combo>=11 && _combo<=20){
score+=100;
}
2018-09-13 01:10:00 +08:00
else if(_combo>=21 && _combo<=30){
score+=200;
}
else if(_combo>=31 && _combo<=40){
score+=300;
}
else if(_combo>=41 && _combo<=50){
score+=400;
}
else if(_combo>=51 && _combo<=60){
score+=500;
}
else if(_combo>=61 && _combo<=70){
score+=500;
}
else if(_combo>=71 && _combo<=80){
score+=600;
}
else if(_combo>=81 && _combo<=90){
score+=700;
}
else if(_combo>=91 && _combo<=100){
score+=800;
}
2015-07-17 16:22:46 +08:00
2018-09-13 01:10:00 +08:00
_globalScore.points+=score;
2015-07-17 16:22:46 +08:00
}
}