#enemies_normal.py
#imported pygame and random libraries
import pygame
import random
from settings import * #import the global settings from settings.py

class Enemy: #here we create a class called enemy, all enemies will share attributes and methods defined here
    def __init__(self): #this function initializes enemies
        self.radius = ENEMY_SIZE #set radius of each enemy to the defined enemy size in settings

        #basically generates random float for the x position of the enemy inside the arena (not safezones). 
        # #I started at safezone_width + radius and ended at width - safezone_width - radius 
        # because I want the enemy to be fully inside the arena.

        x = random.uniform(SAFEZONE_WIDTH + self.radius,
                           WIDTH - SAFEZONE_WIDTH - self.radius)
        
        #same thing here except for the y position
        y = random.uniform(WALL_THICKNESS + self.radius,
                           HEIGHT - WALL_THICKNESS - self.radius)
        
        #initial position of the enemy made into a pygame vector, which holds 2 numbers and is the standard for pygame positions
        self.pos = pygame.Vector2(x, y)

        #initial velocity direction of the enemy made into a pygame vector, which holds 2 numbers and is the standard for pygame velocities
        #I wanted enemy velocity direction to be any direction possible, so x and y is just a range of values from -1 to 1.
        self.vel = pygame.Vector2(random.uniform(-1, 1), random.uniform(-1, 1))
        
        #this while loop here just checks for the rare case where velocity directions can somehow be (0, 0), which means
        # we need to regenerate as many times as possible so velocity directions is not 0, 0 (which means enemy won't move)
        #I'm pretty sure it's almost impossible for this to happen
        while self.vel == pygame.Vector2(0, 0): 
            self.vel = pygame.Vector2(random.uniform(-1, 1), random.uniform(-1, 1)) #

    def update(self, dt): #this function will update the enemies on the screen each frame
        
        new_pos = self.pos + self.vel * ENEMY_SPEED * dt #the new position vector is just the currrent position
        # of the enemy + dt * enemy base speed + velocity direction. Enemies move in straight lines. 
        # I explain what dt is in comments in heroes.py. But essentially dt keeps track of REAL time and scales
        # velocity and how much an enemy moved in real time, instead of the number of frames that passed.

        #Here we make sure that the enemies stay within the upper and lower walls and bounce off them

        #check if any part of the enemy is outside the top wall
        if new_pos.y - self.radius < WALL_THICKNESS:
            self.vel.y *= -1 #vertical velocity gets reversed (bouncing off walls)
            new_pos.y = WALL_THICKNESS + self.radius #vertical position starts off at the wall edge but completely inside
            #the enemy will now travel the other direction

        #this part is same as above except to check the bottom wall
        elif new_pos.y + self.radius > HEIGHT - WALL_THICKNESS:
            self.vel.y *= -1
            new_pos.y = HEIGHT - WALL_THICKNESS - self.radius

        #this part is same as above except it checks the left wall, which is the right side of the left safezone
        #enemies can't enter safezones
        #instead of vertical velocity getting flipped, now horizontal velocity gets flipped for bouncing off left wall
        if new_pos.x - self.radius < SAFEZONE_WIDTH:
            self.vel.x *= -1
            new_pos.x = SAFEZONE_WIDTH + self.radius

        #this is same as above except for enemies to bounce off the right wall, which is the left side of the right safezone
        elif new_pos.x + self.radius > WIDTH - SAFEZONE_WIDTH:
            self.vel.x *= -1
            new_pos.x = WIDTH - SAFEZONE_WIDTH - self.radius

        #this actually changes the position of the enemy to the new position according to above
        self.pos = new_pos

    def draw(self, screen): #very short function to actually draw enemies on the screen,
        pygame.draw.circle(screen, (0, 0, 0), self.pos, self.radius) #we use pygame.draw.circle
        #enemies are circular shaped, their color is black (0, 0, 0), their position and radius are also inputted.
        