Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- startRight = function(self){ return function(){self.thrusters.left(true)}}(this)
- startLeft = function(self){ return function(){self.thrusters.right(true)}}(this)
- startUp = function(self){ return function(){self.thrusters.bottom(true)}}(this)
- startDown = function(self){ return function(){self.thrusters.top(true)}}(this)
- stopRight = function(self){ return function(){self.thrusters.left(false)}}(this)
- stopLeft = function(self){ return function(){self.thrusters.right(false)}}(this)
- stopUp = function(self){ return function(){self.thrusters.bottom(false)}}(this)
- stopDown = function(self){ return function(){self.thrusters.top(false)}}(this)
- function fullStop(){stopUp(); stopDown(); stopLeft(); stopRight()}
- function moveRight(){stopLeft(); startRight();}
- function moveUp(){stopDown(); startUp();}
- function moveLeft(){stopRight(); startLeft();}
- function moveDown(){stopUp(); startDown();}
- function moveDownRight(){moveDown(); moveRight();}
- function moveDownLeft(){moveDown(); moveLeft();}
- function moveUpRight(){moveUp(); moveRight();}
- function moveUpLeft(){moveUp(); moveLeft();}
- function forceRight(){fullStop(); startRight();}
- function forceUp(){fullStop(); startUp();}
- function forceLeft(){fullStop(); startLeft();}
- function forceDown(){fullStop(); startDown();}
- function forceDownRight(){fullStop(); moveDown(); moveRight();}
- function forceDownLeft(){fullStop(); moveDown(); moveLeft();}
- function forceUpRight(){fullStop(); moveUp(); moveRight();}
- function forceUpLeft(){fullStop(); moveUp(); moveLeft();}
- function select()
- {
- for(var i = 0; i < arguments.length; ++i){
- if(arguments[i] != null && arguments[i] != undefined)
- return arguments[i]
- }
- }
- current_state = ''
- next_state = ''
- function signal(signal)
- {
- var state = select(current_state, "")
- var key = state + "__" + signal
- if(key in STATES){
- next_state = select(STATES[key][0], current_state, '')
- console.log('RECV ' + key + ' --> [' + next_state + ']')
- var rstate = next_state
- var _func = STATES[key][1]
- _func()
- current_state = next_state
- if(current_state != rstate){
- console.log('Next state overriden to ' + current_state)
- }
- next_state = ''
- } else {
- console.log('RECV ' + key + '(ignoring)')
- }
- }
- function signalSensor(sensor, contact){
- if(contact) signal('hit_'+sensor)
- else signal('free_'+sensor)
- }
- this.on('start', function() { signal('start') });
- this.on('sensor:right', function(contact) {signalSensor('right', contact)});
- this.on('sensor:left', function(contact) {signalSensor('left', contact)});
- this.on('sensor:bottom', function(contact) {signalSensor('down', contact)});
- this.on('sensor:top', function(contact) {signalSensor('up', contact)});
- STATES = {
- '__start': ['p1', forceUp], //p1 - place first block
- 'p1__hit_up': [null, forceRight],
- 'p1__hit_right': [null, forceDown],
- 'p1__hit_left': ['tr', forceRight], //tr - transit
- 'tr__hit_right': [null, forceDown],
- 'tr__hit_down': ['tr2', forceRight],
- 'tr2__hit_right': ['p2', forceUp],
- 'p2__hit_left': ['ext', forceRight]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement