<html>
<head>
<style>
*{
	margin: 0;
	padding: 0;
	box-sizing: border-box;
	background-color: rgb(50,50,50);
}
::-webkit-scrollbar {
	width: 0px;  /* Remove scrollbar space */
	background: transparent;  /* Optional: just make scrollbar invisible */
}
img{
	display: none;
	background: transparent;
}
#Gscreen{
	border: 1px solid black;
}
.Gscreen-container{
	display: flex;
	justify-content: center;
}
a{
	position: absolute;
	top: 10px;
	left: 10px;
	text-decoration: none;
	color: white;
}
</style>
<img id="img_button" src="assets/button.png">
<img id="img_button_back" src="assets/button-back.png">
<img id="img_button_back_white" src="assets/button-back-white.png">
<img id="img_button_back_red" src="assets/button-back-red.png">
<img id="img_panel" src="assets/panel.png">
<img id="img_flever" src="assets/lever.png">
<img id="img_flever_left" src="assets/lever-left.png">
<img id="img_flever_right" src="assets/lever-right.png">
<img id="img_flever_forward" src="assets/lever-forward.png">
<img id="img_flever_back" src="assets/lever-back.png">
<img id="img_flever_mobile" src="assets/lever-mobile-buttons.png">
<img id="img_text_panel" src="assets/text-panel.png">
<img id="img_arrow_left" src="assets/arrow-left.png">
<img id="img_arrow_right" src="assets/arrow-right.png">
<img id="img_switch" src="assets/switch.png">
<img id="img_switch_up" src="assets/switch-up.png">
<img id="img_switch_down" src="assets/switch-down.png">
<img id="img_throttle_base" src="assets/throttle-lever-base.png">
<img id="img_throttle_handle" src="assets/throttle-lever-handle.png">
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div class="Gscreen-container"><canvas id="Gscreen" width=1000px height=600px></canvas></div>
<a href="../index.html">go home</a>

