Example #1
0
    def collision_rectangle_calculate_corners(self):
        """This method is used as an optimisation for recangle collisions.
        We store the location of corners and only do it either once per frame
        or if a relevant value has changed. (x/y/rotation/scale)

        Returns a dictionary containing four tuples, 'ul', 'ur', 'll', 'lr'.
        The tuples are coordinates pointing to the four corners of the rotated and
        scaled rectangle (upper left, upper right, lower left and lower right)"""
        if not self._collision_rectangle_recalculate_corners:
            return self._collision_rectangle_calculated_corners

        # Determine the size of the rectangle to use
        width, height = self.collision_rectangle_size()

        # Rotate each point of the rectangle as the Entitiy is to calculate
        # it's true position.
        rot = Game.rotate_point(0, 0, self.rotation)
        self._collision_rectangle_calculated_corners['ul'] = float(self.x + rot[0]), float(self.y + rot[1])
        rot = Game.rotate_point(width, 0, self.rotation)
        self._collision_rectangle_calculated_corners['ur'] = float(self.x + rot[0]), float(self.y + rot[1])
        rot = Game.rotate_point(0, height, self.rotation)
        self._collision_rectangle_calculated_corners['ll'] = float(self.x + rot[0]), float(self.y + rot[1])
        rot = Game.rotate_point(width, height, self.rotation)
        self._collision_rectangle_calculated_corners['lr'] = float(self.x + rot[0]), float(self.y + rot[1])

        # Flag so we don't do this more than we need to.
        self._collision_rectangle_recalculate_corners = False

        return self._collision_rectangle_calculated_corners
Example #2
0
    def execute(self, colour_from, colour_to, blocking, pos, size, z, callback):
        self.colour_from = colour_from
        self.colour_to = colour_to
        self.blocking = blocking
        self.x, self.y = pos
        self.width, self.height = size
        self.z = z
        self.callback = callback

        while True:
            if self.fading:
                for frame,total in self.fade_speed:
                    self.current_colour = (
                        Game.lerp(self.current_colour_from[0], self.current_colour_to[0], frame / total),
                        Game.lerp(self.current_colour_from[1], self.current_colour_to[1], frame / total),
                        Game.lerp(self.current_colour_from[2], self.current_colour_to[2], frame / total),
                        Game.lerp(self.current_colour_from[3], self.current_colour_to[3], frame / total)
                        )
                    yield
                if self.blocking:
                    Game.disable_entity_execution = False
                if not self.callback is None:
                    self.callback()
                    self.callback = None
                if self.kill_after_fade:
                    Game.screen_overlay = None
                    self.destroy()
                self.fading = False
            yield
Example #3
0
    def show(self, tree = False):
        """Start this Entity rendering again if previously stopped.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be shown too. (default False)
        """
        Game.show_entities(self, tree = tree)
Example #4
0
    def show(self, tree=False):
        """Start this Entity rendering again if previously stopped.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be shown too. (default False)
        """
        Game.show_entities(self, tree=tree)
Example #5
0
    def start_executing(self, tree=False):
        """Start this Entity executing code if previously stopped.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be started too. (default False)
        """
        Game.start_entities_executing(self, tree=tree)
Example #6
0
    def start_executing(self, tree = False):
        """Start this Entity executing code if previously stopped.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be started too. (default False)
        """
        Game.start_entities_executing(self, tree = tree)
Example #7
0
    def toggle_executing(self, tree = False):
        """Toggle the execution of this Entity. If started it will be stopped
        and vise-versa.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be toggled too. (default False)
        """
        Game.toggle_entities_executing(self, tree = tree)
Example #8
0
    def toggle_display(self, tree=False):
        """Toggle the rendering of this Entity. If hidden it will be shown
        and vise-versa.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be toggled too. (default False)
        """
        Game.toggle_entities_display(self, tree=tree)
Example #9
0
    def hide(self, tree=False):
        """Stops this Entity rendering. Can be set to draw again with show or
        toggle_display.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be hidden too. (default False)
        """
        Game.hide_entities(self, tree=tree)
Example #10
0
    def toggle_executing(self, tree=False):
        """Toggle the execution of this Entity. If started it will be stopped
        and vise-versa.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be toggled too. (default False)
        """
        Game.toggle_entities_executing(self, tree=tree)
