from random import random, randint
from time import time
from sys import argv, exit
from os import environ
import pygame

from PodSix.RDC import *

SCREENSIZE = (640, 480)

collisions = 0

class Square(RDCEntity):
	def __init__(self):
		self.color = [100, 100, 100]
		self.border = [0, 0, 0]
		self.width = 20
		self.height = 20
		self.left = random() * (SCREENSIZE[0] - self.width)
		self.top = random() * (SCREENSIZE[1] - self.height)
	
	# if we collide with someone
	def Collide(self, who):
		self.border = [255, 0, 0]
		global collisions
		collisions += 1
	
	def SetGroup(self, g, friends):
		self.color = [(g * 10) % 255, ((g - 50) * 35) % 255, (g * -15) % 255]
	
	# functions for returning bounding box sides
	def GetLeft(self):
		return self.left
	
	def GetRight(self):
		return self.left + self.width
	
	def GetTop(self):
		return self.top
	
	def GetBottom(self):
		return self.top + self.height

environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
screen = pygame.display.set_mode(SCREENSIZE)

pygame.font.init()
fnt = pygame.font.SysFont("Arial", 12)
many = len(argv) > 1 and int(argv[1]) or 100
pause = False
frames = 0
total = 0

while True:
        for event in pygame.event.get():
                if event.type == pygame.QUIT or event.type == pygame.KEYDOWN:
                        exit()

		if event.type == pygame.MOUSEBUTTONDOWN:
			pause = not pause
	
	rdc = RDC(RDCEntity)
	if not pause:
		squares = [Square() for s in range(many)]
		start = time()
		rdc.DoRDC(squares)
		elapse = int(1 / (time() - start))
		total += elapse
		frames += 1
	
	txt = fnt.render(str("%d objects collided an average of %d times at %d fps" % (many, collisions / frames / 2, total / frames)), 1, (0, 0, 0))
	
        screen.fill([255, 255, 255])
	[pygame.draw.rect(screen, s.color, (s.left, s.top, s.width, s.height), 0) for s in squares]
	[pygame.draw.rect(screen, s.border, (s.left, s.top, s.width, s.height), 1) for s in squares]
	screen.blit(txt, (10, 10))
        pygame.display.flip()

