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