def create_action(self, action_type, animation = None, direction = PlayerStates.FACING_RIGHT, key = pygame.K_UP): return_action = None factory = ActionFactory(self) if action_type == PlayerStates.STANDING: return_action = factory.create_stand(animation) elif action_type == PlayerStates.LANDING: return_action = factory.create_land(animation) elif action_type == PlayerStates.FLOATING: return_action = factory.create_float(animation) elif action_type == PlayerStates.STUNNED: return_action = factory.create_stun() elif action_type == PlayerStates.JUMPING: return_action = factory.create_jump(animation) elif action_type == PlayerStates.CROUCHING: return_action = factory.create_crouch(animation) elif action_type == PlayerStates.WALKING: return_action = factory.create_walk(animation) elif action_type == PlayerStates.RUNNING: return_action = factory.create_run(animation) elif (action_type in InputActionTypes.ATTACKS or action_type in AttackTypes.ATTACK_TYPES): return_action = factory.create_attack(action_type, animation, self.model) return return_action
class ControllerFactory(): def __init__(self, input_player): self.action_factory = ActionFactory(input_player) def create_controller_from_moveset(self, moveset, input_player): """Creates a controller object from a moveset""" #Create the command handlers for the controller aerial_movement_command_types = [command_type for command_type in CommandCollections.AERIAL_MOVEMENTS] aerial_movement_command_handler = CommandHandler( aerial_movement_command_types ) aerial_action_command_types = [command_type for command_type in CommandCollections.AERIAL_ACTIONS] aerial_action_command_handler = CommandHandler( aerial_action_command_types ) stun_movement_command_handler = CommandHandler( [command_type for command_type in CommandCollections.STUN_MOVEMENTS] ) ground_movement_command_types = [command_type for command_type in CommandCollections.GROUND_MOVEMENTS] ground_movement_command_handler = CommandHandler( ground_movement_command_types, True ) attack_command_handler = CommandHandler( [command_type for command_type in CommandCollections.ATTACK_ACTIONS] ) #create the key to command mappings for the controller movement_key_to_command_type = dict( [(get_control_key(input_player.player_position, action_type), action_type) for action_type in InputActionTypes.MOVEMENTS] ) attack_key_to_command_type = dict( [(get_control_key(input_player.player_position, action_type), action_type) for action_type in CommandCollections.ATTACK_ACTIONS if action_type != InputActionTypes.FORWARD] ) #Set aerial no movement actions float_action = self.create_action( PlayerStates.FLOATING, moveset.movement_animations[PlayerStates.FLOATING] ) tap_no_movement = Command( InputActionTypes.NO_MOVEMENT, CommandDurations.TAP ) aerial_action_command_handler.add_command([tap_no_movement], float_action) hold_no_movement = Command( InputActionTypes.NO_MOVEMENT, CommandDurations.HOLD ) aerial_action_command_handler.add_command([hold_no_movement], float_action) #Set ground no movement actions stand_action = self.create_action( PlayerStates.STANDING, moveset.movement_animations[PlayerStates.STANDING] ) tap_no_movement = Command( InputActionTypes.NO_MOVEMENT, CommandDurations.TAP ) ground_movement_command_handler.add_command([tap_no_movement], stand_action) hold_no_movement = Command( InputActionTypes.NO_MOVEMENT, CommandDurations.HOLD ) ground_movement_command_handler.add_command([hold_no_movement], stand_action) #Set jump actions jump_action = self.create_action( PlayerStates.JUMPING, moveset.movement_animations[PlayerStates.JUMPING], None ) tap_jump_command = Command( InputActionTypes.JUMP, CommandDurations.TAP ) hold_jump_command = Command( InputActionTypes.JUMP, CommandDurations.HOLD ) ground_movement_command_handler.add_command( [tap_jump_command], jump_action ) aerial_action_command_handler.add_command( [tap_jump_command], jump_action ) ground_movement_command_handler.add_command( [hold_jump_command], jump_action ) aerial_action_command_handler.add_command( [hold_jump_command], jump_action ) #Set move up actions tap_up_command = Command( InputActionTypes.MOVE_UP, CommandDurations.TAP ) hold_up_command = Command( InputActionTypes.MOVE_UP, CommandDurations.HOLD ) stun_movement_command_handler.add_command( [tap_up_command], StunMotion( (0, -1 * input_player.tap_stun_acceleration), (0, -1 * input_player.max_stun_velocity) ) ) stun_movement_command_handler.add_command( [hold_up_command], StunMotion( (0, -1 * input_player.hold_stun_acceleration), (0, -1 * input_player.max_stun_velocity) ) ) #Set move down actions crouch_action = self.create_action( PlayerStates.CROUCHING, moveset.movement_animations[PlayerStates.CROUCHING], None ) tap_down_command = Command( InputActionTypes.MOVE_DOWN, CommandDurations.TAP ) hold_down_command = Command( InputActionTypes.MOVE_DOWN, CommandDurations.HOLD ) ground_movement_command_handler.add_command( [tap_down_command], crouch_action ) ground_movement_command_handler.add_command( [hold_down_command], crouch_action ) aerial_movement_command_handler.add_command( [tap_down_command], AerialMotion( (0, input_player.aerial_acceleration), (0, input_player.max_aerial_velocity) ) ) aerial_movement_command_handler.add_command( [hold_down_command], AerialMotion( (0, input_player.aerial_acceleration), (0, input_player.max_aerial_velocity) ) ) stun_movement_command_handler.add_command( [tap_down_command], StunMotion( (0, input_player.tap_stun_acceleration), (0, input_player.max_stun_velocity) ) ) stun_movement_command_handler.add_command( [hold_down_command], StunMotion( (0, input_player.hold_stun_acceleration), (0, input_player.max_stun_velocity) ) ) #Set move right actions tap_right_command = Command( InputActionTypes.MOVE_RIGHT, CommandDurations.TAP ) hold_right_command = Command( InputActionTypes.MOVE_RIGHT, CommandDurations.HOLD ) input_player.walk_right_action = self.create_action( PlayerStates.WALKING, moveset.movement_animations[PlayerStates.WALKING], PlayerStates.FACING_RIGHT ) input_player.run_right_action = self.create_action( PlayerStates.RUNNING, moveset.movement_animations[PlayerStates.RUNNING], PlayerStates.FACING_RIGHT ) ground_movement_command_handler.add_command( [tap_right_command, tap_right_command], input_player.run_right_action ) ground_movement_command_handler.add_command( [hold_right_command, tap_right_command], input_player.run_right_action ) ground_movement_command_handler.add_command( [tap_right_command], input_player.walk_right_action ) ground_movement_command_handler.add_command( [hold_right_command], input_player.walk_right_action ) aerial_movement_command_handler.add_command( [tap_right_command], AerialMotion( (input_player.aerial_acceleration, 0), (input_player.max_aerial_velocity, 0) ) ) aerial_movement_command_handler.add_command( [hold_right_command], AerialMotion( (input_player.aerial_acceleration, 0), (input_player.max_aerial_velocity, 0) ) ) stun_movement_command_handler.add_command( [tap_right_command], StunMotion( (input_player.tap_stun_acceleration, 0), (input_player.max_stun_velocity, 0) ) ) stun_movement_command_handler.add_command( [hold_right_command], StunMotion( (input_player.hold_stun_acceleration, 0), (input_player.max_stun_velocity, 0) ) ) #Set move left actions tap_left_command = Command( InputActionTypes.MOVE_LEFT, CommandDurations.TAP ) hold_left_command = Command( InputActionTypes.MOVE_LEFT, CommandDurations.HOLD ) input_player.walk_left_action = self.create_action( PlayerStates.WALKING, moveset.movement_animations[PlayerStates.WALKING], PlayerStates.FACING_LEFT ) input_player.run_left_action = self.create_action( PlayerStates.RUNNING, moveset.movement_animations[PlayerStates.RUNNING], PlayerStates.FACING_LEFT ) ground_movement_command_handler.add_command( [tap_left_command, tap_left_command], input_player.run_left_action ) ground_movement_command_handler.add_command( [hold_left_command, tap_left_command], input_player.run_left_action ) ground_movement_command_handler.add_command( [tap_left_command], input_player.walk_left_action ) ground_movement_command_handler.add_command( [hold_left_command], input_player.walk_left_action ) aerial_movement_command_handler.add_command( [tap_left_command], AerialMotion( (-1 * input_player.aerial_acceleration, 0), (-1 * input_player.max_aerial_velocity, 0) ) ) aerial_movement_command_handler.add_command( [hold_left_command], AerialMotion( (-1 * input_player.aerial_acceleration, 0), (-1 * input_player.max_aerial_velocity, 0) ) ) stun_movement_command_handler.add_command( [tap_left_command], StunMotion( (-1 * input_player.tap_stun_acceleration, 0), (-1 * input_player.max_stun_velocity, 0) ) ) stun_movement_command_handler.add_command( [hold_left_command], StunMotion( (-1 * input_player.hold_stun_acceleration, 0), (-1 * input_player.max_stun_velocity, 0) ) ) #Set attack actions for attack_name in moveset.get_attacks(): attack_type = moveset.get_attack_type(attack_name) attack_action = None if InputActionTypes.JUMP in moveset.attack_key_combinations[attack_name]: attack_action = JumpAttack(attack_type) else: attack_action = Attack(attack_type) attack_action.set_acceleration(attack_type) self.action_factory._set_action_animations( attack_action, moveset.attack_animations[attack_name], attack_action.acceleration ) attack_action.set_attack_data(input_player.model) input_player.actions[attack_name] = attack_action if InputActionTypes.JUMP in moveset.attack_key_combinations[attack_name]: short_command_list = [ command_type for command_type in moveset.attack_key_combinations[attack_name] if command_type != InputActionTypes.JUMP ] tap_commands = [] for command_type in short_command_list: tap_commands.append(Command(command_type, CommandDurations.TAP)) aerial_action_command_handler.add_command( tap_commands, attack_action ) hold_commands = [] for command_type in short_command_list: if (command_type in InputActionTypes.MOVEMENTS or command_type == InputActionTypes.FORWARD): hold_commands.append(Command(command_type, CommandDurations.HOLD)) else: hold_commands.append(Command(command_type, CommandDurations.TAP)) aerial_action_command_handler.add_command( hold_commands, attack_action ) tap_commands = [] for command_type in moveset.attack_key_combinations[attack_name]: tap_commands.append(Command(command_type, CommandDurations.TAP)) aerial_action_command_handler.add_command( tap_commands, attack_action ) hold_commands = [] for command_type in moveset.attack_key_combinations[attack_name]: if (command_type in InputActionTypes.MOVEMENTS or command_type == InputActionTypes.FORWARD): hold_commands.append(Command(command_type, CommandDurations.HOLD)) else: hold_commands.append(Command(command_type, CommandDurations.TAP)) aerial_action_command_handler.add_command( hold_commands, attack_action ) else: tap_commands = [] for command_type in moveset.attack_key_combinations[attack_name]: tap_commands.append(Command(command_type, CommandDurations.TAP)) attack_command_handler.add_command( tap_commands, attack_action ) hold_commands = [] for command_type in moveset.attack_key_combinations[attack_name]: if (command_type in InputActionTypes.MOVEMENTS or command_type == InputActionTypes.FORWARD): hold_commands.append(Command(command_type, CommandDurations.TAP)) else: hold_commands.append(Command(command_type, CommandDurations.TAP)) attack_command_handler.add_command( hold_commands, attack_action ) return Controller( movement_key_to_command_type, attack_key_to_command_type, aerial_movement_command_handler, aerial_action_command_handler, stun_movement_command_handler, ground_movement_command_handler, attack_command_handler ) def create_action(self, action_type, animation = None, direction = None, key = None): return_action = None if action_type == PlayerStates.STANDING: return_action = self.action_factory.create_stand(animation) elif action_type == PlayerStates.LANDING: return_action = self.action_factory.create_land(animation) elif action_type ==PlayerStates.FLOATING: return_action = self.action_factory.create_float(animation) elif action_type == PlayerStates.STUNNED: return_action = self.action_factory.create_stun() elif action_type == PlayerStates.JUMPING: return_action = self.action_factory.create_jump(animation) elif action_type == PlayerStates.CROUCHING: return_action = self.action_factory.create_crouch(animation) elif action_type == PlayerStates.WALKING: return_action = self.action_factory.create_walk(animation) return_action.direction = direction elif action_type == PlayerStates.RUNNING: return_action = self.action_factory.create_run(animation) return_action.direction = direction return return_action