사용자:Bd3076/common.js: 두 판 사이의 차이

리버티게임, 모두가 만들어가는 자유로운 게임
백괴게임>Bd3076
(문서를 비움)
백괴게임>Bd3076
(플러그인 switched_maze설치)
1번째 줄: 1번째 줄:


/** 플러그인 switched_maze***************************
* 스위치 미로 게임
* 버전 => 1.0
* 작성자 : [[사용자:Bd3076|Bd3076]]
* JSON => switched_maze = {"name":"switched_maze","descript":"스위치 미로 게임","version":"1.0","local":true,"creat":"Bd3076","state":"스위치 미로/plugin","executable":true};
*/
function plugin_switched_maze(){
  if($("[data-name='switched_maze']").length >= 1){
  // 이부분에 코드 입력 //
var keyPress = new Array(128);
document.onkeydown = function(e){
  e = e || window.event;
  keyPress[e.keyCode] = 1;
}
document.onkeyup = function(e){
  e = e || window.event;
  keyPress[e.keyCode] = 0;
}
var cvs = document.getElementById("cvs");
cvs.innerHTML = ('<canvas id="canvas" width="480" height="480"></canvas>')
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 500, 500);
var stopWatchH = 0;
var stopWatchM = 0;
var stopWatchS = 0;
var stopWatchSS = 0;
var addStopwatch = function(){
  stopWatchSS++;
  if(stopWatchSS == 10){
    stopWatchS++;
    stopWatchSS = 0;
    if(stopWatchS == 60){
      stopWatchM++;
      stopWatchS = 0;
      if(stopWatchM == 60){
        stopWatchH++;
        stopWatchM = 0;
      }
    }
  }
}
var setStopwatch = function(){
  stopwatchH = 0;
  stopwatchM = 0;
  stopwatchS = 0;
  stopwatchSS = 0;
  setInterval(addStopwatch, 100);
}
var setCookie = function(name, value) {
  var date = new Date();
  date.setTime(date.getTime() + 12345678987654321);
  document.cookie = name + '=' + value + ';expires=' + date.toUTCString() + ';path=/';
};
var getCookie = function(name) {
  var value = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  return value? value[2] : null;
};
// road variable
// 2^0: right (+x)
// 2^1: down (-y)
// 2^2: left (-x)
// 2^3: up (+y)
var levelCount = 1;
var levelData = new Array(levelCount);
levelData[0] = {
  x: 4,
  y: 4,
  startX: 0,
  startY: 0,
  finishX: 1,
  finishY: 0,
  road: [
    [8, 3, 5, 12],
    [11, 4, 1, 14],
    [10, 8, 1, 6],
    [2, 3, 5, 4]
  ],
  switch_road: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [[{dir:1, num:2}], [{dir:4, num:2}],0,0]
  ],
  switch_num: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [2, 0, 0, 0],
    [0, 0, 0, 0]
  ],
  lever_road: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, [{dir:8, num:2}], 0],
    [0, 0, [{dir:2, num:2}], 0]
  ],
  lever: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 2]
  ],
  lever_point: [
    0, 0, 3
  ],
  switch_open: [
    0, 0, 0
  ],
  lever_open: [
    0, 0, 0
  ]
};
var levelClear = function(level){
  ctx.fillStyle = "white";
  ctx.fillText("레벨 " + level + " 클리어!", 240, 200);
  ctx.fillText("걸린 시간: " + stopWatchH + "시간 " + stopWatchM + "분 " + stopWatchS + "." + stopWatchSS + "초", 240, 240);
  ctx.fillText("다음 레벨로 넘어가시려면 [Space]를 누르세요.", 240, 280);
  setCookie("level", level+1);
  waitToPlayLevel(level+1);
}
var displayRoom = function(level, x, y){
  ctx.fillStyle = "black";
  ctx.fillRect(0, 0, 500, 500);
 
  console.log("x: " + x + ", y: " + y + ", road: " + levelData[level].road[y][x]);
 
  if(x == levelData[level].finishX && y == levelData[level].finishY){
    levelClear(level);
    return;
  }
 
  var road = levelData[level].road[y][x];
  var switch_road = levelData[level].switch_road[y][x];
  var switch_num = levelData[level].switch_num[y][x];
  var lever_road = levelData[level].lever_road[y][x];
  var lever = levelData[level].lever[y][x];
 
  var roadRight = (road & 1);
  var roadDown = (road & 2) / 2;
  var roadLeft = (road & 4) / 4;
  var roadUp = (road & 8) / 8;
 
  for(var i=0; i<switch_road.length; i++){
    var t_sdir = switch_road[i].dir;
    if(t_sdir & 1) roadRight = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
    if(t_sdir & 2) roadDown = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
    if(t_sdir & 4) roadLeft = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
    if(t_sdir & 8) roadUp = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
  }
 
  for(var i=0; i<lever_road.length; i++){
    var t_ldir = lever_road[i].dir;
    if(t_ldir & 1) roadRight = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
    if(t_ldir & 2) roadDown = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
    if(t_ldir & 4) roadLeft = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
    if(t_ldir & 8) roadUp = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
  }
 
  ctx.fillStyle = "white";
  ctx.fillRect(120, 120, 240, 240);
  if(roadRight){
    ctx.fillStyle = roadRight == 1 ? "white" : roadRight == 2 ? "blue" : "purple";
    ctx.fillRect(360, 180, 120, 120);
  }
  if(roadDown){
    ctx.fillStyle = roadDown == 1 ? "white" : roadDown == 2 ? "blue" : "purple";
    ctx.fillRect(180, 360, 120, 120);
  }
  if(roadLeft){
    ctx.fillStyle = roadLeft == 1 ? "white" : roadLeft == 2 ? "blue" : "purple";
    ctx.fillRect(0, 180, 120, 120);
  }
  if(roadUp){
    ctx.fillStyle = roadUp == 1 ? "white" : roadUp == 2 ? "blue" : "purple";
    ctx.fillRect(180, 0, 120, 120);
  }
 
  if(roadRight == 1) road |= 1;
  if(roadDown == 1) road |= 2;
  if(roadLeft == 1) road |= 4;
  if(roadUp == 1) road |= 8;
 
  setTimeout(waitForKey, 200, level, x, y, road);
 
  if(levelData[level].switch_num[y][x]){
    ctx.fillStyle = "blue";
    if(levelData[level].switch_open[levelData[level].switch_num[y][x]]){
      ctx.fillRect(130, 130, 50, 220);
    }
    else{
      ctx.fillRect(140, 140, 30, 200);
    }
  }
  if(levelData[level].lever[y][x]){
    ctx.fillStyle = "purple";
    if(levelData[level].lever_open[levelData[level].lever[y][x]]){
      ctx.fillRect(300, 130, 50, 220);
    }
    else{
      ctx.fillRect(310, 140, 30, 200);
    }
  }
}
var counter = 0;
var waitForKey = function(level, x, y, road){
  counter++;
  var canPressSpace;
  if(levelData[level].switch_num[y][x] == 0 && levelData[level].lever[y][x] == 0){
    canPressSpace = 0;
  }
  else{
    canPressSpace = 1;
  }
 
  if((!keyPress[32] || !canPressSpace) &&
    (!keyPress[37] || x==0 || !(road&4)) &&
    (!keyPress[38] || y==levelData[level].y-1 || !(road&8)) &&
    (!keyPress[39] || x==levelData[level].x-1 || !(road&1)) &&
    (!keyPress[40] || y==0 || !(road&2))){
    setTimeout(waitForKey, 30, level, x, y, road);
    counter--;
    return;
  }
  else{
    if(keyPress[32] && canPressSpace){
      console.log("pressed space");
      keyPress[32] = 0;
      if(levelData[level].switch_num[y][x] > 0){
        levelData[level].switch_open[levelData[level].switch_num[y][x]] = 1 - levelData[level].switch_open[levelData[level].switch_num[y][x]];
        displayRoom(level, x, y);
        return;
      }
      if(levelData[level].lever[y][x] > 0){
        levelData[level].lever_open[levelData[level].lever[y][x]] = levelData[level].lever_point[levelData[level].lever[y][x]];
        displayRoom(level, x, y);
        return;
      }
      counter--;
      return;
    }
    else{
      for(var i=0; i<levelData[level].lever_open.length; i++){
        levelData[level].lever_open[i]--;
        if(levelData[level].lever_open[i] < 0) levelData[level].lever_open[i] = 0;
      }
      if(keyPress[37] && x!=0 && road&4){
        console.log("pressed left");
        displayRoom(level, x-1, y);
        keyPress[37] = 0;
        counter--;
        return;
      }
      else if(keyPress[38] && y!=levelData[level].y-1 && road&8){
        console.log("pressed up");
        displayRoom(level, x, y+1);
        keyPress[38] = 0;
        counter--;
        return;
      }
      else if(keyPress[39] && x!=levelData[level].x-1 && road&1){
        console.log("pressed down");
        displayRoom(level, x+1, y);
        keyPress[39] = 0;
        counter--;
        return;
      }
      else if(keyPress[40] && y!=0 && road&2){
        console.log("pressed right");
        displayRoom(level, x, y-1);
        keyPress[40] = 0;
        counter--;
        return;
      }
    }
  }
}
var playLevel = function(level){
  var thisLevelData = levelData[level];
  setStopwatch();
  addStopwatch();
  console.log(thisLevelData);
  displayRoom(level, thisLevelData.startX, thisLevelData.startY);
}
var main = function(){
  ctx.font = "15px Dotum";
  ctx.textAlign = "center";
  ctx.fillStyle = "white";
  var level = getCookie("level");
  if(level != null){
    ctx.fillText("저장된 파일이 있습니다. 이어 플레이하시겠습니까?", 240, 200);
    ctx.fillText("예: [Space], 아니오: [↑]", 240, 240);
    waitToPlayLevel(level, 1);
  }
  else{
  level = 0;
    ctx.fillText("스위치 미로", 240, 120);
    ctx.fillText("Bd3076", 240, 150);
    ctx.fillText("움직이려면 [방향키]를 누르세요.", 240, 240);
    ctx.fillText("스위치나 레버를 누르려면 [Space]를 누르세요.", 240, 270);
    ctx.fillText("플레이하려면 [Space]를 누르세요.", 240, 360);
  }
  waitToPlayLevel(level, 0);
}
var waitToPlayLevel = function(level, mode){
  if(keyPress[32]){
    if(levelCount <= level){
      lastLevel();
      return;
    }
    playLevel(level);
  }
  else if(mode==1 && keyPress[38]) playLevel(0);
  else setTimeout(waitToPlayLevel, 100, level, mode);
}
var lastLevel = function(){
  ctx.fillStyle = "white";
  ctx.fillText("죄송합니다. 더 이상의 레벨이 준비되어 있지 않습니다.", 240, 300);
  ctx.fillText("빠른 시일 내에 새로운 레벨을 준비하겠습니다. 감사합니다.", 240, 340);
}
main();
// 여기까지 코드 입력 //
  }
}
$( plugin_switched_maze );
/* switched_maze 끝 */

