import pygame
import math
import random

# ================= 核心配置 =================
WIDTH, HEIGHT = 800, 600
FPS = 60
TITLE = "Pygame Dave the Diver"

# 颜色
SKY_BLUE = (135, 206, 235)
DEEP_BLUE = (0, 0, 50)
PLAYER_COLOR = (255, 100, 0)
FISH_COLOR = (0, 200, 255)

# 玩家设置
PLAYER_SPEED = 5
MAX_OXYGEN = 100
OXYGEN_DECAY = 0.05
DEPTH_OXYGEN_MULT = 0.002

# 鱼与网设置
FISH_COUNT = 15
FISH_SPEED = 2
NET_SPEED = 10
NET_RADIUS = 40

# ================= 玩家与捕鱼系统 =================
class Player:
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 30, 40)
        self.oxygen = MAX_OXYGEN
        self.depth = 0
        self.inventory = []
        self.color = PLAYER_COLOR
        self.net_active = False
        self.net_pos = (0, 0)
        self.net_target = (0, 0)

    def update(self, keys, mouse_pos, camera_y):
        dx, dy = 0, 0
        if keys[pygame.K_w]: dy -= PLAYER_SPEED
        if keys[pygame.K_s]: dy += PLAYER_SPEED
        if keys[pygame.K_a]: dx -= PLAYER_SPEED
        if keys[pygame.K_d]: dx += PLAYER_SPEED
        
        self.rect.x += dx
        self.rect.y += dy
        self.rect.clamp_ip(pygame.Rect(0, 0, WIDTH, HEIGHT))
        
        real_y = -camera_y + self.rect.y
        self.depth = max(0, real_y // 10)
        
        move_penalty = 0.1 if (dx != 0 or dy != 0) else 0
        depth_penalty = self.depth * DEPTH_OXYGEN_MULT
        self.oxygen -= (OXYGEN_DECAY + depth_penalty + move_penalty)
        self.oxygen = max(0, self.oxygen)

        if self.net_active:
            nx, ny = self.net_pos
            tx, ty = self.net_target
            dist = math.hypot(tx - nx, ty - ny)
            if dist < 10:
                self.net_active = False
            else:
                dx_net = (tx - nx) / dist * NET_SPEED
                dy_net = (ty - ny) / dist * NET_SPEED
                self.net_pos = (nx + dx_net, ny + dy_net)

    def throw_net(self, screen_mouse_pos, camera_y):
        if self.net_active: return
        world_x = screen_mouse_pos[0]
        world_y = screen_mouse_pos[1] - camera_y
        self.net_target = (world_x, world_y)
        self.net_pos = (self.rect.centerx, self.rect.centery - camera_y)
        self.net_active = True

    def draw(self, screen, camera_y):
        pygame.draw.rect(screen, self.color, self.rect)
        if self.net_active:
            nx, ny = self.net_pos
            screen_y = ny + camera_y
            pygame.draw.circle(screen, (255, 255, 255), (int(nx), int(screen_y)), NET_RADIUS, 2)
            pygame.draw.line(screen, (200, 200, 200), self.rect.center, (int(nx), int(screen_y)), 2)

        bar_width = 200
        fill = (self.oxygen / MAX_OXYGEN) * bar_width
        pygame.draw.rect(screen, (50, 50, 50), (10, 10, bar_width, 20))
        pygame.draw.rect(screen, (0, 200, 255), (10, 10, fill, 20))
        
        font = pygame.font.Font(None, 30)
        text = font.render(f"Depth: {self.depth}m | O2: {int(self.oxygen)}% | Fish: {len(self.inventory)}", True, (255, 255, 255))
        screen.blit(text, (10, 40))

# ================= 鱼群 AI =================
class Fish:
    def __init__(self, x, y):
        self.rect = pygame.Rect(x, y, 20, 15)
        self.vel_x = random.choice([-1, 1]) * FISH_SPEED
        self.vel_y = random.uniform(-0.5, 0.5)
        self.color = FISH_COLOR
        self.state = "patrol"
        self.flee_timer = 0

    def update(self, player_rect, net_pos, net_active, camera_y):
        if net_active:
            dist_net = math.hypot(self.rect.centerx - net_pos[0], self.rect.centery - net_pos[1])
            if dist_net < NET_RADIUS * 2:
                self.state = "flee"
                self.flee_timer = 60
        
        if self.state == "flee":
            dx = self.rect.centerx - player_rect.centerx
            dy = self.rect.centery - player_rect.centery
            dist = math.hypot(dx, dy) + 0.1
            self.vel_x = (dx / dist) * FISH_SPEED * 2
            self.vel_y = (dy / dist) * FISH_SPEED * 2
            self.flee_timer -= 1
            if self.flee_timer <= 0:
                self.state = "patrol"
        else:
            if random.random() < 0.02: self.vel_x *= -1
            if random.random() < 0.01: self.vel_y = random.uniform(-0.5, 0.5)

        self.rect.x += self.vel_x
        self.rect.y += self.vel_y
        
        if self.rect.left < 0 or self.rect.right > WIDTH: self.vel_x *= -1
        if self.rect.top < 0 or self.rect.bottom > HEIGHT: self.vel_y *= -1

    def draw(self, screen, camera_y):
        screen_y = self.rect.y + camera_y
        screen_rect = pygame.Rect(self.rect.x, screen_y, self.rect.width, self.rect.height)
        pygame.draw.rect(screen, self.color, screen_rect)
        eye_x = screen_rect.x + (15 if self.vel_x > 0 else 5)
        pygame.draw.circle(screen, (0, 0, 0), (eye_x, screen_rect.y + 5), 2)

# ================= 主循环与海洋背景 =================
class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption(TITLE)
        self.clock = pygame.time.Clock()
        
        self.player = Player(WIDTH//2, HEIGHT//2)
        self.fishes = [Fish(random.randint(0, WIDTH), random.randint(0, HEIGHT)) for _ in range(FISH_COUNT)]
        self.camera_y = 0

    def run(self):
        running = True
        while running:
            self.clock.tick(FPS)
            self._handle_events()
            self._update()
            self._draw()
        pygame.quit()

    def _handle_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT: return
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                self.player.throw_net(pygame.mouse.get_pos(), self.camera_y)

    def _update(self):
        keys = pygame.key.get_pressed()
        mouse_pos = pygame.mouse.get_pos()
        self.player.update(keys, mouse_pos, self.camera_y)
        
        target_cam_y = -self.player.rect.centery + HEIGHT // 2
        self.camera_y += (target_cam_y - self.camera_y) * 0.1
        
        net_pos = self.player.net_pos if self.player.net_active else (0, 0)
        for fish in self.fishes:
            fish.update(self.player.rect, net_pos, self.player.net_active, self.camera_y)
            
        if self.player.net_active:
            nx, ny = self.player.net_pos
            dist = ((self.player.rect.centerx - nx)**2 + (self.player.rect.centery - ny)**2)**0.5
            if dist < 20:
                for fish in self.fishes[:]:
                    if fish.rect.colliderect(pygame.Rect(nx-NET_RADIUS, ny-NET_RADIUS, NET_RADIUS*2, NET_RADIUS*2)):
                        self.player.inventory.append("Fish")
                        self.fishes.remove(fish)
                        self.fishes.append(Fish(random.randint(0, WIDTH), random.randint(0, HEIGHT)))

    def _draw(self):
        depth_ratio = min(1.0, self.player.depth / 500.0)
        r = int(SKY_BLUE[0] * (1 - depth_ratio))
        g = int(SKY_BLUE[1] * (1 - depth_ratio))
        b = int(SKY_BLUE[2] * (1 - depth_ratio) + DEEP_BLUE[2] * depth_ratio)
        self.screen.fill((r, g, b))

        for fish in self.fishes:
            fish.draw(self.screen, self.camera_y)
        self.player.draw(self.screen, self.camera_y)

        font = pygame.font.Font(None, 24)
        text = font.render("WASD: Move | LClick: Net | Fish: Catch", True, (255, 255, 255))
        self.screen.blit(text, (10, HEIGHT - 30))

        pygame.display.flip()

if __name__ == "__main__":
    Game().run()