Example #11
0
    def stop_executing(self, tree=False):
        """Stops this Entity executing code. Can be woke up again with start_executing
        or toggle_executing.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be stopped too. (default False)
        """
        Game.stop_entities_executing(self, tree=tree)
Example #12
0
    def hide(self, tree = False):
        """Stops this Entity rendering. Can be set to draw again with show or
        toggle_display.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be hidden too. (default False)
        """
        Game.hide_entities(self, tree = tree)
Example #13
0
    def stop_executing(self, tree = False):
        """Stops this Entity executing code. Can be woke up again with start_executing
        or toggle_executing.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be stopped too. (default False)
        """
        Game.stop_entities_executing(self, tree = tree)
Example #14
0
    def destroy(self, tree = False):
        """Kills this Entity, stopping it from executing and displaying and
        irreversibly destroying it.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be destroyed too. (default False)
        """
        Game.destroy_entities(self, tree = tree)
Example #15
0
    def destroy(self, tree=False):
        """Kills this Entity, stopping it from executing and displaying and
        irreversibly destroying it.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be destroyed too. (default False)
        """
        Game.destroy_entities(self, tree=tree)
Example #16
0
    def toggle_display(self, tree = False):
        """Toggle the rendering of this Entity. If hidden it will be shown
        and vise-versa.

        Keyword arguments:
        -- tree: If True then all children of this Entity (and their children
          etc) will be toggled too. (default False)
        """
        Game.toggle_entities_display(self, tree = tree)
Example #17
0
 def collision_point_calculate_point(self):
     """Returns the coordinates of the collision point to use, taking into account
     any offset assigned to collision_point_offset.
     """
     point = (self.x, self.y)
     if not self.collision_point_offset is None:
         rot = Game.rotate_point(self.collision_point_offset[0], self.collision_point_offset[1], self.rotation)
         point = (point[0] + rot[0], point[1] + rot[1])
     return point
Example #18
0
 def collision_point_calculate_point(self):
     """Returns the coordinates of the collision point to use, taking into account
     any offset assigned to collision_point_offset.
     """
     point = (self.x, self.y)
     if not self.collision_point_offset is None:
         rot = Game.rotate_point(self.collision_point_offset[0],
                                 self.collision_point_offset[1],
                                 self.rotation)
         point = (point[0] + rot[0], point[1] + rot[1])
     return point
Example #19
0
    def __init__(self, *args, **kwargs):
        if not Game.started:
            Game.first_registered_entity = self
            Game.start_game()
        else:
            Game.entity_register(self)

        self._collision_rectangle_calculated_corners = {'ul' : (0.0, 0.0), 'ur' : (0.0, 0.0), 'll' : (0.0, 0.0), 'lr' : (0.0, 0.0)}
        self._state_list = {}
        self._state_generators = {}

        self.add_state(self.execute, *args, **kwargs)

        Game.remember_current_entity_executing.append(Game.current_entity_executing)
        Game.current_entity_executing = self
        self._executing = True
        self._drawing = True
        self._iterate_generator()
        Game.current_entity_executing = Game.remember_current_entity_executing.pop()

        for x in self._module_list:
            x._module_setup(self)

        if not Game.started:
            Game.started = True
            Game.run_game()
Example #20
0
    def __init__(self, *args, **kargs):
        if not Game.started:
            Game.start_game()

        Game.entity_register(self)

        self.z = 0.0
        self.x = 0.0
        self.y = 0.0
        self.priority = 0
        self._collision_rectangle_calculated_corners = {'ul' : (0.0, 0.0), 'ur' : (0.0, 0.0), 'll' : (0.0, 0.0), 'lr' : (0.0, 0.0)}

        Game.remember_current_entity_executing.append(Game.current_entity_executing)
        Game.current_entity_executing = self
        self._executing = True
        self._drawing = True        
        self._generator = self.execute(*args, **kargs)
        self._iterate_generator()
        Game.current_entity_executing = Game.remember_current_entity_executing.pop()

        for x in self._module_list:
            x._module_setup(self)
            
        if not Game.started:
            Game.started = True             
            Game.run_game()
