<s> """<STR_LIT>""" <EOL> from random import Random <EOL> from herculeum . ui . controllers import MoveController <EOL> from herculeum . ui . gui . animations import AnimationFactory <EOL> from herculeum . ui . gui . eventdisplay import EventMessageWidget <EOL> from herculeum . ui . gui . widgets import ( EffectsWidget , HitPointsWidget , <EOL> SpellSelectorWidget , TimerAdapter ) <EOL> from herculeum . ui . gui . layers import ( zorder_floor , zorder_wall , zorder_ornament , <EOL> zorder_item , zorder_character , <EOL> zorder_counter , zorder_trap ) <EOL> from pyherc . data . model import DIED_IN_DUNGEON <EOL> from pyherc . events import e_event_type <EOL> from pyherc . ports import ( is_move_legal , move , attack , is_dig_legal , dig , <EOL> wait , pick_up , cast ) <EOL> from PyQt4 . QtCore import ( pyqtProperty , pyqtSignal , QAbstractAnimation , <EOL> QEasingCurve , QEvent , QObject , QPropertyAnimation , <EOL> QSequentialAnimationGroup , QSize , Qt , QTimer ) <EOL> from PyQt4 . QtGui import ( QColor , QFont , QGraphicsPixmapItem , QGraphicsScene , <EOL> QGraphicsSimpleTextItem , QGraphicsView , QHBoxLayout , <EOL> QTransform , QVBoxLayout , QWidget ) <EOL> from pyherc . data import get_characters , get_items , get_tiles <EOL> class PlayMapWindow ( QWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , model , surface_manager , action_factory , rng , <EOL> rules_engine , configuration ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( parent ) <EOL> self . model = model <EOL> self . surface_manager = surface_manager <EOL> self . action_factory = action_factory <EOL> self . rng = rng <EOL> self . current_level = None <EOL> self . configuration = configuration <EOL> self . hit_points_widget = None <EOL> self . message_widget = None <EOL> self . map_widget = None <EOL> self . effects_widget = None <EOL> self . spell_selector = None <EOL> self . __set_layout ( model , surface_manager , action_factory , rng , <EOL> rules_engine , configuration ) <EOL> MenuRequested = pyqtSignal ( name = '<STR_LIT>' ) <EOL> EndScreenRequested = pyqtSignal ( name = '<STR_LIT>' ) <EOL> def __set_layout ( self , model , surface_manager , action_factory , rng , <EOL> rules_engine , configuration ) : <EOL> """<STR_LIT>""" <EOL> layout = QVBoxLayout ( ) <EOL> status_layout = QHBoxLayout ( ) <EOL> self . hit_points_widget = HitPointsWidget ( parent = self , <EOL> surface_manager = surface_manager ) <EOL> self . effects_widget = EffectsWidget ( parent = self , <EOL> surface_manager = surface_manager ) <EOL> self . spell_selector = SpellSelectorWidget ( parent = self , <EOL> surface_manager = surface_manager ) <EOL> self . map_widget = PlayMapWidget ( parent = self , <EOL> model = model , <EOL> surface_manager = surface_manager , <EOL> action_factory = action_factory , <EOL> rng = rng , <EOL> rules_engine = rules_engine , <EOL> configuration = configuration ) <EOL> self . map_widget . MenuRequested . connect ( self . on_menu_requested ) <EOL> self . map_widget . EndScreenRequested . connect ( self . on_end_screen_requested ) <EOL> self . map_widget . NextSpellRequested . connect ( self . on_next_spell ) <EOL> self . map_widget . PreviousSpellRequested . connect ( self . on_previous_spell ) <EOL> self . message_widget = EventMessageWidget ( parent = self ) <EOL> self . message_widget . setMaximumHeight ( <NUM_LIT:100> ) <EOL> status_layout . addWidget ( self . hit_points_widget ) <EOL> status_layout . addWidget ( self . spell_selector ) <EOL> status_layout . addWidget ( self . effects_widget ) <EOL> status_layout . addStretch ( ) <EOL> layout . addLayout ( status_layout ) <EOL> layout . addWidget ( self . map_widget ) <EOL> layout . addWidget ( self . message_widget ) <EOL> self . setLayout ( layout ) <EOL> self . resize ( QSize ( <NUM_LIT> , <NUM_LIT> ) ) <EOL> def construct_scene ( self ) : <EOL> """<STR_LIT>""" <EOL> self . map_widget . construct_scene ( ) <EOL> self . model . player . register_event_listener ( self . message_widget ) <EOL> self . message_widget . set_point_of_view ( self . model . player ) <EOL> self . model . player . register_for_updates ( self . effects_widget ) <EOL> def on_menu_requested ( self ) : <EOL> """<STR_LIT>""" <EOL> self . MenuRequested . emit ( ) <EOL> def on_end_screen_requested ( self ) : <EOL> """<STR_LIT>""" <EOL> self . EndScreenRequested . emit ( ) <EOL> def on_next_spell ( self ) : <EOL> """<STR_LIT>""" <EOL> self . spell_selector . next_spell ( ) <EOL> def on_previous_spell ( self ) : <EOL> """<STR_LIT>""" <EOL> self . spell_selector . previous_spell ( ) <EOL> class PlayMapWidget ( QWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , model , surface_manager , action_factory , rng , <EOL> rules_engine , configuration ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( parent ) <EOL> self . model = model <EOL> self . scene = None <EOL> self . surface_manager = surface_manager <EOL> self . action_factory = action_factory <EOL> self . rng = rng <EOL> self . rules_engine = rules_engine <EOL> self . configuration = configuration <EOL> self . current_level = None <EOL> self . view = None <EOL> self . animation_adapters = [ ] <EOL> self . animation_timers = [ ] <EOL> for adapter in range ( <NUM_LIT:10> ) : <EOL> self . animation_adapters . append ( TimerAdapter ( ) ) <EOL> self . animation_timers . append ( QTimer ( self ) ) <EOL> self . animation_timers [ adapter ] . timeout . connect ( self . animation_adapters [ adapter ] . trigger_animations ) <EOL> self . animation_timers [ adapter ] . start ( <NUM_LIT> + adapter * <NUM_LIT:10> ) <EOL> self . animations = [ ] <EOL> self . move_controller = MoveController ( action_factory = action_factory , <EOL> rng = rng ) <EOL> self . __set_layout ( ) <EOL> self . keymap , self . move_key_map = self . _construct_keymaps ( <EOL> configuration . controls ) <EOL> self . animation_factory = AnimationFactory ( ) <EOL> MenuRequested = pyqtSignal ( name = '<STR_LIT>' ) <EOL> EndScreenRequested = pyqtSignal ( name = '<STR_LIT>' ) <EOL> NextSpellRequested = pyqtSignal ( name = '<STR_LIT>' ) <EOL> PreviousSpellRequested = pyqtSignal ( name = '<STR_LIT>' ) <EOL> def _construct_keymaps ( self , config ) : <EOL> """<STR_LIT>""" <EOL> keymap = { } <EOL> move_keymap = { } <EOL> for key in config . move_left : <EOL> keymap [ key ] = self . _move <EOL> move_keymap [ key ] = <NUM_LIT:7> <EOL> for key in config . move_up : <EOL> keymap [ key ] = self . _move <EOL> move_keymap [ key ] = <NUM_LIT:1> <EOL> for key in config . move_right : <EOL> keymap [ key ] = self . _move <EOL> move_keymap [ key ] = <NUM_LIT:3> <EOL> for key in config . move_down : <EOL> keymap [ key ] = self . _move <EOL> move_keymap [ key ] = <NUM_LIT:5> <EOL> for key in config . start : <EOL> keymap [ key ] = self . _menu <EOL> for key in config . action_a : <EOL> keymap [ key ] = self . _action_a <EOL> for key in config . back : <EOL> keymap [ key ] = self . _back <EOL> for key in config . left_shoulder : <EOL> keymap [ key ] = self . _shoulder_left <EOL> for key in config . right_shoulder : <EOL> keymap [ key ] = self . _shoulder_right <EOL> for key in config . mode_1 : <EOL> keymap [ key ] = self . _zoom_out <EOL> for key in config . mode_2 : <EOL> keymap [ key ] = self . _zoom_in <EOL> return keymap , move_keymap <EOL> def __set_layout ( self ) : <EOL> """<STR_LIT>""" <EOL> self . scene = QGraphicsScene ( ) <EOL> layout = QHBoxLayout ( ) <EOL> self . view = QGraphicsView ( self . scene ) <EOL> self . view . setFocusPolicy ( Qt . StrongFocus ) <EOL> self . view . installEventFilter ( self ) <EOL> self . view . setHorizontalScrollBarPolicy ( Qt . ScrollBarAlwaysOff ) <EOL> self . view . setVerticalScrollBarPolicy ( Qt . ScrollBarAlwaysOff ) <EOL> layout . addWidget ( self . view ) <EOL> self . setLayout ( layout ) <EOL> def construct_scene ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __construct_scene ( self . model , self . scene ) <EOL> self . model . player . register_for_updates ( self ) <EOL> self . model . register_event_listener ( self ) <EOL> self . __center_view_on_character ( self . model . player ) <EOL> def __center_view_on_character ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> location = entity . location <EOL> width = <NUM_LIT:32> <EOL> height = <NUM_LIT:32> <EOL> self . view . setSceneRect ( ( location [ <NUM_LIT:0> ] * <NUM_LIT:32> ) - width // <NUM_LIT:2> , <EOL> ( location [ <NUM_LIT:1> ] * <NUM_LIT:32> ) - height // <NUM_LIT:2> , <EOL> width , <EOL> height ) <EOL> def __construct_scene ( self , model , scene ) : <EOL> """<STR_LIT>""" <EOL> for anim in [ x for x in self . animations ] : <EOL> anim . stop ( ) <EOL> anim . clear ( ) <EOL> for adapter in self . animation_adapters : <EOL> adapter . glyphs . clear ( ) <EOL> self . animations = [ ] <EOL> scene . clear ( ) <EOL> self . current_level = model . player . level <EOL> for location , tile in get_tiles ( self . current_level ) : <EOL> if tile [ '<STR_LIT>' ] : <EOL> new_glyph = MapGlyph ( self . surface_manager . get_icon ( tile [ '<STR_LIT>' ] ) , <EOL> None , <EOL> self . animation_adapters [ <NUM_LIT:0> ] ) <EOL> new_glyph . setZValue ( zorder_floor ) <EOL> new_glyph . setPos ( location [ <NUM_LIT:0> ] * <NUM_LIT:32> , location [ <NUM_LIT:1> ] * <NUM_LIT:32> ) <EOL> scene . addItem ( new_glyph ) <EOL> if tile [ '<STR_LIT>' ] : <EOL> new_glyph = MapGlyph ( self . surface_manager . get_icon ( tile [ '<STR_LIT>' ] ) , <EOL> None ) <EOL> new_glyph . setZValue ( zorder_wall ) <EOL> new_glyph . setPos ( location [ <NUM_LIT:0> ] * <NUM_LIT:32> , location [ <NUM_LIT:1> ] * <NUM_LIT:32> ) <EOL> scene . addItem ( new_glyph ) <EOL> for tile_id in tile [ '<STR_LIT>' ] : <EOL> new_glyph = MapGlyph ( self . surface_manager . get_icon ( tile_id ) , <EOL> None , <EOL> self . rng . choice ( self . animation_adapters ) ) <EOL> new_glyph . setZValue ( zorder_ornament ) <EOL> new_glyph . setPos ( location [ <NUM_LIT:0> ] * <NUM_LIT:32> , location [ <NUM_LIT:1> ] * <NUM_LIT:32> ) <EOL> scene . addItem ( new_glyph ) <EOL> for item in tile [ '<STR_LIT>' ] : <EOL> self . add_glyph ( item , scene , zorder_item ) <EOL> for trap in tile [ '<STR_LIT>' ] : <EOL> self . add_glyph ( trap , scene , zorder_trap ) <EOL> for creature in get_characters ( self . current_level ) : <EOL> self . add_glyph ( creature , <EOL> scene , <EOL> zorder_character ) <EOL> def add_glyph ( self , entity , scene , z_order ) : <EOL> """<STR_LIT>""" <EOL> new_glyph = MapGlyph ( self . surface_manager . get_icon ( entity . icon ) , <EOL> entity , self . rng . choice ( self . animation_adapters ) ) <EOL> new_glyph . setZValue ( z_order ) <EOL> new_glyph . setPos ( entity . location [ <NUM_LIT:0> ] * <NUM_LIT:32> , <EOL> entity . location [ <NUM_LIT:1> ] * <NUM_LIT:32> ) <EOL> scene . addItem ( new_glyph ) <EOL> def remove_glyph ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> glyphs = [ x for x in self . view . items ( ) <EOL> if ( hasattr ( x , '<STR_LIT>' ) ) <EOL> and ( x . entity == entity ) ] <EOL> for glyph in glyphs : <EOL> self . view . scene ( ) . removeItem ( glyph ) <EOL> def receive_event ( self , event ) : <EOL> """<STR_LIT>""" <EOL> anim = self . animation_factory . create_animation ( event ) <EOL> anim . trigger ( self ) <EOL> def remove_finished_animation ( self ) : <EOL> """<STR_LIT>""" <EOL> finished_animations = [ x for x in self . animations <EOL> if x . state ( ) == QAbstractAnimation . Stopped ] <EOL> counters = [ x . animationAt ( <NUM_LIT:0> ) . targetObject ( ) . object_to_animate <EOL> for x in finished_animations ] <EOL> for item in finished_animations : <EOL> item . clear ( ) <EOL> self . animations . remove ( item ) <EOL> def receive_update ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if e_event_type ( event ) == '<STR_LIT>' : <EOL> if self . model . player . level != self . current_level : <EOL> self . __construct_scene ( self . model , self . scene ) <EOL> self . __center_view_on_character ( self . model . player ) <EOL> def eventFilter ( self , qobject , event ) : <EOL> """<STR_LIT>""" <EOL> result = False <EOL> if event . type ( ) == QEvent . KeyPress : <EOL> self . keyPressEvent ( event ) <EOL> result = True <EOL> else : <EOL> result = super ( ) . eventFilter ( qobject , event ) <EOL> return result <EOL> def keyPressEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> if self . model . player is None : <EOL> return <EOL> key_code = event . key ( ) <EOL> player = self . model . player <EOL> next_creature = self . model . get_next_creature ( self . rules_engine ) <EOL> if next_creature == player : <EOL> if key_code in self . keymap : <EOL> self . keymap [ key_code ] ( key_code , event . modifiers ( ) ) <EOL> next_creature = self . model . get_next_creature ( self . rules_engine ) <EOL> if next_creature is None : <EOL> self . model . end_condition = DIED_IN_DUNGEON <EOL> while ( next_creature != player <EOL> and next_creature is not None <EOL> and self . model . end_condition == <NUM_LIT:0> ) : <EOL> next_creature . act ( ) <EOL> next_creature = self . model . get_next_creature ( self . rules_engine ) <EOL> if next_creature is None : <EOL> self . model . end_condition = DIED_IN_DUNGEON <EOL> if self . model . end_condition != <NUM_LIT:0> : <EOL> self . EndScreenRequested . emit ( ) <EOL> def _move ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> player = self . model . player <EOL> direction = self . move_key_map [ key ] <EOL> if modifiers & Qt . ControlModifier : <EOL> if direction != <NUM_LIT:9> : <EOL> attack ( player , <EOL> direction , <EOL> self . rng ) <EOL> elif modifiers & Qt . AltModifier : <EOL> if direction != <NUM_LIT:9> : <EOL> cast ( player , <EOL> direction , <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . move_controller . move_or_attack ( player , direction ) <EOL> def _menu ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> self . MenuRequested . emit ( ) <EOL> def _back ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> wait ( self . model . player ) <EOL> def _zoom_in ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> self . view . scale ( <NUM_LIT> , <NUM_LIT> ) <EOL> def _zoom_out ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> self . view . scale ( <NUM_LIT> , <NUM_LIT> ) <EOL> def _shoulder_right ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> self . NextSpellRequested . emit ( ) <EOL> def _shoulder_left ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> self . PreviousSpellRequested . emit ( ) <EOL> def _action_a ( self , key , modifiers ) : <EOL> """<STR_LIT>""" <EOL> player = self . model . player <EOL> level = player . level <EOL> items = list ( get_items ( level , player . location ) ) <EOL> if items is not None and len ( items ) > <NUM_LIT:0> : <EOL> pick_up ( player , <EOL> items [ <NUM_LIT:0> ] ) <EOL> elif is_move_legal ( player , <NUM_LIT:9> ) : <EOL> move ( player , <NUM_LIT:9> ) <EOL> elif is_dig_legal ( player ) : <EOL> dig ( player ) <EOL> class DamageCounter ( QGraphicsSimpleTextItem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , damage , colour , parent ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( ) <EOL> font = QFont ( '<STR_LIT>' , <EOL> <NUM_LIT:12> , <EOL> QFont . Bold , <EOL> False ) <EOL> self . setText ( str ( damage ) ) <EOL> self . setBrush ( QColor ( colour ) ) <EOL> self . setFont ( font ) <EOL> self . adapter = DamageCounterAdapter ( self , self ) <EOL> class DamageCounterAdapter ( QObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , object_to_animate ) : <EOL> """<STR_LIT>""" <EOL> super ( ) . __init__ ( ) <EOL> self . object_to_animate = object_to_animate <EOL> def __get_y_location ( self ) : <EOL> return self . object_to_animate . y ( ) <EOL> def __set_y_location ( self , y ) : <EOL> self . object_to_animate . setY ( y ) <EOL> def __get_opacity ( self ) : <EOL> return self . object_to_animate . opacity ( ) <EOL> def __set_opacity ( self , opacity ) : <EOL> self . object_to_animate . setOpacity ( opacity ) <EOL> y_location = pyqtProperty ( int , __get_y_location , __set_y_location ) <EOL> opacity = pyqtProperty ( float , __get_opacity , __set_opacity ) <EOL> class MapGlyph ( QGraphicsPixmapItem ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pixmap , entity , timer = None ) : <EOL> """<STR_LIT>""" <EOL> self . tiles = [ ] <EOL> if hasattr ( pixmap , '<STR_LIT>' ) : <EOL> super ( ) . __init__ ( pixmap , None ) <EOL> else : <EOL> assert len ( pixmap ) > <NUM_LIT:0> <EOL> super ( ) . __init__ ( pixmap [ <NUM_LIT:0> ] , None ) <EOL> if timer : <EOL> timer . register ( self ) <EOL> self . tiles = pixmap <EOL> self . entity = entity <EOL> self . flipped = False <EOL> self . offset = <NUM_LIT:0> <EOL> def animate ( self , frame ) : <EOL> """<STR_LIT>""" <EOL> self . setPixmap ( self . tiles [ frame ] ) </s>
