import pyxel


pyxel.init(366, 235, fps=60)
pyxel.load("patch3.pyxres")

vy = 4
vy2 = 4
u = 4
v = 7

# Coordonnees generales
epstein_x1 = 330
epstein_y1 = 111
dony_x2 = 2
dony_y2 = 110
epstein_w = 19
dony_w = 19

wall_timer = 0
wall_active = False

main_attack_x = epstein_x1 + 8
main_attack_y = dony_y2 + 4
timer_atk = -50
timer_atk_basic = -50
epstein_life = 100
dony_life = 100
special1_hit = False

wall_extremity = dony_x2 + 28
wall_heightup = dony_y2 - 5
wall_heightdown = wall_heightup + 39

# Projectile Epstein
proj_x = 0
proj_y = 0
proj_active = False
PROJ_SPEED = 5

# Projectile Dony
proj2_x = 0
proj2_y = 0
proj2_active = False


def update_wall_bounds():
    global wall_extremity, wall_heightup, wall_heightdown
    wall_extremity = dony_x2 + 28
    wall_heightup = dony_y2 - 5
    wall_heightdown = wall_heightup + 39


def launch_projectile():
    global proj_x, proj_y, proj_active
    if not proj_active:
        proj_x = epstein_x1
        proj_y = epstein_y1 + 10
        proj_active = True


def update_projectile():
    global proj_x, proj_active, dony_life
    if not proj_active:
        return

    proj_x -= PROJ_SPEED
    if proj_x < -10:
        proj_active = False
        return

    # Bloquer le projectile par le mur
    if (
        wall_active
        and wall_extremity - 8 < proj_x < wall_extremity
        and wall_heightup < proj_y < wall_heightdown
    ):
        proj_active = False
        return

    # Collision Dony
    if (
        dony_x2 < proj_x + 8 < dony_x2 + abs(dony_w) + 10
        and dony_y2 < proj_y < dony_y2 + 44
    ):
        proj_active = False
        dony_life -= 10


def draw_projectile():
    if proj_active and timer_atk < -10:
        pyxel.blt(proj_x, proj_y, 0, 5, 156, 6, 8, 1)


def launch_projectile2():
    global proj2_x, proj2_y, proj2_active
    if not proj2_active:
        proj2_x = dony_x2 + abs(dony_w)
        proj2_y = dony_y2 + 10
        proj2_active = True


def update_projectile2():
    global proj2_x, proj2_active, epstein_life
    if not proj2_active:
        return

    proj2_x += PROJ_SPEED
    if proj2_x > pyxel.width + 10:
        proj2_active = False
        return

    if (
        epstein_x1 < proj2_x < epstein_x1 + abs(epstein_w) + 10
        and epstein_y1 < proj2_y < epstein_y1 + 44
    ):
        proj2_active = False
        epstein_life -= 10


def draw_projectile2():
    if proj2_active:
        pyxel.blt(proj2_x, proj2_y, 0, 8, 202, 36, 12, 2)


def deplacement1():
    global epstein_x1, epstein_y1
    if timer_atk > -20:
        return
    if pyxel.btn(pyxel.KEY_RIGHT):
        epstein_x1 += 2
    if pyxel.btn(pyxel.KEY_LEFT):
        epstein_x1 -= 2
    if pyxel.btn(pyxel.KEY_DOWN):
        epstein_y1 += 2


def deplacement2():
    global dony_x2, dony_y2
    if pyxel.btn(pyxel.KEY_D):
        dony_x2 += 2
    if pyxel.btn(pyxel.KEY_Q):
        dony_x2 -= 2
    if pyxel.btn(pyxel.KEY_S):
        dony_y2 += 2


def atk_basic():
    if timer_atk_basic >= 0:
        pyxel.blt(epstein_x1 - 12, epstein_y1, 0, 5, 85, 31, 43, 0)
    elif -10 <= timer_atk_basic < 0:
        pyxel.blt(epstein_x1 - 12, epstein_y1, 0, 37, 29, 31, 43, 0)
    elif -15 <= timer_atk_basic < -10:
        pyxel.blt(epstein_x1, epstein_y1, 0, 41, 57, 26, 42, 0)


def attack_special1():
    if epstein_x1 < dony_x2:
        return

    if timer_atk > 0:
        pyxel.blt(epstein_x1, epstein_y1, 0, 49, 85, 18, 42, 0)
    elif -20 < timer_atk <= 0:
        pyxel.blt(epstein_x1, epstein_y1, 0, 97, 85, 44, 42, 0)
        for i in range(30):
            pyxel.blt(epstein_x1 - 16 * i - 11, epstein_y1 + 4, 0, 96, 79, 16, 1)


