summaryrefslogtreecommitdiff
path: root/particle.bmx
blob: 6eba5d3a679c393198bb3dbddd84c39c6bc86eab (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
' Hardwire
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import "types.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
	
	Function Dust:TParticle(x:Int, y:Int)
		Local o:TParticle=New TParticle
		o.life=50
		o.x=x
		o.y=y
		o.a=1
		o.ai=-0.05
		o.dx=0
		o.dy=0
		o.s=1
		o.si=0.1
		o.i=GameGFX.dust
		Return o
	End Function

	Function Image:TParticle(i:TImage, x:Int, y:Int)
		Local o:TParticle=New TParticle
		o.life=120
		o.x=x
		o.y=y
		o.a=1
		o.ai=-0.01
		o.dx=0
		o.dy=-0.1
		o.s=1
		o.si=0
		o.i=i
		Return o
	End Function
	
	Method Update:Int()
		SetAlpha(a)
		SetScale(s,s)
		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 AddDust(x:Int, y:Int)
		plist.AddLast(TParticle.Dust(x,y))
	End Function
	
	Function AddImage(i:TImage, x:Int, y:Int)
		plist.AddLast(TParticle.Image(i,x,y))
	End Function
	
	Function Draw()
		SetColor(255,255,255)
		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)
	End Function
End Type

Type TTextParticle
	Field txt:String
	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
	Field fnt:TBitmapFont
	
	Function Create:TTextParticle(fnt:TBitmapFont, txt:String, x:Double, y:Double, dx:Double, dy:Double, r:Int, g:Int, b:Int, a:Double, ai:Double, s:Double, si:Double)
		Local o:TTextParticle=New TTextParticle
		
		If x=-1
			x=GraphicsWidth()/2
		EndIf
		
		o.x=x
		o.y=y
		o.a=a
		o.ai=ai
		o.dx=dx
		o.dy=dy
		o.s=s
		o.si=si
		o.txt=txt
		o.r=r
		o.g=g
		o.b=b
		o.fnt=fnt
		Return o
	End Function

	Method Update:Int()
		SetAlpha(a)
		SetScale(s,s)
		fnt.CentreOn(txt,x,y,r,g,b)
		x:+dx
		y:+dy
		a:+ai
		s:+si
		Return a>0
	End Method
End Type

Type TextParticles
	Global plist:TList
	Global sy:Int
	Const sx:Int=646
	
	Function Init()
		plist=CreateList()
		sy=300
	End Function
	
	Function Clear()
		plist.Clear()
		sy=300
	End Function
	
	Function Big(txt:String, r:Int, g:Int, b:Int)
		plist.AddLast(TTextParticle.Create(GameGFX.font,txt,-1,GraphicsHeight()-GameGFX.font.MaxHeight()/2,0,0,r,g,b,1,-0.01,1,0))
		plist.AddLast(TTextParticle.Create(GameGFX.font,txt,-1,GraphicsHeight()-GameGFX.font.MaxHeight()/2,0,0,r,g,b,1,-0.02,1,0.1))
	End Function
	
	Function Score(txt:String)
		Local r:Int=Rand(128,255)
		Local g:Int=Rand(128,255)
		Local b:Int=Rand(128,255)
		
		plist.AddLast(TTextParticle.Create(GameGFX.font,txt,sx,sy,0,0,r,g,b,1,-0.01,1,0))
		plist.AddLast(TTextParticle.Create(GameGFX.font,txt,sx,sy,0,0,r,g,b,1,-0.02,1,0.1))
		
		sy:+GameGFX.font.MaxHeight()
		
		If sy>GraphicsHeight()-20
			sy=300
		EndIf
	End Function
	
	Function Draw()
		Local l:TEasyLink=TEasyLink.Create(plist)
		
		While l.Value()
			Local p:TTextParticle=TTextParticle(l.Value())

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