def react_to_collision(self): if not self.should_die: if len(self.collision_rect.collided_shapes_this_frame) > 0: self.should_die = True for shape in self.collision_rect.collided_shapes_this_frame: if shape.game_type == GameCollisionType.MONSTER: shape.owner.take_damage(Damage(self.damage, DamageType.PHYSICAL))
def __init__(self, start_pos, explosion_sheet, size, damage_amount, damage_type): self.radius = size self.collide_radius = self.radius self.explosion_sheet = explosion_sheet self.explosion_frames = 16 self.explosion_images = [] random_explosion_int = random.randrange(0, 512, 64) for i in range(0, self.explosion_frames): x_start_index = (i * 64) explosion_frame = self.explosion_sheet.subsurface( pygame.Rect(x_start_index + 1, random_explosion_int + 1, 62, 62)) explosion_frame = pygame.transform.scale( explosion_frame, (self.radius * 2, self.radius * 2)) self.explosion_images.append(explosion_frame) self.sprite = pygame.sprite.Sprite() self.sprite.image = self.explosion_images[0] self.sprite.rect = self.explosion_images[0].get_rect() self.sprite.rect.center = start_pos self.position = [ float(self.sprite.rect.center[0]), float(self.sprite.rect.center[1]) ] self.should_die = False self.life_time = 0.45 self.time = self.life_time self.frame_time = self.life_time / self.explosion_frames self.frame = 1 self.damage = Damage(damage_amount, damage_type)
def update_movement_and_collision(self, monsters, time_delta, new_explosions, explosions): if self.target is not None and not self.target.should_die: self.damage_acc += time_delta if self.damage_acc > self.time_to_tick: self.damage_acc = 0.0 self.target.take_damage( Damage(self.damage_per_tick, DamageType.LASER))
def update_movement_and_collision(self, collideable_tiles, player, monsters, time_delta, tiled_level, new_explosions, explosions): for monster in monsters: if self.owner != monster: if monster.test_projectile_collision(self.sprite.rect): monster.take_damage(Damage(self.damage, 'PHYSICAL')) self.should_die = True if self.owner != player: if player.test_projectile_collision(self.sprite.rect): player.take_damage(Damage(self.damage, 'PHYSICAL')) self.should_die = True self.shot_range -= time_delta * self.bullet_speed self.world_position[0] += (self.current_vector[0] * time_delta * self.bullet_speed) self.world_position[1] += (self.current_vector[1] * time_delta * self.bullet_speed) self.position[ 0] = self.world_position[0] - tiled_level.position_offset[0] self.position[ 1] = self.world_position[1] - tiled_level.position_offset[1] self.sprite.rect.center = self.position if self.shot_range <= 0.0: self.should_die = True # calc facing angle direction_magnitude = math.sqrt(self.current_vector[0]**2 + self.current_vector[1]**2) unit_dir_vector = [0, 0] if direction_magnitude > 0.0: unit_dir_vector = [ self.current_vector[0] / direction_magnitude, self.current_vector[1] / direction_magnitude ] facing_angle = math.atan2(-unit_dir_vector[0], -unit_dir_vector[1]) * 180 / math.pi bullet_centre_position = self.sprite.rect.center self.image = pygame.transform.rotate(self.original_image, facing_angle) self.sprite.rect = self.image.get_rect() self.sprite.rect.center = bullet_centre_position
def __init__(self, start_pos, explosion_sheet, size, damage_amount, damage_type, collision_grid, *groups): super().__init__(*groups) self.collision_grid = collision_grid self.radius = size self.collide_radius = self.radius self.explosion_sheet = explosion_sheet self.explosion_frames = 16 self.explosion_images = [] random_explosion_int = random.randrange(0, 512, 64) for i in range(0, self.explosion_frames): x_start_index = (i * 64) explosion_frame = self.explosion_sheet.subsurface( pygame.Rect(x_start_index + 1, random_explosion_int + 1, 62, 62)) explosion_frame = pygame.transform.scale( explosion_frame, (self.radius * 2, self.radius * 2)) self.explosion_images.append(explosion_frame) self.image = self.explosion_images[0] self.rect = self.explosion_images[0].get_rect() self.rect.center = start_pos self.position = [ float(self.rect.center[0]), float(self.rect.center[1]) ] # handle collision shape setup self.collision_grid = collision_grid handlers_by_type = { GameCollisionType.MONSTER: self.collision_grid.no_handler } self.collision_shape = CollisionCircle(self.position[0], self.position[1], self.collide_radius, handlers_by_type, GameCollisionType.EXPLOSION, [GameCollisionType.MONSTER]) self.collision_shape.set_owner(self) self.collision_grid.add_new_shape_to_grid(self.collision_shape) self.should_die = False self.life_time = 0.45 self.time = self.life_time self.frame_time = self.life_time / self.explosion_frames self.frame = 1 self.damage = Damage(damage_amount, damage_type) self.should_kill_explosion_collision = False self.has_collision = True
def fire(self, monsters: list): self.deduct_mana_cost() if len(monsters) > 0: for monster in monsters: # check if the closest monster is in range of our spell if monster.player_distance <= self.magic_range: # if in range blast the monster with damage... monster.take_damage(Damage(self.magic_damage, DamageType.MAGIC)) # ...then fling it away from the player. x_dist = float(monster.position[0]) - float(self.player_position[0]) y_dist = float(monster.position[1]) - float(self.player_position[1]) away_from_player_dir = [(x_dist/monster.player_distance) * self.distance_to_fling_per_second, (y_dist/monster.player_distance) * self.distance_to_fling_per_second] monster.fling_monster(away_from_player_dir, self.time_to_fling)