from time import sleep
from graphic import *
from mass import *
from spring import *
from vector import *


TOP_MASS = head(vector(12.0,12.0))
LEFT_MASS = thruster(vector(11.3,11.5))
RIGHT_MASS = thruster(vector(12.7,11.5))
BOTTOM_MASS = mass(vector(12.0,11.0))
masses = [TOP_MASS, LEFT_MASS, RIGHT_MASS, BOTTOM_MASS]

def draw_masses():
    draw_head(TOP_MASS)
    draw_thruster(LEFT_MASS)
    draw_thruster(RIGHT_MASS)
    draw_mass(BOTTOM_MASS)

LEFT_MASS.set_reference(BOTTOM_MASS, TOP_MASS)
RIGHT_MASS.set_reference(BOTTOM_MASS, TOP_MASS)

SPRING0 = spring(1,TOP_MASS, LEFT_MASS)
SPRING1 = spring(1,TOP_MASS, BOTTOM_MASS)
SPRING2 = spring(1,TOP_MASS, RIGHT_MASS)
SPRING3 = spring(1,BOTTOM_MASS, LEFT_MASS)
SPRING4 = spring(1,BOTTOM_MASS, RIGHT_MASS)

springs = [SPRING0,SPRING1,SPRING2,SPRING3,SPRING4]

def draw_springs():
    for SPRING in springs:
        draw_spring(SPRING)

def run_springs():
    for SPRING in springs:
        SPRING.detect_error()
        SPRING.exert_force()
def run_masses():
    for MASS in masses:
        MASS.receive_force()
        MASS.move()

key_states = {
    "q":False,
    "w":False,
    "a":False,
    "s":False,
}

def F0(event):
    key_states["q"] = True
def F1(event):
    key_states["w"] = True
def F2(event):
    key_states["a"] = True
def F3(event):
    key_states["s"] = True

def F4(event):
    key_states["q"] = False
def F5(event):
    key_states["w"] = False
def F6(event):
    key_states["a"] = False
def F7(event):
    key_states["s"] = False


display.bind_key_press("q",F0)
display.bind_key_press("w",F1)
display.bind_key_press("a",F2)
display.bind_key_press("s",F3)
display.bind_key_release("q",F4)
display.bind_key_release("w",F5)
display.bind_key_release("a",F6)
display.bind_key_release("s",F7)

def run_thrusters():
    if key_states["q"]:
        LEFT_MASS.fire_backward()
    if key_states["w"]:
        RIGHT_MASS.fire_backward()
    if key_states["a"]:
        LEFT_MASS.fire()
    if key_states["s"]:
        RIGHT_MASS.fire()



while True:
    display.clear()
    run_springs()
    run_masses()
    run_thrusters()
    draw_springs()
    draw_masses()
    display.update()
