2013-11-26 08:06:55 +08:00
|
|
|
var Cylon = require('../..');
|
|
|
|
|
|
|
|
var bots = [
|
|
|
|
{ port: '/dev/rfcomm0', name: 'Thelma' },
|
|
|
|
{ port: '/dev/rfcomm1', name: 'Louise' },
|
|
|
|
{ port: '/dev/rfcomm2', name: 'Grace' },
|
|
|
|
{ port: '/dev/rfcomm3', name: 'Ada' }
|
|
|
|
];
|
|
|
|
|
|
|
|
var Green = 0x0000FF;
|
|
|
|
var Red = 0xFF0000;
|
|
|
|
|
|
|
|
var ConwayRobot = (function() {
|
|
|
|
function ConwayRobot() {}
|
|
|
|
|
|
|
|
ConwayRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' };
|
|
|
|
ConwayRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
|
|
|
|
|
|
|
|
ConwayRobot.prototype.born = function() {
|
|
|
|
this.contacts = 0;
|
|
|
|
this.age = 0;
|
|
|
|
this.life();
|
2014-03-27 07:25:09 +08:00
|
|
|
this.move();
|
2013-11-26 08:06:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ConwayRobot.prototype.move = function() {
|
2014-03-27 07:25:09 +08:00
|
|
|
this.sphero.roll(60, Math.floor(Math.random() * 360));
|
2013-11-26 08:06:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ConwayRobot.prototype.life = function() {
|
|
|
|
this.alive = true;
|
2014-03-27 07:25:09 +08:00
|
|
|
this.sphero.setRGB(Green);
|
2013-11-26 08:06:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ConwayRobot.prototype.death = function() {
|
|
|
|
this.alive = false;
|
2013-11-28 02:40:37 +08:00
|
|
|
this.sphero.setRGB(Red);
|
2014-03-27 07:25:09 +08:00
|
|
|
this.sphero.stop();
|
2013-11-26 08:06:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ConwayRobot.prototype.enoughContacts = function() {
|
2014-03-27 07:25:09 +08:00
|
|
|
return (this.contacts >= 2 && this.contacts < 7);
|
2013-11-26 08:06:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ConwayRobot.prototype.birthday = function() {
|
|
|
|
this.age += 1;
|
|
|
|
|
2014-01-04 09:18:09 +08:00
|
|
|
console.log("Happy birthday, " + this.name + ". You are " + this.age + " and had " + this.contacts + " contacts.");
|
2013-11-26 08:06:55 +08:00
|
|
|
|
|
|
|
if (this.enoughContacts()) {
|
|
|
|
if (this.alive == null) { this.rebirth(); }
|
|
|
|
} else {
|
|
|
|
this.death();
|
|
|
|
}
|
2014-03-27 07:25:09 +08:00
|
|
|
|
|
|
|
this.contacts = 0;
|
2013-11-26 08:06:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ConwayRobot.prototype.work = function(me) {
|
|
|
|
me.born();
|
|
|
|
|
|
|
|
me.sphero.on('collision', function() {
|
2014-03-27 07:25:09 +08:00
|
|
|
this.contacts += 1;
|
2013-11-26 08:06:55 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
every((3).seconds(), function() {
|
2014-03-27 07:25:09 +08:00
|
|
|
if (me.alive != null) { me.move(); }
|
2013-11-26 08:06:55 +08:00
|
|
|
});
|
|
|
|
|
2014-03-27 07:25:09 +08:00
|
|
|
every((10).seconds(), function() {
|
|
|
|
if (me.alive != null) { me.birthday(); }
|
2013-11-26 08:06:55 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return ConwayRobot;
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
for (var i = 0; i < bots.length; i++) {
|
|
|
|
var bot = bots[i];
|
|
|
|
var robot = new ConwayRobot;
|
|
|
|
|
|
|
|
robot.connection.port = bot.port;
|
|
|
|
robot.name = bot.name;
|
|
|
|
|
|
|
|
Cylon.robot(robot);
|
|
|
|
}
|
|
|
|
|
|
|
|
Cylon.start();
|