summaryrefslogtreecommitdiff
path: root/types.bmx
blob: caf62adcf777772e75a2a42bff6c92e918b720a7 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
' Particle Pinch
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vector
Import noddybox.bitmapfont

Const MASSSIZE:Int=8
Const MASSRAD:Int=4
Const SHIPSIZE:Int=12
Const SHIPRAD:Int=6

Type Lookup
	Global si:Double[]
	Global co:Double[]
	
	Function Init()
		si=New Double[360]
		co=New Double[360]
		
		For Local a:Int=0 To 359
			si[a]=Sin(a)
			co[a]=Cos(a)
		Next
	End Function
End Type

Type GameGFX
	Global font:TBitmapFont
	Global smallfont:TBitmapFont
	Global star:TImage
	Global ship:TImage
	Global collector:TImage
	Global point:TImage
	Global particle:Timage
	Global pointer:TImage
	Global shock: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
	Global keys_button:TImage
End Type

Type GameConfig
	Global kleft:Int
	Global kright:Int
	Global kthrust:Int
	Global kreverse:Int
	Global kblast:Int
	
	Function Load()
		Local s:TStream=ReadStream("ppinch.config")
		
		If s=Null
			kleft=KEY_O
			kright=KEY_P
			kthrust=KEY_Q
			kreverse=KEY_A
			kblast=KEY_SPACE
			Return
		EndIf
		
		s=LittleEndianStream(s)
		
		kleft=s.ReadInt()
		kright=s.ReadInt()
		kthrust=s.ReadInt()
		kreverse=s.ReadInt()
		kblast=s.ReadInt()
		
		s.Close()
	End Function
	
	Function Save()
		Local s:TStream=WriteStream("ppinch.config")
		
		If s=Null
			Return
		EndIf
		
		s=LittleEndianStream(s)
		
		s.WriteInt(kleft)
		s.WriteInt(kright)
		s.WriteInt(kthrust)
		s.WriteInt(kreverse)
		s.WriteInt(kblast)
		
		s.Close()
	End Function
EndType

Type TGfxObject
	Field x:Double
	Field y:Double
EndType

Type TMass Extends TGfxObject
	Field v:TVector
	Field mass:Double
	Field friend:Int
	Field inverse:Int
	Field img:TImage
	Field swallow:Int
	Field rad:Int
	
	Method New()
		v=TVector.Create()
		x=Rnd(0,GraphicsWidth())
		y=Rnd(0,GraphicsWidth())
		mass=25
		friend=True
		inverse=False
		swallow=0
		rad=MASSRAD
	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 v.Length()>MASSSIZE
				v.SetLength(MASSSIZE)
			EndIf
		EndIf
	End Method
	
	Method Move(wrap:Int)
		x:+v.x
		y:+v.y

		If wrap
			If x<0 
				x:+GraphicsWidth()
			EndIf
			If x>GraphicsWidth()
				x:-GraphicsWidth()
			EndIf
			If y<0 
				y:+GraphicsHeight()
			EndIf
			If y>GraphicsHeight() 
				y:-GraphicsHeight()
			EndIf
		Else
			If x<0 
				x=0
			EndIf
			If x>GraphicsWidth()
				x=GraphicsWidth()
			EndIf
			If y<0 
				y=0
			EndIf
			If y>GraphicsHeight() 
				y=GraphicsHeight()
			EndIf
		EndIf
	End Method

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


Type TShip Extends TMass
	Field a:Int
	
	Method New()
		v=TVector.Create()
		x=-1
		y=-1
		mass=25
		friend=False
		inverse=False
		swallow=0
		rad=SHIPRAD
		a=0
	End Method
	
	Method RotateLeft()
		a:-2
		If a<0
			a:+359
		EndIf
	End Method
	
	Method RotateRight()
		a=(a+2) Mod 360
	End Method
	
	Method Thrust()
		v.x:+Lookup.si[a]*0.1
		v.y:+Lookup.co[a]*-0.1
		If v.Length()>MASSSIZE
			v.SetLength(MASSSIZE)
		EndIf
	End Method
	
	Method Reverse()
		v.x:+Lookup.si[a]*-0.05
		v.y:+Lookup.co[a]*0.05
		If v.Length()>MASSSIZE
			v.SetLength(MASSSIZE)
		EndIf
	End Method
	
	Method Draw()
		SetColor(255,255,255)
		SetRotation(a)
		DrawImage(img,x,y)
		SetRotation(0)
		swallow=0
	End Method
End Type


Type TPoint Extends TGfxObject
	Global img:TImage
	
	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<o.rad
			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 v.Length()>MASSRAD
				v.SetLength(MASSRAD)
			EndIf
		EndIf
		
		Return dead
	End Method
	
	Method Move(wrap:Int=False)
		If (Not dead) And (Not lost)
			lx=x
			ly=y
			x:+v.x
			y:+v.y
			
			If wrap
				If x<0 
					x:+GraphicsWidth()
				EndIf
				If x>GraphicsWidth()
					x:-GraphicsWidth()
				EndIf
				If y<0 
					y:+GraphicsHeight()
				EndIf
				If y>GraphicsHeight() 
					y:-GraphicsHeight()
				EndIf
			Else
				If x<0 Or y<0 Or x>GraphicsWidth() Or y>GraphicsHeight()
					lost=True
					TParticleMachine.AddLost(Self)
				EndIf
			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 parent:TGfxObject
	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
	
	Function Angular:TParticle(x:Int, y:Int, a:Int, sp:Double, r:Int, g:Int, b:Int, al:Double, ali:Double, parent:TGfxObject)
		Local o:TParticle=New TParticle
		o.parent=parent
		o.x=x
		o.y=y
		o.dx=sp*Lookup.si[a]
		o.dy=sp*Lookup.co[a]
		o.r=r
		o.g=g
		o.b=b
		o.a=al
		o.ai=ali
		Return o
	End Function
	
	Method Update()
		x:+dx
		y:+dy
		a:+ai
		
		If a>0
			SetAlpha(a)
			SetColor(r,g,b)
			
			If parent
				DrawImage(img,parent.x+x,parent.y+y)
			Else
				DrawImage(img,x,y)
			EndIf
		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 AddShockwave(o:TGfxObject)
		For Local a:Int=0 To 359
			list.AddLast(TParticle.Angular(0,0,a,1.0,255,255,0,1,-0.01,o))
			list.AddLast(TParticle.Angular(0,0,a,Rnd(0.8,1.2),255,0,0,1,-0.01,o))
			list.AddLast(TParticle.Angular(0,0,a,Rnd(0.8,1.2),255,0,0,1,-0.01,o))
			list.AddLast(TParticle.Angular(0,0,a,Rnd(0.8,1.2),255,0,0,1,-0.01,o))
		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