diff --git a/posts/keyboard-event/index.js b/posts/keyboard-event/index.js new file mode 100644 index 0000000..7384108 --- /dev/null +++ b/posts/keyboard-event/index.js @@ -0,0 +1,32 @@ +var pg = require('pg'); +var secret = require('../../private'); + +module.exports.index = function(req, res) { + + var user_id = req.session.user_id; + var camera = req.body.camera; + + pg.connect(secret.url, function(err, client, release) { + client.query( + "INSERT INTO keyboardevent(user_id, direction, camera)" + + "VALUES($1, NULL, ROW(ROW($2,$3,$4),ROW($5,$6,$7)));" , + [ + user_id, + camera.position.x, + camera.position.y, + camera.position.z, + + camera.target.x, + camera.target.y, + camera.target.z + ], + function(err, result) { + release(); + } + ); + }); + + res.setHeader('Content-Type', 'text/html'); + res.send("user_id = " + user_id); + +} diff --git a/posts/keyboard-event/urls.js b/posts/keyboard-event/urls.js new file mode 100644 index 0000000..c9ede51 --- /dev/null +++ b/posts/keyboard-event/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/keyboard-event': 'index' +} diff --git a/server.js b/server.js index 32ad754..f925a21 100644 --- a/server.js +++ b/server.js @@ -30,6 +30,8 @@ app.use(function(req, res, next) { next(); }); +// Set a cookie to know if already came. If not, France laws force to +// warn the user that the website uses cookies. app.use(function(req, res, next) { if (req.cookies.alreadyCame) { res.locals.alertCookie = false; @@ -48,6 +50,7 @@ require('./lib/controllers')(app, { verbose: !module.parent }); console.log("Loading posts :"); require('./lib/posts')(app, { verbose: !module.parent }); +// Static files app.use('/static', express.static('static')); // When error raised @@ -81,4 +84,6 @@ if ( app.get('env') === 'development' ) { server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'; } +// Start server +console.log("Server started on " + server_ip_address + ":" + server_port); app.listen(server_port, server_ip_address); diff --git a/static/js/PointerCamera.js b/static/js/PointerCamera.js index add00c7..7a3099f 100644 --- a/static/js/PointerCamera.js +++ b/static/js/PointerCamera.js @@ -26,10 +26,7 @@ var PointerCamera = function() { this.target = new THREE.Vector3(0,1,0); // Stuff for events - this.moveForward = false; - this.moveBackward = false; - this.moveRight = false; - this.moveLeft = false; + this.motion = {}; this.sensitivity = 0.05; this.speed = 1; @@ -104,10 +101,10 @@ PointerCamera.prototype.hermiteMotion = function(time) { PointerCamera.prototype.normalMotion = function(time) { // Update angles - if (this.increasePhi) {this.phi += this.sensitivity; this.changed = true; } - if (this.decreasePhi) {this.phi -= this.sensitivity; this.changed = true; } - if (this.increaseTheta) {this.theta += this.sensitivity; this.changed = true; } - if (this.decreaseTheta) {this.theta -= this.sensitivity; this.changed = true; } + if (this.motion.increasePhi) {this.phi += this.sensitivity; this.changed = true; } + if (this.motion.decreasePhi) {this.phi -= this.sensitivity; this.changed = true; } + if (this.motion.increaseTheta) {this.theta += this.sensitivity; this.changed = true; } + if (this.motion.decreaseTheta) {this.theta -= this.sensitivity; this.changed = true; } if (this.dragging) { this.theta += this.mouseMove.x; @@ -139,11 +136,11 @@ PointerCamera.prototype.normalMotion = function(time) { var speed = this.speed * time / 20; var direction = new THREE.Vector3(); - if (this.boost) speed *= 10; - if (this.moveForward) {direction.add(Tools.mul(forward, speed)); this.changed = true;} - if (this.moveBackward) {direction.sub(Tools.mul(forward, speed)); this.changed = true;} - if (this.moveLeft) {direction.add(Tools.mul(left, speed)); this.changed = true;} - if (this.moveRight) {direction.sub(Tools.mul(left, speed)); this.changed = true;} + if (this.motion.boost) speed *= 10; + if (this.motion.moveForward) {direction.add(Tools.mul(forward, speed)); this.changed = true;} + if (this.motion.moveBackward) {direction.sub(Tools.mul(forward, speed)); this.changed = true;} + if (this.motion.moveLeft) {direction.add(Tools.mul(left, speed)); this.changed = true;} + if (this.motion.moveRight) {direction.sub(Tools.mul(left, speed)); this.changed = true;} if (!this.collisions || !this.isColliding(direction)) { this.position.add(direction); @@ -265,27 +262,36 @@ PointerCamera.prototype.addToScene = function(scene) { } PointerCamera.prototype.onKeyEvent = function(event, toSet) { + // Create copy of state + var motionJsonCopy = JSON.stringify(this.motion); + switch ( event.keyCode ) { // Azerty keyboards - case 38: case 90: this.moveForward = toSet; break; // up / z - case 37: case 81: this.moveLeft = toSet; break; // left / q - case 40: case 83: this.moveBackward = toSet; break; // down / s - case 39: case 68: this.moveRight = toSet; break; // right / d - case 32: this.boost = toSet; break; + case 38: case 90: this.motion.moveForward = toSet; break; // up / z + case 37: case 81: this.motion.moveLeft = toSet; break; // left / q + case 40: case 83: this.motion.moveBackward = toSet; break; // down / s + case 39: case 68: this.motion.moveRight = toSet; break; // right / d + case 32: this.motion.boost = toSet; break; // Qwerty keyboards - case 38: case 87: this.moveForward = toSet; break; // up / w - case 37: case 65: this.moveLeft = toSet; break; // left / a - case 40: case 83: this.moveBackward = toSet; break; // down / s - case 39: case 68: this.moveRight = toSet; break; // right / d + case 38: case 87: this.motion.moveForward = toSet; break; // up / w + case 37: case 65: this.motion.moveLeft = toSet; break; // left / a + case 40: case 83: this.motion.moveBackward = toSet; break; // down / s + case 39: case 68: this.motion.moveRight = toSet; break; // right / d - case 73: case 104: this.increasePhi = toSet; break; // 8 Up for angle - case 75: case 98: this.decreasePhi = toSet; break; // 2 Down for angle - case 74: case 100: this.increaseTheta = toSet; break; // 4 Left for angle - case 76: case 102: this.decreaseTheta = toSet; break; // 6 Right for angle + case 73: case 104: this.motion.increasePhi = toSet; break; // 8 Up for angle + case 75: case 98: this.motion.decreasePhi = toSet; break; // 2 Down for angle + case 74: case 100: this.motion.increaseTheta = toSet; break; // 4 Left for angle + case 76: case 102: this.motion.decreaseTheta = toSet; break; // 6 Right for angle case 13: if (toSet) this.log(); break; } + if (motionJsonCopy != JSON.stringify(this.motion)) { + // Log any change + var event = new BD.Event.KeyboardEvent(); + event.camera = this; + event.send(); + } } PointerCamera.prototype.onKeyDown = function(event) {