2018년 10월 25일 (목) 19:43 판

/** 플러그인 switched_maze***************************
* 스위치 미로 게임
* 버전 => 1.0
* 작성자 : [[사용자:Bd3076|Bd3076]] 
* JSON => switched_maze = {"name":"switched_maze","descript":"스위치 미로 게임","version":"1.0","local":true,"creat":"Bd3076","state":"스위치 미로/plugin","executable":true}; 
*/ 
function plugin_switched_maze(){
  if($("[data-name='switched_maze']").length >= 1){
		  // 이부분에 코드 입력 //


var keyPress = new Array(128);

document.onkeydown = function(e){
  e = e || window.event;
  keyPress[e.keyCode] = 1;
}

document.onkeyup = function(e){
  e = e || window.event;
  keyPress[e.keyCode] = 0;
}

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

ctx.fillStyle = "black";
ctx.fillRect(0, 0, 500, 500);

var stopWatchH = 0;
var stopWatchM = 0;
var stopWatchS = 0;
var stopWatchSS = 0;

var addStopwatch = function(){
  stopWatchSS++;
  if(stopWatchSS == 10){
    stopWatchS++;
    stopWatchSS = 0;
    if(stopWatchS == 60){
      stopWatchM++;
      stopWatchS = 0;
      if(stopWatchM == 60){
        stopWatchH++;
        stopWatchM = 0;
      }
    }
  }
}

var setStopwatch = function(){
  stopwatchH = 0;
  stopwatchM = 0;
  stopwatchS = 0;
  stopwatchSS = 0;
  setInterval(addStopwatch, 100);
}

var setCookie = function(name, value) {
  var date = new Date();
  date.setTime(date.getTime() + 12345678987654321);
  document.cookie = name + '=' + value + ';expires=' + date.toUTCString() + ';path=/';
};

var getCookie = function(name) {
  var value = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  return value? value[2] : null;
};

// road variable
// 2^0: right (+x)
// 2^1: down (-y)
// 2^2: left (-x)
// 2^3: up (+y)

var levelCount = 1;

var levelData = new Array(levelCount);
levelData[0] = {
  x: 4,
  y: 4,
  startX: 0,
  startY: 0,
  finishX: 1,
  finishY: 0,
  road: [
    [8, 3, 5, 12],
    [11, 4, 1, 14],
    [10, 8, 1, 6],
    [2, 3, 5, 4]
  ],
  switch_road: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [[{dir:1, num:2}], [{dir:4, num:2}],0,0]
  ],
  switch_num: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [2, 0, 0, 0],
    [0, 0, 0, 0]
  ],
  lever_road: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, [{dir:8, num:2}], 0],
    [0, 0, [{dir:2, num:2}], 0]
  ],
  lever: [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 2]
  ],
  lever_point: [
    0, 0, 3
  ],
  switch_open: [
    0, 0, 0
  ],
  lever_open: [
    0, 0, 0
  ]
};

