# objet.py
"""Objets ramassables pour le jeu Bucheron - Projet NSI Première

Actuellement : HacheAmelioree
  → Spawn aléatoire sur la carte
  → Ramassée quand le joueur passe dessus
  → Réduit le délai de coupe de 25 → 8 frames pendant 10 secondes
"""

import pygame
import math
from constantes import *


class HacheAmelioree:
    """Objet ramassable : hache en acier qui accélère les coups.

    Attributs :
        x, y       : position sur la carte
        rayon      : rayon de ramassage (collision joueur ↔ objet)
        ramassee   : True une fois collectée (on arrête de la dessiner)
    """

    RAYON_RAMASSAGE = 28

    def __init__(self, x: int, y: int):
        self.x        = x
        self.y        = y
        self.rayon    = self.RAYON_RAMASSAGE
        self.ramassee = False
        self._timer   = 0   # compteur de frames pour l'animation de pulsation

    # ── Logique ────────────────────────────────────────────────────────
    def mettre_a_jour(self) -> None:
        """Avance l'animation de pulsation."""
        self._timer += 1

    def verifier_ramassage(self, jx: float, jy: float) -> bool:
        """Retourne True si le joueur (jx, jy) est assez proche pour ramasser.

        Marque aussi l'objet comme ramassé.
        """
        if not self.ramassee:
            dist = math.dist((jx, jy), (self.x, self.y))
            if dist < self.rayon:
                self.ramassee = True
                return True
        return False

    # ── Dessin ─────────────────────────────────────────────────────────
    def dessiner(self, surface: pygame.Surface) -> None:
        """Dessine la hache améliorée avec un effet de pulsation dorée."""
        if self.ramassee:
            return

        cx = int(self.x)
        cy = int(self.y)

        # Halo pulsant (rayon oscille entre 16 et 22 px)
        halo_r = int(18 + math.sin(self._timer * 0.08) * 4)
        halo   = pygame.Surface((halo_r * 2 + 4, halo_r * 2 + 4), pygame.SRCALPHA)
        alpha  = int(80 + math.sin(self._timer * 0.08) * 40)
        pygame.draw.circle(halo, (255, 215, 0, alpha),
                           (halo_r + 2, halo_r + 2), halo_r)
        surface.blit(halo, (cx - halo_r - 2, cy - halo_r - 2))

        # Fond circulaire
        pygame.draw.circle(surface, (60, 50, 20), (cx, cy), 15)
        pygame.draw.circle(surface, JAUNE,        (cx, cy), 15, 2)

        # Manche de la hache
        pygame.draw.line(surface, MARRON,
                         (cx - 4, cy + 8), (cx + 6, cy - 8), 3)

        # Lame de la hache (dorée)
        pygame.draw.polygon(surface, JAUNE, [
            (cx + 6,  cy - 8),
            (cx + 13, cy - 3),
            (cx + 9,  cy - 15),
        ])
        pygame.draw.polygon(surface, ORANGE, [
            (cx + 6,  cy - 8),
            (cx + 13, cy - 3),
            (cx + 9,  cy - 15),
        ], 1)

        # Étiquette "+" pour indiquer que c'est un bonus
        font = pygame.font.SysFont("Arial", 11, bold=True)
        t    = font.render("+", True, BLANC)
        surface.blit(t, (cx - t.get_width() // 2 - 6,
                          cy + 10 - t.get_height() // 2))
