Added global event handlers.
1.1 --- a/scripts/machine.js Fri Apr 15 23:02:28 2011 +0200
1.2 +++ b/scripts/machine.js Fri Apr 15 23:22:22 2011 +0200
1.3 @@ -2,7 +2,7 @@
1.4 var renderer;
1.5 var controller;
1.6 var camera;
1.7 -
1.8 +var cameraSpeed = {'x': 0.1, 'y': 0.1};
1.9
1.10 function main()
1.11 {
1.12 @@ -14,7 +14,7 @@
1.13 gl.clearColor(0.0, 0.0, 0.0, 1.0);
1.14 gl.enable(gl.DEPTH_TEST);
1.15 machine = new Machine(object);
1.16 - camera = new Camera();
1.17 + camera = new Camera(cameraSpeed);
1.18 renderer = new Renderer(camera, context);
1.19 controller = new Controller(camera, machine, renderer);
1.20 update();
1.21 @@ -36,26 +36,47 @@
1.22 return (min + Math.random() * (max - min));
1.23 }
1.24
1.25 -function Camera()
1.26 +function Camera(speed)
1.27 {
1.28 this.x = 0.0;
1.29 this.y = 0.0;
1.30 + this.speed = speed;
1.31 }
1.32 Camera.prototype.moveLeft = function()
1.33 {
1.34 - camera.x += 0.1;
1.35 + this.x += this.speed.x;
1.36 }
1.37 Camera.prototype.moveRight = function()
1.38 {
1.39 - camera.x -= 0.1;
1.40 + this.x -= this.speed.x;
1.41 }
1.42 Camera.prototype.moveUp = function()
1.43 {
1.44 - camera.y -= 0.1;
1.45 + this.y -= this.speed.y;
1.46 }
1.47 Camera.prototype.moveDown = function()
1.48 {
1.49 - camera.y += 0.1;
1.50 + this.y += this.speed.y;
1.51 +}
1.52 +
1.53 +function moveCameraLeft()
1.54 +{
1.55 + camera.moveLeft();
1.56 +}
1.57 +
1.58 +function moveCameraRight()
1.59 +{
1.60 + camera.moveRight();
1.61 +}
1.62 +
1.63 +function moveCameraUp()
1.64 +{
1.65 + camera.moveUp();
1.66 +}
1.67 +
1.68 +function moveCameraDown()
1.69 +{
1.70 + camera.moveDown();
1.71 }
1.72
1.73 function ucode(char)
1.74 @@ -65,10 +86,10 @@
1.75
1.76 function Controller(camera, machine, renderer)
1.77 {
1.78 - this.actionMap = {'A': camera.moveLeft,
1.79 - 'D': camera.moveRight,
1.80 - 'W': camera.moveUp,
1.81 - 'S': camera.moveDown};
1.82 + this.actionMap = {'A': moveCameraLeft,
1.83 + 'D': moveCameraRight,
1.84 + 'W': moveCameraUp,
1.85 + 'S': moveCameraDown};
1.86 this.keyboard = new Keyboard(this.actionMap);
1.87 this.mouse = new Mouse();
1.88
1.89 @@ -102,7 +123,6 @@
1.90 {
1.91 if (this.pressed[key])
1.92 {
1.93 - //alert(this.pressed[key]);
1.94 key = String.fromCharCode(key);
1.95 if (this.actionMap[key]) this.actionMap[key]();
1.96 }