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

Type Pit
	Const WIDTH=10
	Const HEIGHT=14
	
	Function X:Int(p:Int)
		Return 230+p*32
	End Function
	
	Function Y:Int(p:Int)
		Return 96+p*32
	End Function
End Type

Type TWire
	Const CROSS:Int=0
	Const LEFT_RIGHT:Int=1
	Const TOP_BOTTOM:Int=2
	Const TOP_LEFT:Int=3
	Const TOP_RIGHT:Int=4
	Const BOTTOM_LEFT:Int=5
	Const BOTTOM_RIGHT:Int=6
	
	Const DIR_NONE:Int=0
	Const DIR_UP:Int=1
	Const DIR_RIGHT:Int=2
	Const DIR_DOWN:Int=3
	Const DIR_LEFT:Int=4

	Global rotright:Int[]
	Global rotleft:Int[]
	Global img:TImage[]
	Global dir:Int[][]
	Global dx:Int[]
	Global dy:Int[]
	
	Global dirname:String[]
	Global typename:String[]

	Field t:Int
	
	Function Init()
		rotright=[0,2,1,4,6,3,5]
		rotleft=[0,2,1,5,3,6,4]
		
		img=[GameGFX.cross,GameGFX.left_right,GameGFX.top_bottom,GameGFX.top_left,GameGFX.top_right,GameGFX.bottom_left,GameGFX.bottom_right]
	
		'	 CROSS							   LEFT_RIGHT							  TOP_BOTTOM						  TOP_LEFT					   TOP_RIGHT				      BOTTOM_LEFT				    BOTTOM_RIGHT
		dir=[[DIR_UP,DIR_RIGHT,DIR_DOWN,DIR_LEFT], [DIR_NONE,DIR_RIGHT,DIR_NONE,DIR_LEFT], [DIR_UP,DIR_NONE,DIR_DOWN,DIR_NONE], [DIR_NONE,DIR_UP,DIR_LEFT,DIR_NONE], [DIR_NONE,DIR_NONE,DIR_RIGHT,DIR_UP], [DIR_LEFT,DIR_DOWN,DIR_NONE,DIR_NONE], [DIR_RIGHT,DIR_NONE,DIR_NONE,DIR_DOWN]]
		
		dx=[0,0,1,0,-1]
		dy=[0,-1,0,1,0]
		
		dirname=["NONE","UP","RIGHT","DOWN","LEFT"]
		typename=["CROSS","LEFT_RIGHT","TOP_BOTTOM","TOP_LEFT","TOP_RIGHT","BOTTOM_LEFT","BOTTOM_RIGHT"]
	End Function
	
	Method New()
		t=Rand(0,6)
	End Method
	
	Function Create:TWire(t:Int)
		Local o:TWire=New TWire
		o.t=t
		Return o
	End Function
	
	Method Image:TImage()
		Return img[t]
	End Method
	
	Method RotateLeft()
		t=rotleft[t]
	End Method
	
	Method RotateRight()
		t=rotright[t]
	End Method
	
	Method Traverse:Int(d:Int)
		If d=DIR_NONE
			Return DIR_NONE
		EndIf
		
		Return dir[t][d-1]
	End Method
	
	Function DirX:Int(d:Int)
		Return dx[d]
	End Function

	Function DirY:Int(d:Int)
		Return dy[d]
	End Function
End Type

Type TFallingBlock
	Field w:TWire
	Field x:Int
	Field y:Double
	Field yi:Double
	
	Function Create:TFallingBlock(x:Int, y:Double)
		Local o:TFallingBlock=New TFallingBlock
		o.w=New TWire
		o.x=x
		o.y=y
		o.yi=0
		Return o
	End Function

	Method Update()
		y:+yi
		yi=Max(4.0,yi+0.1)
	End Method
	
	Method Draw()
		SetColor(255,255,255)
		DrawImage(GameGFX.tile,Pit.X(x),y)
		SetColor(128,128,128)
		DrawImage(w.Image(),Pit.X(x),y)
	End Method
End Type

Type TWireListEnt
	Field w:TWire
	Field x:Int
	Field y:Int
	
	Function Create:TWireListEnt(x:Int, y:Int, w:TWire)
		Local o:TWireListEnt=New TWireListEnt
		o.x=x
		o.y=y
		o.w=w
		Return o
	End Function
End Type