var levelClear = function(level){
  ctx.fillStyle = "white";
  ctx.fillText("레벨 " + level + " 클리어!", 240, 200);
  ctx.fillText("걸린 시간: " + stopWatchH + "시간 " + stopWatchM + "분 " + stopWatchS + "." + stopWatchSS + "초", 240, 240);
  ctx.fillText("다음 레벨로 넘어가시려면 [Space]를 누르세요.", 240, 280);
  setCookie("level", level+1);
  waitToPlayLevel(level+1);
}

var displayRoom = function(level, x, y){
  ctx.fillStyle = "black";
  ctx.fillRect(0, 0, 500, 500);
  
  console.log("x: " + x + ", y: " + y + ", road: " + levelData[level].road[y][x]);
  
  if(x == levelData[level].finishX && y == levelData[level].finishY){
    levelClear(level);
    return;
  }
  
  var road = levelData[level].road[y][x];
  var switch_road = levelData[level].switch_road[y][x];
  var switch_num = levelData[level].switch_num[y][x];
  var lever_road = levelData[level].lever_road[y][x];
  var lever = levelData[level].lever[y][x];
  
  var roadRight = (road & 1);
  var roadDown = (road & 2) / 2;
  var roadLeft = (road & 4) / 4;
  var roadUp = (road & 8) / 8;
  
  for(var i=0; i<switch_road.length; i++){
    var t_sdir = switch_road[i].dir;
    if(t_sdir & 1) roadRight = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
    if(t_sdir & 2) roadDown = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
    if(t_sdir & 4) roadLeft = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
    if(t_sdir & 8) roadUp = levelData[level].switch_open[switch_road[i].num] ? 1 : 2;
  }
  
  for(var i=0; i<lever_road.length; i++){
    var t_ldir = lever_road[i].dir;
    if(t_ldir & 1) roadRight = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
    if(t_ldir & 2) roadDown = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
    if(t_ldir & 4) roadLeft = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
    if(t_ldir & 8) roadUp = levelData[level].lever_open[lever_road[i].num] ? 1 : 3;
  }
  
  ctx.fillStyle = "white";
  ctx.fillRect(120, 120, 240, 240);
  if(roadRight){
    ctx.fillStyle = roadRight == 1 ? "white" : roadRight == 2 ? "blue" : "purple";
    ctx.fillRect(360, 180, 120, 120);
  }
  if(roadDown){
    ctx.fillStyle = roadDown == 1 ? "white" : roadDown == 2 ? "blue" : "purple";
    ctx.fillRect(180, 360, 120, 120);
  }
  if(roadLeft){
    ctx.fillStyle = roadLeft == 1 ? "white" : roadLeft == 2 ? "blue" : "purple";
    ctx.fillRect(0, 180, 120, 120);
  }
  if(roadUp){
    ctx.fillStyle = roadUp == 1 ? "white" : roadUp == 2 ? "blue" : "purple";
    ctx.fillRect(180, 0, 120, 120);
  }
  
  if(roadRight == 1) road |= 1;
  if(roadDown == 1) road |= 2;
  if(roadLeft == 1) road |= 4;
  if(roadUp == 1) road |= 8;
  
  setTimeout(waitForKey, 200, level, x, y, road);
  
  if(levelData[level].switch_num[y][x]){
    ctx.fillStyle = "blue";
    if(levelData[level].switch_open[levelData[level].switch_num[y][x]]){
      ctx.fillRect(130, 130, 50, 220);
    }
    else{
      ctx.fillRect(140, 140, 30, 200);
    }
  }
  if(levelData[level].lever[y][x]){
    ctx.fillStyle = "purple";
    if(levelData[level].lever_open[levelData[level].lever[y][x]]){
      ctx.fillRect(300, 130, 50, 220);
    }
    else{
      ctx.fillRect(310, 140, 30, 200);
    }
  }
}

