summaryrefslogtreecommitdiff
path: root/types.bmx
blob: 2f2175a7b0c3814734e1634c77607ac3de231b17 (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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
' Vectoroids
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vectorgfx
Import noddybox.vector
Import noddybox.bitmapfont

Import "sounds.bmx"

Incbin "GFX/font.bmf"
Incbin "2D/ship.2d"
Incbin "2D/large.2d"
Incbin "2D/medium.2d"
Incbin "2D/small.2d"
Incbin "2D/ssaucer.2d"
Incbin "2D/lsaucer.2d"
Incbin "2D/debris.2d"

Type Lookup
	Global si:Double[]
	Global co:Double[]
	
	Function Init()
		si=New Double[3600]
		co=New Double[3600]
		
		For Local a:Int=0 To 3599
			si[a]=Sin(180-Double(a)/10)
			co[a]=Cos(180-Double(a)/10)
		Next
	End Function
End Type

Type GameGFX
	Global font:TBitmapFont
	Global ship:TVectorGfxObject
	Global large:TVectorGfxObject
	Global medium:TVectorGfxObject
	Global small:TVectorGfxObject
	Global debris:TVectorGfxObject
	Global lsaucer:TVectorGfxObject
	Global ssaucer:TVectorGfxObject
	
	Function Init()
		font=TBitmapFont.Load("incbin::GFX/font.bmf",0)
		ship=TVectorGfxObject.Load("incbin::2D/ship.2d")
		large=TVectorGfxObject.Load("incbin::2D/large.2d")
		medium=TVectorGfxObject.Load("incbin::2D/medium.2d")
		small=TVectorGfxObject.Load("incbin::2D/small.2d")
		debris=TVectorGfxObject.Load("incbin::2D/debris.2d")
		lsaucer=TVectorGfxObject.Load("incbin::2D/lsaucer.2d")
		ssaucer=TVectorGfxObject.Load("incbin::2D/ssaucer.2d")
		
		large.scale=1.5
		medium.scale=1.5
		small.scale=1.5

		lsaucer.scale=1.5
		ssaucer.scale=2
	End Function
End Type

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

Type TAsteroid
	Global minspeed:Double
	Global maxspeed:Double
	
	Field x:Double
	Field y:Double
	Field dx:Double
	Field dy:Double
	Field obj:TVectorGfxObject
	Field size:Int
	
	Function RndSpeed:Double()
		Local r:Double=Rnd(minspeed,maxspeed)
		If Rand(100)<50
			Return r
		Else
			Return -r
		EndIf
	End Function

	Function Create:TAsteroid()
		Local no:TAsteroid=New TAsteroid
		no.obj=GameGFX.large.Clone()
		no.obj.ang=Rand(3600)
		If Rand(10)<5
			no.x=Rnd(0,GraphicsWidth()/3.0)
		Else
			no.x=Rnd(GraphicsWidth()/3.0*2.0,GraphicsWidth())
		EndIf
		no.y=Rnd(0,GraphicsHeight())
		no.dx=RndSpeed()/2
		no.dy=RndSpeed()
		no.size=2
		Return no
	End Function
	
	Method CreateShard:TAsteroid()
		Local no:TAsteroid=New TAsteroid
		no.x=x
		no.y=y
		no.size=size-1
		
		If no.size=1
			no.obj=GameGFX.medium.Clone()
		Else
			no.obj=GameGFX.small.Clone()
		EndIf

		no.dx=RndSpeed()
		no.dy=RndSpeed()
		
		no.x:+no.dx*8
		no.y:+no.dy*8
		no.obj.ang=Rand(3600)
		
		Return no
	End Method
	
	Method Update()
		x:+dx
		y:+dy
		
		If x<-obj.width
			x:+GraphicsWidth()+obj.width*2
		ElseIf x>GraphicsWidth()+obj.width
			x:-GraphicsWidth()+obj.width*2
		EndIf
		
		If y<-obj.height
			y:+GraphicsHeight()+obj.height*2
		ElseIf y>GraphicsHeight()+obj.height
			y:-GraphicsHeight()+obj.height*2
		EndIf
		
		obj.x=x
		obj.y=y
		obj.Draw()
	End Method
	
	Method Blowup:Int(astlist:TList)
		Local sc:Int=100-size*20
		
		If size>0
			astlist.AddLast(CreateShard())
			astlist.AddLast(CreateShard())
		EndIf
		
		astlist.Remove(Self)
		
		Return sc
	End Method
EndType

Type TShot
	Field x:Double
	Field y:Double
	Field dx:Double
	Field dy:Double
	Field life:Int
	
	Function Create:TShot(ship:TVectorGfxObject)
		Local o:TShot=New TShot
		o.dx=Lookup.si[ship.ang]*2
		o.dy=Lookup.co[ship.ang]*2
		o.x=ship.x+o.dx*4
		o.y=ship.y+o.dy*4
		o.life=200
		Return o
	End Function 
	
	Method Update:Int(shotlist:TList, astlist:TList, saucer:TSaucer)
		life:-1

		If life=0
			shotlist.Remove(Self)
			Return 0
		EndIf
		
		x:+dx
		y:+dy
		
		If x<0
			x:+GraphicsWidth()
		EndIf

		If x>=GraphicsWidth()
			x:-GraphicsWidth()
		EndIf

		If y<0
			y:+GraphicsHeight()
		EndIf

		If y>=GraphicsHeight()
			x:-GraphicsHeight()
		EndIf
		
		If saucer And Not saucer.dead And saucer.obj.IsInside(x,y)
			TParticleMachine.ShipExplosion(saucer.x,saucer.y)
			Sounds.ShipExplosion()
			Sounds.NoSaucer()
			shotlist.Remove(Self)
			saucer.dead=True
			
			If saucer.big
				Return 500
			Else
				Return 1000
			EndIf
		EndIf

		For Local o:TAsteroid=EachIn astlist
			If o.obj.IsInside(x,y)
				TParticleMachine.AsteroidExplosion(o.x,o.y)
				Sounds.AsteroidExplosion(o.size)
				shotlist.Remove(Self)
				Return o.Blowup(astlist)
			EndIf
		Next
		
		SetColor(255,255,255)
		Plot(x,y)
		
		Return 0
	End Method
End Type

Type TEnemyShot
	Field x:Double
	Field y:Double
	Field dx:Double
	Field dy:Double
	Field life:Int
	
	Function Create:TEnemyShot(ship:TVectorGfxObject, v:TVector)
		Local o:TEnemyShot=New TEnemyShot
		o.dx=v.x
		o.dy=v.y
		o.x=ship.x
		o.y=ship.y
		o.life=250
		Return o
	End Function 
	
	Method Update:Int(shotlist:TList, ship:TVectorGfxObject)
		life:-1

		If life=0
			shotlist.Remove(Self)
			Return False
		EndIf
		
		x:+dx
		y:+dy
		
		If x<0
			x:+GraphicsWidth()
		EndIf

		If x>=GraphicsWidth()
			x:-GraphicsWidth()
		EndIf

		If y<0
			y:+GraphicsHeight()
		EndIf

		If y>=GraphicsHeight()
			x:-GraphicsHeight()
		EndIf

		If ship And ship.IsInside(x,y)
			Sounds.ShipExplosion()
			shotlist.Remove(Self)
			Return True
		EndIf
		
		SetColor(255,255,255)
		Plot(x,y)
		
		Return False
	End Method
End Type

Type TSaucer
	Field x:Double
	Field y:Double
	Field dx:Double
	Field dy:Double
	Field dirx:Int
	Field dirchange:Int
	Field shotdel:Int
	Field shotwait:Int
	Field shotlist:TList
	Field big:Int
	Field obj:TVectorGfxObject
	Field dead:Int
	
	Function Create:TSaucer(big:Int, shotdel:Int)
		Local o:TSaucer=New TSaucer
	
		o.dead=False	
		o.dirx=Rand(100)<50
		
		If o.dirx
			If Rand(100)<50
				o.x=0
				o.dx=Rnd(1,2)
			Else
				o.x=GraphicsWidth()-1
				o.dx=Rnd(-2,-1)
			EndIf
			o.y=Rand(10,GraphicsHeight()-10)
		Else
			If Rand(100)<50
				o.y=0
				o.dy=Rnd(1,2)
			Else
				o.y=GraphicsHeight()-1
				o.dy=Rnd(-2,-1)
			EndIf
			o.x=Rand(10,GraphicsWidth()-10)
		EndIf
		
		o.Turn()
		
		o.shotlist=New TList
		o.big=big
		o.shotdel=shotdel
		o.shotwait=Rand(shotdel)
		
		If big
			Sounds.LargeSaucer()
			o.obj=GameGFX.lsaucer.Clone()
		Else
			Sounds.SmallSaucer()
			o.obj=GameGFX.ssaucer.Clone()
		EndIf
		
		Return o
	End Function
	
	Method Turn()
		dirchange=Rand(10,100)
		If dirx
			dy=Rnd(-2,2)
		Else
			dx=Rnd(-2,2)
		EndIf
	End Method
	
	Method Fire(ship:TVectorGfxObject)
		shotwait=0
		Local v:TVector
		
		If big
			v=TVector.Create(ship.x-x+Rnd(-40,40),ship.y-y+Rnd(-40,40))
		Else
			v=TVector.Create(ship.x-x+Rnd(-10,10),ship.y-y+Rnd(-10,10))
		EndIf
		
		v.SetLength(2)
		
		Sounds.SaucerFire()
		
		shotlist.AddLast(TEnemyShot.Create(obj,v))
	End Method
	
	Method OffScreen:Int()
		If dirx
			Return x<0 Or x>GraphicsWidth()
		Else
			Return y<0 Or y>GraphicsHeight()
		EndIf
	End Method
	
	Method Finished:Int()
		Return shotlist.Count()=0 And (dead Or OffScreen())
	End Method	
	
	' Returns True if ship hit by saucer's bullets
	'
	Method Update:Int(ship:TVectorGfxObject)
		If Not dead
			x:+dx
			y:+dy
			
			If dirx
				If y<0
					y:+GraphicsHeight()
				ElseIf y>GraphicsHeight()
					y:-GraphicsHeight()
				EndIf
			Else
				If x<0
					x:+GraphicsWidth()
				ElseIf x>GraphicsWidth()
					x:-GraphicsWidth()
				EndIf
			EndIf
			
			dirchange:-1
			
			If dirchange=0
				Turn()
			EndIf
			
			obj.x=x
			obj.y=y
			obj.Draw()
			
			If ship And Not OffScreen()
				shotwait:+1
				If shotwait>shotdel
					Fire(ship)
				EndIf
			EndIf
		EndIf
		
		Local ret:Int=False
		
		For Local s:TEnemyShot=EachIn shotlist
			ret=s.Update(shotlist,ship) Or ret
		Next
		
		Return ret
	End Method
	
End Type


Type TParticle
	Field obj:TVectorGfxObject
	Field x:Double
	Field y:Double
	Field a:Double
	Field dx:Double
	Field dy:Double
	Field ai:Double
	
	Function CreatePoint:TParticle(x:Int, y:Int, v:TVector, ai:Double=-0.05)
		Local o:TParticle=New TParticle
		o.obj=Null
		o.x=x
		o.y=y
		o.dx=v.x+Rnd(-0.1,0.1)
		o.dy=v.y+Rnd(-0.1,0.1)
		o.a=1
		o.ai=ai
		Return o
	End Function
	
	Function CreateDebris:TParticle(x:Int, y:Int, v:TVector, ai:Double=-0.01)
		Local o:TParticle=New TParticle
		o.obj=GameGFX.debris.Clone()
		o.obj.ang=Rand(0,3599)
		o.x=x
		o.y=y
		o.dx=v.x+Rnd(-0.1,0.1)
		o.dy=v.y+Rnd(-0.1,0.1)
		o.a=1
		o.ai=-0.02
		Return o
	End Function
	
	Method Update()
		x:+dx
		y:+dy
		a:+ai
		
		If a>0
			SetAlpha(a)
			
			If obj
				obj.x=x
				obj.y=y
				obj.ang=(obj.ang+30) Mod 3600
				obj.Draw()
			Else
				Plot(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 AsteroidExplosion(x:Int,y:Int)
		For Local f:Int=0 To 20
			list.AddLast(TParticle.CreatePoint(x+Rand(-3,3),y+Rand(-3,3),TVector.Create(Rnd(-2,2),Rnd(-2,2))))
		Next
	End Function
	
	Function ShipExplosion(x:Int,y:Int)
		For Local f:Int=0 To 30
			list.AddLast(TParticle.CreatePoint(x+Rand(-1,1),y+Rand(-1,1),TVector.Create(Rnd(-2,2),Rnd(-2,2)),-0.02))
		Next
		For Local f:Int=0 To 3
			list.AddLast(TParticle.CreateDebris(x+Rand(-1,1),y+Rand(-1,1),TVector.Create(Rnd(-2,2),Rnd(-2,2)),-0.01))
		Next
	End Function
	
	Function Process()
		Local l:TLink=list.FirstLink()
		Local t:TLink
		
		SetColor(255,255,255)

		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)
	End Function
End Type