Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created December 30, 2010 23:17
Show Gist options
  • Select an option

  • Save mizchi/760472 to your computer and use it in GitHub Desktop.

Select an option

Save mizchi/760472 to your computer and use it in GitHub Desktop.
memo
var doc = new GLGE.Document();
doc.onLoad=function(){
//context init
var gameRenderer=new GLGE.Renderer(document.getElementById('canvas'));
gameScene=new GLGE.Scene();
gameScene=doc.getElement("mainscene");
gameRenderer.setScene(gameScene);
//interface init
var mouse=new GLGE.MouseInput(document.getElementById('canvas'));
var keys=new GLGE.KeyInput();
//param
var mouseovercanvas;
// var hoverobj;
function mouselook(){
if(mouseovercanvas){
var mousepos=mouse.getMousePosition();
mousepos.x=mousepos.x-document.getElementById("container").offsetLeft;
mousepos.y=mousepos.y-document.getElementById("container").offsetTop;
var camera=gameScene.camera;
camerarot=camera.getRotation();
inc=(mousepos.y-(document.getElementById('canvas').offsetHeight/2))/500;
// var trans=camera.getRotMatrix().x([0,0,-1,1]);
var trans=GLGE.mulMat4Vec4(camera.getRotMatrix(),[0,0,-1,1]);
var mag=Math.pow(Math.pow(trans[0],2)+Math.pow(trans[1],2),0.5);
trans[0]=trans[0]/mag;
trans[1]=trans[1]/mag;
camera.setRotX(1.56-trans[1]*inc);
camera.setRotZ(-trans[0]*inc);
var width=document.getElementById('canvas').offsetWidth;
if(mousepos.x<width*0.3){
var turn=Math.pow((mousepos.x-width*0.3)/(width*0.3),2)*0.005*(now-lasttime);
camera.setRotY(camerarot.y+turn);
}
if(mousepos.x>width*0.7){
var turn=Math.pow((mousepos.x-width*0.7)/(width*0.3),2)*0.005*(now-lasttime);
camera.setRotY(camerarot.y-turn);
}
}
}
function checkkeys(){
var camera=gameScene.camera;
camerapos=camera.getPosition();
camerarot=camera.getRotation();
var mat=camera.getRotMatrix();
// var trans=mat.x([0,0,-1]);
var trans=GLGE.mulMat4Vec4(mat,[0,0,-1,1]);
var mag=Math.pow(
Math.pow(trans[0],2) + Math.pow(trans[1],2),
Math.pow(trans[2],2) //0.5
);
trans[0]=trans[0]/mag;
trans[1]=trans[1]/mag;
trans[2]=trans[2]/mag;
var yinc=0;
var xinc=0;
var zinc=0;
// if(keys.isKeyPressed(GLGE.KI_M)) {
// addduck();}
//WASD character move
if(keys.isKeyPressed(GLGE.KI_W)) {
yinc += parseFloat(trans[1]);
xinc += parseFloat(trans[0]);
}
if(keys.isKeyPressed(GLGE.KI_S)) {
yinc -= parseFloat(trans[1]);
xinc -= parseFloat(trans[0]);
}
if(keys.isKeyPressed(GLGE.KI_A)){
yinc += parseFloat(trans[0]);
xinc -= parseFloat(trans[1]);
}
if(keys.isKeyPressed(GLGE.KI_D)) {
yinc -= parseFloat(trans[0]);
xinc += parseFloat(trans[1]);
}
//z rot
if(keys.isKeyPressed(GLGE.KI_E)) {
zinc-=parseFloat(trans[2]);
}
if(keys.isKeyPressed(GLGE.KI_Q)) {
zinc+=parseFloat(trans[2]);
}
//camera rot
if(keys.isKeyPressed(GLGE.KI_LEFT_ARROW)) {
camera.setRotY(camerarot.y+0.002*(now-lasttime) );
}
if(keys.isKeyPressed(GLGE.KI_RIGHT_ARROW)) {
camera.setRotY(camerarot.y-0.002*(now-lasttime) );
}
if(keys.isKeyPressed(GLGE.KI_UP_ARROW)) {
camera.setRotX(1.56+trans[1]*0.0002*(now-lasttime));
// camera.setRotX(camerarot.x-0.002*(now-lasttime) );
}
if(keys.isKeyPressed(GLGE.KI_DOWN_ARROW)) {
camera.setRotX(1.56-trans[1]*0.0002*(now-lasttime));
// camera.setRotX(camerarot.x+0.002*(now-lasttime) );
}
// カメラ傾き
// if(keys.isKeyPressed(GLGE.KI_LEFT_ARROW)){
// camera.setRotZ(0.3);
// }
// 壁判定
if(levelmap.getHeightAt(camerapos.x+xinc,camerapos.y)>30)
xinc=0;
if(levelmap.getHeightAt(camerapos.x,camerapos.y+yinc)>30)
yinc=0;
if(levelmap.getHeightAt(camerapos.x+xinc,camerapos.y+yinc)>30){
yinc=0;
xinc=0;
}
// else{
// camera.setLocZ(levelmap.getHeightAt(camerapos.x+xinc,camerapos.y+yinc)+8);
// }
//camera移動
if(xinc!=0 || yinc!=0 || zinc!=0){
camera.setLocY(camerapos.y+yinc*0.05*(now-lasttime));
camera.setLocX(camerapos.x+xinc*0.05*(now-lasttime));
camera.setLocZ(camerapos.z+zinc*0.2*(now-lasttime));
}
}
levelmap=new GLGE.HeightMap("images/map.png",120,120,-50,50,-50,50,0,50);
var lasttime=0;
var frameratebuffer=24;
start=parseInt(new Date().getTime());
var now;
//mainloop
function render(){
now=parseInt(new Date().getTime());
frameratebuffer=Math.round(((frameratebuffer*9)+1000/(now-lasttime))/10);
document.getElementById("debug").innerHTML="Frame Rate:"+frameratebuffer;
var camera=gameScene.camera;
camerapos=camera.getPosition();
document.getElementById("debug").innerHTML+=" "+camerapos.x+" ";
document.getElementById("debug").innerHTML+=" "+camerapos.y+" ";
document.getElementById("debug").innerHTML+=" "+camerapos.z+" ";
// mouselook();
checkkeys();
gameRenderer.render();
lasttime=now;
}
//set fps
var FPS = 24;
setInterval(render,Math.round(1000/FPS));
// var inc=0.2;
document.getElementById("canvas").onmouseover=function(e){mouseovercanvas=true;}
document.getElementById("canvas").onmouseout=function(e){mouseovercanvas=false;}
}
doc.load("level.xml");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment