# Pyxel Studio

import pyxel
import random

pyxel.init(320,180)
pyxel.load("res.pyxres")

state = "MENU"
dialogue = 1
dialogue_flag = False
game = ""
pyxel.mouse(True)

def menu_interface():
    pyxel.text(70, 85, "DEMO", 0)
    pyxel.text(70, 105, "appuyez space ou click pour commencer le jeu", 0)
    pyxel.rect(5, 115, 50, 10, 0)
    pyxel.text(5, 115, "GAME shortcut", 1)

def talk_interface():
    global state, dialogue_flag
    pyxel.rect(5, 115, 310, 60, 0)
    
    if dialogue_flag:
        if pyxel.frame_count%5==0:
            talk_interface.dither -= 0.1
        pyxel.dither(talk_interface.dither)
    
        if talk_interface.dither <= 0.0:
            state = "COUNTDOWN"
talk_interface.dither: float = 1.0

def talk_sprite():
    pyxel.rect(135,60,50,60,5)

def talk_dialogue(dialogue):
    global dialogue_flag
    text_colour = 7
    x = 25
    y = 125
    match dialogue:
        case 1:
            pyxel.text(x, y, "Salut.. Je sais que c'est ton premier jour mais euh...", text_colour)
        case 2:
            pyxel.text(x, y, "Y'a un client dehors", text_colour)
        case 3:
            pyxel.text(x, y, "Il dit qu'il est.. Critique Gastronomique.", text_colour)
        case 4:
            pyxel.text(x, y, "Il nous demande le MEILLEUR happy meal qu'il n'a jamais mange", text_colour)
        case 5:
            pyxel.text(x, y, "( Aussi il nous a dit qu'il allait monter un dossier chez les ", text_colour)
            pyxel.text(x, y+10, "inspecteurs sanitaires sinon ) .", text_colour)
        case 6:
            pyxel.text(x, y, "Ah d'ailleurs t'as 7 minutes", text_colour)
        case 7:
            pyxel.text(x, y, "Bon ben euh fait de ton mieux hein", text_colour)
        case 8:
            pyxel.text(x, y, "Bon ben euh fait de ton mieux hein", text_colour)
            dialogue_flag = True
            dialogue = 0

def stopwatch():
    stopwatch.millisecondes -= 2
    if pyxel.frame_count%30==0:
        stopwatch.seconds -= 1
        stopwatch.millisecondes = 59
        if stopwatch.seconds == 0:
            stopwatch.minutes -= 1
            stopwatch.seconds = 59
    str_stopwatch = str(stopwatch.minutes)+":"+str(stopwatch.seconds)+"."+str(stopwatch.millisecondes)
    pyxel.text(145, 10, str_stopwatch, 7)
    return str_stopwatch
stopwatch.millisecondes=59
stopwatch.seconds=59
stopwatch.minutes=6
stopwatch.y =-84

def background():
    pyxel.rect(0, background.y, 320, 60, 7)
    pyxel.rect(0, background.y+10, 320, 60, 13)
    pyxel.rect(0, background.y+30, 320, 60, 13)
    pyxel.line(0, background.y+30, 320, background.y+30, 1)
    pyxel.rect(0, background.y+90, 320, 60, 1)
    
    pyxel.rect(20, background.y+20, 30, 30, 5)
    pyxel.rect(60, background.y+30, 30, 30, 5)
    pyxel.rect(100, background.y+25, 30, 30, 5)
    pyxel.rect(145, background.y+20, 30, 30, 5)
    pyxel.rect(180, background.y+25, 30, 30, 5)
    pyxel.rect(220, background.y+30, 30, 30, 5)
background.y = 180
background.expected = 80
background.objects = [[20, 100],[60, 110],[100, 105],[145, 100],[180, 105],[220, 110]]

def running_sprites_spawn():
    new_sprite=[]
    direction = ["right","left"]
    col = ["green","blue","yellow"]
    if pyxel.frame_count%running_sprites_spawn.frequency==0:
        direction = random.choice(direction)
        if direction == "right":
            pos = -60
        else:
            pos = 400
        colour = random.choice(col)
        new_sprite.append(pos)
        new_sprite.append(direction)
        new_sprite.append(colour)
        running_sprites.whereandwhat.append(new_sprite)
    if stopwatch.minutes <= 5:
        running_sprites_spawn.frequency = 150
        if stopwatch.minutes <= 1:
            running_sprites_spawn.frequency = 60
running_sprites_spawn.frequency = 300
    
