# Very Simple Particle emitter

import pygame
import math
import random
import globals

import anim

class Emitter:
	
	def __init__(self, pos, img):
		self.pos = pos
		self.number = 0
		self.speed = 0.0
		self.rate = 0
		self.img = []
		self.img.append(pygame.image.load(img))
		self.stopped = 1
		self.count = 0
		self.timer = 0
		self.particles = []
		self.disabled = 0
		
	def Start(self, num, speed, rate, life):
		self.number = num
		self.count = 0
		self.timer = 0
		self.life = life
		self.rate = rate
		self.speed = speed
		self.disabled = 0
		self.stopped = 0
		
		self.particles = []
		
	def NewParticle(self):
		if self.count > self.number:
			self.stopped = 1
			return
			
		velocity = [ math.sin(math.radians(random.random() * 360.0)) * self.speed, math.sin(math.radians(random.random() * 360.0)) * self.speed ]
		particle = [ self.pos, velocity, self.life ]
		self.particles.append(particle)
		self.count += 1
		
	def Stop(self):
		self.stopped = 1
		
	def Reset(self,start):
		self.stopped = start-1
		self.count = 0
		self.disabled = 0
		
	def Update(self, ms):
		
		if self.stopped and self.disabled == self.number: return
		
		if (self.timer + ms > self.rate):
			self.NewParticle()
			self.timer -= self.rate
		
		for i in range(0, len(self.particles)):
			p = self.particles[i]
			vel = p[1]
			pos = p[0]
			globals.UpdateRegions.append([pos[0]-8, pos[1]-8, 16, 16])

			if(p[2] > 0):
				temp =  ms / globals.forcefactor
				newvel = [vel[0] * temp, vel[1] * temp]
				vel = [vel[0] + globals.Gravity[0] * 2.0, vel[1] + globals.Gravity[1] * 2.0]	
				pos = [pos[0] + newvel[0], pos[1] + newvel[1]]
				p = [pos, vel, p[2]-ms, 1 ]
				self.particles[i] = p
			else:
				self.disabled += 1
			
		self.timer += ms
		
	def Draw(self):
		if(self.disabled >= self.number): return
		
		for p in self.particles:
			pos = p[0]
			pos = [pos[0]-8, pos[1]-8]
#			life = p[2]
#			if(life < 0.0): life = 0.0
				
#			alpha = int( 255.0 * life / self.life)
#			print(alpha)
#			self.img[0].set_alpha( alpha )
			globals.screen.blit(self.img[0], pos, [0, 0, 16, 16])



# -------------------------------------------------------------------
# Special feature class - late minute addon

class Special:
	
	def __init__(self):
		self.images = []
		self.images.append(pygame.image.load(globals.String_Special))
		self.images.append(pygame.image.load(globals.String_SpecialExp))
		
		self.explosions = []
		self.score = 0
		self.stopped = 0
		
		self.b52 = [76, -220]
		
	def AddExplosion(self):
		exp = anim.Interp(16, 10, 0)
		exp.Start()
		pos = [ int(random.random() * 320.0) + 10 , self.b52[1] ]
		self.explosions.append( [ pos, exp ] )
		
	def Start(self):
		self.explosions = []
		self.score = 0
		self.b52 = [76, -220]
		self.stopped = 0
		
	def Stop(self):
		del self.explosions
		self.stopped = 1
		
	def Reset(self):
		self.explosions = []
		self.score = 0
		self.b52 = [76, -220]
		self.stopped = 1
		
	def Update(self, ms):
		newexp = 0
		
		if(self.b52[1] > 770):
			self.Stop()
			return 0
		else:
			for exp in self.explosions:
				pos = exp[0]
				globals.UpdateRegions.append( [pos[0], pos[1], 64, 64] )
				exp[1].Update(ms)
			
			globals.UpdateRegions.append( [self.b52[0], self.b52[1], 256, 216] )
			self.b52 = [self.b52[0], self.b52[1] + 3]
			if( int(random.random() * 10.0) == 5 ):
				self.AddExplosion()
				self.score += globals.ScoreSpinner
				newexp += 1
		
		return newexp
		
	def Draw(self):
		
		for anim in self.explosions:
			frame = anim[1].count
			x = frame % 4
			y = frame / 4
			globals.screen.blit(self.images[1], anim[0], [x * 64, y * 64, 64, 64])
			
		globals.screen.blit(self.images[0], self.b52)
	