This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
83 lines (65 loc) · 1.6 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var EventEmitter = require('events').EventEmitter;
var game = new EventEmitter();
var input = require('./input');
var util = require('util');
var arDrone = require('ar-drone');
var client = arDrone.createClient();
var minDroneHeight = 0.2;
input.on('quit', quit);
input.on('enter', jump);
var gameState = 'OVER';
function gameover() {
if(gameState !== 'STARTED') return;
console.log('GAME OVER!!!');
client.stop();
client.land();
gameState = 'OVER';
game.emit('over');
}
function setup() {
if(gameState !== 'OVER') return;
console.log('SETTING UP!!!');
console.log('BATTERY STATUS:', client.battery());
client.disableEmergency()
client.takeoff();
gameState = 'SETTINGUP';
game.emit('setting-up');
setTimeout(function() {
console.log('STARTED!!!');
gameState = 'STARTED';
game.emit('started');
}, 5000);
}
function quit(){
client.stop();
client.land();
console.log('Killing!')
setTimeout(process.exit, 5000);
// prevent being called again
quit = function(){};
}
var currentJump;
function jump() {
if(gameState === 'OVER') {
setup();
} else if(gameState === 'STARTED'){
if(currentJump){
clearTimeout(currentJump);
}
console.log('drone going up');
client.up(1);
currentJump = setTimeout(function(){
client.down(1);
}, 500);
}
}
client.on('navdata', function(data){
if(!data.demo) return;
// If height below tolerance, end the game!
if(data.demo.altitude < minDroneHeight && gameState === 'STARTED'){
gameover();
}
});
module.exports.jump = jump;
module.exports.clientEmitter = client;
module.exports.game = game;