# controller.py
# Walker White (wmw2), Lillian Lee (ljl2), and Steve Marschner (srm2)
# November 17, 2013
"""Breakout-like Controller module for lecture demos."""
import colormodel
import random
import math
from game2d import *

### CONSTANTS ###

# Window Size
WINDOW_WIDTH  = 512
WINDOW_HEIGHT = 512

NUM_PARTICLES = 10
PSIZE = 3.0
MAX_VEL = 10.0
INIT_VEL_X = 10.0
INIT_VEL_Y = -5.0
G = -2.0

### CONTROLLER CLASS ###

class Controller(Game):
    """Demo controller class for CS1110 game2d framework.
        
    Instance Attributes:
        view:      the view (inherited from Game) [GView]
        particles [list of Particle]
    """

    # THREE MAIN METHODS
    
    def init(self):
        """Initialize the program state."""
        self.particles = []
        self.bg = GRectangle(x=0, y=0, width=self.width, height=self.height,
                             fillcolor=colormodel.BLACK)

    def update(self,dt):
        """Animate a single frame. Called every frame."""
        if self.view.touch is not None:
            for i in range(NUM_PARTICLES):
                self.particles.append(Particle(self.view.touch.x, self.view.touch.y))
        for particle in self.particles:
            particle.update(self)

    def draw(self):
        """Draw all particles in the view."""
        self.bg.draw(self.view)
        for particle in self.particles:
            particle.draw(self.view)
        
    # HELPER METHODS


### MODEL CLASSES ###

class Particle(GEllipse):
    """An ellipse that also has a velocity.
    Instance vars:
       vy [float]
    """
    
    def __init__(self, x, y):
        """A particle at (x,y)"""
        GEllipse.__init__(self, x=x, y=y, width=PSIZE, height=PSIZE)
        self.vx = random.uniform(-MAX_VEL, MAX_VEL)
        self.vy = random.uniform(-MAX_VEL, MAX_VEL)
        
    def update(self, c):
        self.x += self.vx
        self.y += self.vy
        if self.y < PSIZE:
            c.particles.remove(self)
        self.vy += G




# Script Code
if __name__ == '__main__':
    Controller(width=WINDOW_WIDTH,height=WINDOW_HEIGHT,fps=30.0).run()