Advertisement
Guest User

Untitled

a guest
Jul 8th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. startRight = function(self){ return function(){self.thrusters.left(true)}}(this)
  2. startLeft = function(self){ return function(){self.thrusters.right(true)}}(this)
  3. startUp = function(self){ return function(){self.thrusters.bottom(true)}}(this)
  4. startDown = function(self){ return function(){self.thrusters.top(true)}}(this)
  5. stopRight = function(self){ return function(){self.thrusters.left(false)}}(this)
  6. stopLeft = function(self){ return function(){self.thrusters.right(false)}}(this)
  7. stopUp = function(self){ return function(){self.thrusters.bottom(false)}}(this)
  8. stopDown = function(self){ return function(){self.thrusters.top(false)}}(this)
  9.  
  10. function fullStop(){stopUp(); stopDown(); stopLeft(); stopRight()}
  11.  
  12. function moveRight(){stopLeft(); startRight();}
  13. function moveUp(){stopDown(); startUp();}
  14. function moveLeft(){stopRight(); startLeft();}
  15. function moveDown(){stopUp(); startDown();}
  16. function moveDownRight(){moveDown(); moveRight();}
  17. function moveDownLeft(){moveDown(); moveLeft();}
  18. function moveUpRight(){moveUp(); moveRight();}
  19. function moveUpLeft(){moveUp(); moveLeft();}
  20.  
  21. function forceRight(){fullStop(); startRight();}
  22. function forceUp(){fullStop(); startUp();}
  23. function forceLeft(){fullStop(); startLeft();}
  24. function forceDown(){fullStop(); startDown();}
  25. function forceDownRight(){fullStop(); moveDown(); moveRight();}
  26. function forceDownLeft(){fullStop(); moveDown(); moveLeft();}
  27. function forceUpRight(){fullStop(); moveUp(); moveRight();}
  28. function forceUpLeft(){fullStop(); moveUp(); moveLeft();}
  29.  
  30. function select()
  31. {
  32.     for(var i = 0; i < arguments.length; ++i){
  33.         if(arguments[i] != null && arguments[i] != undefined)
  34.             return arguments[i]
  35.     }
  36. }
  37.  
  38.  
  39. current_state = ''
  40. next_state = ''
  41. function signal(signal)
  42. {
  43.     var state = select(current_state, "")
  44.     var key = state + "__" + signal
  45.  
  46.     if(key in STATES){        
  47.         next_state = select(STATES[key][0], current_state, '')
  48.         console.log('RECV ' + key + ' --> [' + next_state + ']')
  49.         var rstate = next_state
  50.         var _func = STATES[key][1]
  51.         _func()
  52.         current_state = next_state
  53.         if(current_state != rstate){
  54.             console.log('Next state overriden to ' + current_state)
  55.         }
  56.         next_state = ''
  57.     } else {
  58.         console.log('RECV ' + key + '(ignoring)')
  59.     }
  60.  
  61. }
  62.  
  63.  
  64. function signalSensor(sensor, contact){
  65.     if(contact) signal('hit_'+sensor)
  66.     else signal('free_'+sensor)
  67. }
  68.  
  69. this.on('start', function() { signal('start') });
  70. this.on('sensor:right', function(contact) {signalSensor('right', contact)});
  71. this.on('sensor:left', function(contact) {signalSensor('left', contact)});
  72. this.on('sensor:bottom', function(contact) {signalSensor('down', contact)});
  73. this.on('sensor:top', function(contact) {signalSensor('up', contact)});
  74.  
  75.  
  76. STATES = {  
  77.     '__start':          ['p1', forceUp], //p1 - place first block
  78.     'p1__hit_up':       [null, forceRight],
  79.     'p1__hit_right':    [null, forceDown],
  80.     'p1__hit_left':     ['tr', forceRight], //tr - transit
  81.    
  82.     'tr__hit_right':    [null, forceDown],
  83.     'tr__hit_down':     ['tr2', forceRight],
  84.     'tr2__hit_right':   ['p2', forceUp],
  85.  
  86.     'p2__hit_left':     ['ext', forceRight]
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement