# This file will store the buildings, birds, and planes as obstacles.
# The buildings will move along the bottom of the screen to give a parallax effect.
# Each building will have a different height and a different roof color.
# The birds and planes will fly across the screen at different heights.
import pyxel
import random

class Building:
    def __init__(self):
        """Initialize building attributes."""
        self.x = 160  # Start off-screen to the right
        self.y = 144  # Ground level
        self.speed = 1  # Speed at which buildings move left
        # Stores list of buildings from image bank 2.
        pyxel.load("my_resource.pyxres")
        self.building_list = [
                            {"img": 2, "u":3, "v":0, "w":18, "h":23, "colkey":0, "scale": 2, "color":"blue", "level":"high"}, # High Blue
                            {"img": 2, "u":3, "v":26, "w":18, "h":23, "colkey":0, "scale": 2, "color":"red", "level":"high"}, # High Red
                            {"img": 2, "u":3, "v":51, "w":18, "h":23, "colkey":0, "scale": 2, "color":"green", "level":"high"}, # High Green
                            {"img": 2, "u":29, "v":6, "w":18, "h":17, "colkey":0, "scale": 2, "color":"blue", "level":"mid"}, # Mid Blue
                            {"img": 2, "u":29, "v":31, "w":18, "h":17, "colkey":0, "scale": 2, "color":"red", "level":"mid"}, # Mid Red
                            {"img": 2, "u":29, "v":57, "w":18, "h":17, "colkey":0, "scale": 2, "color":"green", "level":"mid"}, # Mid Green
                            {"img": 2, "u":54, "v":12, "w":18, "h":11, "colkey":0, "scale": 2, "color":"blue", "level":"low"}, # Low Blue
                            {"img": 2, "u":54, "v":38, "w":18, "h":11, "colkey":0, "scale": 2, "color":"red", "level":"low"}, # Low Red
                            {"img": 2, "u":54, "v":63, "w":18, "h":11, "colkey":0, "scale": 2, "color":"green", "level":"low"}, # Low Green
                            ]
        # List to store current buildings on screen
        self.current_buildings = []
        # Timer to control building generation
        self.building_timer = 0
        self.building_interval = 90  # Frames between building generation

    def update(self):
        """Handle building movement on screen."""
        self.building_timer += 1
        if self.building_timer >= self.building_interval:
            # Randomly select a building from the list
            self.random_building = random.choice(self.building_list)
            # Add new building to current buildings list
            self.current_buildings.append({"x": 160, "y": self.y - self.random_building["h"], **self.random_building})
            self.building_timer = 0  # Reset timer

        # Move buildings to the left
        for building in self.current_buildings:
            building["x"] -= self.speed  # Move speed

        # Remove buildings that have moved off screen
        self.current_buildings = [building for building in self.current_buildings if building["x"] + building["w"] > 0]

    def draw(self):
        """Draw buildings on screen."""
        # This will be used to draw the buildings
        for random_building in self.current_buildings:
            pyxel.blt(random_building["x"],
                      random_building["y"],
                      random_building["img"],
                      random_building["u"],
                      random_building["v"],
                      random_building["w"],
                      random_building["h"],
                      random_building["colkey"],
                      scale=random_building["scale"]
                      )

class Bird:
    def __init__(self):
        """Initialize bird attributes."""
        self.x = 160
        self.y = random.randint(10, 120)
        self.speed = 2.5  # Speed at which bird moves left
        self.frame_count = 0
        self.animation_speed = 1 # Speed of bird animation

        self.bird_list = [{"img": 0, "u":0, "v":144, "w":15, "h":15, "colkey":0},
                          {"img": 0, "u":0, "v":160, "w":15, "h":15, "colkey":0}
                          ]
        
        self.bird_list *= 3  # Duplicate to increase variety
        self.current_bird = random.choice(self.bird_list)

    def update(self):
        """Update bird position and reset if off screen."""
        # Update frame count for animation
        self.frame_count += 1
        # Move bird to the left
        self.x -= self.speed
        # Reset bird position if it moves off screen and choose a new bird
        if self.x < -self.current_bird["w"]:
            self.x = 160
            self.y = random.randint(10, 120)
            self.frame_count = 0
            self.animation_speed = 4
            self.current_bird = random.choice(self.bird_list)

    def draw(self):
        """Draw bird on screen and handle animation."""
        # Calculate the current frame for animation
        current_frame = (self.frame_count // self.animation_speed) % 4
        self.current_bird["u"] = current_frame * 15  # Assuming each frame is 15 pixels wide
        pyxel.blt(self.x,
                  self.y,
                  self.current_bird["img"],
                  self.current_bird["u"],
                  self.current_bird["v"],
                  self.current_bird["w"],
                  self.current_bird["h"],
                  self.current_bird["colkey"]
                  )

class Plane:
    def __init__(self):
        """Initialize plane attributes."""
        self.x = 160
        self.y = random.randint(10, 70)
        self.speed = 1.5  # Speed at which plane moves left
        # Plane sprite size used in draw (no scale). Provide hitbox width/height for collisions.
        self.w = 50
        self.h = 15

    def update(self):
        """Update plane position and reset if off screen."""
        self.x -= self.speed
        if self.x < -53:  # Assuming plane width is 53
            self.x = 160
            self.y = random.randint(10, 70)

    def draw(self):
        """Draw plane on screen."""
        pyxel.blt(self.x, self.y, 0, 165, 2, 53, 25, 0)