1 line
6.5 KiB
JavaScript
1 line
6.5 KiB
JavaScript
function main(){this.dom={body:document.body,gameGroup:document.getElementById("gameGroup"),mainTips:document.getElementById("mainTips"),musicBtn:document.getElementById("musicBtn"),startPanel:document.getElementById("startPanel"),startTop:document.getElementById("startTop"),startTopProgressBar:document.getElementById("startTopProgressBar"),startTopProgress:document.getElementById("startTopProgress"),startTopLoadTips:document.getElementById("startTopLoadTips"),startBackground:document.getElementById("startBackground"),startButtonGroup:document.getElementById("startButtonGroup"),floorMsgGroup:document.getElementById("floorMsgGroup"),versionLabel:document.getElementById("versionLabel"),floorNameLabel:document.getElementById("floorNameLabel"),statusBar:document.getElementById("statusBar"),toolBar:document.getElementById("toolBar"),gameCanvas:document.getElementsByClassName("gameCanvas"),startButtons:document.getElementById("startButtons"),playGame:document.getElementById("playGame"),loadGame:document.getElementById("loadGame"),aboutGame:document.getElementById("aboutGame"),levelChooseButtons:document.getElementById("levelChooseButtons"),easyLevel:document.getElementById("easyLevel"),normalLevel:document.getElementById("normalLevel"),hardLevel:document.getElementById("hardLevel"),data:document.getElementById("data"),statusLabels:document.getElementsByClassName("statusLabel")};this.loadList=["items","icons","maps","enemys","events","npcs","data","ui","core"];this.images=["animates","enemys","heros","items","npcs","terrains"];this.sounds={mp3:["bgm-loop","floor"],ogg:["attack","door","item"]};this.statusBar={image:{floor:document.getElementById("img-floor"),hp:document.getElementById("img-hp"),atk:document.getElementById("img-atk"),def:document.getElementById("img-def"),money:document.getElementById("img-money"),experience:document.getElementById("img-experience"),book:document.getElementById("img-book"),fly:document.getElementById("img-fly"),toolbox:document.getElementById("img-toolbox"),shop:document.getElementById("img-shop"),save:document.getElementById("img-save"),load:document.getElementById("img-load"),settings:document.getElementById("img-settings")},floor:document.getElementById("floor"),hp:document.getElementById("hp"),atk:document.getElementById("atk"),def:document.getElementById("def"),money:document.getElementById("money"),experience:document.getElementById("experience"),yellowKey:document.getElementById("yellowKey"),blueKey:document.getElementById("blueKey"),redKey:document.getElementById("redKey"),hard:document.getElementById("hard")};this.instance={};this.canvas={}}main.prototype.init=function(){for(var a=0;a<main.dom.gameCanvas.length;a++){main.canvas[main.dom.gameCanvas[a].id]=main.dom.gameCanvas[a].getContext("2d")}main.loader(function(){var c={};for(a=0;a<main.loadList.length;a++){var b=main.loadList[a];if(b.indexOf(".min")==b.length-4){b=b.substring(0,b.length-4)}if(b==="core"){continue}main[b].init(main.dom);c[b]=main[b]}main.core.init(main.dom,main.statusBar,main.canvas,main.images,main.sounds,c);main.core.resize(main.dom.body.clientWidth,main.dom.body.clientHeight)})};main.prototype.loader=function(c){var b=0;for(var a=0;a<main.loadList.length;a++){main.loadMod(main.loadList[a],function(e){b=0;main.setMainTipsText(e+".js 加载完毕");for(var d in main.instance){b++}if(b===main.loadList.length){delete main.instance;main.dom.mainTips.style.display="none";c()}})}};main.prototype.loadMod=function(c,d){var a=document.createElement("script");var b=c;if(b.indexOf(".min")==b.length-4){b=b.substring(0,b.length-4)}a.src="libs/"+c+".js";main.dom.body.appendChild(a);a.onload=function(){main[b]=main.instance[b];d(b)}};main.prototype.setMainTipsText=function(a){main.dom.mainTips.innerHTML=a};var main=new main();main.init();window.onresize=function(){try{main.core.resize(main.dom.body.clientWidth,main.dom.body.clientHeight)}catch(a){}};main.dom.body.onkeydown=function(a){if(main.core.isPlaying()){main.core.keyDown(a)}};main.dom.body.onkeyup=function(a){try{if(main.core.isPlaying()){main.core.keyUp(a)}}catch(a){}};main.dom.body.onselectstart=function(){return false};document.onmousemove=function(){try{main.core.loadSound()}catch(a){}};document.ontouchstart=function(){try{main.core.loadSound()}catch(a){}};main.dom.data.onmousedown=function(b){try{b.stopPropagation();var c=main.core.getClickLoc(b.clientX,b.clientY);if(c==null){return}var a=parseInt(c.x/c.size),d=parseInt(c.y/c.size);main.core.ondown(a,d)}catch(b){}};main.dom.data.onmousemove=function(b){try{b.stopPropagation();var c=main.core.getClickLoc(b.clientX,b.clientY);if(c==null){return}var a=parseInt(c.x/c.size),d=parseInt(c.y/c.size);main.core.onmove(a,d)}catch(b){}};main.dom.data.onmouseup=function(){try{main.core.onup()}catch(a){}};main.dom.data.onmousewheel=function(b){try{if(b.wheelDelta){main.core.onmousewheel(Math.sign(b.wheelDelta))}else{if(b.detal){main.core.onmousewheel(Math.sign(b.detail))}}}catch(a){}};main.dom.data.ontouchstart=function(b){try{b.preventDefault();var c=main.core.getClickLoc(b.targetTouches[0].clientX,b.targetTouches[0].clientY);if(c==null){return}var a=parseInt(c.x/c.size),d=parseInt(c.y/c.size);main.core.ondown(a,d)}catch(b){}};main.dom.data.ontouchmove=function(b){try{b.preventDefault();var c=main.core.getClickLoc(b.targetTouches[0].clientX,b.targetTouches[0].clientY);if(c==null){return}var a=parseInt(c.x/c.size),d=parseInt(c.y/c.size);main.core.onmove(a,d)}catch(b){}};main.dom.data.ontouchend=function(){try{main.core.onup()}catch(a){}};main.statusBar.image.book.onclick=function(){if(main.core.isPlaying()){main.core.openBook(true)}};main.statusBar.image.fly.onclick=function(a){if(main.core.isPlaying()){main.core.useFly(true)}};main.statusBar.image.toolbox.onclick=function(a){if(main.core.isPlaying()){main.core.openToolbox(true)}};main.statusBar.image.shop.onclick=function(){if(main.core.isPlaying()){main.core.ui.drawSelectShop(true)}};main.statusBar.image.save.onclick=function(a){if(main.core.isPlaying()){main.core.save(true)}};main.statusBar.image.load.onclick=function(a){if(main.core.isPlaying()){main.core.load(true)}};main.statusBar.image.settings.onclick=function(a){if(main.core.isPlaying()){main.core.ui.drawSettings(true)}};main.dom.playGame.onclick=function(){main.dom.startButtons.style.display="none";core.events.startGame()};main.dom.loadGame.onclick=function(){main.core.load()};main.dom.aboutGame.onclick=function(){main.core.ui.drawAbout()};main.dom.easyLevel.onclick=function(){core.events.startGame("easy")};main.dom.normalLevel.onclick=function(){core.events.startGame("normal")};main.dom.hardLevel.onclick=function(){core.events.startGame("hard")}; |