Exemplo n.º 1
0
    def step(self):
        if self.activated:
            zombies = self.player.world.zombie_handler.zombies
            zombies_to_delete = []

            for zombie in zombies:
                player_angle = self.player.angle
                zombie_angle = atan2(zombie.y - self.player.y,
                                     self.player.x - zombie.x)
                zombie_angle_min = zombie_angle - self.knife_range
                zombie_angle_max = zombie_angle + self.knife_range

                if distance((self.player.x, self.player.y), (zombie.x, zombie.y)) < 25.0 \
                        and zombie_angle_min < player_angle < zombie_angle_max:
                    self.player.world.zombie_handler.delete_zombie(zombie)
Exemplo n.º 2
0
    def step(self):
        if self.dist_to_mouse < 0:
            return

        dx = cos(self.angle) * self.speed
        dy = sin(self.angle) * self.speed

        # Check whether we can move anywhere
        if can_move_to(self.x + dx, self.y + dy, self.world.grid):
            self.x += dx
            self.y += dy
        elif can_move_to(self.x + dx, self.y, self.world.grid):
            self.x += dx
        elif can_move_to(self.x, self.y + dy, self.world.grid):
            self.y += dy

        self.dist_to_mouse -= distance((0, 0), (dx, dy))
Exemplo n.º 3
0
    def _spawn_in_tile(self, region):
        dist_to_center = distance(
            region, (Constant.GRID_WIDTH // 2, Constant.GRID_HEIGHT // 2))
        world_x, world_y = region[0] * Constant.TILE_SIZE, region[
            1] * Constant.TILE_SIZE

        if self.grid.grid[region[0]][region[1]].type in {
                CellType.NATURE, CellType.ROAD
        } and dist_to_center > 4:
            zombies = 0
            if int(Constant.AVG_ZOMBIES_PER_TILE_DISTANCE_FROM_CENTER *
                   dist_to_center) > 0:
                zombies = random.randint(
                    0,
                    int(Constant.AVG_ZOMBIES_PER_TILE_DISTANCE_FROM_CENTER *
                        dist_to_center))
            blue_zombie = Constant.BLUE_ZOMBIE_PROB_INCREASE_PER_TILE_DISTANCE_FROM_CENTER * dist_to_center > random.random(
            )
            dog = Constant.DOG_PROB_INCREASE_PER_TILE_DISTANCE_FROM_CENTER * dist_to_center > random.random(
            )

            for _ in range(zombies):
                x, y = int(world_x +
                           random.random() * Constant.TILE_SIZE), int(
                               world_y + random.random() * Constant.TILE_SIZE)
                self.zombie_handler.add_zombie(Zombie(x, y, self))

            if blue_zombie:
                x, y = int(world_x +
                           random.random() * Constant.TILE_SIZE), int(
                               world_y + random.random() * Constant.TILE_SIZE)
                self.zombie_handler.add_zombie(
                    Zombie(x, y, self, is_super_zombie=True))

            if dog:
                x, y = int(world_x +
                           random.random() * Constant.TILE_SIZE), int(
                               world_y + random.random() * Constant.TILE_SIZE)
                self.dog_handler.add_dog(Dog(x, y, self.player, self))
Exemplo n.º 4
0
 def get_loudness_at_position(self, x, y):
     dist = distance((x, y), (self.x, self.y))
     if dist == 0:
         return self.get_loudness()
     return (self.get_loudness()) / dist
Exemplo n.º 5
0
    def step(self):
        currently_playing_fire_sfx = self.playing_fire_sfx
        currently_playing_empty_sfx = self.playing_empty_sfx

        if self.playing_fire_sfx or self.playing_empty_sfx:
            self.flame_sound_emit_counter += 1
            if self.flame_sound_emit_counter % self.flame_sound_emit_counter_max == 0:
                self.player.world.emitter_handler.add_emitter(
                    FlamethrowerEmitter(self.player.x, self.player.y))
                self.flame_sound_emit_counter = 0

        if (self.playing_fire_sfx or self.playing_empty_sfx
            ) and not self.activated and self.flamethrower_sound:
            self.player.world.audio_manager.unload_sfx(self.flamethrower_sound)
            self.flamethrower_sound = None
            self.playing_fire_sfx = False
            self.playing_empty_sfx = False

        if self.activated and self.empty:
            self.playing_empty_sfx = True
            if currently_playing_empty_sfx != self.playing_empty_sfx:
                self.flamethrower_sound = self.player.world.audio_manager.play_sfx(
                    SFX.FLAMETHROWER_EMPTY)
                self.player.world.emitter_handler.add_emitter(
                    FlamethrowerEmitter(self.player.x, self.player.y))

            self.empty = True

        if self.activated and not self.empty:

            self.playing_fire_sfx = True
            if currently_playing_fire_sfx != self.playing_fire_sfx:
                self.flamethrower_sound = self.player.world.audio_manager.play_sfx(
                    SFX.FLAMETHROWER_FIRE)
                self.player.world.emitter_handler.add_emitter(
                    FlamethrowerEmitter(self.player.x, self.player.y))

            self.fuel_left -= 1
            if self.fuel_left < 1:

                if self.playing_fire_sfx:
                    self.player.world.audio_manager.unload_sfx(
                        self.flamethrower_sound)
                    self.playing_fire_sfx = False

                self.playing_empty_sfx = True
                if currently_playing_empty_sfx != self.playing_empty_sfx:
                    self.flamethrower_sound = self.player.world.audio_manager.play_sfx(
                        SFX.FLAMETHROWER_EMPTY)
                self.player.world.emitter_handler.add_emitter(
                    FlamethrowerEmitter(self.player.x, self.player.y))

                self.empty = True

            if not self.empty:
                zombies = self.player.world.zombie_handler.zombies

                for zombie in zombies:
                    player_angle = self.player.angle
                    zombie_angle = atan2(zombie.y - self.player.y,
                                         self.player.x - zombie.x)
                    zombie_angle_min = zombie_angle - self.flamethrower_thickness
                    zombie_angle_max = zombie_angle + self.flamethrower_thickness

                    if distance((self.player.x, self.player.y), (zombie.x, zombie.y)) < 250.0 \
                            and zombie_angle_min < player_angle < zombie_angle_max:
                        self.player.world.zombie_handler.delete_zombie(zombie)
Exemplo n.º 6
0
    def step(self):
        if self.target is not None:
            if distance((self.x, self.y),
                        (self.world.player.x,
                         self.world.player.y)) < self.VISION_RANGE:
                self.state = ZombieState.ATTACKING_PLAYER
                self.target = self.world.player
            elif distance((self.x, self.y),
                          (self.target.x, self.target.y)) > self.VISION_RANGE:
                if self.state == ZombieState.ATTACKING_PLAYER:
                    # We've lost the player, got to last known location until sound overrides this
                    self.target = LostPlayerEntity(self.target.x,
                                                   self.target.y)
                self.state = ZombieState.REACTING_TO_NOISE

            if distance((self.x, self.y),
                        (self.target.x, self.target.y)) < self.max_speed:
                if self.state == ZombieState.ATTACKING_PLAYER:
                    self.speed = 0
                if self.state == ZombieState.REACTING_TO_NOISE:
                    self.state = ZombieState.IDLE
                    self.target = None

                self.angle = random.random() * 2 * pi

        if self.state == ZombieState.IDLE:
            self.angle = self.angle + ((random.random() - 0.5) * 0.05)
            self.speed = self.max_speed * (0.2 + random.random() * 0.1)
        else:
            self.angle = atan2(self.target.y - self.y, self.target.x -
                               self.x) + ((random.random() - 0.5) * 0.05)
            if self.is_super_zombie:
                self.speed = self.max_speed * (3.0 + random.random() * 0.1)
            else:
                self.speed = self.max_speed * (1.0 + random.random() * 0.1)

        dx = cos(self.angle) * self.speed
        dy = sin(self.angle) * self.speed

        # Check whether we can move anywhere
        if can_move_to(self.x + dx, self.y + dy, self.world.grid):
            self.x += dx
            self.y += dy
        elif can_move_to(self.x + dx, self.y, self.world.grid):
            self.x += dx
        elif can_move_to(self.x, self.y + dy, self.world.grid):
            self.y += dy

        if self.state == ZombieState.IDLE and not can_move_to(
                self.x + dx, self.y + dy, self.world.grid):
            self.angle = random.random() * 2 * pi

        # De-spawn the zombie when out of range
        gx, gy = convert_world_to_grid_position(self.x, self.y)
        pgx, pgy = convert_world_to_grid_position(self.world.player.x,
                                                  self.world.player.y)
        if abs(gx - pgx) > Constant.GRID_SPAWN_RANGE or abs(
                gy - pgy) > Constant.GRID_SPAWN_RANGE:
            self.world.zombie_handler.delete_zombie(self)

        self.check_collision()
Exemplo n.º 7
0
 def check_collision(self):
     self.is_colliding = distance(
         (self.world.player.x, self.world.player.y), (self.x, self.y)) <= 50
     if self.is_colliding:
         self.world.player.take_damage(1)
Exemplo n.º 8
0
    def step(self):
        # Get mouse position
        mouse_x, mouse_y = pygame.mouse.get_pos()
        self.angle = atan2(-(Constant.SCREEN_HEIGHT // 2 - mouse_y),
                           Constant.SCREEN_WIDTH // 2 - mouse_x)

        old_movement_type = self.current_movement_type
        speed = Constant.PLAYER_SPEED
        self.current_movement_type = "legs"

        if self.world.inventory.items[self.world.inventory.current_item]:
            if self.world.inventory.items[
                    self.world.inventory.
                    current_item].item_type == InventoryItem.SKATEBOARD:
                self.current_movement_type = "skateboard"
                speed = Constant.PLAYER_SPEED_SKATEBOARD
        player_pos = self.get_grid_position()
        if self.grid.grid[player_pos[0]][
                player_pos[1]].type == CellType.NATURE:
            # If we are on grass we are always slow, also with a skateboard
            speed = Constant.PLAYER_SPEED * Constant.PLAYER_SPEED_GRASS_MULTIPLIER

        if self.held_keys[pygame.K_LSHIFT]:
            speed *= Constant.PLAYER_SPEED_SLOW_WALKING_MULTIPLIER

        # Silly Python has no switch case statement >:-(
        delta_x = 0
        delta_y = 0
        if self.held_keys[pygame.K_w] or self.held_keys[pygame.K_UP]:
            delta_y -= speed
        if self.held_keys[pygame.K_s] or self.held_keys[pygame.K_DOWN]:
            delta_y += speed
        if self.held_keys[pygame.K_a] or self.held_keys[pygame.K_LEFT]:
            delta_x -= speed
        if self.held_keys[pygame.K_d] or self.held_keys[pygame.K_RIGHT]:
            delta_x += speed

        if delta_x != 0 and delta_y != 0:
            delta_x /= sqrt(2)
            delta_y /= sqrt(2)

        new_grid_x = (self.x + delta_x) // Constant.TILE_SIZE
        new_grid_y = (self.y + delta_y) // Constant.TILE_SIZE

        old_moving = self.moving

        if (delta_x != 0 or delta_y != 0) and \
                new_grid_x >= 0 and new_grid_y >= 0 and new_grid_x < Constant.GRID_WIDTH and new_grid_y < Constant.GRID_HEIGHT:
            if self.grid.grid[int(new_grid_x)][self.get_grid_position(
                    True)[1]].type not in [
                        CellType.BUILDING, CellType.DOOMINOS
                    ]:
                self.x += delta_x
            if self.grid.grid[self.get_grid_position(True)[0]][int(
                    new_grid_y)].type not in [
                        CellType.BUILDING, CellType.DOOMINOS
                    ]:
                self.y += delta_y

            self.moving = True
        else:
            self.moving = False
            if self.moving_sound:
                self.moving_sound.stop()
                self.moving_sound = None

        if not old_moving and self.moving:
            if self.world.inventory.items[
                    self.world.inventory.
                    current_item] and self.world.inventory.items[
                        self.world.inventory.
                        current_item].item_type == InventoryItem.SKATEBOARD:
                self.moving_sound = self.audio_manager.play_sfx(SFX.SKATEBOARD)
            else:
                self.moving_sound = self.audio_manager.play_sfx(SFX.FAST_WALK)

        if self.moving and (old_movement_type != self.current_movement_type):
            if self.moving_sound:
                self.moving_sound.stop()
            if self.current_movement_type == "skateboard":
                self.moving_sound = self.audio_manager.play_sfx(SFX.SKATEBOARD)
            elif self.current_movement_type == "legs":
                self.moving_sound = self.audio_manager.play_sfx(SFX.FAST_WALK)

        if not self.moving:
            if self.moving_sound:
                self.moving_sound.stop()
                self.moving_sound = None

        self.step_no += 1

        if self.step_no % 15 == 0 and self.moving:
            self.world.emitter_handler.add_emitter(
                Footstep(self.x, self.y, distance((0, 0), (delta_x, delta_y))))

        self.world.inventory.step()
Exemplo n.º 9
0
    def throw_pizza(self, location):
        player_location_on_screen = Constant.SCREEN_WIDTH / 2, Constant.SCREEN_HEIGHT / 2
        dist_player_to_mouse = distance(player_location_on_screen, location)

        self.world.pizza = Pizza(self.x, self.y, dist_player_to_mouse,
                                 self.angle, self.world)