import config 
import pygame

from game import *
from point import *
from polygon import *
from rock import *
from ship import *
from bullet import *
from star import *

class Asteroids(Game):
    def __init__(self, name, screen_x, screen_y, frames_per_second):
        Game.__init__(self, name, screen_x, screen_y, frames_per_second)

        self.stars = []
        for i in range(config.STAR_COUNT):
            star = Star()
            self.stars.append(star)


        self.ship = Ship(Point(config.SCREEN_X/2, config.SCREEN_Y/2), config.SHIP_INITIAL_DIRECTION, config.SHIP_COLOR)
        ship_points = self.ship.getPoints()
        nose_point = ship_points[1]
        (nx, ny) = nose_point.pair()
        
        self.bullet = Bullet(Point(nx, ny),config.BULLET_RADIUS,config.SHIP_INITIAL_DIRECTION,config.BULLET_COLOR)

        self.rocks = []
        for i in range(config.ROCK_COUNT):
            direction = random.choice([1,-1])
            rock_rotation_speed = random.uniform(config.ROCK_MIN_ROTATION_SPEED, config.ROCK_MAX_ROTATION_SPEED)
            rock_forward_speed = random.uniform(config.ROCK_MIN_SPEED, config.ROCK_MAX_SPEED)
            rock_rotation_speed *= direction
            x = random.randrange(1, config.SCREEN_X)
            y = random.randrange(1, config.SCREEN_Y)
            rock = Rock(Point(x,y), rock_rotation_speed, random.uniform(0.0,359.99), config.ROCK_COLOR, rock_forward_speed)
            self.rocks.append(rock)
            
    def game_logic(self, keys, newkeys, buttons, newbuttons, mouse_position, dt):
        for star in self.stars:
            star.game_logic(keys,newkeys)
        self.ship.move()
        self.ship.game_logic(keys, newkeys)
        if pygame.K_SPACE in newkeys:
            ship_points = self.ship.getPoints()
            nose_point = ship_points[1]
            nose_point = nose_point.pair()
            self.bullet.fire(nose_point, self.ship.get_rotation())
        self.bullet.game_logic(keys, newkeys)

        for rock in self.rocks:
            rock.move()
            rock.game_logic(keys, newkeys)
            if self.ship.is_active():
                if rock.intersect(self.ship):
                    if rock.is_active():
                        self.ship.set_inactive()
                        self.bullet.set_inactive()
                if self.bullet.is_active():
                    if self.bullet.intersect(rock):
                        self.bullet.set_inactive()
                        rock.set_inactive()

                ##Destroyer Mode##
            #if rock.is_active():
                #if self.ship.intersect(rock):
                    #rock.set_inactive()


    def paint(self, surface):
        surface.fill((0,0,0))
        for star in self.stars:
            star.paint(surface)
        if self.ship.is_active():
            self.ship.paint(surface) 
            if self.bullet.is_active():
                self.bullet.paint(surface)
        for rock in self.rocks:
            if rock.is_active():
                rock.paint(surface)
        
    
def main():
    a = Asteroids(config.TITLE, config.SCREEN_X, config.SCREEN_Y,
            config.FRAMES_PER_SECOND)
    a.main_loop()

main()