def running_sprites():
    for i in running_sprites.whereandwhat:
        position = i[0]
        match i[2]:
            case "green":
                if i[1]=="left":
                    pyxel.blt(i[0], 0, 0, 0, 0, 90, 99, 2)
                else:
                    pyxel.blt(i[0], 0, 0, 0, 0, -90, 99, 2)
            case "blue":
                if i[1]=="left":
                    pyxel.blt(i[0], 0, 0, 95, 0, 47, 100, 2)
                else:
                    pyxel.blt(i[0], 0, 0, 95, 0, -47, 100, 2)
            case "yellow":
                if i[1]=="left":
                    pyxel.blt(i[0], 0, 0, 143, 0, 95, 100, 2)
                else:
                    pyxel.blt(i[0], 0, 0, 143, 0, -95, 100, 2)
running_sprites.whereandwhat = []

def countdown():
    global state
    count = [3,2,1,"GO!!"," "]
    if pyxel.frame_count%30 == 0:
        countdown.number +=1
    pyxel.text(150, 60, str(count[countdown.number]), 0)
    
    if countdown.number == len(count)-1:
        state = "GAME"
countdown.number = 0

def game_detection():
    global game
    if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
        for i in background.objects:
            if i[0]<pyxel.mouse_x<i[0]+30 and i[1]< pyxel.mouse_y<i[1]+30:
                if game_open_or_closed.check == "closed":
                    match background.objects.index(i):
                        case 0:
                            game = "pour fries"
                            game_open_or_closed.check = "open"
                        case 1:
                            game = "fry fries"
                            game_open_or_closed.check = "open"
                        case 2:
                            game = "salt fries"
                            game_open_or_closed.check = "open"
                        case 3:
                            game = "cut ingredients"
                            game_open_or_closed.check = "open"
                        case 4:
                            game = "cook steak"
                            game_open_or_closed.check = "open"
                        case 5:
                            game = "stack ingredients"
                            game_open_or_closed.check = "open"
    
def game_open_or_closed():
    global game
    if game_open_or_closed.check == "open":
        if game_open.y < 20:
            game_open.y +=15
    if game_open_or_closed.check == "closed":
        if -140 <= game_open.y:
            game_open.y -=20
        else:
            game=""
game_open_or_closed.check = "closed"

def game_open():
    bottom_tab_width = 15
    pyxel.rect(game_open.x, game_open.y, game_open.width, game_open.line_width, 1)
    pyxel.rect(game_open.x, game_open.y, game_open.line_width, game_open.height, 1)
    pyxel.rect((game_open.x + game_open.width) - game_open.line_width, game_open.y, game_open.line_width, game_open.height, 1)
    pyxel.rect(game_open.x, game_open.y+(game_open.height - bottom_tab_width), game_open.width, bottom_tab_width, 1)
    
    pyxel.rect(game_open.button_x, game_open.y + game_open.button_y, game_open.button_size, game_open.button_size, 6)
    pyxel.text(game_open.button_x + (game_open.button_size/3), game_open.y + game_open.button_y + (game_open.button_size/4), "x", 4)
game_open.x = 20
game_open.y=-140
game_open.y_expected = 20
game_open.width = 280
game_open.height = 140
game_open.line_width = 5
game_open.button_x = 275
game_open.button_size = 11
game_open.button_y = 127
    
def game_open_detection():
    global game
    if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
        if game_open.button_x < pyxel.mouse_x < game_open.button_x + game_open.button_size and game_open.y + game_open.button_y < pyxel.mouse_y < game_open.y + game_open.button_y + game_open.button_size:
            game_open_or_closed.check = "closed"
    if game_open_detection.game_ended==True:
        game_open_or_closed.check = "closed"
        game_open_detection.game_ended = False
game_open_detection.game_ended = False

def plate():
    plate.y = (game_open.height-(ingredients.height*2)) - start_game.change
plate.y_expected = 100
plate.width = 42

