import os
from random import randint
from time import sleep

import pygame
from pygame.locals import *
from euclid import Vector2

from FlockingMixIn import FlockingMixIn

class Flocker(FlockingMixIn):
	def __init__(self):
		FlockingMixIn.__init__(self)
		self.position = Vector2(100, 100)
		self.velocity = Vector2(randint(-20, 20), randint(-20, 20))
		self.rules.append(self.StayOnScreen)
	
	def GetPosition(self):
		return self.position
	
	def SetPosition(self, position):
		self.position = position
	
	def GetVelocity(self):
		return self.velocity
	
	def SetVelocity(self, velocity):
		self.velocity = velocity
	
	def Update(self):
		self.Flock()
	
	def Draw(self, surface):
		self.UpdateFlockingVelocity()
		self.position += self.velocity
		pygame.draw.aaline(surface, [0, 0, 255], self.position, self.position + self.velocity)
	
	def StayOnScreen(self):
		# a rule to make the flock tend towards the center of the screen
		return (Vector2(300, 300) - self.GetPosition()) * 0.01

def main():
	# setup pygame
	pygame.init()
	size = 600, 600
	os.environ['SDL_VIDEO_CENTERED'] = '1'
	screen = pygame.display.set_mode(size, NOFRAME, 0)
	pygame.event.set_blocked(MOUSEMOTION)
	srf = pygame.display.get_surface()
	
	srf.fill([255, 255, 255])
	boids = [Flocker() for f in range(10)]
	[f.SetFlockFriends(boids) for f in boids]
	
	quit = False
	# now wait until the user hits exit
	while not quit:
		srf.fill([255, 255, 255])
		[f.Update() for f in boids]
		[f.Draw(srf) for f in boids]
		pygame.display.flip()
		events = pygame.event.get()
		for e in events:
			if e.type in (QUIT, KEYDOWN, MOUSEBUTTONDOWN):
				quit = True
		sleep(0.001)
			

if __name__ == '__main__': main()


