def on_init(self, controller): # Method for initialization print 'psmove:', controller, 'connected!' # Set the controller to be blank controller.color = psmoveapi.RGB(*SocketRacket.COLOR_CLEAR) controller.rumble = 0
def flash(time, controller, color_rgb): # Given a color to flash around and the controller, flash based on the # time argument (assumed to go from [0, 1]) # Get base power power = (1 - math.cos(time * (2 * math.pi) * freq)) / 2 power = min(1.0, max(0.0, power * scale)) # Get color power color_power = min(1.0, max(0.0, power * color_scale)) color_power = color_power if (invert_color) else (1 - color_power) # Get rumble power rumble_power = min(1.0, max(0.0, power * rumble_scale)) rumble_power = (1 - rumble_power) if (invert_rumble) else rumble_power # Get the color color_flash = tuple(map(lambda x: x * color_power, list(color_rgb))) if (color): controller.color = psmoveapi.RGB(*color_flash) if (rumble): controller.rumble = rumble_power
def on_swing(self, controller, hand): # On a transition, the player should see a blue ball and have max rumble controller.color = psmoveapi.RGB(0, 0, 1.0) controller.rumble = 1.0 # Send swing information obj = ("swing", {"player": self.player_num}) data = pickle.dumps(obj) self.socket.send(data) print 'Sent swing packet!'
def do(self, controller, color): # Reset various parts of the controller if requested print 'event:', self if(self.clear_rumble): controller.rumble = 0 if(self.clear_color): controller.color = psmoveapi.RGB(*color) # Mark as done self._done = True
def on_button(self, controller, pressed, held, released): # Method to parse button presses # Forceful reset if ((self.state != GameState.RESET_WAIT) and (psmoveapi.Button.SELECT in held) and (psmoveapi.Button.START in held) and (psmoveapi.Button.PS in held)): self.sio_game_reset() self.state = GameState.RESET_WAIT # Color choosing logic if (self.state == GameState.COLOR_SELECTION): choices = (psmoveapi.Button.SQUARE, psmoveapi.Button.TRIANGLE, psmoveapi.Button.CROSS, psmoveapi.Button.CIRCLE) # Cycle through button options for button in choices: if (button in pressed): self.color_choice = SocketRacket.COLORS[button] controller.color = psmoveapi.RGB(*self.color_choice) return # Generic animation for a confirmation confirm_data = { 'events': [(event.Event( SocketRacket.COLOR_WAIT_TIME, self.generic_flash(rumble_scale=0.75, color_scale=0.75)), None), (clear_event.ClearEvent(), None)] } # Handedness confirmation if (psmoveapi.Button.SELECT in pressed): self.hand = 0 self.state_data = confirm_data elif (psmoveapi.Button.START in pressed): self.hand = 1 self.state_data = confirm_data # Color confirmation logic elif ((self.color_choice is not None) and (psmoveapi.Button.MOVE in pressed)): self.sio_init_color_choice(self.color_choice, self.hand) # Signal a transition to the next state self.state = GameState.COLOR_WAIT self.state_data = confirm_data
def trans(time, controller, _): # If the source is None, we consider this to actually be the user-chosen # color (from the start) source_rgb = source if (source_rgb is None): source_rgb = self.color_choice # Similar for the target target_rgb = target if (target_rgb is None): target_rgb = self.color_choice # Unpack (sr, sg, sb) = source_rgb (tr, tg, tb) = target_rgb # Scale and color scale = lambda a, b: a + (b - a) * time color = (scale(sr, tr), scale(sg, tg), scale(sb, tb)) controller.color = psmoveapi.RGB(*color)
def on_init(self, controller): print 'Controller', controller, 'connected!' controller.color = psmoveapi.RGB(1.0, 1.0, 1.0)
def on_transition(self, controller, hand): # On a transition, the player should see a green ball and have more rumble controller.color = psmoveapi.RGB(0, 1.0, 0) controller.rumble = 0.66
def on_backswing(self, controller, hand): # On a backswing, the player should see a red ball and have some rumble controller.color = psmoveapi.RGB(1.0, 0, 0) controller.rumble = 0.33
def on_idle(self, controller, hand): # On idle, the player should see a white ball and have no rumble controller.color = psmoveapi.RGB(1.0, 1.0, 1.0) controller.rumble = 0
def on_swing(self, controller, hand): # On a transition, the player should see a blue ball and have max rumble print 'swing', self.hand_to_string(hand) controller.color = psmoveapi.RGB(0, 0, 1.0) controller.rumble = 1.0
def on_idle(self, controller, hand): # On idle, the player should see a white ball and have no rumble print 'idle', self.hand_to_string(hand) controller.color = psmoveapi.RGB(1.0, 1.0, 1.0) controller.rumble = 0
def on_button(self, controller, buttons): # Method to parse button presses # Temporary to cycle through animations # Pressing the PS button simulates an in-order server event if (psmoveapi.Button.PS in buttons): if (self.state == GameState.COLOR_WAIT): if (bool(random.randint(0, 1))): self.on_sio_init_color_reject() else: self.on_sio_init_color_confirm() elif (self.state == GameState.START_WAIT): self.on_sio_game_is_server() elif (self.state == GameState.GAMEPLAY): end_rally = False if (bool(random.randint(0, 3))): self.on_sio_game_hit_ball({'strength': 0.75}) if (not bool(random.randint(0, 5))): self.on_sio_game_won_rally() end_rally = True else: self.on_sio_game_missed_ball() end_rally = True if (end_rally and not bool(random.randint(0, 5))): if (bool(random.randint(0, 1))): self.on_sio_game_over({'is_winner': False}) else: self.on_sio_game_over({'is_winner': True}) return # Color choosing logic if (self.state == GameState.COLOR_SELECTION): choices = (psmoveapi.Button.SQUARE, psmoveapi.Button.TRIANGLE, psmoveapi.Button.CROSS, psmoveapi.Button.CIRCLE) # Cycle through button options for button in choices: if (button in buttons): self.color_choice = SIORacket.COLORS[button] controller.color = psmoveapi.RGB(*self.color_choice) return # Color confirmation logic if ((self.color_choice is not None) and (psmoveapi.Button.MOVE in buttons)): self.sio_init_color_choice(self.color_choice) # Signal a transition to the next state self.state = GameState.COLOR_WAIT self.state_data = { 'events': [(event.Event( SIORacket.COLOR_WAIT_TIME, self.generic_flash(rumble_scale=0.75, color_scale=0.75)), None), (clear_event.ClearEvent(), None)] }