# BoxSeeker.py

from Processing import *

window(500, 500)

boxes = []
rectMode(CENTER)

class Seeker:
    def __init__(self, x, y):
        self.x = x          # Position
        self.y = y
        self.targetX = random( width() )
        self.targetY = random( height() )

    def step(self):
        # Move toward the target
        self.x = self.x + 0.02 * (self.targetX - self.x)
        self.y = self.y + 0.02 * (self.targetY - self.y)

        # Reset the target if it gets too close
        if dist(self.x, self.y, self.targetX, self.targetY) < 40:
            self.targetX = random( width() )
            self.targetY = random( height() )

    def display(self):
        fill(200)
        #ellipse(self.targetX, self.targetY, 5, 5)
        pushMatrix()
        translate(self.x, self.y)
        rect(0, 0, 20, 20)
        popMatrix()

# Create a new box and add to the master list
def createBox(o, e):
    x, y = mouseX(), mouseY()
    b = Seeker(x, y)
    boxes.append( b )

# Draw all boxes
def draw(o, e):
    background(0)
    for b in boxes:
        b.step()
        b.display()

# Create a new box whenever the mouse is clicked
onMousePressed += createBox

# Draw all
frameRate(30)
onLoop += draw
loop()
