var world, camera, gamePaused, jump,robotControl,shadow,currentKey,globalrotY,setEyesPosition,swingArm,shadowsOn; var pause = function () { gamePaused = !gamePaused } window.setup = function () { setGL("agl"); gamePaused = false var agl = new aexolGL(); world = new Scene(); robotControl = {} PATH = "/media/robot/" globalrotY = 0.0 shadowsOn = false robotControl.speed = 0.0 robotControl.speedDccStep = 0.005 robotControl.maxSpeedReverse = 10.0 robotControl.turnStep = 2.5 robotControl.maxTurnReductionSpeed = 30.0 robotControl.maxWheelRotation =45 robotControl.startAcceleration = 10.0 robotControl.brakeForce = 0.0 robotControl.speedAccStep = 0.005 robotControl.maxSpeed = 10.0 robotControl.maxArmRotation = 60.0 robotControl.attack = false robotControl.minArmRotation = 0.0 robotControl.armUpStep = 1.0 robotControl.armDownStep = 3.0 camera = new Camera(1,1.0,200.0,45.0) shaderdiff2 = basicShader({useLights:true,useDiffuse:true}) shaderdiff3 = basicShader({useLights:true,useShadow:shadowsOn}) shaderSky = basicShader({}) currentKey = {} agl.init(); document.addEventListener("keydown",function(e){ currentKey[e.keyCode] = 1 }) document.addEventListener("keyup",function(e){ currentKey[e.keyCode] = 0 }) shaderszybki = basicShader({useLights:true}) shaderoczu = basicShader({}) shaderobudowa = basicShader({useLights:true}) shaders = { szybka:shaderszybki, szybka2:shaderszybki, oko:shaderoczu, oko2:shaderoczu, obudowa:shaderobudowa } pivots = { wheel_left:new Pivot(), wheel_right:new Pivot(), axis:new Pivot(), base:new Pivot(), head:new Pivot(), robot:new Pivot(), arm:new Pivot(), sword:new Pivot(), shield:new Pivot() } matOko = new Material({color:[0.4,1.0,0.2]}) materials = { body: new Material({color:[1.0,1.0,1.0],specularWeight:1.0,shininess:99.0}), wheels_r : new Material({color:[0.1,0.1,0.1],specularWeight:1.0,shininess:10.0}), wheels_l : new Material({color:[0.1,0.1,0.1],specularWeight:1.0,shininess:10.0}), wheels_metal_r : new Material({color:[0.3,0.3,0.3],specularWeight:1.8,shininess:3.0}), wheels_metal_l : new Material({color:[0.3,0.3,0.3],specularWeight:1.8,shininess:3.0}), szybka:new Material({color:[0.1,0.1,0.1],specularWeight:1.8,shininess:93.0,alpha:0.1}), szybka2:new Material({color:[0.3,0.3,0.3],specularWeight:1.8,shininess:93.0,alpha:0.9}), oko:matOko, oko2:matOko, obudowa:new Material({color:[0.02,0.06,0.01],specularWeight:1.3,shininess:92.0}) } if(shadowsOn){ shadow = new Shadow(world,new Vector(-0.05,3.43,2.8),0.1,200.0) } lights = new Light([{ lightPosition: new Vector(-0.05,3.43,2.8), intensity: 0.6, attenuation: 20.0, shadow:shadowsOn, color:[0.4,1.0,0.2] },{ lightPosition: new Vector(0.2, 3.43, 0.6), intensity: 5.0, attenuation: 0.2, color:[0.4,1.0,0.2] },{ lightPosition: new Vector(-0.3, 3.43, 0.6), intensity: 5.0, attenuation: 0.2, color:[0.4,1.0,0.2] },{ intensity: 1.0, lightType:2.0, color:[0.42,0.42,0.423] }]) if(shadowsOn){ lights.setShadow(shadow) } Mesh.obj(PATH+"robot.obj",function(e){ for(var single in e){ var name = single var mesh = e[single] robotControl[name] = new GameObject(world,{ light:lights, shader : shaders[name] || shaderdiff3, material : materials[name] || new Material({color:[1.0,1.0,1.0]}), mesh: mesh.computeNormals() }) robotControl[name].aex.centerPivot() } pivots.wheel_left.add(robotControl.wheels_l).add(robotControl.wheels_metal_l).setPivotToCenter() pivots.wheel_right.add(robotControl.wheels_r).add(robotControl.wheels_metal_r).setPivotToCenter() pivots.axis.add(pivots.wheel_left).add(pivots.wheel_right).add(robotControl.axis_chassis) .add(robotControl.axis_inner).add(robotControl.axis_inner2).add(robotControl.axis_inside1) .add(robotControl.axis_outer).add(robotControl.engine1).add(robotControl.engine2) .add(robotControl.engine3).setPivotToCenter() pivots.base.add(robotControl.base_base).add(robotControl.base_dots).add(robotControl.base_hemisphere) .add(robotControl.base_upper).add(robotControl.base_upper2).add(robotControl.sensor_base) .add(robotControl.sensor_upper).add(robotControl.body).setPivotToCenter() Mesh.obj(PATH+"glowa_robot.obj",function(ee){ for(var single in ee){ var name = single var mesh = ee[single] robotControl[name] = new GameObject(world,{ light:lights, shader : shaders[name] || shaderdiff3, material : materials[name] || new Material({color:[1.0,1.0,1.0]}), mesh: mesh.computeNormals() }) robotControl[name].aex.centerPivot() pivots.head.add(robotControl[name]) } robotControl.szybka2.z = 0.54 pivots.head.setPivotToCenter(); // lights.lights[1].lightPosition = robotControl.oko.position pivots.robot.add(pivots.base).add(pivots.axis).add(pivots.head) Mesh.obj(PATH+"ramie_miecz.obj",function(eee){ console.log(eee) for(var single in eee){ var name = single var mesh = eee[single] robotControl[name] = new GameObject(world,{ light:lights, shader : shaders[name] || shaderdiff3, material : materials[name] || new Material({color:[1.0,1.0,1.0]}), mesh: mesh.computeNormals() }) robotControl[name].aex.centerPivot() pivots.arm.add(robotControl[name]) } pivots.arm.setPivotToCenter() pivots.arm.setPivot(new Vector(-0.6, 2.65, 0.18)) pivots.robot.add(pivots.arm) robotControl.loaded = true }) }) }) horizonPlane = new GameObject(world,{ light:lights, shader:shaderdiff3, material:new Material({color:[0.3,0.3,0.3]}), mesh:Mesh.plane().rotate(-90,0,0).scaleUniform(100.0).move(0,0.52,0) }) testSphere = new GameObject(world,{ shader:shaderoczu, material:matOko, mesh:Mesh.sphere().scaleUniform(0.1) }) camera.position = new Vector(5.967406989213715, 12.195963853446816, 8.465739833831659); camera.rotation = new Vector(43.0,-43.0, 0); camera.on(0.1) camera.setScene(world); } function setEyesPosition(){ lights.lights[0].lightPosition = pivots.robot.modelView.transformPoint(new Vector(-0.05,3.43,2.8)) lights.lights[1].lightPosition = pivots.robot.modelView.transformPoint(new Vector(0.2,3.43,0.6)) lights.lights[2].lightPosition = pivots.robot.modelView.transformPoint(new Vector(-0.3,3.43,0.6)) if(shadowsOn){ shadow.position = lights.lights[0].lightPosition } } function brake(){ robotControl.brakeForce += 0.0005 robotControl.speed *= 1.0-robotControl.brakeForce } function checkKey(which){ var mtr = robotControl.maxTurnReductionSpeed var turnFriction = Math.sin(((robotControl.speed>mtr?mtr:robotControl.speed)/mtr)*Math.PI/2.0) var ms = robotControl.maxSpeed var acceleration = Math.cos(((robotControl.speed>ms?ms:robotControl.speed)/ms)*Math.PI/2.0)*robotControl.startAcceleration //LEFT if(which == 37){ var rotY = globalrotY + robotControl.turnStep*(1.1-turnFriction) rotY = rotY<=robotControl.maxWheelRotation?rotY:robotControl.maxWheelRotation; globalrotY = rotY } //RIGHT if(which == 39){ var rotY = globalrotY - robotControl.turnStep*(1.1-turnFriction) var mwr = robotControl.maxWheelRotation rotY = rotY<=mwr?(rotY>=-mwr?rotY:-mwr):mwr; console.log(rotY) globalrotY = rotY } //UP if(which == 38){ if(robotControl.speed < -robotControl.speedDccStep){ //brake brake() }else{ //accelerate robotControl.brakeForce =0.0 var ns = robotControl.speed + robotControl.speedAccStep*acceleration ns = ns>robotControl.maxSpeed?robotControl.maxSpeed:ns robotControl.speed = ns robotControl.brakeForce } } //DOWN if(which == 40){ if(robotControl.speed > robotControl.speedDccStep){ //brake brake() }else{ //accelerate reverse robotControl.brakeForce =0.0 var ns = robotControl.speed - robotControl.speedAccStep*acceleration ns = ns<-robotControl.maxSpeedReverse?-robotControl.maxSpeedReverse:ns robotControl.speed = ns robotControl.brakeForce } } //ATTACK -R if(which == 82){ var newR = pivots.arm.rotX+robotControl.armDownStep var ma = robotControl.maxArmRotation newR = newR<ma?newR:ma console.log("ARM",newR) pivots.arm.rotX = newR } //SWORD BACK - F if(which == 70){ var newR = pivots.arm.rotX-robotControl.armUpStep var ma = robotControl.minArmRotation newR = newR>ma?newR:ma pivots.arm.rotX = newR } } window.logic = function () { if (!gamePaused) { if(robotControl.loaded){ // camera.position.x = carControl.car.x-Math.sin(cmY*Math.degToRad)*7 // camera.position.z = carControl.car.z+Math.cos(cmY*Math.degToRad)*7 var wheelSpeed = ((((robotControl.speed/3.6)/0.4)*360.0)/60); activeKeys = {} for( var key in currentKey){ if(currentKey[key] == 1){ checkKey(key) activeKeys[key] = 1 } } // Decceleration if (!( 38 in activeKeys || 40 in activeKeys)){ if (robotControl.speed >robotControl.speedDccStep){ robotControl.speed -= robotControl.speedDccStep/6.0 + robotControl.speedDccStep*robotControl.speed/robotControl.maxSpeed }else if(robotControl.speed<-robotControl.speedDccStep){ robotControl.speed += robotControl.speedDccStep/6.0 + robotControl.speedDccStep*robotControl.speed/robotControl.maxSpeed } else{ robotControl.speed = 0.0 } } var ry = globalrotY pivots.robot.rotY += (ry*robotControl.speed)*0.002 var crY = pivots.robot.rotY*Math.degToRad var addSpeed = (robotControl.speed/3.6)/60.0 pivots.robot.z += addSpeed*Math.cos(crY) pivots.robot.x += addSpeed*Math.sin(crY) setEyesPosition() if(shadowsOn){ shadow.draw() } } } } window.draw = function () { if (!gamePaused && robotControl.loaded) { world.draw(camera); } } alert("Use arrows to move robot, R,F keys to attack/block") glStart(window.setup);
RUN