summaryrefslogtreecommitdiff
path: root/particle.bmx
blob: da31401596ead8ea7b255ab4d871309ce1ccf638 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
' Missile Lock
'
' Copyright (C) 2006  Ian Cowburn (ianc@noddybox.co.uk)
'
' This program is free software; you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation; either version 2 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
'
' -------------------------------------------------------------------------
'
' $Id$
'
Strict
Import "global.bmx"

Type TParticle
	Field life:Int
	Field i:TImage
	Field x:Double
	Field y:Double
	Field a:Double
	Field dx:Double
	Field dy:Double
	Field ai:Double
	Field s:Double
	Field si:Double
	Field r:Int
	Field g:Int
	Field b:Int
	
	Function Image:TParticle(i:TImage, x:Double, y:Double, dx:Double, dy:Double, r:Int, g:Int, b:Int)
		Local o:TParticle=New TParticle
		o.life=120
		o.x=x
		o.y=y
		o.a=1
		o.ai=-0.01
		o.dx=dx
		o.dy=dy
		o.s=1
		o.si=0
		o.i=i
		o.r=r
		o.g=g
		o.b=b
		Return o
	End Function
	
	Function ScaleImage:TParticle(i:TImage, x:Double, y:Double, dx:Double, dy:Double, si:Double, r:Int, g:Int, b:Int)
		Local o:TParticle=New TParticle
		o.life=120
		o.x=x
		o.y=y
		o.a=1
		o.ai=-0.01
		o.dx=dx
		o.dy=dy
		o.s=1
		o.si=si
		o.i=i
		o.r=r
		o.g=g
		o.b=b
		Return o
	End Function
	
	Method Update:Int()
		SetAlpha(a)
		SetScale(s,s)
		SetColor(r,g,b)
		DrawImage(i,x,y)
		x:+dx
		y:+dy
		life:-1
		a:+ai
		s:+si
		Return life>0
	End Method
End Type

Type Particles
	Global plist:TList
	
	Function Init()
		plist=CreateList()
	End Function
	
	Function Clear()
		plist.Clear()
	End Function
	
	Function AddImage(i:TImage, x:Double, y:Double, dx:Double=0, dy:Double=0)
		plist.AddLast(TParticle.Image(i,x,y,dx,dy,255,255,255))
	End Function
	
	Function AddScaledImage(i:TImage, x:Double, y:Double, si:Double=0.1, dx:Double=0, dy:Double=0)
		plist.AddLast(TParticle.ScaleImage(i,x,y,dx,dy,si,255,255,255))
	End Function
	
	Function AddExplosion(x:Double, y:Double)
		For Local f:Int=0 To 20
			plist.AddLast(TParticle.Image(GFX.star,x,y,Rnd(-2,2),Rnd(-2,2),Rand(100,255),Rand(100,255),Rand(100,255)))
		Next
	End Function
	
	Function AddBigExplosion(x:Double, y:Double)
		plist.AddLast(TParticle.ScaleImage(GFX.fireball,x,y,0,0,0.1,255,255,255))
		For Local f:Int=0 To 50
			plist.AddLast(TParticle.Image(GFX.star,x,y,Rnd(-2,2),Rnd(-2,2),Rand(100,255),Rand(100,255),Rand(100,255)))
		Next
	End Function
	
	Function Draw()
		Local l:TEasyLink=TEasyLink.Create(plist)
		
		While l.Value()
			Local p:TParticle=TParticle(l.Value())

			If (p.Update())
				l.MoveNext()
			Else
				l.Remove()
			EndIf
		Wend
		SetAlpha(1)
		SetScale(1,1)
		SetColor(255,255,255)
	End Function
End Type