2014-12-14 08:19:25 +08:00
|
|
|
"use strict";
|
|
|
|
|
2014-09-29 23:31:46 +08:00
|
|
|
var Cylon = require("../..");
|
|
|
|
|
|
|
|
var Green = 0x0000FF,
|
|
|
|
Red = 0xFF0000;
|
|
|
|
|
|
|
|
var bots = {
|
2015-04-15 12:49:12 +08:00
|
|
|
Thelma: "/dev/rfcomm0",
|
|
|
|
Louise: "/dev/rfcomm1",
|
|
|
|
Grace: "/dev/rfcomm2",
|
|
|
|
Ada: "/dev/rfcomm3"
|
2014-09-29 23:31:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Object.keys(bots).forEach(function(name) {
|
|
|
|
var port = bots[name];
|
|
|
|
|
|
|
|
Cylon.robot({
|
|
|
|
name: name,
|
|
|
|
|
2014-11-26 08:01:31 +08:00
|
|
|
connections: {
|
2014-12-14 08:19:25 +08:00
|
|
|
sphero: { adaptor: "sphero", port: port }
|
2014-11-26 08:01:31 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
devices: {
|
2014-12-14 08:19:25 +08:00
|
|
|
sphero: { driver: "sphero" }
|
2014-11-26 08:01:31 +08:00
|
|
|
},
|
2014-09-29 23:31:46 +08:00
|
|
|
|
|
|
|
work: function(my) {
|
|
|
|
my.born();
|
|
|
|
|
2014-12-14 08:19:25 +08:00
|
|
|
my.sphero.on("collision", function() {
|
2014-09-29 23:31:46 +08:00
|
|
|
my.contacts += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
every((3).seconds(), function() {
|
|
|
|
if (my.alive) {
|
|
|
|
my.move();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
every((10).seconds(), function() {
|
|
|
|
my.birthday();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
move: function() {
|
|
|
|
this.sphero.roll(60, Math.floor(Math.random() * 360));
|
|
|
|
},
|
|
|
|
|
|
|
|
born: function() {
|
|
|
|
this.contacts = 0;
|
|
|
|
this.age = 0;
|
|
|
|
this.life();
|
|
|
|
this.move();
|
|
|
|
},
|
|
|
|
|
|
|
|
life: function() {
|
|
|
|
this.alive = true;
|
|
|
|
this.sphero.setRGB(Green);
|
|
|
|
},
|
|
|
|
|
|
|
|
death: function() {
|
|
|
|
this.alive = false;
|
|
|
|
this.sphero.setRGB(Red);
|
|
|
|
this.sphero.stop();
|
|
|
|
},
|
|
|
|
|
|
|
|
enoughContacts: function() {
|
|
|
|
return this.contacts >= 2 && this.contacts < 7;
|
|
|
|
},
|
|
|
|
|
|
|
|
birthday: function() {
|
|
|
|
this.age += 1;
|
|
|
|
|
|
|
|
if (this.alive) {
|
2014-12-14 08:19:25 +08:00
|
|
|
var str = "Happy birthday, ";
|
|
|
|
str += this.name;
|
|
|
|
str += ". You are ";
|
|
|
|
str += this.age;
|
|
|
|
str += " and had ";
|
|
|
|
str += this.contacts;
|
|
|
|
str += " contacts.";
|
|
|
|
|
|
|
|
console.log(str);
|
2014-09-29 23:31:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.enoughContacts()) {
|
|
|
|
if (!this.alive) {
|
|
|
|
this.born();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.death();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.contacts = 0;
|
2013-11-26 08:06:55 +08:00
|
|
|
}
|
2014-09-29 23:31:46 +08:00
|
|
|
});
|
|
|
|
});
|
2013-11-26 08:06:55 +08:00
|
|
|
|
|
|
|
Cylon.start();
|