summaryrefslogtreecommitdiff
path: root/game.bmx
blob: d07d1a38d351ee1d8e1977eee9cfd48ef49b8f2d (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
' Hardwire
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import "types.bmx"
Import "particle.bmx"
Import "sounds.bmx"
Import "gametypes.bmx"

Type TBufferedKey
	Global list:TList
	Field key:Int
	Field del:Int
	Field cnt:Int
	
	Function Clear()
		list=CreateList()
	End Function
	
	Function Create:TBufferedKey(key:Int, del:Int=30)
		Local o:TBufferedKey=New TBufferedKey
		o.key=key
		o.del=del
		o.cnt=0
		list.AddLast(o)
		Return o
	End Function
	
	Function Flush()
		For Local k:TBufferedKey=EachIn list
			k.cnt=0
		Next
	End Function
	
	Function Idle()
		For Local k:TBufferedKey=EachIn list
			If k.cnt
				k.cnt:-1
			EndIf
		Next
	End Function
	
	Method ForceRelease()
		cnt=999999
	End Method
	
	Method Poll:Int()
		If KeyDown(key)
			If cnt
				cnt:-1
				Return False
			Else
				cnt=del
				Return True
			EndIf
		Else
			cnt=0
			Return False
		EndIf
	End Method
End Type

Type TGame
	Field level:Int
	Field score:Int
	Field gm:TGameMap
	Field timer:Int
	Field alpha:Double
	Field alphainc:Double
	Field count:Int
	Field total:Int
	Field block:TPiece
	Field nextblock:TPiece
	Field drop:Int
	Field fade:TFadeScreen
	Field bdrop:TGameBackdrop
	Field kleft:TBufferedKey
	Field kright:TBufferedKey
	Field krotleft:TBufferedKey
	Field krotright:TBufferedKey
	Field kdrop:TBufferedKey
	
	Method New()
		score=0
		gm=New TGameMap
		level=1
		Particles.Clear()
		TextParticles.Clear()
		ExplosionParticles.Clear()
		alpha=0.0
		alphainc=0.01
		count=0
		total=0
		nextblock=TPiece.Create(False)
		drop=False
		CreateNext(False)
		fade=TFadeScreen.FadeIn()
		bdrop=New TGameBackdrop

		TBufferedKey.Clear()
		kleft=TBufferedKey.Create(GameConfig.kleft)
		kright=TBufferedKey.Create(GameConfig.kright)
		krotleft=TBufferedKey.Create(GameConfig.krotleft)
		krotright=TBufferedKey.Create(GameConfig.krotright)
		kdrop=TBufferedKey.Create(GameConfig.kdrop,1)
	End Method
	
	Method SetInitLevel(l:Int)
		level=l
		SetTimer()
	End Method
	
	Method SetTimer()
		If drop
			timer=5
		Else
			timer=Max(HERTZ/5,(16-level)*HERTZ/5)
		EndIf
	End Method
	
	Method LevelUp()
		level:+1
		count=0
	End Method
	
	Method CreateNext(special:Int)
		block=nextblock
		block.y=-3
		block.x=Pit.WIDTH/2
		nextblock=TPiece.Create(special)
		nextblock.x=Pit.WIDTH+3
		nextblock.y=1
		drop=False
	End Method
	
	Method Pause()
		Local i:Timage=CreateImage(GraphicsWidth(),GraphicsHeight(),1,MASKEDIMAGE|DYNAMICIMAGE|FILTEREDIMAGE)
		
		GrabImage(i,0,0)
		MidHandleImage(i)
		SetColor(255,255,255)
		
		FlushKeys()
		
		Local a:Int[]=[0,0,0,0,0,0,0,0]
		Local ac:Int[]=[7,6,5,4,3,2,1,0]
		
		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*2,al*2)
				DrawImage(i,GraphicsWidth()/2,GraphicsHeight()/2)
				If ac[f]>0
					ac[f]:-1
				Else
					If f Mod 2
						a[f]:+1
					Else
						a[f]:-1
					EndIf
				EndIf
				al:+0.1
			Next
			
			SetRotation(0)
			SetScale(1,1)
			GameGFX.large.Centre("PAUSED",GraphicsHeight()/2-16)
			
			Flip
		Wend
		
		SetAlpha(1)
		SetRotation(0)
		SetScale(1,1)
		
		FlushKeys
		TBufferedKey.Flush()
	End Method
	
	Method Play:Int()
		Local playing:Int=True
		
		Cls
		
		bdrop.Draw()
		
		GameGFX.large.Draw("SCORE",0,0,255,255,0)
		GameGFX.large.Draw(score,0,20)
		
		GameGFX.large.Draw("LEVEL",0,100,255,255,0)
		GameGFX.large.Draw(level,0,120)

		GameGFX.large.Draw("HISCORE",0,200,255,255,0)
		If score>GameConfig.hiscore
			GameConfig.hiscore=score
		EndIf
		GameGFX.large.Draw(GameConfig.hiscore,0,220)
		
		nextblock.Draw()
			
		block.Draw()
			
		If Not gm.overflow
			If KeyHit(KEY_ESCAPE)
				playing=False
			EndIf
			
			If Not gm.BlockInteract()
				timer:-1
				
				If timer<=0
					SetTimer()
					block.y:+1
					
					If block.Collides(gm)
						block.y:-1
						block.AddToMap(gm)
						count:+1
						total:+1
						If count>10
							LevelUp()
						EndIf
						
						kdrop.ForceRelease()
						
						If Not gm.overflow
							score:+level*2
							CreateNext((total Mod 17)=0) ' 17
						Else
							FlushKeys()
							TBufferedKey.Flush()
						EndIf
					EndIf
				EndIf
				
				If kleft.Poll()
					block.x:-1
					If block.Collides(gm)
						block.x:+1
					EndIf
				EndIf
	
				If kright.Poll()
					block.x:+1
					If block.Collides(gm)
						block.x:-1
					EndIf
				EndIf
				
				If krotright.Poll()
					block.RotateRight()
					If block.Collides(gm)
						block.x:-1
						If block.Collides(gm)
							block.x:+1
							block.RotateLeft()
						EndIf
					EndIf
				EndIf
				
				If krotleft.Poll()
					block.RotateLeft()
					If block.Collides(gm)
						block.x:+1
						If block.Collides(gm)
							block.x:-1
							block.RotateRight()
						EndIf
					EndIf
				EndIf
				
				If kdrop.Poll()
					timer=0
					'drop=True
				EndIf
			Else
				TBufferedKey.Idle()
			EndIf
			
			score:+gm.Draw()*level*20
		Else
			gm.Draw()

			SetAlpha(alpha)
			SetColor(255,255,255)
			DrawImage(GameGFX.gameover,GraphicsWidth()/2,GraphicsHeight()/2)
			SetAlpha(1)
			alpha:+alphainc
			If (alpha<=0.7 And alphainc<0) Or (alpha>=1.0 And alphainc>0)
				alpha=Max(0,Min(1,alpha))
				alphainc=-alphainc
			EndIf
			
			If KeyHit(KEY_ENTER) Or KeyHit(KEY_ESCAPE)
				playing=False
			EndIf
		EndIf
		
		Particles.Draw()
		ExplosionParticles.Draw()
		TextParticles.Draw()

		If KeyHit(GameConfig.kpause) And Not gm.overflow
			Pause()
		EndIf
		
		Sound.Process()
		
		If fade
			If fade.Fade()
				fade.Draw()
			Else
				fade=Null
			EndIf
		EndIf
		
		Flip
		
		Return playing
	End Method
End Type