def attack_special2():
    if wall_active:
        pyxel.blt(dony_x2 + 20, dony_y2 + 5, 0, 180, 87, 8, 39, 0)


def special1_blocked_by_wall():
    if not wall_active:
        return False

    laser_y = epstein_y1 + 4
    wall_left = wall_extremity - 8
    wall_right = wall_extremity

    return (
        wall_right < epstein_x1
        and wall_left > dony_x2
        and wall_heightup <= laser_y <= wall_heightdown
    )


def inversion():
    global epstein_w, dony_w
    if timer_atk > -20:
        return
    if abs(epstein_x1 - dony_x2) < 6:
        return

    if epstein_x1 > dony_x2:
        epstein_w = abs(epstein_w)
    else:
        epstein_w = -abs(epstein_w)

    if dony_x2 < epstein_x1:
        dony_w = abs(dony_w)
    else:
        dony_w = -abs(dony_w)


def jump1():
    global vy, epstein_y1
    if timer_atk > -20:
        return
    if pyxel.btnp(pyxel.KEY_UP) and epstein_y1 >= 100:
        vy = -8
    vy += 0.4
    epstein_y1 += vy
    if epstein_y1 > 111:
        epstein_y1 = 111
        vy = 0


def depassement():
    global epstein_x1
    if epstein_x1 > 345:
        epstein_x1 = 345
    elif epstein_x1 < 5:
        epstein_x1 = 5


def jump2():
    global vy2, dony_y2
    if timer_atk > -20:
        return
    if pyxel.btnp(pyxel.KEY_Z) and dony_y2 >= 100:
        vy2 = -8
    vy2 += 0.4
    dony_y2 += vy2
    if dony_y2 > 111:
        dony_y2 = 111
        vy2 = 0


def depassement2():
    global dony_x2
    if dony_x2 > 345:
        dony_x2 = 345
    elif dony_x2 < 5:
        dony_x2 = 5


def gameover():
    if dony_life <= 0:
        pyxel.rect(0, 0, pyxel.width, pyxel.height, 5)
        pyxel.text(150, 200, "Epstein wins", 0)
    elif epstein_life <= 0:
        pyxel.rect(0, 0, pyxel.width, pyxel.height, 2)
        pyxel.text(150, 200, "Donald wins", 0)

def update():
    global timer_atk_basic, timer_atk, wall_timer, special1_hit, dony_life, wall_active

    if epstein_life <= 0 or dony_life <= 0:
        return

    inversion()
    depassement()
    depassement2()
    deplacement1()
    deplacement2()
    jump1()
    jump2()

    update_wall_bounds()
    update_projectile()
    update_projectile2()

    wall_timer -= 1
    timer_atk -= 1
    timer_atk_basic -= 1

    if pyxel.btn(pyxel.KEY_KP_0):
        timer_atk = 30

    if pyxel.btn(pyxel.KEY_RETURN):
        timer_atk_basic = 10

    # Mur de Dony
    if pyxel.btnp(pyxel.KEY_SPACE) and wall_timer <= 0:
        wall_active = True
        wall_timer = 180

    if wall_timer <= 0:
        wall_active = False

    # Projectile Epstein
    if pyxel.btnp(pyxel.KEY_RETURN) and timer_atk < -10:
        launch_projectile()

    # Projectile Dony
    if pyxel.btnp(pyxel.KEY_F):
        launch_projectile2()

    # Detection laser
    if -20 < timer_atk <= 0 and epstein_x1 > dony_x2:
        if (
            not special1_hit
            and not special1_blocked_by_wall()
            and dony_y2 <= epstein_y1 + 4 <= dony_y2 + 44
        ):
            dony_life -= 30
            special1_hit = True

    if timer_atk <= -20 or timer_atk > 0:
        special1_hit = False


def draw():
    pyxel.cls(0)
    pyxel.bltm(0, 0, 0, 0, 0, 375, 235)
    gameover()
    atk_basic()

    if timer_atk_basic < -10:
        pyxel.blt(epstein_x1, epstein_y1, 0, 17, 29, epstein_w, 43, 0)

    pyxel.blt(dony_x2, dony_y2, 0, 142, 82, dony_w, 44, 0)
    attack_special1()
    attack_special2()
    draw_projectile()
    draw_projectile2()


pyxel.run(update, draw)