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 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
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)
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)
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)
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)
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)
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)
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)
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_offset is None: rot = Game.rotate_point(self.collision_offset[0], self.collision_offset[1], self.rotation) point = (point[0] + rot[0], point[1] + rot[1]) return point
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._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()
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] if not self.collision_offset is None: x += self.collision_offset[0] y += self.collision_offset[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
def get_distance_squared(self, pos): return Game.get_distance_squared((self.x, self.y), pos)
def get_distance(self, pos): return Game.get_distance((self.x, self.y), pos)
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)