class FindBallState(State): def __init__(self, worldWrapper): # Create our internal state machine self.stateMachine = StateMachine(DriveStraightState(worldWrapper)) def step(self, worldWrapper): world = worldWrapper.world # Do the global checks # newState, changed = self.checkGlobal(worldWrapper) # if changepd == STATE_CHANGE_FLAG: # return newState, changed # If we see any balls # Step the state machine self.stateMachine.step(worldWrapper) return self, self.stateMachine.goal
class BallAcquisitionState(State): TIMEOUT = 10 def __init__(self, worldWrapper): # Create and keep track of our state machine self.stateMachine = StateMachine(SeekBallState(worldWrapper)) # Keep track of time for a timeout self.lastTime = time.time() def step(self, worldWrapper): world = worldWrapper.world # Check globals newState, changed = self.checkGlobal(worldWrapper) if changed == STATE_CHANGE_FLAG: return newState, changed # Check for a timeout if time.time() - self.lastTime > self.TIMEOUT: return EscapeState(worldWrapper), STATE_CHANGE_FLAG # Check for having lost all the balls if len(world.balls) == 0: return FindBallState(worldWrapper), STATE_CHANGE_FLAG # TODO: run the Ferrous # Otherwise, step the state machine oldState = self.stateMachine.state self.stateMachine.step(worldWrapper) # Update our timer if self.stateMachine.state != oldState: self.lastTime = time.time() # Step the state machine self.stateMachine.step(worldWrapper) return self, self.stateMachine.goal