# This will store my drone class.
import pyxel

class Drone:
    """Class representing the delivery drone."""
    def __init__(self):
        pyxel.load("my_resource.pyxres")
        self.x = 0
        self.y = 60
        # Sprite original size
        self.sprite_w = 51
        self.sprite_h = 23
        # Render scale (matches draw call). Adjust this to tweak visual size.
        self.scale = 0.5
        # Hitbox size should match the rendered size (and can be tuned smaller if needed)
        # hitbox_shrink lets you reduce the collision box relative to the rendered sprite
        self.hitbox_shrink = 0.85
        self.w = int(self.sprite_w * self.scale * self.hitbox_shrink)  # Hitbox width
        self.h = int(self.sprite_h * self.scale * self.hitbox_shrink)  # Hitbox height

        self.frame_count = 0
        self.animation_speed = 1

    def update(self):
        """Updates the drone's position."""
        self.frame_count += 1

    def draw(self):
        """Draws the drone at its current position."""
        # Calculate the current frame for animation
        current_frame = (self.frame_count // self.animation_speed) % 4
        drone_u = current_frame * self.sprite_w

        pyxel.blt(self.x, self.y, 0, drone_u, 176, self.sprite_w, self.sprite_h, 0, scale=self.scale)  # Draw the drone sprite

class Package:
    """Class representing the package carried by the drone."""
    def __init__(self, drone):
        self.drone = drone
        self.offset_x = 18  # Offset to position the package relative to the drone
        self.offset_y = 13  # Offset to position the package relative to the drone
        self.package_dropped = False # State to track if the package is dropped
        self.fall_speed = 2  # Speed at which the package falls

        self.package_list = [
                            {"img": 0, "u":177, "v":48, "w":16, "h":17, "colkey":0, "color":"red"}, # Red package
                            {"img": 0, "u":177, "v":80, "w":16, "h":17, "colkey":0, "color":"blue"}, # Blue package
                            {"img": 0, "u":177, "v":112, "w":16, "h":17, "colkey":0, "color":"green"} # Green package
                            ]
        
        self.current_package = self.package_list[0].copy()  # Default to the first package

        self.x = self.drone.x + self.offset_x
        self.y = self.drone.y + self.offset_y

    def drop_package(self):
        """Simulates dropping the package."""
        if not self.package_dropped:    
            # Set state for dropped package
            self.package_dropped = True
            # Save last x position and y position.
            self.x = self.drone.x + self.offset_x
            self.y = self.drone.y + self.offset_y

    def update(self):
        """Updates the package's position if it has been dropped."""
        if self.package_dropped:
            self.y += self.fall_speed  # Move the package downwards
    
    def reset_package(self):
        """Resets the package to the drone's position."""
        self.package_dropped = False
        self.x = self.drone.x + self.offset_x
        self.y = self.drone.y + self.offset_y

    def draw(self):
        """Draws the package at the drone's position."""
        if not self.package_dropped:
            # Draw package at drone's position    
            package_x = self.drone.x + self.offset_x
            package_y = self.drone.y + self.offset_y
        else:
            # Draw package at its current falling position
            package_x = self.x
            package_y = self.y
            
        pyxel.blt(package_x, package_y,
                  self.current_package["img"],
                  self.current_package["u"],
                  self.current_package["v"],
                  self.current_package["w"],
                  self.current_package["h"],
                  self.current_package["colkey"],
                  scale=0.75
                  )  # Draw the package sprite