Team:SYSU-Software/game/game.js
From 2013.igem.org
var stage; var display; var chain; var MAINIMAGESIZE = 0.675; var proimagesize = 100; var GRADEPHOTOSIZE = 0.5; var LEVELPHOTOSIZE = 0.45; var sTime = 30; var updateSplit = [2, 4, 7, 11, 16, 22, 29]; var decrease = [8, 12, 16, 10]; var imageSrc = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]; // var imageSrc = new Array(); var himageSrc = ["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]; // var himageSrc = new Array(); var item = new Array(); var shape, oldX, oldY, size, color, len; var alive; var valid = true; var firstError; var isend; var isMousedown; var proteins; var grade; var gradePhoto = []; var level; var split; var splitTime; var first; var rst; var isSpro; var standSTime; var enzymeNum; var instructionPage; var number;
var levelName =["Coli",
"Nematode", "Fly", "Frog", "Cat", "Pig", "Human", "Alien"];
var levelPhoto; var levelNum; var levelPhotoSrc = ["", "", "", "", "", "", "", ""]; // var levelPhotoSrc = new Array(); var levelshowSrc = [ "", "", "", "", "", "", "", "", ""];
var words = [ "", "", "", "" ];
var preload; var loadingInterval = 0;
var isMusic;
function init() {
$('.gamelayer').hide(); $('.gameover').hide(); $('.main').hide(); $('.showinstruction').hide(); stage = new createjs.Stage("Main"); display = new createjs.Stage("Display");
isMusic = true; var manifest = [ {id:"begin", src:"https://static.igem.org/mediawiki/2013/8/88/Thunder1.mp3|https://static.igem.org/mediawiki/2013/b/bb/Thunder1.ogg"}, {id:"success", src:"https://static.igem.org/mediawiki/2013/6/61/SYSU-ToneWobble.mp3|https://static.igem.org/mediawiki/2013/1/10/ToneWobble.ogg", data:6}, {id:"death", src:"https://static.igem.org/mediawiki/2013/3/30/Game-Death.mp3|https://static.igem.org/mediawiki/2013/e/e5/Game-Death.ogg"}, {id:"get", src:"https://static.igem.org/mediawiki/2013/5/52/Game-Spawn.mp3|https://static.igem.org/mediawiki/2013/1/14/Game-Spawn.ogg", data:6}, {id:"fail", src:"https://static.igem.org/mediawiki/2013/c/ce/Game-Shot.mp3|https://static.igem.org/mediawiki/2013/6/69/Game-Shot.ogg"}]; preload = new createjs.LoadQueue(); preload.installPlugin(createjs.Sound); preload.addEventListener("complete", doneLoading); // add an event listener for when load is completed preload.addEventListener("progress", updateLoading); preload.loadManifest(manifest); preload.load(imageSrc); preload.load(himageSrc); preload.load(levelPhotoshow); preload.load(levelshowSrc); createjs.Sound.play("begin"); $("#levelPhotoshow")[0].src = levelshowSrc[0]; $('.gamelayer').show('slow'); $('.showlevel').show('slow'); stage.update();
}
function firstinstruction() {
$("#words")[0].src = words[0]; instructionPage = 1; $('.gamelayer').hide(); $('.showlevel').hide(); $('.icons1').hide(); $('.icons2').show(); $('.showstart').hide(); $('.showinstruction').show();
}
function secondinstruction() {
$("#words")[0].src = words[0]; instructionPage = 1; $('.gameover').hide(); $('.showlevel').hide(); $('.icons1').hide(); $('.icons2').show(); $('.showstart').hide(); $('.showinstruction').show();
}
function previous() {
--instructionPage; if (instructionPage == 1) { $('.icons1').hide(); } else if (instructionPage == 3) { $('.icons2').show(); $('.showstart').hide(); } $("#words")[0].src = words[instructionPage - 1];
}
function next() {
++instructionPage; if (instructionPage == 4) { $('.icons2').hide(); $('.showstart').show(); } else if (instructionPage == 2) { $('.icons1').show(); } $("#words")[0].src = words[instructionPage - 1];
}
function updateLoading() {
stage.update();
}
function doneLoading(event) {
clearInterval(loadingInterval); stage.update();
}
function backgroundMusic() {
var musicImage = $("#ctrlmusic")[0]; if (preload != null) { preload.close(); } if (isMusic) { musicImage.src = ""; isMusic = false; } else { musicImage.src = ""; isMusic = true; }
}
function handleClick() {
$('.gamelayer').hide('slow'); stage.removeAllChildren(); display.removeAllChildren(); start();
}
function refresh() {
var y = Math.floor(stage.canvas.height / (proimagesize * MAINIMAGESIZE)); var x = Math.floor(stage.canvas.width / (proimagesize * MAINIMAGESIZE)); var isterminal = false, ispromoter = false, isprotein = false; if (valid) { do { stage.removeAllChildren(); for (var i = 0, positionY = 0; i < y; ++i, positionX = 0, positionY += proimagesize * MAINIMAGESIZE + 2) { item[i] = new Array(); for (var j = 0, positionX = 0; j < x; ++j) { var srcindex = Math.round(Math.random() * 5); if (srcindex == 0) { ispromoter = true; } else if (srcindex == 5) { isterminal = true; } else { isprotein = true; }
item[i][j] = new createjs.Bitmap(imageSrc[srcindex]); item[i][j].x = positionX; positionX += proimagesize * MAINIMAGESIZE + 2; item[i][j].y = positionY; item[i][j].scaleX = item[i][j].scaleY = MAINIMAGESIZE; item[i][j].index = srcindex; item[i][j].hlight = false; stage.addChild(item[i][j]); } } } while(!(isterminal && ispromoter && isprotein)) var n = Math.floor(Math.random() * 3); for (var i = 0; i < n; ++i) { var pX = Math.floor(Math.random() * x), pY = Math.floor(Math.random() * y); if (item[pY][pX].index == 0 || item[pY][pX].index > 4) { --i; continue; } item[pY][pX].image.src = imageSrc[level + 5]; item[pY][pX].index = level + 5; }
var n = Math.floor(Math.random() * 3); for (var i = 0; i < n; ++i) { var pX = Math.floor(Math.random() * x), pY = Math.floor(Math.random() * y); if (item[pY][pX].index == 0 || item[pY][pX].index > 4) { --i; continue; } item[pY][pX].index = Math.floor(Math.random() * 4) + 13; item[pY][pX].image.src = imageSrc[item[pY][pX].index]; } } else { for (var i = 0; i < y; ++i) { for (var j = 0; j < x; ++j) { if (item[i][j].hlight) { item[i][j].hlight = false; item[i][j].image.src = imageSrc[item[i][j].index]; } } } }
firstError = false; valid = true; isend = false; stage.update(); display.update();
}
function watchRestart() {
$('.gameover').hide('slow'); display.removeAllChildren(); stage.removeAllChildren(); start();
}
function start() {
$('.showlevel').hide('slow'); $('.main').show('slow'); $('.showinstruction').hide('slow'); number = 0; level = 1; levelPhoto = new createjs.Bitmap(levelPhotoSrc[level - 1]); levelPhoto.x = 50; levelPhoto.y = 30; levelPhoto.scaleX = levelPhoto.scaleY = LEVELPHOTOSIZE;
gradePhoto[0] = new createjs.Bitmap(imageSrc[1]); gradePhoto[1] = new createjs.Bitmap(imageSrc[2]); gradePhoto[2] = new createjs.Bitmap(imageSrc[3]); gradePhoto[3] = new createjs.Bitmap(imageSrc[4]); gradePhoto[4] = new createjs.Bitmap(imageSrc[level + 5]); gradePhoto[5] = new createjs.Bitmap(imageSrc[16]);
gradePhoto[0].x = gradePhoto[2].x = 10; gradePhoto[0].y = gradePhoto[1].y = 150; gradePhoto[1].x = gradePhoto[3].x = 110; gradePhoto[2].y = gradePhoto[3].y = 220; gradePhoto[4].x = 100; gradePhoto[4].y = 265; gradePhoto[5].x = 10; gradePhoto[5].y = 310;
for (var i = 0; i < 6; ++i) { gradePhoto[i].scaleX = gradePhoto[i].scaleY = GRADEPHOTOSIZE; display.addChild(gradePhoto[i]); }
alive = true; valid = true; isSpro = false; proteins = [10, 10, 10, 10]; enzymeNum = 0; splitTime = 0; split = 0; standSTime = sTime; preTime = createjs.Ticker.getTime(true); levelNum = new createjs.Text("" + level, "bold 30px Arial", "yellow"); levelNum.x = 140; levelNum.y = 120; grade = new createjs.Text("" + proteins[0] + " " + proteins[1] + "\n\n\n" + proteins[2] + " " + proteins[3] , "bold 20px Arial", "white"); grade.x = 70; grade.y = 170; rst = new createjs.Text("" + splitTime, "bold 30px Arial", "red"); rst.x = 120; rst.y = 417; sprotext = new createjs.Text("No","bold 20px Arial", "white"); sprotext.x = 155; sprotext.y = 275; meinum = new createjs.Text("0","bold 20px Arial", "white"); meinum.x = 70; meinum.y = 325; generation = new createjs.Text("0","bold 20px Arial", "white"); generation.x = 155; generation.y = 352; stage.addEventListener("stagemousedown",handlemousedown); stage.addEventListener("stagemouseup", handlemouseup); createjs.Ticker.addEventListener("tick", tick); display.addChild(levelNum); display.addChild(rst); display.addChild(grade); display.addChild(levelPhoto); display.addChild(sprotext); display.addChild(meinum); display.addChild(generation); refresh();
}
function handlemousedown(event) {
var indexX = -1, indexY = -1, errorX = -1, errorY = -1; len = 0; isMousedown = true; chain = new Array(); stage.addEventListener("stagemousemove",function(evt) { var newX = Math.floor(evt.stageX / (proimagesize * MAINIMAGESIZE + 2)), newY = Math.floor(evt.stageY / (proimagesize * MAINIMAGESIZE + 2)) if (!isend && valid && alive && isMousedown && ( newX != indexX || newY != indexY) && (newX != errorX || newY != errorY)) { if ((len > 0 && (item[newY][newX].index == 0 || item[newY][newX].hlight == true || (Math.abs(newY - indexY) + Math.abs(newX - indexX) > 1))) || (len == 0 && item[newY][newX].index != 0)) { errorX = newX; errorY = newY; if (!firstError) { firstError == true; } else { valid == false; } } else { indexX = newX; indexY = newY; item[indexY][indexX].image.src = himageSrc[item[indexY][indexX].index]; item[indexY][indexX].hlight = true; stage.update(); chain[len] = new Object(); chain[len].index = item[indexY][indexX].index; chain[len].x = indexX; chain[len].y = indexY; isend = chain[len].index == 5; ++len; if (isMusic) { createjs.Sound.play("get", createjs.Sound.INTERUPT_LATE, 0, 0.8); } } } oldX = evt.stageX; oldY = evt.stageY; });
}
function handlemouseup(event) {
var tempcount = [0, 0, 0, 0]; isMousedown = false; if (len < 3) { valid = false; } else { if (chain[len - 1].index != 5) { valid = false; } else { for (var i = 1; i < len - 1; ++i) { if (chain[i].index > 5 && chain[i].index < 13) { isSpro = true; gradePhoto[4].image.src = himageSrc[chain[i].index]; sprotext.text = "Yes"; changelevel(); } else if (chain[i].index == 13) { splitTime = 0; preTime = createjs.Ticker.getTime(true); ++split; changelevel(); standSTime = sTime; for (var j = 0; j < 4; ++j) { proteins[j] = Math.floor(proteins[j] / 2); } } else if (chain[i].index == 14) { standSTime = sTime * 2; splitTime = 0; preTime = createjs.Ticker.getTime(true); } else if (chain[i].index == 15) { if (enzymeNum != 0) { --enzymeNum; } else { if(split == 0) { alive = false; } else { --split; } } } else if (chain[i].index == 16) { ++enzymeNum; } else { ++tempcount[chain[i].index - 1]; } } } } if (alive && len != 0) { if (valid) { for (var i = 0; i < 4; ++i) { proteins[i] += tempcount[i]; } if (isMusic) { createjs.Sound.play("success"); } } else { if (isMusic) { createjs.Sound.play("fail"); } } chain = []; len = 0; levelNum.text = "" + level; grade.text = "" + proteins[0] + " " + proteins[1] + "\n\n\n" + proteins[2] + " " + proteins[3]; rst.text = "" + splitTime; meinum.text = "" + enzymeNum + "."; display.update(); } if (alive) refresh();
}
function tick(event) {
if (alive) { var oldSplitTime = splitTime; if ((((createjs.Ticker.getTime(true) - preTime) / 100) % (standSTime / 10)) == 0) { ++number; number = number % 100; } splitTime = standSTime - Math.round((createjs.Ticker.getTime(true) - preTime) / 1000) % (standSTime + 1); if (splitTime != oldSplitTime) { if (splitTime == 0) { ++split; changelevel(); standSTime = sTime; for (var i = 0; i < 4; ++i) { proteins[i] = Math.floor(proteins[i] / 2); } } if (splitTime % decrease[0] == 0) { --proteins[0]; } if (splitTime % decrease[1] == 0) { --proteins[1]; } if (splitTime % decrease[2] == 0) { --proteins[2]; } if (splitTime % decrease[3] == 0) { --proteins[3]; } for (var i = 0; i < 4; ++i) { if (proteins[i] < 1) { alive = false; break; } } rst.text = "" + splitTime; grade.text = "" + proteins[0] + " " + proteins[1] + "\n\n\n" + proteins[2] + " " + proteins[3]; }
if (!alive) { stage.removeAllChildren(); if (isMusic) { createjs.Sound.play("death"); } var levelshowimage = $("#levelPhotoshow")[0]; levelshowimage.src = levelshowSrc[level]; $('.main').hide('slow'); $('.showlevel').show('slow'); $('.gameover').show('slow'); stage.update(); } } if (number < 10) { generation.text = "" + split + ".0" + number; } else { generation.text = "" + split + "." + number; } rst.text = "" + splitTime; display.update(event);
}
function changelevel() {
var oldLevel = level; if (isSpro) { if (split < updateSplit[0]) { level = 1; } else if (split < updateSplit[1]) { level = 2; } else if (split < updateSplit[2]) { level = 3; } else if (split < updateSplit[3]) { level = 4; } else if (split < updateSplit[4]) { level = 5; } else if (split < updateSplit[5]) { level = 6; } else if (split < updateSplit[6]) { level = 7; } else { level = 8; } } if (oldLevel != level) { level = oldLevel + 1; levelPhoto.image.src = levelPhotoSrc[level - 1]; levelNum.text = "" + level; isSpro = false; if (level != 8) { gradePhoto[4].image.src = imageSrc[level + 5]; sprotext.text = "No"; } else { display.removeChild(gradePhoto[4]); display.removeChild(sprotext); } display.update(); }
}