사용자:Bd3076/UncyBeat/Play

리버티게임, 모두가 만들어가는 자유로운 게임
< 사용자:Bd3076‎ | UncyBeat(UncyBeat/Play에서 넘어옴)
var getParameters = function (paramName) {
    var returnValue;

    var url = location.href;
    
    console.log(url);
    
    if(url[8] == 'f'){
      return 0;
    }

    var parameters = (url.slice(url.indexOf('?') + 1, url.length)).split('&');

    for (var i = 0; i < parameters.length; i++) {
        var varName = parameters[i].split('=')[0];
        if (varName.toUpperCase() == paramName.toUpperCase()) {
            returnValue = parameters[i].split('=')[1];
            return decodeURIComponent(returnValue);
        }
    }
};

function updateURLParameter(url, param, paramVal){
    var newAdditionalURL = "";
    var tempArray = url.split("?");
    var baseURL = tempArray[0];
    var additionalURL = tempArray[1];
    var temp = "";
    if (additionalURL) {
        tempArray = additionalURL.split("&");
        for (var i=0; i<tempArray.length; i++){
            if(tempArray[i].split('=')[0] != param){
                newAdditionalURL += temp + tempArray[i];
                temp = "&";
            }
        }
    }

    var rows_txt = temp + "" + param + "=" + paramVal;
    return baseURL + "?" + newAdditionalURL + rows_txt;
}

function Queue(){
  this.arr = [];
  this.s = 0;
  this.e = 0;
  this.size = 0;
  this.front = function(){
    return arr[s];
  }
  this.pop = function(){
    s++;
    size--;
    return;
  }
  this.push = function(k){
    arr[e++] = k;
    size--;
    return;
  }
  this.empty = function(){
    return size==0;
  }
}

var timer = 0;
function calculateTimer(){
	timer += 1;
}

var cvs = document.getElementById('spanArea');
cvs.innerHTML = "<canvas width='720' height='720' id='spanCanvas'></canvas>"
var canvas = document.getElementById('spanCanvas');
var ctx = canvas.getContext('2d');

var level = getParameters('level');
const levelCount = 1;

const levelNames = [
  'Dreams'
];

const levelMusicLink = [
  'https://k003.kiwi6.com/hotlink/9f13ktbumx/bensound-dreams.mp3'
];

function clearCanvas(){
  ctx.clearRect(0, 0, 720, 720);
}

function makeGameArea(){
  clearCanvas();
  ctx.strokeStyle = "black";
  ctx.strokeRect(20, 20, 680, 680);
  
  for(var i=0; i<5; i++){
  	for(var j=0; j<5; j++){
    	ctx.strokeRect(40+i*132, 40+j*132, 112, 112);
    }
  }
}

function playGameMusic(){
  console.log(level);
  var audioElement = new Audio(levelMusicLink[level]);
  var audio = audioElement.play();
  audioElement.onended = function(){
  	setTimeout(levelEnd, 3000);
  };
  setInterval(calculateTimer, 50);
}

var x = 2;
var y = 2;
var color = 0;

var perfect = 0;
var good = 0;
var miss = 0;

var score = 0;

function levelEnd(){
	var totalNotes = perfect+good+miss;
	score = (perfect / totalNotes) * 1000000 + (good / totalNotes) * 500000;
  clearInterval(calculateTimer);
  clearInterval(showCursor);
  clearInterval(displayNotes);
  
  console.log(score);
  
  var nowScore = getParameters('progress'+level);
  var maxScore = nowScore < score ? score : nowScore;
  location.href = updateURLParameter(location.href, 'progress'+level, maxScore);
}

var noteTimer = new Array(5);
for(var i=0; i<5; i++){
  noteTimer[i] = new Array(5);
  for(var j=0; j<5; j++){
    noteTimer[i][j] = -400;
  }
}

var noteColor = new Array(5);
for(var i=0; i<5; i++){
  noteColor[i] = new Array(5);
}

function showCursor(){
	for(var i=0; i<5; i++){
  	for(var j=0; j<5; j++){
      ctx.fillStyle = noteColor[i][j]==0?"red":"blue";
      if(noteTimer[i][j] > 0){
        ctx.fillRect(40+i*132, 40+j*132, (400-noteTimer[i][j]) * 112 / 400, 112);
      }
      else{
        ctx.clearRect(40+i*132, 40+j*132, 112, 112);
      }
      if(i==x && j==y){
        ctx.strokeStyle = color==0?"red":"blue";
      }
      else{
        ctx.strokeStyle = "black";
      }
    	ctx.strokeRect(40+i*132, 40+j*132, 112, 112);
    }
  }
}

