#################################################
# hw14.py
#
# Your name:
# Your andrew id:
#################################################

#################################################################

# asteroids.py should contain your code from hw13
import asteroids

#################################################################

# Starter Code begins here. Read and understand it!

from cmu_112_graphics import *
import random, math

# Helper function for drawing the Rocket
def drawTriangle(canvas, cx, cy, angle, size, fill="black"):
    angleChange = 2*math.pi/3
    p1x, p1y = (cx + size*math.cos(angle),
                    cy - size*math.sin(angle))
    p2x, p2y = (cx + size*math.cos(angle + angleChange),
                    cy - size*math.sin(angle + angleChange))
    p3x, p3y = (cx, cy)
    p4x, p4y = (cx + size*math.cos(angle + 2*angleChange),
                    cy - size*math.sin(angle + 2*angleChange))

    canvas.create_polygon((p1x, p1y), (p2x, p2y), (p3x, p3y), (p4x, p4y),
                                                                 fill=fill)

# Read this class carefully! You'll need to call the methods!
class Rocket(object):
    def __init__(self, cx, cy):
        self.cx = cx
        self.cy = cy
        self.angle = 90

    def rotate(self, numDegrees):
        self.angle += numDegrees

    def makeBullet(self):
        offset = 10
        dx, dy = (offset*math.cos(math.radians(self.angle)),
                            offset*math.sin(math.radians(self.angle)))
        speedLow, speedHigh = 20, 40

        return Bullet(self.cx+dx, self.cy-dy,
                self.angle,random.randint(speedLow, speedHigh))

    def draw(self, canvas):
        size = 30
        drawTriangle(canvas, self.cx, self.cy,
            math.radians(self.angle), size, fill="green2")

# Read this class carefully! You'll need to call the methods!
class Bullet(object):
    def __init__(self, cx, cy, angle, speed=20):
        self.cx = cx
        self.cy = cy
        self.r = 5
        self.angle = angle
        self.speed = speed

    def moveBullet(self):
        dx = math.cos(math.radians(self.angle))*self.speed
        dy = math.sin(math.radians(self.angle))*self.speed
        self.cx, self.cy = self.cx + dx, self.cy - dy

    def isCollidingWithAsteroid(self, other):
        # in this case, other must be an asteroid
        if(not isinstance(other, asteroids.Asteroid)):
            return False
        else:
            return (math.sqrt((other.cx - self.cx)**2 +
                                (other.cy - self.cy)**2)
                                        < self.r + other.r)

    def draw(self, canvas):
        cx, cy, r = self.cx, self.cy, self.r
        canvas.create_oval(cx - r, cy - r, cx + r, cy + r,
            fill="white", outline=None)

    def onTimerFired(self, app):
        self.moveBullet()

#################################################################

from tkinter import *

####################################
# customize these functions
####################################

def appStarted(app):
    # load app.xyz as appropriate
    app.rocket = Rocket(app.width//2, app.height//2)
    # what else do you need to store here?


def mousePressed(app, event):
    # use event.x and event.y
    pass

def keyPressed(app, event):
    # use event.key
    # make the rocket rotate and fire!
    pass

def timerFired(app):
    # it might be a good idea to define onTimerFired methods in your classes...
    pass

def redrawAll(app, canvas):
    # draws the rocket and background
    canvas.create_rectangle(0, 0, app.width, app.height, fill="gray3")
    app.rocket.draw(canvas)
    # don't forget to draw asteroids and bullets!

runApp(width=600, height=600)
