summaryrefslogtreecommitdiff
path: root/game.bmx
blob: 6dea78792840507f461dc5476a5fded033eb6f9f (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
' Particle Pinch
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vector
Import noddybox.bitmapfont
Import "types.bmx"
Import "level.bmx"

Type TGame

	Const LEVEL_NOTOVER:Int=	0
	Const LEVEL_WON:Int=		1
	Const LEVEL_LOST:Int=		2
	Const LEVEL_FINISHED:Int=	3
	Const LEVEL_CANCELLED:Int=	4
	
	Field level:TLevel
	Field ship:TShip
	Field mass:TList
	Field point:TList
	Field timer:Int
	Field num:Int
	Field lost:Int
	Field captured:Int
	Field done:Int
	Field frame:Int
	Field placed:Int
	Field txtoff:Int[]
	Field playing:Int
	Field col:Int
	Field coli:Int
	
	Field final_percent:Int
	Field pass_time:Int
	
	Function Create:TGame(level:TLevel)
		Local o:TGame=New TGame
		
		o.level=level
		o.mass=CreateList()
		o.point=CreateList()
		o.ship=o.level.CreatePlayfield(o.mass,o.point)
		o.mass.AddLast(o.ship)
		o.timer=o.level.timer
		o.num=o.point.Count()
		o.done=LEVEL_NOTOVER
		o.frame=0
		o.placed=0
		o.txtoff=[GameGFX.font.TextWidth("PARTICLES"),GameGFX.font.TextWidth("CAPTURED"),GameGFX.font.TextWidth("TIMER"),GameGFX.font.TextWidth("WAVES")]
		o.playing=False
		o.col=0
		o.coli=5
		o.final_percent=0
		o.pass_time=0
		
		TParticleMachine.Clear()
		
		FlushKeys()
		
		Return o
	End Function
	
	Method LabelMass(m:TMass)
		Local x:Int=m.x
		Local y:Int=m.y
		Local tx:Int
		Local lx:Int
		Local ty:Int
		Local ly:Int
		Local s:String
		Local l:Int
		
		If m.inverse
			s="-"+Int(m.mass)+" NEWTONS"
		Else
			s=Int(m.mass)+" NEWTONS"
		EndIf
		
		If m.friend
			s="COLLECTOR: " + s
		Else
			s="MASS: " + s
		EndIf
		
		l=GameGFX.smallfont.TextWidth(s)
		
		ly=y
		ty=Max(0,Min(GraphicsHeight()-GameGFX.smallfont.MaxHeight(),y-3))
		
		If x<GraphicsWidth()/2
			tx=0
			lx=l+2
			x:-4
		Else
			tx=GraphicsWidth()-GameGFX.smallfont.TextWidth(s)
			lx=tx-3
			x:+4
		EndIf
		
		SetColor(255,col,col)
		DrawLine(x,y,lx,ly)
		GameGFX.smallfont.Draw(s,tx,ty+2,255,col,col)
	End Method
	
	Method Intro()
		Local y:Int=GraphicsHeight()/4
		Local yi:Int=25

		col:+coli
		
		If col=255 Or col=0
			coli=-coli
		EndIf
		
		For Local m:TMass=EachIn mass
			LabelMass(m)
		Next
		
		SetScale(2,2)
	
		GameGFX.font.Centre(level.name,y,col,col,255-col)
		y:+yi
	
		GameGFX.font.Centre("Need "+level.winpercent+"% to clear",y,col/2,col,col)
		y:+yi
		
		Local n:Int=Int(level.shipmass)
		Local w:Int=Int(level.wavemass)
		
		If level.invmass
			n=-n
			w=-w
		EndIf

		GameGFX.font.Centre("Your ship has a mass of " + n + " Newtons",y,col/2,col,col)
		y:+yi

		If level.maxwave>1
			GameGFX.font.Centre("You can fire "+level.maxwave+" Gravity Waves of " + w + " Newtons",y,col/2,col,col)
		ElseIf level.maxwave=1
			GameGFX.font.Centre("You can fire 1 Gravity Wave of " + w + " Newtons",y,col/2,col,col)
		EndIf
		y:+yi

		If level.wrap
			GameGFX.font.Centre("THE UNIVERSE IS WARPED!",y,col,col/2,col/2)
			y:+yi
		EndIf
	
		y:+yi
		GameGFX.font.Centre("Press Space",y)
		y:+yi

		SetScale(1,1)
	End Method
	
	Method Play:Int()
		captured=0
		lost=0

		If playing
			If done=LEVEL_NOTOVER
				If KeyDown(GameConfig.kleft)
					ship.RotateLeft()
				EndIf
	
				If KeyDown(GameConfig.kright)
					ship.RotateRight()
				EndIf
	
				If KeyDown(GameConfig.kthrust)
					ship.Thrust()
				EndIf
	
				If KeyDown(GameConfig.kreverse)
					ship.Reverse()
				EndIf

				If KeyHit(GameConfig.kblast) And placed<level.maxwave
					If level.invmass
						ship.mass=-level.wavemass
					Else
						ship.mass=level.wavemass
					EndIf
					TParticleMachine.AddShockwave(ship)
					placed:+1
				Else
					If level.invmass
						ship.mass=-level.shipmass
					Else
						ship.mass=level.shipmass
					EndIf
				EndIf
			EndIf

			For Local m:TMass=EachIn mass
				ship.Attract(m)
				
				For Local s:TPoint=EachIn point
					s.Attract(m)
				Next
			Next
		EndIf
		
		TParticleMachine.Process()
		
		If playing
			For Local m:TMass=EachIn mass
				m.Move(True)'(level.wrap)
				m.Draw()
			Next
		Else
			For Local m:TMass=EachIn mass
				m.Draw()
			Next
		EndIf
		
		If playing		
			For Local s:TPoint=EachIn point
				s.Move(level.wrap)
				s.Draw()
				
				If s.dead
					captured:+1
				ElseIf s.lost
					lost:+1
				EndIf
			Next
		Else
			For Local s:TPoint=EachIn point
				s.Draw()
			Next
		EndIf			
		
		If playing And done=LEVEL_NOTOVER
			frame:+1
			
			If frame=60 And timer>0
				frame=0
				timer:-1
			EndIf
		EndIf
		
		Local percent:Int
		
		If done<>LEVEL_NOTOVER
			percent=final_percent
		Else
			percent:Int=Int(Double(captured)/Double(num)*100.0)
			
			If percent>=level.winpercent And pass_time=0
				pass_time=timer
			EndIf
			
			Local lostpercent:Int=Int(Double(lost)/Double(num)*100.0)
			
			If lostpercent>(101-level.winpercent)
				done=LEVEL_LOST
				FlushKeys()
				final_percent=percent
			EndIf
		EndIf
		
		If (timer=0 Or num=captured+lost) And done=LEVEL_NOTOVER
			final_percent=percent
			If percent>=level.winpercent
				done=LEVEL_WON
			Else
				done=LEVEL_LOST
			EndIf
			FlushKeys()
		EndIf
		
		GameGFX.font.Draw("PARTICLES",0,0)
		GameGFX.font.Draw(num-captured-lost,txtoff[0]+10,0,255,255,0)

		GameGFX.font.Draw("CAPTURED",200,0)

		If percent<level.winpercent
			GameGFX.font.Draw(percent+"%",txtoff[1]+210,0,255,50,50)
		Else
			GameGFX.font.Draw(percent+"%",txtoff[1]+210,0,50,255,50)
		EndIf
		
		GameGFX.font.Draw("WAVES",400,0)
		
		SetColor(255,255,255)
		For Local f:Int=0 Until level.maxwave-placed
			DrawImage(GameGFX.shock,410+txtoff[3]+f*10,3)
		Next
		
		GameGFX.font.Draw("TIMER",600,0)
		
		If timer>10
			GameGFX.font.Draw(timer,txtoff[2]+610,0)
		Else
			GameGFX.font.Draw(timer,txtoff[2]+610,0,255,0,0)
		EndIf
		
		Select done
			Case LEVEL_NOTOVER
				If playing
					Rem
					If placed<level.maxmass And mass.Count()<MAX_GRAV
						If KeyHit(KEY_SPACE)
							Local m:TMass=New TMass
							m.friend=level.placefriend
							m.inverse=level.invmass
							m.x=MouseX()
							m.y=MouseY()
							If m.friend
								m.img=GameGFX.collector
							Else
								m.img=GameGFX.star
							EndIf
							m.mass=level.placemass
							mass.AddLast(m)
							placed:+1
						EndIf
					EndIf
					EndRem
				Else
					Intro()
					If KeyHit(KEY_SPACE)
						playing=True
						FlushKeys()
					EndIf
				EndIf
				
				If KeyHit(KEY_ESCAPE)
					done=LEVEL_CANCELLED
					FlushKeys()
				EndIf
			Case LEVEL_WON
				If final_percent=100
					SetScale(4,4)
					GameGFX.font.Centre("PERFECT!",50,col,255-col,col/2)
					col:+coli
					
					If col=255 Or col=0
						TParticleMachine.AddFirework(Rand(0,GraphicsWidth()),Rand(0,GraphicsHeight()))
						'TParticleMachine.AddShockwave(Rand(0,GraphicsWidth()),Rand(0,GraphicsHeight()))
						coli=-coli
					EndIf
				EndIf
				
				SetScale(2,2)
				SetAlpha(0.7)
				GameGFX.font.Centre("LEVEL COMPLETED!",GraphicsHeight()/2+20,255,255,0)
				GameGFX.font.Centre("You got the pass mark with "+pass_time+" left on the clock",GraphicsHeight()/2+40,255,255,0)
				GameGFX.font.Centre("Press Space",GraphicsHeight()/2+80)
				SetScale(1,1)
				SetAlpha(1)
				
				If KeyHit(KEY_SPACE)
					done=LEVEL_FINISHED
					FlushKeys()
				EndIf
			Case LEVEL_LOST
				SetScale(2,2)
				SetAlpha(0.7)
				GameGFX.font.Centre("LEVEL FAILED!",GraphicsHeight()/2+20,255,64,64)
				GameGFX.font.Centre("Press Space",GraphicsHeight()/2+60)
				SetScale(1,1)
				SetAlpha(1)

				If KeyHit(KEY_SPACE)
					done=LEVEL_FINISHED
					FlushKeys()
				EndIf
		EndSelect
		
		Return done
	End Method	

End Type