def ingredients():
    possible = ["steak","lettuce","tomato","onions","pickle","ketchup","mayonnaise"]
    if ingredients.listmade == True:
        pass
    else:
        for i in range(6):
            if ingredients.order[-1] == "steak":
                next = "cheese"
            else:
                next = random.choice(possible)
            ingredients.order.append(next)
        ingredients.order.append("bun_up")
        ingredients.listmade = True
    
    next_throw = []
    if ingredients.counter <= len(ingredients.order)-1:
        if pyxel.frame_count%ingredients.frequency==0:
            next_throw.append(ingredients.order[ingredients.counter])
            next_throw.append(random.randint(game_open.x+(game_open.line_width*2),game_open.width-(game_open.line_width*2)))
            next_throw.append(game_open.y)
            ingredients.whereandwhat.append(next_throw)
            ingredients.counter +=1
    else:
        pass
    
    ingredient_hold = []
    for i in ingredients.whereandwhat:
        i[2]+=5
        if i[2]>game_open.y+game_open.height-game_open.line_width:
            ingredients.whereandwhat.remove(i)
        elif plate.x-plate.width < i[1] < plate.x+plate.width:
            if ingredients.lastx-ingredients.width < i[1] < ingredients.lastx+ingredients.width or ingredients.lastx == 0:
                if ingredients.ylevel+20 > i[2] >= ingredients.ylevel:
                    ingredient_hold.append(i[0])
                    ingredient_hold.append(i[1]-plate.x)
                    ingredient_hold.append(ingredients.ylevel-(game_open.y_expected+2))
                    ingredients.onplate.append(ingredient_hold)
                    ingredients.whereandwhat.remove(i)
                    ingredients.ylevel -= ingredients.height
                    if ingredients.counter == len(ingredients.order)-1:
                        ingredients.ylevel -= ingredients.height
ingredients.frequency = 30
ingredients.order = ["bun_down","steak", "cheese"]
ingredients.whereandwhat = []
ingredients.onplate = []
ingredients.counter = 0
ingredients.listmade = False
ingredients.ylevel = game_open.y_expected + plate.y_expected
ingredients.lastx = 0
ingredients.height = 3
ingredients.width = 28
ingredients.game_done = False

def ingredients_draw():
    for i in ingredients.whereandwhat:
        match i[0]:
            case "bun_down":
                pyxel.blt(i[1], i[2], 0, 7, 100, 28, -7, 2)
            case "bun_up":
                pyxel.blt(i[1], i[2], 0, 7, 100, 28, 7, 2)
            case "ketchup":
                pyxel.blt(i[1], i[2], 0, 7, 108, 28, 5, 2)
            case "mayonnaise":
                pyxel.blt(i[1], i[2], 0, 8, 114, 28, 4, 0)
            case "pickle":
                pyxel.blt(i[1], i[2], 0, 9, 118, 28, 2, 0)
            case "onions":
                pyxel.blt(i[1], i[2], 0, 9, 121, 28, 2, 0)
            case "tomato":
                pyxel.blt(i[1], i[2], 0, 8, 124, 28, 3, 0)
            case "lettuce":
                pyxel.blt(i[1], i[2], 0, 8, 128, 28, 3, 0)
            case "cheese":
                pyxel.blt(i[1], i[2], 0, 8, 133, 28, 5, 0)
            case "steak":
                pyxel.blt(i[1], i[2], 0, 9, 140, 28, 4, 0)
    for i in ingredients.onplate:
        match i[0]:
            case "bun_down":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 7, 100, 28, -7, 2)
            case "bun_up":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 7, 100, 28, 7, 2)
            case "ketchup":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 7, 108, 28, 5, 2)
            case "mayonnaise":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 8, 114, 28, 4, 0)
            case "pickle":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 9, 118, 28, 2, 0)
            case "onions":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 9, 121, 28, 2, 0)
            case "tomato":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 8, 124, 28, 3, 0)
            case "lettuce":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 8, 128, 28, 3, 0)
            case "cheese":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 8, 133, 28, 5, 0)
            case "steak":
                pyxel.blt(plate.x+i[1], game_open.y+i[2], 0, 9, 140, 28, 4, 0)
def start_game():
    if pyxel.frame_count%120 == 0:
        start_game.var = True
    if start_game.var == False:
        match game:
            case "pour fries":
                pass 
            case "fry fries":
                pyxel.text(150, 60, str("STOP THE RATS !!!"), 7)
            case "salt fries":
                pass
            case "cut ingredients":
                pass
            case "cook steak":
                pass
            case "stack ingredients":
                pyxel.text(150, 60, str("STACK INGREDIENTS !!!"), 7)
start_game.var = False
start_game.change = 0

def final_rank():
    pass
final_rank.stack_points = 0

