import pygame
import random
import os

# ====================== 基础设置 ======================
WIDTH = 900
HEIGHT = 650
FPS = 60
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("沙威玛传奇 Python复刻")
clock = pygame.time.Clock()

# 颜色
BG_COLOR = (30, 30, 30)
TABLE = (130, 90, 50)
GRILL = (50, 50, 50)
MEAT_RAW = (150, 60, 30)
MEAT_COOK = (200, 110, 40)
BREAD = (235, 195, 130)
CUSTOMER = (60, 120, 180)
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (40,180,40)
RED = (220,30,30)
GOLD = (255, 210, 0)

# =========【修复字体】兼容海龟编辑器！不再用simhei =========
try:
    font_small = pygame.font.SysFont(["Microsoft YaHei", "SimHei", "Arial"],18)
    font_mid = pygame.font.SysFont(["Microsoft YaHei", "SimHei", "Arial"],26)
    font_big = pygame.font.SysFont(["Microsoft YaHei", "SimHei", "Arial"],36)
except:
    # 终极兜底：系统默认字体
    font_small = pygame.font.Font(None,18)
    font_mid = pygame.font.Font(None,26)
    font_big = pygame.font.Font(None,36)

# ====================== 顾客类 ======================
class Customer:
    def __init__(self):
        self.x = WIDTH + 30
        self.y = 380
        self.speed = 1.3
        self.target_x = 640
        self.need_meat = random.randint(1,5)
        self.patience_max = 2500
        self.patience = self.patience_max
        self.waiting = False
        self.leave = False
        self.complete = False

    def update(self):
        if self.leave:
            self.x += 2
            return
        if not self.waiting:
            if self.x > self.target_x:
                self.x -= self.speed
            else:
                self.waiting = True
        else:
            self.patience -= 1
            if self.patience <= 0:
                self.leave = True

    def draw(self):
        # 人物
        pygame.draw.rect(screen, CUSTOMER, (self.x, self.y, 46,72))
        pygame.draw.circle(screen, (255,224,210), (self.x+23, self.y-8),19)
        if self.waiting and not self.complete:
            # 订单气泡
            pygame.draw.rect(screen, WHITE, (self.x-10, self.y-64, 92,38))
            txt = font_small.render(f"需要 {self.need_meat} 片肉", True, BLACK)
            screen.blit(txt, (self.x, self.y-60))
            # 耐心条
            rate = max(0, self.patience / self.patience_max)
            bar_color = GREEN if rate>0.35 else RED
            pygame.draw.rect(screen, BLACK, (self.x, self.y-14,46,7))
            pygame.draw.rect(screen, bar_color, (self.x, self.y-14,46*rate,7))

    def check_food(self, have):
        if have >= self.need_meat and not self.complete:
            self.complete = True
            self.leave = True
            return self.need_meat * 10
        return 0

# ====================== 游戏主逻辑 ======================
class Game:
    def __init__(self):
        self.money = 0
        self.raw_meat = 10          # 生肉库存
        self.on_grill = 0           # 烤架上肉数量
        self.cook_progress = 0     # 当前烤制进度
        self.cook_full = 160        # 烤熟需要帧数
        self.cooked_slices = 0      # 已经烤好的肉片
        self.plate_meat = 0         # 盘子里打包好的肉片

        self.customers = []
        self.spawn_cd = 0

    def spawn_customer(self):
        self.spawn_cd +=1
        if self.spawn_cd >= 320:
            self.customers.append(Customer())
            self.spawn_cd = 0

    def update(self):
        self.spawn_customer()
        # 更新顾客
        for cus in self.customers:
            cus.update()
        # 删除离开屏幕顾客
        self.customers = [c for c in self.customers if c.x < WIDTH + 100]

        # 烤肉进度
        if self.on_grill > 0:
            self.cook_progress += 1
            if self.cook_progress >= self.cook_full:
                self.cooked_slices += self.on_grill
                self.on_grill = 0
                self.cook_progress = 0

    def draw_ui(self):
        screen.fill(BG_COLOR)
        # 地面
        pygame.draw.rect(screen, (60,40,30), (0,450,WIDTH,200))
        # 操作台
        pygame.draw.rect(screen, TABLE, (15,270,590,180))

        # 烤架区域
        pygame.draw.rect(screen, GRILL, (35,285,160,110))
        if self.on_grill >0:
            per = self.cook_progress / self.cook_full
            pygame.draw.rect(screen, MEAT_RAW, (42,292, 145*per,96))
        text1 = font_mid.render(f"烤架肉:{self.on_grill}", True, WHITE)
        screen.blit(text1, (40,290))

        # 熟肉区
        pygame.draw.rect(screen, BREAD, (230,285,140,110))
        text2 = font_mid.render(f"熟肉片:{self.cooked_slices}", True, BLACK)
        screen.blit(text2, (240,290))

        # 打包盘
        pygame.draw.rect(screen, BREAD, (415,285,140,110))
        text3 = font_mid.render(f"盘中:{self.plate_meat}", True, BLACK)
        screen.blit(text3, (425,290))

        # 右上角信息
        money_txt = font_big.render(f"💰{self.money}", True, GOLD)
        screen.blit(money_txt, (660,15))
        raw_txt = font_mid.render(f"生肉:{self.raw_meat}", True, WHITE)
        screen.blit(raw_txt, (660,70))

        # 操作提示
        hint = font_small.render("【1】放肉上烤架｜【2】取熟肉到盘子｜【3】送餐", True, WHITE)
        screen.blit(hint, (15,15))

    def key_action(self, key):
        # 1 放生肉到烤架
        if key == pygame.K_1:
            if self.raw_meat > 0 and self.on_grill == 0:
                self.raw_meat -= 1
                self.on_grill = 1
                self.cook_progress = 0
        # 2 熟肉放入打包盘
        elif key == pygame.K_2:
            if self.cooked_slices > 0:
                self.cooked_slices -=1
                self.plate_meat +=1
        # 3 送餐给等待顾客
        elif key == pygame.K_3:
            if self.plate_meat > 0:
                for cus in self.customers:
                    if cus.waiting and not cus.complete:
                        reward = cus.check_food(self.plate_meat)
                        if reward > 0:
                            self.money += reward
                            self.plate_meat = 0
                        break

game = Game()
running = True

while running:
    clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            game.key_action(event.key)

    game.update()
    game.draw_ui()
    # 绘制顾客
    for c in game.customers:
        c.draw()
    pygame.display.update()

pygame.quit()