function perfect_(x, y){
  perfect++;
  noteTimer[x][y] = -400;
}

function good_(x, y){
  good++;
  noteTimer[x][y] = -400;
}

function miss_(x, y){
  miss++;
}

function onKeyDown(e){
  var ek = e.keyCode;
  console.log(ek);
  if(37 <= ek && ek <= 40){
  	if(ek == 37){
      if(x!=0) x--;
    }
    else if(ek==38){
      if(y!=0) y--;
    }
    else if(ek==39){
      if(x!=4) x++;
    }
    else if(ek==40){
      if(y!=4) y++;
    }
    console.log(x + "and" + y);
    var point = Math.abs(noteTimer[x][y]);
    if(point <= 20 && color == noteColor[x][y]){
      perfect_(x, y);
    }
    else if((point <= 40 || noteTimer[x][y] > 20) && color == noteColor[x][y]){
      good_(x, y);
    }
  }
  else if(ek==32){
  	color = !color;
  }
}

window.addEventListener("keydown", onKeyDown, false);

var levelData = [
  "0|2|1|0/21|2|2|0/52|2|3|0/72|2|2|0/101|1|2|0/121|2|2|0/153|3|2|0/172|2|2|0/203|2|1|0/223|2|0|0/256|2|1|0/274|2|2|0/305|2|3|0/323|2|4|0/355|2|3|0/375|2|2|0/406|2|1|0/419|3|1|0/431|3|2|0/443|2|2|0/456|1|2|0/469|1|3|0/481|2|3|0/493|2|2|0/506|2|1|0/519|1|1|0/528|1|2|0/544|2|2|0/558|3|2|0/571|3|3|0/583|2|3|0/595|2|2|0/608|2|1|0/621|1|1|0/633|1|2|0/646|2|2|0/659|3|2|0/672|3|1|0/683|2|1|0/697|2|2|0/709|2|3|0/723|3|3|0/735|3|2|0/749|2|2|0/760|1|2|0/772|1|3|0/784|2|3|0/798|2|2|0/811|2|1|0/823|2|0|0/836|2|1|0/850|2|2|0/862|3|2|0/874|4|2|0/888|3|2|0/900|2|2|0/912|1|2|0/924|0|2|0/936|1|2|0/950|2|2|0/964|2|3|0/976|2|4|0/988|2|3|0/1001|2|2|0/1015|2|1|0/1033|2|2|0/1063|2|3|0/1082|2|2|0/1113|1|2|0/1133|2|2|0/1166|3|2|0/1184|2|2|0/1216|2|1|0/1227|2|0|0/1239|1|0|0/1252|0|0|0/1265|0|1|0/1277|0|2|0/1290|1|2|0/1302|2|2|0/1315|3|2|0/1328|4|2|0/1341|4|3|0/1353|4|4|0/1366|3|4|0/1379|2|4|0/1392|2|3|0/1404|2|2|0/1417|3|2|0/1431|4|2|0/1442|3|2|0/1454|2|2|0/1468|1|2|0/1480|0|2|0/1494|1|2|0/1506|2|2|0/1519|3|2|0/1533|4|2|0/1544|4|1|0/1557|4|0|0/1568|3|0|0/1581|2|0|0/1594|2|1|0/1607|2|2|0/1620|2|3|0/1633|2|4|0/1645|2|3|0/1658|2|2|0/1670|2|1|0/1683|2|0|0/1695|2|1|0/1708|2|2|0/1721|2|3|0/1733|2|4|0/1746|1|4|0/1759|0|4|0/1771|0|3|0/1783|0|2|0/1796|1|2|0/1809|2|2|0/1822|2|1|0/1837|2|0|0/1847|2|1|0/1859|2|2|0/1871|1|2|0/1885|0|2|0/1898|1|2|0/1910|2|2|0/1922|2|3|0/1935|2|4|0/1948|2|3|0/1959|2|2|0/1972|3|2|0/1984|4|2|0/1998|3|2|0/2010|2|2|0/2024|2|1|0/2042|2|0|0/2073|1|0|0/2091|0|0|0/2121|0|1|0/2142|0|2|0/2173|1|2|0/2193|2|2|0/2225|3|2|0/2244|4|2|0/2277|4|3|0/2295|4|4|0/2326|3|4|0/2345|2|4|0/2380|2|3|0/2396|2|2|0/2427|1|2|0/2445|0|2|0/2477|1|2|0/2496|2|2|0/2528|3|2|0/2547|4|2|0/2578|3|2|0/2597|2|2|0/2629|2|3|0/2655|2|2|0/2680|2|1|0/2705|2|2|0/2730|1|2|0/2754|2|2|0/2781|3|2|0/2804|2|2|0/2832|2|3|0/2841|3|3|0/2850|3|2|0/2856|3|1|0/2863|2|1|0/2866|1|1|0/2880|1|2|0/2891|1|3|0/2900|2|3|0/2907|3|3|0/2912|4|3|0/2932|3|3|0/2941|2|3|0/2951|1|3|0/2957|1|2|0/2961|1|1|0/2963|1|0|0/2982|2|0|0/2991|2|1|0/3001|2|2|0/3034|1|2|0/3044|0|2|0/3052|1|2|0/3059|2|2|0/3063|3|2|0/3069|3|3|0/3084|2|3|0/3095|2|2|0/3104|2|1|0/3109|1|1|0/3112|0|1|0/3115|0|2|0/3118|0|3|0/3134|1|3|0/3144|2|3|0/3152|2|2|0/3155|2|1|0/3158|2|0|0/3160|3|0|0/3185|3|1|0/3195|3|2|0/3202|2|2|0/3205|1|2|0/3208|1|3|0/3211|2|3|0/3236|2|2|0/3254|2|1|0/3285|2|2|0/3304|2|3|0/3330|2|2|0/3336|1|2|0/3356|2|2|0/3387|2|1|0/3406|2|2|0/3436|3|2|0/3457|2|2|0/3487|1|2|0/3507|2|2|0/3538|2|1|0/3557|2|2|0/3589|2|3|0/3608|2|2|0/3639|1|2|0/3660|2|2|0/3691|3|2|0/3712|2|2|0/3741|2|1|0/3759|2|2|0/3791|2|3|0/3810|2|2|0/3842|1|2|0/3860|2|2|0/3892|3|2|0/3912|2|2|0/3943|2|1|0/3960|2|2|0/3991|2|3|0/4011|2|2|0/4045|1|2|0/4063|2|2|0/4093|3|2|0/4113|2|2|0/"
];

