class IntroLayer(Layer): is_event_handler = True def __init__(self): super(IntroLayer, self).__init__() self.img = pyglet.resource.image('background.png') self.intro_text = Label('Made by some Asshole:', font_size=24, font_name='Times New Roman', color=(255,255,255,255), anchor_x='left', anchor_y='bottom') self.intro_text.position = (0, 0) self.add(self.intro_text) def draw(self): glPushMatrix() self.transform() self.intro_text.draw() self.img.blit(0, 0) glPopMatrix() def start_game(self): scene = Scene() scene.add(MultiplexLayer( MainMenu(), OptionsMenu(), HiScoresLayer(), ), z=1) scene.add(BackgroundLayer(), z=0) director.push(ShuffleTransition(scene, 1.5)) def on_key_press(self, k, m): if k in (pyglet.window.key.ENTER, pyglet.window.key.ESCAPE, pyglet.window.key.SPACE): self.start_game() return True def on_mouse_press(self, x, y, b, m): self.start_game() return True
class GenericControllerBehavior(CtrlBehavior): def __init__(self): super(GenericControllerBehavior, self).__init__() global behavior_name self.display_name = behavior_name self.label = Label("", font_size=9, font_name="Cabin Regular", anchor_x="center", anchor_y="top", color=(255, 255, 255, 255)) self.init_attribute('control_distance', 'ControlDistance', (0,1000), 'generic_controller_default_control_distance', 10) def associate(self, behavior): super(GenericControllerBehavior, self).associate(behavior) self.set_control_menu_mode() def update(self): super(GenericControllerBehavior, self).update() if self.controlled_attribute: distance = self.object.translation.distance(self.association.object.translation) distance_ratio = max(0, min(1.3, distance * 1.0 / self.attributes['control_distance'].value) - 0.3) value = self.controlled_attribute.range[0] + (distance_ratio * (self.controlled_attribute.range[1] - self.controlled_attribute.range[0])) self.controlled_attribute.set(value) def draw_foreground(self): super(GenericControllerBehavior, self).draw_foreground() if self.controlled_attribute: coords = self.object.screen_coords() self.label.position = coords.x, coords.y - 10 self.label.element.text = "%s: %s" % (self.controlled_attribute.display_name, self.controlled_attribute.to_string()) self.label.element.color = (255, 255, 255, int(self.object.alpha() * 255)) self.label.draw()
class Interface(Layer): def __init__(self, model): self.model = model self.portraits = {'wizard': Hero_portriat_DM(), 'priest': Hero_portriat_DM(), 'warrior': Hero_portriat_DM(),'rogue': Hero_portriat_DM()} for c in self.portraits: self.portraits[c].reload(model, c) self.money = starting_money def draw(self): w, h = director.get_window_size() sc = 1920/w self.label = Label('%d' %self.money, font_name='Times New Roman', font_size=20//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255)) self.label.position = (1670//sc, 1030//sc) self.label.draw() for c in self.portraits: self.portraits[c].draw()
class GenericControllerBehavior(CtrlBehavior): def __init__(self): super(GenericControllerBehavior, self).__init__() global behavior_name self.display_name = behavior_name self.label = Label("", font_size=9, font_name="Cabin Regular", anchor_x="center", anchor_y="top", color=(255, 255, 255, 255)) self.init_attribute('control_distance', 'ControlDistance', (0, 1000), 'generic_controller_default_control_distance', 10) def associate(self, behavior): super(GenericControllerBehavior, self).associate(behavior) self.set_control_menu_mode() def update(self): super(GenericControllerBehavior, self).update() if self.controlled_attribute: distance = self.object.translation.distance( self.association.object.translation) distance_ratio = max( 0, min(1.3, distance * 1.0 / self.attributes['control_distance'].value) - 0.3) value = self.controlled_attribute.range[0] + ( distance_ratio * (self.controlled_attribute.range[1] - self.controlled_attribute.range[0])) self.controlled_attribute.set(value) def draw_foreground(self): super(GenericControllerBehavior, self).draw_foreground() if self.controlled_attribute: coords = self.object.screen_coords() self.label.position = coords.x, coords.y - 10 self.label.element.text = "%s: %s" % ( self.controlled_attribute.display_name, self.controlled_attribute.to_string()) self.label.element.color = (255, 255, 255, int(self.object.alpha() * 255)) self.label.draw()
class FileInfoLayer(Layer): is_event_handler = False def __init__(self, text='', position=(0, 0), **kwargs): print("INIT >>> FileInfoLayer.init() ") super(FileInfoLayer, self).__init__() self.model = None self.background = cocos.layer.util_layers.ColorLayer(0, 0, 0, 128, 1000, 30) self.label = Label( "Label1", x=10, y=10, font_name='Gill Sans', font_size=10, color=(250, 250, 250, 255), anchor_x = "left", anchor_y = "baseline", multiline = False ) self.add(self.background, name="background") self.add(self.label, name="label") def on_slideshow_model_update(self, model): print("FileInfoLayer.ON_SLIDESOW_MODEL_UPDATE") self.model = model self.label.element.text = self.text print("FileInfoLayer: %s" % self.text) def on_draw(self): self.background.draw() self.label.draw() @property def text(self): if self.model is None: return '' else: return '%d / %d: %s' % \ (self.model.current_id+1, self.model.total_files, self.model.current_file)
class Skill(): def __init__(self, name, number, hero, model): w, h = director.get_window_size() sc = 1920/w self.name = name self.number = number self.sprite = Sprite(Images.skill_image[self.name], ((1208)//sc, (899 - skill_pos[self.number])//sc), scale = 1/sc) self.skill = Skills[self.name](hero , model) def draw(self): w, h = director.get_window_size() sc = 1920/w if (self.skill.learnt): self.sprite.draw() if self.skill.cd_left: self.label = Label('%d' %self.skill.cd_left, font_name='Times New Roman', font_size=20//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.label.position = ((1208)//sc, (899 - skill_pos[self.number])//sc) self.label.draw() def use(self): if self.skill.available: self.skill.use()
class Hero_portriat_DM(): def reload(self, model, hero_name): w, h = director.get_window_size() sc = 1920/w self.model = model self.name = hero_name if self.name == 'wizard': self.hero = self.model.heroes[self.name] self.sprite = Sprite(Images.hero_icons[self.name], (1600//sc, 900//sc), scale = 1/sc) self.sprite_black = Sprite(Images.hero_icons_black[self.name], (1600//sc, 900//sc), scale = 1/sc) self.label = Label('%d' %self.hero.stats.health, font_name='Times New Roman', font_size=20//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.label.position = 1600//sc, 870//sc if self.name == 'priest': self.hero = self.model.heroes[self.name] self.sprite = Sprite(Images.hero_icons[self.name], (1720//sc, 900//sc), scale = 1/sc) self.sprite_black = Sprite(Images.hero_icons_black[self.name], (1720//sc, 900//sc), scale = 1/sc) self.label = Label('%d' %self.hero.stats.health, font_name='Times New Roman', font_size=20//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.label.position = 1720//sc, 870//sc if self.name == 'warrior': self.hero = self.model.heroes[self.name] self.sprite = Sprite(Images.hero_icons[self.name], (1600//sc, 780//sc), scale = 1/sc) self.sprite_black = Sprite(Images.hero_icons_black[self.name], (1600//sc, 780//sc), scale = 1/sc) self.label = Label('%d' %self.hero.stats.health, font_name='Times New Roman', font_size=20//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.label.position = 1600//sc, 750//sc if self.name == 'rogue': self.hero = self.model.heroes[self.name] self.sprite = Sprite(Images.hero_icons[self.name], (1720//sc, 780//sc), scale = 1/sc) self.sprite_black = Sprite(Images.hero_icons_black[self.name], (1720//sc, 780//sc), scale = 1/sc) self.label = Label('%d' %self.hero.stats.health, font_name='Times New Roman', font_size=20//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.label.position = 1720//sc, 750//sc def draw(self): if self.hero.alive: self.sprite.draw() self.label.font_name = '%d' %self.hero.stats.health self.label.draw() else: self.sprite_black.draw()
class Behavior(object): """A behavior defines the virtual part of a tracked object. A behavior can interact with its environment through 5 entry points that can be overridden : update_tracking -- New data arrives from the tracking subsystem. update -- Update the internal state. draw_background -- Draw the background part in 2D. draw_3d -- Draw the 3D part. draw_foreground -- Draw the foreground part in 2D.""" def __init__(self): """Initialize an empty behavior.""" super(Behavior, self).__init__ self.display_name = None self.display_name_cached = None self.display_name_label = Label("", font_size=8, font_name="Cabin Bold", anchor_x="center", anchor_y="bottom", color=(255, 255, 255, 255)) self.object = None self.observers = collections.defaultdict(list) self.attributes = collections.defaultdict(BehaviorAttribute) self.association = None self.association_selection = None self.association_selection_shaker = ShakeRecognizer.ShakeRecognizer() self.association_name = "Associate" self.association_mode = False def init_attribute(self, name, display_name, range, option_name, default_value, formatter=None): self.attributes[name].display_name = display_name self.attributes[name].range = range if formatter: self.attributes[name].formatter = formatter self.attributes[name].set( director.app.options.setdefault(option_name, default_value)) def max_association_selection_time(self): return director.app.options.setdefault( 'max_association_selection_time', 2) def register_for(self, notification, observer): """Register for a notification type as an observer.""" if not observer in self.observers[notification]: self.observers[notification].append(observer) def unregister_from(self, notification, observer): """Stop observing a notification type.""" try: self.observers[notification].remove(observer) except ValueError: pass def notify_for(self, notification, *args): """Notify observers for something.""" for observer in self.observers[notification]: observer.notification_arrived_for(notification, self, *args) def notification_arrived_for(self, notification, sender, *args): """Handle new notifications.""" if notification == "detached": self.behavior_detached(sender, *args) def behavior_detached(self, sender, *args): """Handle a 'detached' notification.""" if self.association == sender: self.unassociate() def will_detach(self, object): self.object = None self.notify_for("detached") self.has_just_detached() def did_attach(self, object): self.object = object self.has_just_attached(object) def associate(self, behavior): """Start an unilateral association from this behavior to a specified behavior.""" if self.association: unassociate() self.association = behavior behavior.register_for("detached", self) return True def unassociate(self): """Stop being associated with the current associated behavior, if any.""" if not self.association: return self.association.unregister_from("detached", self) self.association = None def has_just_attached(self, object): """Do something when the behavior will cease to be attached to a tracked object.""" pass def has_just_detached(self): """Do something when the behavior will attach to a tracked object.""" pass def has_object(self): """Tell if the behavior is attached to a tracked object.""" return self.object != None def draw_association(self): """Draw a line between the associated behavior's object and this behavior's object.""" if not self.association: return me_color = self.object.color + (0.5 * self.object.alpha(), ) ass_color = self.association.object.color + ( 0.5 * self.association.object.alpha(), ) mecoords, asscoords = self.object.screen_coords( ), self.association.object.screen_coords() glLineWidth(3) glBegin(GL_LINES) glColor4f(*me_color) glVertex2f(mecoords.x, mecoords.y) glColor4f(*ass_color) glVertex2f(asscoords.x, asscoords.y) glEnd() glLineWidth(1) def update_tracking(self): pass def update(self): if not self.association_mode: return new_selection = None min_distance = sys.float_info.max for id in director.app.objects: current = director.app.objects[id] dist = current.translation.distance(self.object.translation) if current != self.object and not current.is_zombie( ) and dist < min_distance: min_distance = dist new_selection = current if self.association_selection != new_selection: # The selection has changed. self.association_selection = new_selection self.association_selection_shaker.reset() else: if self.association_selection and self.association_selection_shaker.update( self.object.raw_translation): # Associate the selection self.association_mode = False self.associate(self.association_selection.behavior) self.association_selection = None def draw_background(self): self.draw_association() if self.display_name != self.display_name_cached: self.display_name_label.element.text = self.display_name self.display_name_cached = self.display_name if self.display_name: self.display_name_label.element.color = (255, 255, 255, int(self.object.alpha() * 255)) coords = self.object.screen_coords() self.display_name_label.position = coords.x, coords.y + 10 self.display_name_label.draw() def draw_3d(self): if self.association_mode and self.association_selection: self.association_selection.model_view_matrix.load_gl() Graphics2d.draw_rect(-1.5, -1.5, 3, 3, (0.0, 0.0, 0.0, self.object.alpha()), filled=False, line_width=2) Graphics2d.draw_rect(-1.5, -1.5, 3, 3, self.object.color + (self.object.alpha() * 0.4, ), filled=True) self.object.model_view_matrix.load_gl() def draw_foreground(self): pass
class HeroStats(Label): def __init__(self, hero): w, h = director.get_window_size() sc = 1920/w self.hero = hero self.health_label = Label('%d' %self.hero.stats.health, font_name='Times New Roman', font_size=28//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.health_label.position = 1300//sc, 315//sc self.exp_label = Label('%d' %self.hero.stats.exp, font_name='Times New Roman', font_size=28//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.exp_label.position = 1300//sc, 155//sc self.luck_label = Label('%d%%' %self.hero.stats.luck, font_name='Times New Roman', font_size=28//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.luck_label.position = 1300//sc, 245//sc self.lvl_label = Label('%d' %self.hero.stats.lvl, font_name='Times New Roman', font_size=18//sc, anchor_x='center', anchor_y='center', color = (0, 0, 0, 255) ) self.lvl_label.position = 1618//sc, 112//sc self.attack_label = Label('%d' %self.hero.stats.attack, font_name='Times New Roman', font_size=28//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255) ) self.attack_label.position = 1550//sc, 315//sc self.armor_label = Label('%d' %self.hero.stats.armor, font_name='Times New Roman', font_size=28//sc, anchor_x='center', anchor_y='center', color = (255, 0, 0, 255)) self.armor_label.position = 1550//sc, 245//sc def draw(self): self.health_label.draw() self.lvl_label.draw() self.exp_label.draw() self.luck_label.draw() self.attack_label.draw() self.armor_label.draw()
class Behavior(object): """A behavior defines the virtual part of a tracked object. A behavior can interact with its environment through 5 entry points that can be overridden : update_tracking -- New data arrives from the tracking subsystem. update -- Update the internal state. draw_background -- Draw the background part in 2D. draw_3d -- Draw the 3D part. draw_foreground -- Draw the foreground part in 2D.""" def __init__(self): """Initialize an empty behavior.""" super(Behavior, self).__init__ self.display_name = None self.display_name_cached = None self.display_name_label = Label("", font_size=8, font_name="Cabin Bold", anchor_x="center", anchor_y="bottom", color=(255, 255, 255, 255)) self.object = None self.observers = collections.defaultdict(list) self.attributes = collections.defaultdict(BehaviorAttribute) self.association = None self.association_selection = None self.association_selection_shaker = ShakeRecognizer.ShakeRecognizer() self.association_name = "Associate" self.association_mode = False def init_attribute(self, name, display_name, range, option_name, default_value, formatter = None): self.attributes[name].display_name = display_name self.attributes[name].range = range if formatter: self.attributes[name].formatter = formatter self.attributes[name].set(director.app.options.setdefault(option_name, default_value)) def max_association_selection_time(self): return director.app.options.setdefault('max_association_selection_time', 2) def register_for(self, notification, observer): """Register for a notification type as an observer.""" if not observer in self.observers[notification]: self.observers[notification].append(observer) def unregister_from(self, notification, observer): """Stop observing a notification type.""" try: self.observers[notification].remove(observer) except ValueError: pass def notify_for(self, notification, *args): """Notify observers for something.""" for observer in self.observers[notification]: observer.notification_arrived_for(notification, self, *args) def notification_arrived_for(self, notification, sender, *args): """Handle new notifications.""" if notification == "detached": self.behavior_detached(sender, *args) def behavior_detached(self, sender, *args): """Handle a 'detached' notification.""" if self.association == sender: self.unassociate() def will_detach(self, object): self.object = None self.notify_for("detached") self.has_just_detached() def did_attach(self, object): self.object = object self.has_just_attached(object) def associate(self, behavior): """Start an unilateral association from this behavior to a specified behavior.""" if self.association: unassociate() self.association = behavior behavior.register_for("detached", self) return True def unassociate(self): """Stop being associated with the current associated behavior, if any.""" if not self.association: return self.association.unregister_from("detached", self) self.association = None def has_just_attached(self, object): """Do something when the behavior will cease to be attached to a tracked object.""" pass def has_just_detached(self): """Do something when the behavior will attach to a tracked object.""" pass def has_object(self): """Tell if the behavior is attached to a tracked object.""" return self.object != None def draw_association(self): """Draw a line between the associated behavior's object and this behavior's object.""" if not self.association: return me_color = self.object.color + (0.5 * self.object.alpha(),) ass_color = self.association.object.color + (0.5 * self.association.object.alpha(),) mecoords, asscoords = self.object.screen_coords(), self.association.object.screen_coords() glLineWidth(3) glBegin(GL_LINES) glColor4f(*me_color) glVertex2f(mecoords.x, mecoords.y) glColor4f(*ass_color) glVertex2f(asscoords.x, asscoords.y) glEnd() glLineWidth(1) def update_tracking(self): pass def update(self): if not self.association_mode: return new_selection = None min_distance = sys.float_info.max for id in director.app.objects: current = director.app.objects[id] dist = current.translation.distance(self.object.translation) if current != self.object and not current.is_zombie() and dist < min_distance: min_distance = dist new_selection = current if self.association_selection != new_selection: # The selection has changed. self.association_selection = new_selection self.association_selection_shaker.reset() else: if self.association_selection and self.association_selection_shaker.update(self.object.raw_translation): # Associate the selection self.association_mode = False self.associate(self.association_selection.behavior) self.association_selection = None def draw_background(self): self.draw_association() if self.display_name != self.display_name_cached: self.display_name_label.element.text = self.display_name self.display_name_cached = self.display_name if self.display_name: self.display_name_label.element.color = (255, 255, 255, int(self.object.alpha() * 255)) coords = self.object.screen_coords() self.display_name_label.position = coords.x, coords.y + 10 self.display_name_label.draw() def draw_3d(self): if self.association_mode and self.association_selection: self.association_selection.model_view_matrix.load_gl() Graphics2d.draw_rect(-1.5, -1.5, 3, 3, (0.0, 0.0, 0.0, self.object.alpha()), filled=False, line_width=2) Graphics2d.draw_rect(-1.5, -1.5, 3, 3, self.object.color + (self.object.alpha() * 0.4,), filled=True) self.object.model_view_matrix.load_gl() def draw_foreground(self): pass