def update():
    global state, dialogue
    match state:
        case "MENU":
            if pyxel.btn(pyxel.KEY_SPACE):
                state = "TALK"
            if pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
                if 5 < pyxel.mouse_x < 55 and 115 < pyxel.mouse_y < 125:
                    state = "GAME"
                else:
                    state = "TALK"
        case "TALK":
            if pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.MOUSE_BUTTON_LEFT):
                dialogue += 1
        case "COUNTDOWN":
            pass
        case "GAME":
            for i in running_sprites.whereandwhat:
                if i[2] == "blue":
                    sprite_speed=2
                elif i[2] == "yellow":
                    sprite_speed=4
                else:
                    sprite_speed=3
                if i[1]=="right":
                    i[0]+=sprite_speed
                    if i[0]>400:
                        running_sprites.whereandwhat.remove(i)
                else:
                    i[0]-=sprite_speed
                    if i[0]<-100:
                        running_sprites.whereandwhat.remove(i)
            running_sprites_spawn()
            if stopwatch.minutes <= 0 and stopwatch.seconds <= 1:
                state = "RANK"
            game_detection()
            match game:
                case "pour fries":
                    game_open_detection()
                case "fry fries":
                    game_open_detection()
                    pass
                case "salt fries":
                    game_open_detection()
                case "cut ingredients":
                    game_open_detection()
                case "cook steak":
                    game_open_detection()
                case "stack ingredients":
                    game_open_detection()
                    plate()
                    plate.x = pyxel.mouse_x - 20
                    if pyxel.mouse_x < (game_open.x+game_open.line_width)+(plate.width/2):
                        plate.x = game_open.x+game_open.line_width
                    if pyxel.mouse_x > ((game_open.x+game_open.width)-(game_open.line_width+plate.width))+(plate.width/2):
                        plate.x = ((game_open.x+game_open.width)-(game_open.line_width+plate.width))
                    if start_game.var == True:
                        ingredients()
                    #boolean evaluation
                        if ingredients.onplate:
                            ingredients.lastx=plate.x+ingredients.onplate[-1][1]
                        for i in ingredients.onplate:
                            final_rank.stack_points += 10
                        if not ingredients.whereandwhat:
                            if ingredients.counter == len(ingredients.order):
                                if pyxel.frame_count %170 ==0:
                                    game_open_detection.game_ended = True
                                    ingredients.game_done = True
        case "RANK":
            pass
    pass

def draw():
    global dialogue
    pyxel.cls(9)
    match state:
        case "MENU":
            menu_interface()
        case "TALK":
            talk_sprite()
            talk_interface()
            talk_dialogue(dialogue)
        case "COUNTDOWN":
            pyxel.dither(1)
            background()
            pyxel.text(145, stopwatch.y, "7:00:00", 7)
            if background.y >= background.y >= 80:
                background.y-=2
            if stopwatch.y <= stopwatch.y <= 8:
                stopwatch.y +=2
            if 90 >= background.y and -10 <= stopwatch.y:
                countdown()
        case "GAME":
            pyxel.dither(1)
            running_sprites()
            background()
            if background.y == 180:
                background.y = 78
            stopwatch()
            match game:
                case "pour fries":
                    game_open_or_closed()
                    pyxel.rect(game_open.x,game_open.y,game_open.width,game_open.height-15,11)
                    game_open()
                case "fry fries":
                    game_open_or_closed()
                    pyxel.rect(game_open.x,game_open.y,game_open.width,game_open.height,13)
                    game_open()
                case "salt fries":
                    game_open_or_closed()
                    pyxel.rect(game_open.x,game_open.y,game_open.width,game_open.height,7)
                    game_open()
                case "cut ingredients":
                    game_open_or_closed()
                    pyxel.rect(game_open.x,game_open.y,game_open.width,game_open.height,15)
                    game_open()
                case "cook steak":
                    game_open_or_closed()
                    pyxel.rect(game_open.x,game_open.y,game_open.width,game_open.height,5)
                    game_open()
                case "stack ingredients":
                    game_open_or_closed()
                    pyxel.rect(game_open.x,game_open.y,game_open.width,game_open.height,12)
                    pyxel.blt(plate.x,game_open.y+plate.y, 0, 1, 153, 42, 9, 0)
                    if plate.y > plate.y_expected:
                        start_game.change += 2
                    else:
                        start_game()
                    ingredients_draw()
                    game_open()
                    if not ingredients.whereandwhat:
                            if ingredients.counter == len(ingredients.order):
                                pyxel.text(150, game_open.y+40, str("Game Complete"), 1)
        case "RANK":
            pyxel.text(70, 85, "ENDING SEQUENCE", 0)

pyxel.run(update,draw)