import pygame
from pygame.math import Vector3
from OpenGL.GL import *
from OpenGL.GLU import *
import random
import math

# 1. 初始化 Pygame 和 OpenGL 界面
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, pygame.DOUBLEBUF | pygame.OPENGL)
pygame.display.set_caption("Python 3D 飞行模拟器 - 巨大岛屿与降落版")

# OpenGL 全局渲染状态初始化
glClearColor(0.5, 0.7, 1.0, 1.0)   # 天空蓝背景，RGBA 四通道
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glLightfv(GL_LIGHT0, GL_POSITION, (1.0, 1.0, 1.0, 0.0))

# 2. 定义飞行器的初始状态
position = Vector3(0, 30, 80)
velocity = Vector3(0, 0, 0)
is_landing = False

# 3. 随机生成环境元素的数据
# 生成 150 棵树木 (避开中央停机坪)
trees = []
for _ in range(150):
    tx = random.uniform(-180, 180)
    tz = random.uniform(-180, 180)
    if math.sqrt(tx**2 + tz**2) > 25:
        trees.append((tx, tz, random.uniform(4, 8), random.uniform(3, 6)))

# 【修复】补全山脉与云朵的随机生成逻辑
mountains = [(random.uniform(-200, 200), random.uniform(-200, 200), random.uniform(10, 25)) for _ in range(60)]
clouds = [(random.uniform(-150, 150), random.uniform(40, 60), random.uniform(-150, 150)) for _ in range(40)]

# 4. 物理与降落模拟函数
def apply_physics(dt):
    global position, velocity, is_landing

    if is_landing:
        target_y = 0
        position.y += (target_y - position.y) * 2.0 * dt
        position.x += (0 - position.x) * 1.0 * dt
        position.z += (0 - position.z) * 1.0 * dt
        velocity = Vector3(0, 0, 0)
        if position.y < 0.1:
            position.y = 0
            is_landing = False
    else:
        gravity = Vector3(0, -9.8, 0)
        drag = -velocity * 0.1
        force = gravity + drag
        velocity += force * dt
        position += velocity * dt

        dist_from_center = math.sqrt(position.x**2 + position.z**2)
        if dist_from_center < 200 and position.y < 0:
            position.y = 0
            velocity.y = 0

# 5. 绘制场景元素
def draw_ocean():
    glColor3f(0.1, 0.4, 0.8)
    glBegin(GL_QUADS)
    glVertex3f(-500, -1, 500); glVertex3f(500, -1, 500)
    glVertex3f(500, -1, -500); glVertex3f(-500, -1, -500)
    glEnd()

def draw_island():
    glColor3f(0.3, 0.7, 0.3)
    glBegin(GL_POLYGON)
    for i in range(360):
        angle = math.radians(i)
        glVertex3f(200 * math.cos(angle), 0, 200 * math.sin(angle))
    glEnd()

    glColor3f(0.4, 0.4, 0.4)
    glBegin(GL_POLYGON)
    for i in range(360):
        angle = math.radians(i)
        glVertex3f(20 * math.cos(angle), 0.1, 20 * math.sin(angle))
    glEnd()

def draw_trees():
    for x, z, trunk_h, crown_size in trees:
        glColor3f(0.4, 0.25, 0.1)
        glBegin(GL_QUADS)
        glVertex3f(x-0.5, 0, z-0.5); glVertex3f(x+0.5, 0, z-0.5)
        glVertex3f(x+0.5, trunk_h, z-0.5); glVertex3f(x-0.5, trunk_h, z-0.5)
        glVertex3f(x+0.5, 0, z+0.5); glVertex3f(x-0.5, 0, z+0.5)
        glVertex3f(x-0.5, trunk_h, z+0.5); glVertex3f(x+0.5, trunk_h, z+0.5)
        glEnd()

        glColor3f(0.1, 0.6, 0.1)
        glBegin(GL_TRIANGLES)
        glVertex3f(x-crown_size, trunk_h, z-crown_size); glVertex3f(x+crown_size, trunk_h, z-crown_size); glVertex3f(x, trunk_h+crown_size*1.5, z)
        glVertex3f(x+crown_size, trunk_h, z-crown_size); glVertex3f(x+crown_size, trunk_h, z+crown_size); glVertex3f(x, trunk_h+crown_size*1.5, z)
        glVertex3f(x+crown_size, trunk_h, z+crown_size); glVertex3f(x-crown_size, trunk_h, z+crown_size); glVertex3f(x, trunk_h+crown_size*1.5, z)
        glVertex3f(x-crown_size, trunk_h, z+crown_size); glVertex3f(x-crown_size, trunk_h, z-crown_size); glVertex3f(x, trunk_h+crown_size*1.5, z)
        glEnd()

def draw_mountains():
    glColor3f(0.5, 0.4, 0.3)
    for x, z, height in mountains:
        if math.sqrt(x**2 + z**2) < 190:
            glBegin(GL_TRIANGLES)
            glVertex3f(x - 8, 0, z - 8); glVertex3f(x + 8, 0, z - 8); glVertex3f(x, height, z)
            glVertex3f(x + 8, 0, z - 8); glVertex3f(x + 8, 0, z + 8); glVertex3f(x, height, z)
            glVertex3f(x + 8, 0, z + 8); glVertex3f(x - 8, 0, z + 8); glVertex3f(x, height, z)
            glVertex3f(x - 8, 0, z + 8); glVertex3f(x - 8, 0, z - 8); glVertex3f(x, height, z)
            glEnd()

def draw_clouds():
    glColor3f(1.0, 1.0, 1.0)
    for x, y, z in clouds:
        glBegin(GL_QUADS)
        glVertex3f(x - 4, y, z); glVertex3f(x + 4, y, z); glVertex3f(x + 4, y + 2, z); glVertex3f(x - 4, y + 2, z)
        glVertex3f(x - 2, y + 2, z - 2); glVertex3f(x + 2, y + 2, z - 2); glVertex3f(x + 2, y + 4, z - 2); glVertex3f(x - 2, y + 4, z - 2)
        glEnd()

def draw_player():
    glPushMatrix()
    glTranslatef(position.x, position.y + 1, position.z)
    glBegin(GL_TRIANGLES)
    glColor3f(0.2, 0.6, 1.0)
    glVertex3f(0, 0, -1); glVertex3f(-0.5, 0, 1); glVertex3f(0.5, 0, 1)
    glColor3f(1.0, 0.3, 0.3)
    glVertex3f(0, 0, 0); glVertex3f(-1.5, 0, 0.5); glVertex3f(0, 0, 1)
    glVertex3f(0, 0, 0); glVertex3f(1.5, 0, 0.5); glVertex3f(0, 0, 1)
    glEnd()
    glPopMatrix()

# 6. 主游戏循环
clock = pygame.time.Clock()
running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_1:
            is_landing = True

    if not is_landing:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:  velocity.x -= 0.5
        if keys[pygame.K_RIGHT]: velocity.x += 0.5
        if keys[pygame.K_UP]:    velocity.z -= 0.5
        if keys[pygame.K_DOWN]:  velocity.z += 0.5
        if keys[pygame.K_SPACE]: velocity.y += 2.0

    dt = clock.tick(60) / 1000.0
    apply_physics(dt)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, (display[0] / display[1]), 0.1, 500.0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(position.x, position.y + 5, position.z + 15,
              position.x, position.y, position.z,
              0, 1, 0)

    draw_ocean()
    draw_island()
    draw_mountains()
    draw_trees()
    draw_clouds()
    draw_player()

    pygame.display.flip()

pygame.quit()