summaryrefslogtreecommitdiff
path: root/types.bmx
blob: 7fd66cac33f89cbe03945c4cc4c940bb25d41e33 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
' Particle Pinch
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vector
Import noddybox.bitmapfont

Const MASSSIZE:Int=6
Const MASSRAD:Int=3

Type GameGFX
	Global font:TBitmapFont
	Global smallfont:TBitmapFont
	Global star:TImage
	Global mass:TImage
	Global collector:TImage
	Global point:TImage
	Global particle:Timage
	Global pointer:TImage
	
	Global play_button:TImage
	Global edit_button:TImage
	Global load_button:TImage
	Global toy_button:TImage
	Global quit_button:TImage
	Global left_button:TImage
	Global right_button:TImage
	Global scores_button:TImage
End Type

Type TMass
	Field x:Double
	Field y:Double
	Field v:TVector
	Field mass:Double
	Field friend:Int
	Field inverse:Int
	Field img:TImage
	Field swallow:Int
	
	Method New()
		v=TVector.Create()
		x=Rnd(0,GraphicsWidth())
		y=Rnd(0,GraphicsWidth())
		mass=25
		friend=True
		inverse=False
		swallow=0
	End Method
	
	Method Attract(o:TMass)
		If o=Self
			Return
		EndIf
		
		Local d:TVector=TVector.Create(o.x-x,o.y-y)
		Local l:Double=d.Length()
		
		If l>0.1
			l:*l
			d.Normalise()
			d.Scale((1/l)*o.mass)
			
			If o.inverse
				d.Minus()
			EndIf
			
			v.Add(d)
			
			If d.Length()>MASSSIZE
				d.SetLength(MASSSIZE)
			EndIf
		EndIf
	End Method
	
	Method Move()
		x:+v.x
		y:+v.y
	End Method

	Method Draw()
		SetColor(255,255,255)
		DrawImage(img,x,y,swallow)
		swallow=0
	End Method
End Type


Type TPoint
	Global img:TImage
	
	Field x:Double
	Field y:Double
	Field lx:Double
	Field ly:Double
	Field v:TVector
	Field r:Int
	Field g:Int
	Field b:Int
	Field dead:Int
	Field lost:Int
	
	Method New()
		v=TVector.Create()
		x=Rnd(0,GraphicsWidth())
		y=Rnd(0,GraphicsWidth())
		lx=x
		ly=y
		r=Rand(100,200)
		g=Rand(100,200)
		b=Rand(100,200)
		dead=False
		lost=False
	End Method
	
	Method Attract:Int(o:TMass)
		If dead Or lost
			Return
		EndIf
		
		Local d:TVector=TVector.Create(o.x-x,o.y-y)
		Local l:Double=d.Length()
		
		If l<MASSRAD
			If o.friend
				dead=True
			Else
				lost=True
			EndIf
			TParticleMachine.AddCaptured(Self)
			o.swallow=1
		Else
			l:*l
			d.Normalise()
			d.Scale((1/l)*o.mass)

			If o.inverse
				d.Minus()
			EndIf
			
			v.Add(d)
			
			If d.Length()>MASSRAD
				d.SetLength(MASSRAD)
			EndIf
		EndIf
		
		Return dead
	End Method
	
	Method Move()
		If (Not dead) And (Not lost)
			lx=x
			ly=y
			x:+v.x
			y:+v.y
			
			If x<0 Or y<0 Or x>GraphicsWidth() Or y>GraphicsHeight()
				lost=True
				TParticleMachine.AddLost(Self)
			EndIf
		EndIf
	End Method

	Method Draw()
		If (Not dead) And (Not lost)
			SetColor(r,g,b)
			DrawImage(img,x,y,0)
		EndIf
	End Method
End Type


Type TParticle
	Global img:TImage
	Field x:Double
	Field y:Double
	Field a:Double
	Field r:Int
	Field g:Int
	Field b:Int
	Field dx:Double
	Field dy:Double
	Field ai:Double
	
	Function FromLostPoint:TParticle(p:TPoint, d:Int)
		Local o:TParticle=New TParticle
		o.x=p.lx'-p.v.x*d
		o.y=p.ly'-p.v.y*d
		o.dx=-p.v.x*d
		o.dy=-p.v.y*d
		o.r=p.r
		o.g=p.g
		o.b=p.b
		o.a=1
		o.ai=-0.05
		Return o
	End Function
	
	Function FromCapturedPoint:TParticle(p:TPoint, d:Int)
		Local o:TParticle=New TParticle
		o.x=p.x
		o.y=p.y
		o.dx=Rnd(-1,1)
		o.dy=Rnd(-1,1)
		o.r=p.r
		o.g=p.g
		o.b=p.b
		o.a=1
		o.ai=-0.05
		Return o
	End Function
	
	Function FromCoord:TParticle(x:Int, y:Int)
		Local o:TParticle=New TParticle
		o.x=x
		o.y=y
		o.dx=Rnd(-2,2)
		o.dy=Rnd(-2,2)
		o.r=Rand(128,255)
		o.g=Rand(128,255)
		o.b=Rand(128,255)
		o.a=1
		o.ai=-0.01
		Return o
	End Function
	
	Method Update()
		x:+dx
		y:+dy
		a:+ai
		
		If a>0
			SetAlpha(a)
			SetColor(r,g,b)
			DrawImage(img,x,y)
		EndIf
	End Method
End Type


Type TParticleMachine
	Global list:TList
	
	Function Init()
		list=CreateList()
	End Function
	
	Function Clear()
		list.Clear()
	End Function
	
	Function AddLost(p:TPoint)
		For Local f:Int=0 To 5
			list.AddLast(TParticle.FromLostPoint(p,f))
		Next
	End Function
	
	Function AddCaptured(p:TPoint)
		For Local f:Int=0 To 5
			list.AddLast(TParticle.FromCapturedPoint(p,f))
		Next
	End Function
	
	Function AddFirework(x:Int, y:Int)
		For Local f:Int=0 To 20
			list.AddLast(TParticle.FromCoord(x,y))
		Next
	End Function
	
	Function Process()
		Local l:TLink=list.FirstLink()
		Local t:TLink
		
		While l<>Null
			Local p:TParticle=TParticle(l.Value())
			p.Update()
			
			If p.a<0.01
				t=l.NextLink()
				l.Remove()
				l=t
			Else
				l=l.NextLink()
			EndIf
		Wend
		
		SetAlpha(1)
		SetColor(255,255,255)
	End Function
End Type