var startIndex = 0;
var endIndex = 0;
var data;
var dataLength;

function calculateNotes(t, x, y, c){
  noteTimer[x][y] = 400;
  noteColor[x][y] = c;
}

function displayNotes(){
	for(var i=0; i<5; i++){
    for(var j=0; j<5; j++){
      if(noteTimer[i][j] != -400) noteTimer[i][j] -= 20;
      if(noteTimer[i][j] < -40 && noteTimer[i][j] != -400){
        noteTimer[i][j] = -400;
        miss_(i, j);
      }
    }
  }
}

function setInit(){
  data = levelData[level];
  dataLength = String(data).length;
}

function playNotes(){
	setInterval(displayNotes, 20);
  
  console.log(dataLength + ', ' + data);
  
  for(; endIndex < dataLength;){
    var tt, tx, ty, tc;
    while(data[endIndex] != '|') endIndex++;
    tt = data.substring(startIndex, endIndex);
    startIndex = ++endIndex;
    while(data[endIndex] != '|') endIndex++;
    tx = data.substring(startIndex, endIndex);
    startIndex = ++endIndex;
    while(data[endIndex] != '|') endIndex++;
    ty = data.substring(startIndex, endIndex);
    startIndex = ++endIndex;
    while(data[endIndex] != '/') endIndex++;
    tc = data.substring(startIndex, endIndex);
    startIndex = ++endIndex;
    
    console.log(tt + ', ' + tx + ', ' + ty + ', ' + tc);
    
    setTimeout(calculateNotes, tt*50-400+3100, tt, tx, ty, tc);
  }
}

function playGame(){
  makeGameArea();
  setInterval(showCursor, 30);
  setTimeout(playGameMusic, 3000);
  setInit();
  playNotes();
}

playGame();