<script src="classes.js"></script>
<script language="javascript" type="text/javascript">
let scrn=document.getElementById('Gscreen');
let ctx=scrn.getContext('2d');
function locked_game_loop_wrap(loop){
	
//settings & defaults
setwidth=1000;
setheight=600;
let currfuel=100;
let currplanet=[0,0,0];
let currcords=[0,0];
let destcords=[[0,0,0],[0,0]]
panelnum=2;
let tardis_move_status="idle";
let flightloop_isrunning=false;

//scanner class, had to define here because of scope
class TelepathicScanner{
	constructor(x,y,scale){
		this.width=scale;
		this.height=scale;
		this.pos={
			x: setwidth/2-this.width/2+x,
			y: setheight/2-this.height/2+y
		};
	}
	draw(){
		if(power.ison==true){
			this.text=scannertext().split("BR")
			ctx.font = "20px Ariel";
			ctx.fillStyle = "#000"
			ctx.globalAlpha = 0.5;
			ctx.fillRect(this.pos.x,this.pos.y,this.width-30,this.height-13)
			ctx.fillStyle = "#fff";
			ctx.globalAlpha = 1;
			for(var i=0; i<this.text.length; i++){
				ctx.fillText(this.text[i],this.pos.x+2,this.pos.y+this.height/this.text.length*(i+1),this.width/1.21);
			}
		}
	}
}

let planetdata={};
//error cheak and auto sets
$.getJSON("planetdata.json", function(result){
	planetdata=result;
})
.error(function(){ 
	if(alert('error loading planet data')){
		location.reload();
	} else {
		location.reload();
	}
});

//side loops
function moveloop(){
	tardis_move_status="flying";
	insideloop();
	idlefuelloop();
	function insideloop(){
		if(fmbutton.ison==true){
			if(flever.state=="#img_flever_forward"){
				currcords[1]++
			}
			if(flever.state=="#img_flever_back"){
				currcords[1]--
			}
			if(flever.state=="#img_flever_left"){
				currcords[0]--
			}
			if(flever.state=="#img_flever_right"){
				currcords[0]++
			}
			setTimeout(insideloop,500)
		} else {
			if(takeoffbutton.ison==false){
				tardis_move_status="idle";
			}
		}
	}
	function idlefuelloop(){
		if(fmbutton.ison==true){
			currfuel--
			setTimeout(idlefuelloop,3000)
		}
	}
}
function flightloop(){
	if(flightloop_isrunning==false){
		flightloop_isrunning=true;
		fmbutton.ison=false;
		fmbutton.locked=true;
		takeoffbutton.locked=true;
		tardis_move_status="taking off";
		setTimeout(function(){
			tardis_move_status="in vortex";
			takeoffbutton.locked=false;
			currplanet=destcords[0];
			currcords=destcords[1];
			flightloop_idlefuelloop();
			flightloop_waitforoff();
		},3000)
		function flightloop_idlefuelloop(){
			if(takeoffbutton.ison==true){
				currfuel--;
				setTimeout(flightloop_idlefuelloop,15000);
			}
		}
		function flightloop_waitforoff(){
			if(takeoffbutton.ison==false){
				takeoffbutton.locked=true;
				tardis_move_status="landing";
				currplanet=destcords[0];
				currcords=destcords[1];
				setTimeout(function(){
					tardis_move_status="idle";
					takeoffbutton.locked=false;
					fmbutton.locked=false;
					flightloop_isrunning=false;
				},3000);
			} else {
				setTimeout(flightloop_waitforoff,500);
			}
		}
	}
}
function textentryT_runcommands(){
	if(textEntryButton.ison==false){
		let text=textEntryT.text.toString().split(" ")
		if(text[0]=="setdest:"){
			destcords=[[parseInt(text[1]),parseInt(text[2]),parseInt(text[3])],[parseInt(text[4]),parseInt(text[5])]];
		}
		textEntryT.text="";
		textEntryButton.ison=false;
	} else {
		if(document.getElementById("entry_box_mobile").value!==""){
			textEntryT.text=document.getElementById("entry_box_mobile").value;
			document.getElementById("entry_box_mobile").value="";
		}
	}
}

/*
	button syntax
	panel, x, y, scale, back when on, off when no power, red when off, red when no power, name, desc
	switch syntax
	panel, x, y, scale, off when no power, 3 stage button?
*/
let arrowleft = new Arrowbutton(1,setheight/2-70/2,70,70);
let arrowright = new Arrowbutton(1000-70-1,setheight/2-70/2,70,70);
let panel = new Panel(0,150,1000,450);
telepathicScanner = new TelepathicScanner(10,-150,200);
//panel 2 --space
power = new Switch(2,180,200,70,false,false,"power","enables the power....");
let fmbutton = new Button(2,240,200,50,true,true,false,true,"flightmode button","enables flightmode for moving");
let flever = new Flever(2,320,100);
let flightTpanel = new Textpanel(2,0,0,190,80);
//panel 3 --flight
let throttlelever = new Throttlelever(3,290,140,50);
let telepathicCmain = new Switch(3,-180,-40,50,true,false,"telepathic main","enables main telepath functions");
let telepathicCscanner = new Switch(3,-140,-40,50,true,false,"telepathic scanner","enables telepathic scanner");
telepathicChover = new Switch(3,-100,-40,50,true,false,"telepathic hover","what you are reading right now");
let takeoffbutton= new Button(3,220,220,50,true,true,false,true,"takeoffbutton","for teleporting");
let flightTpanel2 = new Textpanel(3,0,0,120,130);
let textEntryT = new Textpanel(3,-240,220,100,30);
let textEntryButton = new Button(3,-160,220,30,true,true,false,true,"text enable","allows you to use commands (keyboard)")
let text_status = new Textpanel(3,135,-30,140,45);



window.onload = function() {

	//gameloop
	function gameloop(){
		if(currfuel<0)currfuel=0;
		if(currfuel>100)currfuel=100;
		if(currfuel<1){
			fmbutton.ison=false;
		}
		//cord updater
		for(var planet in planetdata){
			planetcords=planetdata[planet].cords.split("/")
			planetcords=[parseInt(planetcords[0]),parseInt(planetcords[1]),parseInt(planetcords[2])]
			if(currplanet[0]==planetcords[0]&&currplanet[1]==planetcords[1]&&currplanet[2]==planetcords[2]){
				//get and set based limits
				if(currcords[0]>parseInt(planetdata[planet].scale)){currcords[0]=-parseInt(planetdata[planet].scale)}
				if(currcords[1]>parseInt(planetdata[planet].scale)){currcords[1]=-parseInt(planetdata[planet].scale)}
				if(currcords[0]<-parseInt(planetdata[planet].scale)){currcords[0]=parseInt(planetdata[planet].scale)}
				if(currcords[1]<-parseInt(planetdata[planet].scale)){currcords[1]=parseInt(planetdata[planet].scale)}
				
				//asd
				for(var objects in planetdata[planet].objects){
					for(var ListOfObjects in planetdata[planet].objects[objects]){
						LOOcords=planetdata[planet].objects[objects][ListOfObjects].split("/")
						LOOcords=[parseInt(LOOcords[0]),parseInt(LOOcords[1])]
						if(currcords[0]==LOOcords[0]&&currcords[1]==LOOcords[1]){
						}
					}
				}
			}
		}
		
		//main drawing
		ctx.clearRect(0,0,setwidth,setheight);
		panel.draw();
		arrowleft.draw("#img_arrow_left");
		arrowright.draw("#img_arrow_right");
		//panel 2
		flightTpanel.text="planet x:"+currplanet[0]+" y:"+currplanet[1]+" z:"+currplanet[2]+"BRx:"+currcords[0]+" y:"+currcords[1]+"BRfuel: "+currfuel;
		flightTpanel.draw();
		power.draw();
		fmbutton.draw();
		flever.draw();
		//panel3
		throttlelever.draw();
		telepathicCmain.draw();
		telepathicCscanner.draw();
		telepathicChover.draw();
		takeoffbutton.draw();
		flightTpanel2.text="Current-"+
		"BRspace: "+currplanet[0]+" "+currplanet[1]+" "+currplanet[2]+"BRplanet: "+currcords[0]+" "+currcords[1]+
		"BRDestination-"+
		"BRspace: "+destcords[0][0]+" "+destcords[0][1]+" "+destcords[0][2]+"BRplanet: "+destcords[1][0]+" "+destcords[1][1];
		flightTpanel2.draw();
		textEntryButton.draw();
		textEntryT.draw();
		text_status.draw();
		text_status.text="status: "+tardis_move_status;
		
		if(telepathicCmain.ison==false){
			telepathicCscanner.stage=2;
			telepathicChover.stage=2;
		}
		if(telepathicCscanner.ison==true){
			telepathicScanner.draw();
		}
		//draw hovers
		fmbutton.drawhover();
		power.drawhover();
		flever.drawhover();
		telepathicCmain.drawhover();
		telepathicCscanner.drawhover();
		telepathicChover.drawhover();
		takeoffbutton.drawhover();
		textEntryButton.drawhover();
		requestAnimationFrame(gameloop);
	}
	gameloop();
	
	
	
	//!---mouse and button
	scrn.addEventListener('mousedown',function(event){
		var cx = event.pageX - $('#Gscreen').offset().left;
		var cy = event.pageY - $('#Gscreen').offset().top;
		arrowleft.click(cx,cy,"down");
		arrowright.click(cx,cy,"up");
		//panel 2
		fmbutton.toggle(cx,cy,true);
		if(fmbutton.run_function==true){moveloop();fmbutton.run_function=false;}
		power.toggle(cx,cy,false);
		flever.toggle(cx,cy);
		if(fmbutton.ison==false){flever.mobilebuttons=false}
		//panel3
		throttlelever.mouseon(cx,cy);
		telepathicCmain.toggle(cx,cy);
		telepathicCscanner.toggle(cx,cy);
		telepathicChover.toggle(cx,cy);
		takeoffbutton.toggle(cx,cy,true);
		if(takeoffbutton.run_function==true){flightloop();takeoffbutton.run_function=false;}
		textEntryButton.toggle(cx,cy,true);
		if(textEntryButton.run_function==true){textentryT_runcommands();textEntryButton.run_function=false;}
	},false);
	scrn.addEventListener('mouseup',function(event){
		throttlelever.mouseisdown=false;
	},false);
	scrn.addEventListener('mousemove',function(event){
		var cx = event.pageX - $('#Gscreen').offset().left;
		var cy = event.pageY - $('#Gscreen').offset().top;
		globalcx = event.pageX - $('#Gscreen').offset().left;
		globalcy = event.pageY - $('#Gscreen').offset().top;
		//panel 3
		throttlelever.move(cx,cy);
		fmbutton.move(cx,cy);
		power.move(cx,cy);
		flever.move(cx,cy);
		telepathicCmain.move(cx,cy);
		telepathicCscanner.move(cx,cy);
		telepathicChover.move(cx,cy);
		takeoffbutton.move(cx,cy);
		textEntryButton.move(cx,cy);
	},false);
	document.addEventListener('keydown',function(event){
		if(textEntryButton.ison==true){
			if(event.keyCode==13){
				textEntryButton.ison=false;
				textentryT_runcommands();
			} else if(event.keyCode==16){
			
			} else if(event.keyCode==8){
				textEntryT.text=textEntryT.text.toString().substring(0,textEntryT.text.toString().length-1);
			} else if(event.keyCode==32){
				textEntryT.text=textEntryT.text+" ";
			} else {
				textEntryT.text=textEntryT.text+event.key;
			}
		}
		if(fmbutton.ison==true){
			if(panelnum==2){
				if(event.keyCode==38){
					flever.state="#img_flever_forward";
				}
				if(event.keyCode==37){
					flever.state="#img_flever_left";
				}
				if(event.keyCode==39){
					flever.state="#img_flever_right";
				}
				if(event.keyCode==40){
					flever.state="#img_flever_back";
				}
				if(event.keyCode==70){
					alert('respects payed');
				}
			}
		}
	},false);
	document.addEventListener('keyup',function(event){
		flever.state="#img_flever";
	},false);
}

//scanner text function
function scannertext(){
	text="";
	for(var planet in planetdata){
		planetcords=planetdata[planet].cords.split("/")
		planetcords=[parseInt(planetcords[0]),parseInt(planetcords[1]),parseInt(planetcords[2])]
		if(currplanet[0]==planetcords[0]&&currplanet[1]==planetcords[1]&&currplanet[2]==planetcords[2]&&(tardis_move_status=="idle"|tardis_move_status=="flying")){
			getcords=[]
			for(var objects in planetdata[planet].objects){
				for(var ListOfObjects in planetdata[planet].objects[objects]){
					LOOcords=planetdata[planet].objects[objects][ListOfObjects].split("/")
					LOOcords=[parseInt(LOOcords[0]),parseInt(LOOcords[1])]
					if(objects=="refuel"){
						getcords.push([LOOcords[0],LOOcords[1],"木"])
					}
					if(objects=="space-radio"){
						getcords.push([LOOcords[0],LOOcords[1],"♖"])
					}
				}
			}
			for(var y=(currcords[1]+4);y>=(currcords[1]-4);y--){
				y2=y
				if(y2>parseInt(planetdata[planet].scale)){y2=-((parseInt(planetdata[planet].scale)*2+1)-y)}
				if(y2<-parseInt(planetdata[planet].scale)){y2=((parseInt(planetdata[planet].scale)*2+1)+y)}
				for(var x=(currcords[0]-4);x<=(currcords[0]+4);x++){
					x2=x
					if(x2>parseInt(planetdata[planet].scale)){x2=-((parseInt(planetdata[planet].scale)*2+1)-x)}
					if(x2<-parseInt(planetdata[planet].scale)){x2=((parseInt(planetdata[planet].scale)*2+1)+x)}
					for(i=0;i<getcords.length;i++){
						if(x2==getcords[i][0]&&y2==getcords[i][1]){
							texticon=getcords[i][2]
							break;
						} else if(x2==currcords[0]&&y2==currcords[1]){
							texticon="U"
						} else {
							texticon="+"
						}
					}
					text=text+"["+texticon+"]"
				}
				text=text+"BR"
			}
		}
	}
	return text;
}

}
locked_game_loop_wrap();
</script>

<br><br>
<div style="color: white;" align="center">
	<textarea id="entry_box_mobile" style="color: white; padding: 4px;"></textarea>
	<p>the box below allows mobile users to enter text to things one the game without having to 
	plug in a keyboard</p>
</div>
</body>
</html>