Example #21
0
    def __init__(self, *args, **kwargs):
        if not Game.started:
            Game.first_registered_entity = self
            Game.start_game()
        else:
            Game.entity_register(self)

        self._collision_rectangle_calculated_corners = {
            'ul': (0.0, 0.0),
            'ur': (0.0, 0.0),
            'll': (0.0, 0.0),
            'lr': (0.0, 0.0)
        }
        self._state_list = {}
        self._state_generators = {}

        self.add_state(self.execute, *args, **kwargs)

        Game.remember_current_entity_executing.append(
            Game.current_entity_executing)
        Game.current_entity_executing = self
        self._executing = True
        self._drawing = True
        self._iterate_generator()
        Game.current_entity_executing = Game.remember_current_entity_executing.pop(
        )

        for x in self._module_list:
            x._module_setup(self)

        if not Game.started:
            Game.started = True
            Game.run_game()
Example #22
0
    def execute(self, colour_from, colour_to, blocking, pos, size, z,
                callback):
        self.colour_from = colour_from
        self.colour_to = colour_to
        self.blocking = blocking
        self.x, self.y = pos
        self.width, self.height = size
        self.z = z
        self.callback = callback

        while True:
            if self.fading:
                for frame, total in self.fade_speed:
                    self.current_colour = (Game.lerp(
                        self.current_colour_from[0], self.current_colour_to[0],
                        frame / total),
                                           Game.lerp(
                                               self.current_colour_from[1],
                                               self.current_colour_to[1],
                                               frame / total),
                                           Game.lerp(
                                               self.current_colour_from[2],
                                               self.current_colour_to[2],
                                               frame / total),
                                           Game.lerp(
                                               self.current_colour_from[3],
                                               self.current_colour_to[3],
                                               frame / total))
                    yield
                if self.blocking:
                    Game.disable_entity_execution = False
                if not self.callback is None:
                    self.callback()
                    self.callback = None
                if self.kill_after_fade:
                    Game.screen_overlay = None
                    self.destroy()
                self.fading = False
            yield
Example #23
0
    def collision_rectangle_calculate_corners(self):
        """This method is used as an optimisation for recangle collisions.
        We store the location of corners and only do it either once per frame
        or if a relevant value has changed. (x/y/rotation/scale/centre_point)

        Returns a dictionary containing four tuples, 'ul', 'ur', 'll', 'lr'.
        The tuples are coordinates pointing to the four corners of the rotated and
        scaled rectangle (upper left, upper right, lower left and lower right)"""
        if not self._collision_rectangle_recalculate_corners:
            return self._collision_rectangle_calculated_corners

        # Determine the size of the rectangle to use
        width, height = self.collision_rectangle_size()

        # Get the real x/y
        centre = self.get_centre_point()
        x = self.x - centre[0]
        y = self.y - centre[1]

        # Rotate each point of the rectangle as the Entitiy is to calculate
        # it's true position.
        rot = Game.rotate_point(0, 0, self.rotation)
        self._collision_rectangle_calculated_corners['ul'] = float(
            x + rot[0]), float(y + rot[1])
        rot = Game.rotate_point(width, 0, self.rotation)
        self._collision_rectangle_calculated_corners['ur'] = float(
            x + rot[0]), float(y + rot[1])
        rot = Game.rotate_point(0, height, self.rotation)
        self._collision_rectangle_calculated_corners['ll'] = float(
            x + rot[0]), float(y + rot[1])
        rot = Game.rotate_point(width, height, self.rotation)
        self._collision_rectangle_calculated_corners['lr'] = float(
            x + rot[0]), float(y + rot[1])

        # Flag so we don't do this more than we need to.
        self._collision_rectangle_recalculate_corners = False

        return self._collision_rectangle_calculated_corners
Example #24
0
 def get_distance_squared(self, pos):
     return Game.get_distance_squared((self.x, self.y), pos)
Example #25
0
 def get_distance(self, pos):
     return Game.get_distance((self.x, self.y), pos)
Example #26
0
 def move_forward(self, distance, angle = None):
     self.x, self.y = Game.move_forward((self.x, self.y), distance, self.rotation if angle == None else angle)
 def move_forward(self, distance, angle=None):
     self.x, self.y = Game.move_forward(
         (self.x, self.y), distance,
         self.rotation if angle == None else angle)
 def get_distance(self, pos):
     return Game.get_distance((self.x, self.y), pos)