Type TWireList
	Field list:TList
	Field col:Int
	
	Method New()
		list=CreateList()
		col=128
	End Method
	
	Method Add(x:Int, y:Int, w:TWire)
		list.AddLast(TWireListEnt.Create(x,y,w))
	End Method
	
	Method Draw:Int()
		col=Max(255,col+5)
		SetColor(col,col,col)
		For Local e:TWireListEnt=EachIn list
			DrawImage(e.w.Image(),Pit.X(e.x),Pit.Y(e.y))
		Next
		
		If col<255
			Return True
		Else
			For Local e:TWireListEnt=EachIn list
				Particles.AddImage(e.w.Image(),Pit.X(e.x),Pit.Y(e.y))
			Next
			Return False
		EndIf
	End Method
End Type

Type TGameMap
	Field map:TWire[Pit.WIDTH,Pit.HEIGHT]
	Field trode_col:Int
	Field trode_coli:Int
	Field overflow:Int
	Field top:Int[Pit.WIDTH]
	Field drop:TList
	Field cx:Int
	Field cy:Int
	Field path:TList
	
	Method New()
		trode_col=0
		trode_coli=1
		overflow=False
		drop=CreateList()
		path=CreateList()
		CalcTop()
		cx=0
		cy=Pit.HEIGHT-2
	End Method
	
	Method CalcTop()
		For Local x:Int=0 Until Pit.WIDTH
			top[x]=-1
			For Local y:Int=Pit.Height-1 To 0 Step -1
				If Not map[x,y]
					top[x]=y
					Exit
				EndIf
			Next
		Next
	End Method
	
	Method AddRow(y:Int=-32)
		If BlockInteract() Then Return
		
		For Local x:Int=0 Until Pit.WIDTH
			drop.AddLast(TFallingBlock.Create(x,y))
		Next
	End Method
	
	Method BlockInteract:Int()
		Return overflow Or path.Count()
	End Method
	
	Method FindPath:Int(l:TWireList, x:Int, y:Int, dir:Int)
		If y<0
			Return False
		EndIf
		
		If x<0 Or x=Pit.WIDTH Or y=Pit.HEIGHT
			Return True
		EndIf
		
		Local w:TWire=map[x,y]

		If Not w
			Return False
		EndIf
		
		dir=w.Traverse(dir)
		
		If dir=TWire.DIR_NONE
			Return False
		EndIf
		
		l.Add(x,y,w)
		
		Return FindPath(l,x+TWire.DirX(dir),y+TWire.DirY(dir),dir)
	End Method
	
	Method FindLoop:Int(l:TWireList, ox:Int, oy:Int, x:Int, y:Int, dir:Int)
		If y<0 Or x<0 Or x=Pit.WIDTH Or y=Pit.HEIGHT
			Return False
		EndIf
		
		Local w:TWire=map[x,y]

		If Not w
			Return False
		EndIf
		
		dir=w.Traverse(dir)
		
		If dir=TWire.DIR_NONE
			Return False
		EndIf
		
		l.Add(x,y,w)
		
		x:+TWire.DirX(dir)
		y:+TWire.DirY(dir)
		
		If x=ox And y=oy
			Return True
		EndIf
		
		Return FindLoop(l,ox,oy,x,y,dir)
	End Method
	
	Method CheckWires()
		For Local y:Int=0 Until Pit.HEIGHT
			Local l:TWireList=New TWireList
			
			If FindPath(l,0,y,TWire.DIR_RIGHT)
				path.AddLast(l)
			EndIf
			
			l=New TWireList

			If FindPath(l,Pit.WIDTH-1,y,TWire.DIR_LEFT)
				path.AddLast(l)
			EndIf
		Next

		For Local x:Int=0 Until Pit.WIDTH
			Local l:TWireList=New TWireList
			
			If FindPath(l,x,Pit.HEIGHT-1,TWire.DIR_UP)
				path.AddLast(l)
			EndIf
		Next
		
		For Local x:Int=0 Until Pit.WIDTH
			For Local y:Int=0 Until Pit.HEIGHT
				If map[x,y]
					Local l:TWireList=New TWireList
					Select map[x,y].t
						Case TWire.CROSS, TWire.LEFT_RIGHT, TWire.TOP_LEFT, TWire.BOTTOM_LEFT
							If FindLoop(l,x,y,x,y,TWire.DIR_LEFT)
								path.AddLast(l)
							EndIf
					End Select
				EndIf
			Next
		Next
	End Method
	
	Method CursorLeft()
		If BlockInteract() Then Return
		cx=Max(0,cx-1)
	End Method
	
	Method CursorRight()
		If BlockInteract() Then Return
		cx=Min(Pit.WIDTH-2,cx+1)
	End Method
	
	Method CursorUp()
		If BlockInteract() Then Return
		cy=Max(0,cy-1)
	End Method
	
	Method CursorDown()
		If BlockInteract() Then Return
		cy=Min(Pit.HEIGHT-2,cy+1)
	End Method
	
	Method Rotate()
		If BlockInteract() Then Return
		Local done:Int=False
		For Local x:Int=0 To 1
			For Local y:Int=0 To 1
				If map[cx+x,cy+y]
					map[cx+x,cy+y].RotateRight()
					done=True
				EndIf
			Next
		Next
		
		If done
			Sound.Click()
		EndIf
		
		CheckWires()
	End Method
	
	Method Draw:Int()
		Local score:Int=0
		
		SetColor(255,255,255)
		DrawImage(GameGFX.pit_bottomleft,Pit.X(-1),Pit.Y(Pit.HEIGHT))
		DrawImage(GameGFX.pit_bottomright,Pit.X(Pit.WIDTH),Pit.Y(Pit.HEIGHT))

		DrawImage(GameGFX.pit_top,Pit.X(-1),Pit.Y(0))
		DrawImage(GameGFX.pit_top,Pit.X(Pit.WIDTH),Pit.Y(0))
		SetColor(trode_col,trode_col,0)
		DrawImage(GameGFX.trode_left,Pit.X(-1),Pit.Y(0))
		DrawImage(GameGFX.trode_right,Pit.X(Pit.WIDTH),Pit.Y(0))

		For Local f:Int=0 Until Pit.WIDTH
			SetColor(255,255,255)
			DrawImage(GameGFX.pit_bottom,Pit.X(f),Pit.Y(Pit.HEIGHT))
			SetColor(trode_col,trode_col,0)
			DrawImage(GameGFX.trode_bottom,Pit.X(f),Pit.Y(Pit.HEIGHT))
		Next
		
		For Local f:Int=1 Until Pit.HEIGHT
			SetColor(255,255,255)
			DrawImage(GameGFX.pit_side,Pit.X(-1),Pit.Y(f))
			DrawImage(GameGFX.pit_side,Pit.X(Pit.WIDTH),Pit.Y(f))
			SetColor(trode_col,trode_col,0)
			DrawImage(GameGFX.trode_left,Pit.X(-1),Pit.Y(f))
			DrawImage(GameGFX.trode_right,Pit.X(Pit.WIDTH),Pit.Y(f))
		Next
		
		For Local x:Int=0 Until Pit.WIDTH
			For Local y:Int=0 Until Pit.HEIGHT
				If map[x,y]
					SetColor(255,255,255)
					DrawImage(GameGFX.tile,Pit.X(x),Pit.Y(y))
					SetColor(128,128,128)
					DrawImage(map[x,y].Image(),Pit.X(x),Pit.Y(y))
				EndIf
			Next
		Next
		
		If BlockInteract()
			For Local b:TFallingBlock=EachIn drop
				b.Draw()
			Next
			
			Local l:TEasyLink=TEasyLink.Create(path)
			Local check:Int=False
			
			While l.Value()
				Local b:TWireList=TWireList(l.Value())
				
				If b.Draw()
					l.MoveNext()
				Else
					If Not overflow
						score:+b.list.Count()
						For Local e:TWireListEnt=EachIn b.list
							For Local y:Int=e.y To 1 Step -1
								map[e.x,y]=map[e.x,y-1]
							Next
							map[e.x,0]=Null
							Particles.AddDust(Pit.X(e.x)+16,Pit.Y(e.y)+32)
							check=True
						Next
					EndIf
					l.Remove()
				EndIf
			Wend
			
			If check
				Sound.Path()
				CalcTop()
				CheckWires()
			EndIf
		Else
			Local hit:Int=False
			
			Local l:TEasyLink=TEasyLink.Create(drop)
			
			While l.Value()
				Local b:TFallingBlock=TFallingBlock(l.Value())
				Local t:Int=Pit.Y(top[b.x])'-ImageHeight(GameGFX.tile)

				b.Update()
				
				If b.y>t
					If top[b.x]=-1
						overflow=True
						b.y=t
						l.MoveNext()
					Else
						hit=True
						map[b.x,top[b.x]]=b.w
						Particles.AddDust(Pit.X(b.x)+16,Pit.Y(top[b.x])+32)
						l.Remove()
					EndIf
				Else
					b.Draw()
					l.MoveNext()
				EndIf
			Wend
			
			If hit And Not overflow
				Sound.Click()
				CheckWires()
				CalcTop()
			EndIf
		EndIf
		
		SetColor(trode_col,trode_col,0)
		DrawImage(GameGFX.cursor,Pit.X(cx),Pit.Y(cy))
		
		trode_col:+trode_coli
		
		If (trode_col=255 And trode_coli>0) Or (trode_col=200 And trode_coli<0)
			trode_coli=-trode_coli
		EndIf
		
		Return score
	End Method
End Type