summaryrefslogtreecommitdiff
path: root/game.bmx
blob: 8b8830088279b3ea4b79f30901affc5908acf96d (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
' Vectoroids
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vector
Import noddybox.bitmapfont
Import noddybox.vectorgfx
Import "types.bmx"
Import "sounds.bmx"

Type TGame

	Field score:Int
	Field astlist:TList
	Field bullets:TList
	Field lives:Int
	Field ship:TVectorGfxObject
	Field lifeship:TVectorGfxObject
	Field shotdelay:Int
	Field deaddelay:Int
	Field x:Double
	Field y:Double
	Field dx:Double
	Field dy:Double
	Field waiting:Int
	Field freq:Int
	Field hit:Int
	Field hyperdelay:Int
	Field waitdelay:Int
	Field leveldelay:Int
	Field minspeed:Double
	Field maxspeed:Double
	Field numast:Int
	Field extralife:Int
	Field saucerdelay:Int
	Field saucertime:Int
	Field saucershot:Int
	Field saucer:TSaucer
	
	Method New()
		score=0
		astlist=CreateList()
		bullets=CreateList()
		lives=2
		ship=GameGFX.ship.Clone()
		lifeship=GameGFX.ship.Clone()
		deaddelay=250
		waiting=True
		waitdelay=500
		x=GraphicsWidth()/2
		y=GraphicsHeight()/2
		dx=0
		dy=0
		ship.ang=0
		ship.x=x
		ship.y=y
		leveldelay=0
		extralife=10000
		
		lifeship.y=5
		lifeship.scale=0.5
		
		minspeed=0.1
		maxspeed=1.0
		numast=5
	
		saucertime=120*50
		saucerdelay=0
		saucershot=150
		
		NewLevel()
	End Method
	
	Method NewLevel()
		TAsteroid.minspeed=minspeed
		TAsteroid.maxspeed=maxspeed
		For Local f:Int=0 Until numast
			astlist.AddLast(TAsteroid.Create())
		Next
		freq=30
		hit=0
		bullets.Clear()
		shotdelay=0
		hyperdelay=0
	End Method
	
	Method CheckShipLaunch()
		Local x1:Int=GraphicsWidth()/2-100
		Local y1:Int=GraphicsHeight()/2-100
		Local x2:Int=GraphicsWidth()/2+100
		Local y2:Int=GraphicsHeight()/2+100
		
		If waitdelay=0 Or KeyHit(GameConfig.khyper)
			FlushKeys()
			waiting=False
		EndIf
		
		waitdelay:-1
		
		For Local a:TAsteroid=EachIn astlist
			If saucer Or (a.x>x1 And a.x<x2 And a.y>y1 And a.y<y2)
				SetAlpha(0.7-Float(waitdelay)/1000.0)
				ship.Draw()
				SetColor(255,0,0)
				DrawLine(x1,y1,x2,y1)
				DrawLine(x2,y1,x2,y2)
				DrawLine(x2,y2,x1,y2)
				DrawLine(x1,y2,x1,y1)
				SetColor(255,255,255)
				SetAlpha(1)
				
				GameGFX.font.Centre("press hyperspace to launch early",150)
		
				Return
			EndIf
		Next
		
		waiting=False
	End Method
	
	Method Pause()
		Local i:Timage=CreateImage(GraphicsWidth(),GraphicsHeight(),1,MASKEDIMAGE|DYNAMICIMAGE|FILTEREDIMAGE)
		
		GrabImage(i,0,0)
		MidHandleImage(i)
		
		Local a:Int[]=[0,0,0,0,0,0,0,0]
		Local ac:Int[]=[7,6,5,4,3,2,1,0]
		
		Sounds.Pause()
		
		While Not KeyHit(GameConfig.kpause)
			Cls
			
			Local al:Double=0.3
			
			For Local f:Int=0 Until a.length
				SetAlpha(al)
				SetRotation(a[f])
				SetScale(al,al)
				DrawImage(i,GraphicsWidth()/2,GraphicsHeight()/2)
				If ac[f]>0
					ac[f]:-1
				Else
					a[f]:+1
				EndIf
				al:+0.1
			Next
			
			SetRotation(0)
			SetScale(1,1)
			GameGFX.font.Centre("paused",GraphicsHeight()/2-16)
			
			Flip
		Wend
		
		SetAlpha(1)
		SetRotation(0)
		SetScale(1,1)
		
		FlushKeys
		Sounds.EndPause()
	End Method
	
	Method Play:Int()
		Cls
		
		If waiting And lives>=0
			CheckShipLaunch()
		EndIf
		
		If astlist.Count()=0
			If leveldelay=0
				leveldelay=200
			Else
				leveldelay:-1
				If leveldelay=0
					numast=Min(20,numast+2)
					minspeed=Min(0.5,minspeed+0.05)
					maxspeed=Min(2.0,maxspeed+0.05)
					saucertime=Max(10,saucertime-20)
					saucershot=Max(25,saucershot-10)
					NewLevel()
				EndIf
			EndIf
		EndIf
		
		If lives>-1 And saucer=Null
			saucerdelay:+1
			
			If saucerdelay>saucertime
				saucerdelay=0
				If score<10000
					saucer=TSaucer.Create(True,saucershot)
				ElseIf score<20000
					saucer=TSaucer.Create(Rand(0,1),saucershot)
				Else
					saucer=TSaucer.Create(False,saucershot)
				EndIf
			EndIf
		EndIf
				
		
		If lives>-1 And Not waiting
			If KeyDown(GameConfig.kleft)
				ship.ang:-20
				If ship.ang<0
					ship.ang:+3600
				EndIf
			EndIf
	
			If KeyDown(GameConfig.kright)
				ship.ang=(ship.ang+20) Mod 3600
			EndIf
	
			If KeyDown(GameConfig.kthrust)
				Sounds.PlayerThrust(True)
				dx=Min(2,Max(-2,dx+Lookup.si[ship.ang]*0.1))
				dy=Min(2,Max(-2,dy+Lookup.co[ship.ang]*0.1))
			Else
				Sounds.PlayerThrust(False)
			EndIf
	
			If KeyDown(GameConfig.khyper) And hyperdelay=0
				x=Rand(10,GraphicsWidth()-10)
				y=Rand(10,GraphicsHeight()-10)
				dx=0
				dy=0
				hyperdelay=100
			EndIf
			
			If KeyDown(GameConfig.kfire) And shotdelay=0
				Sounds.PlayerFire()
				bullets.AddLast(TShot.Create(ship))
				shotdelay=25
			EndIf
			
			If shotdelay>0
				shotdelay:-1
			EndIf
			
			If hyperdelay>0
				hyperdelay:-1
			EndIf
			
			If KeyHit(KEY_ESCAPE)
				lives=-1
				TParticleMachine.AsteroidExplosion(ship.x,ship.y)
				Sounds.ShipExplosion()
			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()
				y:-GraphicsHeight()
			EndIf
			
			ship.x=x
			ship.y=y
		EndIf
		
		SetAlpha(0.7)

		For Local a:TAsteroid=EachIn astlist
			a.Update()
		Next
		
		For Local f:Int=1 To lives
			lifeship.x=f*9
			lifeship.Draw()
		Next
		
		SetAlpha(1)
		
		GameGFX.font.Centre(score,0)
		SetColor(255,255,255)
		If lives<0
			For Local s:TShot=EachIn bullets
				s.Update(bullets,astlist,saucer)
			Next
		Else
			For Local s:TShot=EachIn bullets
				Local sc:Int=s.Update(bullets,astlist,saucer)
				score:+sc
				
				If sc
					If score>extralife
						If extralife=10000
							extralife=50000
						Else
							extralife:+50000
						EndIf
						lives:+1
						Sounds.ExtraLife()
					EndIf
					
					hit:+1
					If hit>3
						hit=0
						If freq>15
							freq:-1
						EndIf
					EndIf
				EndIf
			Next
		EndIf
		
		Local hit:Int=False
		
		If saucer
			SetAlpha(0.7)
			If lives>-1 And Not waiting
				hit=saucer.Update(ship)
			Else
				saucer.Update(Null)
			EndIf

			SetAlpha(1)
			
			If saucer.Finished()
				saucer=Null
				Sounds.NoSaucer()
			EndIf
		EndIf
		
		If lives<0
			deaddelay:-1
			GameGFX.font.Centre("game over",292)
		Else
			If Not waiting
				SetAlpha(0.7)
				
				If hit Or Collides(ship.DrawToPointList())
					TParticleMachine.ShipExplosion(ship.x,ship.y)
					Sounds.ShipExplosion()

					lives:-1
					x=GraphicsWidth()/2
					y=GraphicsHeight()/2
					dx=0
					dy=0
					ship.ang=0
					ship.x=x
					ship.y=y
					waiting=True
					waitdelay=500
					FlushKeys()
				EndIf

				SetAlpha(1)
			EndIf
		EndIf
		
		TParticleMachine.Process()
		Sounds.Update(freq)
		
		Flip
		
		If KeyHit(GameConfig.kpause)
			Pause()
		EndIf
		
		Return deaddelay>0
	End Method	
	
	Method Collides:Int(list:TList)
		For Local a:TAsteroid=EachIn astlist
			For Local p:TVectorGfxPoint=EachIn list
				If a.obj.IsInside(p.x,p.y)
					Return True
				EndIf
			Next
		Next
		
		Return False
	End Method

End Type