Setalpha by distance traveled

BlitzMax Forums/BlitzMax Programming/Setalpha by distance traveled

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's one based on your code above, but changed a bit in places.

Rather than set a dx,dy using Ints, here we pass in a random direction (0-359), as well as a random speed. The random speed thing gives you a bigger "spread" of particles.

It now runs on a max number of frames, rather than distance, which it simply counts down, removing the particle when the count is 0.
Fading is based on a range of 1 -> 1/num frames.

SeedRnd MilliSecs()

Global ParticalList:TList = CreateList()


Type TPartical
	Field x:Float,y:Float,a:Float = 1,r:Int
	Field ox:Int,oy:Int
	Field img:TImage
	Field dx:Float,dy:Float, da:Float
	Field maxdistance:Int
	Field fade:Int
	
	Method Create:TPartical(_x:Int, _y:Int, _img:TImage, _dir:Int = 0, _speed:Float = 0, frames:Int = 0, _fade:Int = False)
		ListAddLast(ParticalList,Self)
		x = _x
		y = _y
		ox = _x
		oy = _y
		img = _img
		dx = Sin(_dir) * _speed
		dy = Cos(_dir) * _speed
		da = 1.0 / frames
		maxdistance = frames
		
'		If mxdist
'			maxdistance = mxdist
'		End If
		If _fade
			fade = True
		End If
	End Method
	
	Method Update:TPartical()
		x:+dx
		y:+dy
		If fade a:-da
		SetRotation(r)
		SetAlpha(a)
		DrawImage img,x,y
		SetRotation(0)
		SetAlpha(1)
		
		maxdistance:- 1
		If Not maxdistance Then
			Destroy()
		End If
		
		'If fade
		'	a = DistanceTraveled()/maxdistance
		'End If
	End Method
	
	Method SetPAlpha(alpha:Float)
		a = alpha
	End Method
	
	Method SetPRotation(rot:Int)
		r = rot
	End Method
	
	Method Destroy()
		ParticalList.Remove(Self)
	End Method
	
	Method SetX(_x:Int)
		x = _x
	End Method
	
	Method SetY(_y:Int)
		y = _y
	End Method
	
	Method DistanceTraveled:Float()
		Local returnx:Float=Abs(ox-y)
		Local returny:Float=Abs(oy-y)
		Return returnx+returny	
	End Method
	
	
End Type


'Partical explosion function; creates a explosion of particals
Function ParticalExplosion(_x:Int, _y:Int, image:TImage, n:Int, frames:Int)
	
	For Local i:Int = 1 To n
		Local dir:Int = Rand(0, 359)
		Local speed:Float = Rnd(0.08, 1.5)
		Local part:TPartical = New TPartical.Create(_x, _y, image, dir, speed, frames - 10 + Rand(20), True)
	Next
	
End Function

AppTitle = "Partical test - click to fire"
Graphics 800,600,0
SetBlend alphablend

'Create a dot image (our partical)
Global dot:TImage = CreateImage(5, 5) 
DrawOval 0, 0, 5, 5
GrabImage(dot,0,0)

While Not KeyHit(KEY_ESCAPE)
Cls
	For Local UpdateParticals:TPartical = EachIn ParticalList
		UpdateParticals.Update()
	Next
	If MouseHit(1) ParticalExplosion(MouseX(), MouseY(), dot, 50, 100)
Flip
Wend
End