var counter = 0;

var waitForKey = function(level, x, y, road){
  counter++;
  var canPressSpace;
  if(levelData[level].switch_num[y][x] == 0 && levelData[level].lever[y][x] == 0){
    canPressSpace = 0;
  }
  else{
    canPressSpace = 1;
  }
  
  if((!keyPress[32] || !canPressSpace) &&
     (!keyPress[37] || x==0 || !(road&4)) &&
     (!keyPress[38] || y==levelData[level].y-1 || !(road&8)) &&
     (!keyPress[39] || x==levelData[level].x-1 || !(road&1)) &&
     (!keyPress[40] || y==0 || !(road&2))){
    setTimeout(waitForKey, 30, level, x, y, road);
    counter--;
    return;
  }
  else{
    if(keyPress[32] && canPressSpace){
      console.log("pressed space");
      keyPress[32] = 0;
      if(levelData[level].switch_num[y][x] > 0){
        levelData[level].switch_open[levelData[level].switch_num[y][x]] = 1 - levelData[level].switch_open[levelData[level].switch_num[y][x]];
        displayRoom(level, x, y);
        return;
      }
      if(levelData[level].lever[y][x] > 0){
        levelData[level].lever_open[levelData[level].lever[y][x]] = levelData[level].lever_point[levelData[level].lever[y][x]];
        displayRoom(level, x, y);
        return;
      }
      counter--;
      return;
    }
    else{
      for(var i=0; i<levelData[level].lever_open.length; i++){
        levelData[level].lever_open[i]--;
        if(levelData[level].lever_open[i] < 0) levelData[level].lever_open[i] = 0;
      }
      if(keyPress[37] && x!=0 && road&4){
        console.log("pressed left");
        displayRoom(level, x-1, y);
        keyPress[37] = 0;
        counter--;
        return;
      }
      else if(keyPress[38] && y!=levelData[level].y-1 && road&8){
        console.log("pressed up");
        displayRoom(level, x, y+1);
        keyPress[38] = 0;
        counter--;
        return;
      }
      else if(keyPress[39] && x!=levelData[level].x-1 && road&1){
        console.log("pressed down");
        displayRoom(level, x+1, y);
        keyPress[39] = 0;
        counter--;
        return;
      }
      else if(keyPress[40] && y!=0 && road&2){
        console.log("pressed right");
        displayRoom(level, x, y-1);
        keyPress[40] = 0;
        counter--;
        return;
      }
    }
  }
}

