cylon/examples/conway_sphero/conway_sphero.coffee

69 lines
1.2 KiB
CoffeeScript
Raw Normal View History

Cylon = require '../..'
2013-11-01 00:37:11 +08:00
bots = [
{ port: '/dev/rfcomm0', name: 'Thelma' }
{ 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
connection: { name: 'Sphero', adaptor: 'sphero' }
device: { name: 'sphero', driver: 'sphero' }
2013-11-01 00:37:11 +08:00
born: ->
@contacts = 0
@age = 0
@life()
@move()
2013-11-01 00:37:11 +08:00
move: ->
@sphero.roll 60, Math.floor(Math.random() * 360)
2013-11-01 00:37:11 +08:00
life: ->
@alive = true
@sphero.setRGB Green
2013-11-01 00:37:11 +08:00
death: ->
@alive = false
@sphero.setRGB Red
@sphero.stop()
2013-11-01 00:37:11 +08:00
enoughContacts: ->
if @contacts >= 2 and @contacts < 7 then true else false
2013-11-01 00:37:11 +08:00
birthday: ->
@age += 1
console.log "Happy birthday, #{@name}. You are #{@age} and had #{@contacts} contacts."
if @enoughContacts()
@rebirth() if not @alive?
2013-11-01 00:37:11 +08:00
else
@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()