enemies = {}

dofile( "scripts/enemies/redstuff.lua" )
dofile( "scripts/enemies/bluestuff.lua" )
dofile( "scripts/enemies/bonusstuff.lua" )
dofile( "scripts/enemies/redboomy.lua" )
dofile( "scripts/enemies/boss_si.lua" )
dofile( "scripts/enemies/starthingy.lua" )
dofile( "scripts/enemies/redrayshooter.lua" )
dofile( "scripts/enemies/orangestuff.lua" )
dofile( "scripts/enemies/redstarthingy.lua" )
dofile( "scripts/enemies/orangerayshooter.lua" )
dofile( "scripts/enemies/orangeboomy.lua" )
dofile( "scripts/enemies/purplestuff.lua" )
dofile( "scripts/enemies/boss_pear.lua" )
dofile( "scripts/enemies/greenstuff.lua" )
dofile( "scripts/enemies/longthingy.lua" )
dofile( "scripts/enemies/graystuff.lua" )
dofile( "scripts/enemies/greenrayshooter.lua" )
dofile( "scripts/enemies/boss_cross.lua" )
dofile( "scripts/enemies/bulbs.lua" )

function ProcessEnemies()
	for key, value in pairs( enemies ) do
		for i=1,players_num do
			if intersect( value, players[i] ) and players[i].mortal and players[i].sprite and not value.untouchable then
				PlayerHit( i )
			end
		end
		value.x = value.x + value.speed * value.vel[1]
		value.y = value.y + value.speed * value.vel[2]
		if value.sprite then
			SetObjPos( value.sprite, value.x, value.y )
		end
		value.process( value, key )
	end
end

function EnemyHit( k, pl )
	local v = enemies[k]
	if not v.immortal and not is_dead(enemies, k)  then
		if not v.health or v.health == 1 then
			if v.destroy then
				v.destroy( v.x, v.y, v, k )
			end
			CreateExplosion( v.x, v.y )
			PlaySnd( "enemy_die.wav", true, v.x, v.y )
			delete( enemies, k )
			pl.score = pl.score + (v.score or 0)
			if ( v.score or 0 ) > 0 then
				Resume( NewThread( function()
					local wd = GetCaptionSize( "default", v.score )
					local x, y = v.x-wd/2, v.y
					local w = CreateWidget( constants.wt_Label, "SCORE", nil, x, y, 1, 1 )
					useless_widgets[w] = w
					WidgetSetFixedPosition( w, false )
					WidgetSetCaption( w, v.score )
					for i=0,100 do
						WidgetSetPos( w, x, y-i/10 )
						WidgetSetCaptionColor( w, {1, 1, 1, 1-0.01*i}, false )
						Wait(10)
					end
					table.remove( useless_widgets, w )
					DestroyWidget( w )
				end ))
			end
			if not v.noenergy then
				CreateEnergy( v.x, v.y )
			end
		else
			PlaySnd( "hit.wav", true, v.x, v.y )
			v.health = v.health - 1
			if v.hit then v.hit(v, k) end
		end
	end
end

function ResetEnemies()
	enemies = {}
end