var playLevel = function(level){
  var thisLevelData = levelData[level];
  setStopwatch();
  addStopwatch();
  console.log(thisLevelData);
  displayRoom(level, thisLevelData.startX, thisLevelData.startY);
}

var main = function(){
  ctx.font = "15px Dotum";
  ctx.textAlign = "center";
  ctx.fillStyle = "white";
  var level = getCookie("level");
  if(level != null){
    ctx.fillText("저장된 파일이 있습니다. 이어 플레이하시겠습니까?", 240, 200);
    ctx.fillText("예: [Space], 아니오: [↑]", 240, 240);
    waitToPlayLevel(level, 1);
  }
  else{
  	level = 0;
    ctx.fillText("스위치 미로", 240, 120);
    ctx.fillText("Bd3076", 240, 150);
    ctx.fillText("움직이려면 [방향키]를 누르세요.", 240, 240);
    ctx.fillText("스위치나 레버를 누르려면 [Space]를 누르세요.", 240, 270);
    ctx.fillText("플레이하려면 [Space]를 누르세요.", 240, 360);
  }
  waitToPlayLevel(level, 0);
}

var waitToPlayLevel = function(level, mode){
  if(keyPress[32]){
    if(levelCount <= level){
      lastLevel();
      return;
    }
    playLevel(level);
  }
  else if(mode==1 && keyPress[38]) playLevel(0);
  else setTimeout(waitToPlayLevel, 100, level, mode);
}

var lastLevel = function(){
  ctx.fillStyle = "white";
  ctx.fillText("죄송합니다. 더 이상의 레벨이 준비되어 있지 않습니다.", 240, 300);
  ctx.fillText("빠른 시일 내에 새로운 레벨을 준비하겠습니다. 감사합니다.", 240, 340);
}

main();



 // 여기까지 코드 입력 //

		
  }

}
$( plugin_switched_maze );
/* switched_maze 끝 */