# HG changeset patch # User Eugen Sawin # Date 1302901348 -7200 # Node ID 9c0318d0f1c3e4d767c54b0feb80512416eca75b # Parent 9c21841b3ef9a563900b6d5fb5af77e193abbe39 Added keyboard camera. diff -r 9c21841b3ef9 -r 9c0318d0f1c3 scripts/machine.js --- a/scripts/machine.js Fri Apr 15 21:37:40 2011 +0200 +++ b/scripts/machine.js Fri Apr 15 23:02:28 2011 +0200 @@ -1,5 +1,8 @@ var machine; var renderer; +var controller; +var camera; + function main() { @@ -10,14 +13,17 @@ var object = new Cube(1, context); gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.enable(gl.DEPTH_TEST); - machine = new Machine(object); - renderer = new Renderer(context); + machine = new Machine(object); + camera = new Camera(); + renderer = new Renderer(camera, context); + controller = new Controller(camera, machine, renderer); update(); } function update() { requestAnimFrame(update); + controller.update(); machine.scene.rotation.x += (random(0, 2) - 2) * 0.001; machine.scene.rotation.y += (random(0, 3) - 1) * 0.001; machine.scene.rotation.z += (random(0, 2) - 1) * 0.001; @@ -30,8 +36,92 @@ return (min + Math.random() * (max - min)); } -function Renderer(context) +function Camera() { + this.x = 0.0; + this.y = 0.0; +} +Camera.prototype.moveLeft = function() +{ + camera.x += 0.1; +} +Camera.prototype.moveRight = function() +{ + camera.x -= 0.1; +} +Camera.prototype.moveUp = function() +{ + camera.y -= 0.1; +} +Camera.prototype.moveDown = function() +{ + camera.y += 0.1; +} + +function ucode(char) +{ + return char.charCodeAt(0); +} + +function Controller(camera, machine, renderer) +{ + this.actionMap = {'A': camera.moveLeft, + 'D': camera.moveRight, + 'W': camera.moveUp, + 'S': camera.moveDown}; + this.keyboard = new Keyboard(this.actionMap); + this.mouse = new Mouse(); + +} +Controller.prototype.update = function() +{ + this.keyboard.handle(); +} + +function Mouse() +{ +} +function Keyboard(actionMap) +{ + this.actionMap = actionMap; + this.pressed = {}; + document.onkeydown = handleKeyDown; + document.onkeyup = handleKeyUp; +} +Keyboard.prototype.keyDown = function(event) +{ + this.pressed[event.keyCode] = true; +} +Keyboard.prototype.keyUp = function(event) +{ + this.pressed[event.keyCode] = false; +} +Keyboard.prototype.handle = function() +{ + for (key in this.pressed) + { + if (this.pressed[key]) + { + //alert(this.pressed[key]); + key = String.fromCharCode(key); + if (this.actionMap[key]) this.actionMap[key](); + } + } +} + +function handleKeyDown(event) +{ + controller.keyboard.keyDown(event); +} + +function handleKeyUp(event) +{ + controller.keyboard.keyUp(event); +} + +function Renderer(camera, context) +{ + this.camera = camera; this.context = context; this.gl = context.gl; this.matrixStack = []; @@ -49,7 +139,7 @@ mat4.perspective(45, viewport.width / viewport.height, 0.1, 100.0, pMatrix); mat4.identity(mvMatrix); - mat4.translate(mvMatrix, [0.0, 0.0, -7.0]); + mat4.translate(mvMatrix, [this.camera.x, this.camera.y, -7.0]); this.pushMatrix(mvMatrix); @@ -126,7 +216,6 @@ gl.uniformMatrix4fv(program.pMatrixUniform, false, pMatrix); gl.uniformMatrix4fv(program.mvMatrixUniform, false, mvMatrix); } - Context.prototype.expand = function() { var width = window.innerWidth;