summaryrefslogtreecommitdiff
path: root/demos/2d/tetris/grid.gd
blob: dc89300881e29b118884b5820223685edfb69206 (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


extends Control

# Simple Tetris-like demo, (c) 2012 Juan Linietsky
# Implemented by using a regular Control and drawing on it during the _draw() callback.
# The drawing surface is updated only when changes happen (by calling update())


var score = 0
var score_label=null

const MAX_SHAPES = 7

var block = preload("block.png")

var block_colors=[
	Color(1,0.5,0.5),
	Color(0.5,1,0.5),
	Color(0.5,0.5,1),
	Color(0.8,0.4,0.8),
	Color(0.8,0.8,0.4),
	Color(0.4,0.8,0.8),
	Color(0.7,0.7,0.7)]

var	block_shapes=[
	[ Vector2(0,-1),Vector2(0,0),Vector2(0,1),Vector2(0,2) ], # I
	[ Vector2(0,0),Vector2(1,0),Vector2(1,1),Vector2(0,1) ], # O
	[ Vector2(-1,1),Vector2(0,1),Vector2(0,0),Vector2(1,0) ], # S
	[ Vector2(1,1),Vector2(0,1),Vector2(0,0),Vector2(-1,0) ], # Z
	[ Vector2(-1,1),Vector2(-1,0),Vector2(0,0),Vector2(1,0) ], # L
	[ Vector2(1,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ], # J
	[ Vector2(0,1),Vector2(1,0),Vector2(0,0),Vector2(-1,0) ]] # T
	

var block_rotations=[
	Matrix32( Vector2(1,0),Vector2(0,1), Vector2() ),
	Matrix32( Vector2(0,1),Vector2(-1,0), Vector2() ),
	Matrix32( Vector2(-1,0),Vector2(0,-1), Vector2() ),
	Matrix32( Vector2(0,-1),Vector2(1,0), Vector2() )
]
	

var width=0
var height=0

var cells={}

var piece_active=false
var piece_shape=0
var piece_pos=Vector2()
var piece_rot=0


func piece_cell_xform(p,er=0):
	var r = (4+er+piece_rot)%4
	return piece_pos+block_rotations[r].xform(p)

func _draw():

	var sb = get_stylebox("bg","Tree") # use line edit bg
	draw_style_box(sb,Rect2(Vector2(),get_size()).grow(3))
	
	var bs = block.get_size()
	for y in range(height):
		for x in range(width):
			if (Vector2(x,y) in cells):
				draw_texture_rect(block,Rect2(Vector2(x,y)*bs,bs),false,block_colors[cells[Vector2(x,y)]])
				
	if (piece_active):
		
		for c in block_shapes[piece_shape]:
			draw_texture_rect(block,Rect2(piece_cell_xform(c)*bs,bs),false,block_colors[piece_shape])
			

func piece_check_fit(ofs,er=0):

	for c in block_shapes[piece_shape]:
		var pos = piece_cell_xform(c,er)+ofs
		if (pos.x < 0):
			return false
		if (pos.y < 0):
			return false
		if (pos.x >= width):
			return false
		if (pos.y >= height):
			return false
		if (pos in cells):
			return false
	
	return true	

func new_piece():

	piece_shape = randi() % MAX_SHAPES	
	piece_pos = Vector2(width/2,0)
	piece_active=true
	piece_rot=0
	if (piece_shape==0):
		piece_pos.y+=1
		
	if (not piece_check_fit(Vector2())):
		#game over
		#print("GAME OVER!")
		game_over()
		
	update()
		
	
func test_collapse_rows():
	var accum_down=0
	for i in range(height):
		var y = height - i - 1
		var collapse = true
		for x in range(width):
			if (Vector2(x,y) in cells):
				if (accum_down):
					cells[ Vector2(x,y+accum_down) ] = cells[Vector2(x,y)]
			else:
				collapse=false
				if (accum_down):
					cells.erase( Vector2(x,y+accum_down) )
						
		if (collapse):
			accum_down+=1
		
			
	score+=accum_down*100
	score_label.set_text(str(score))
			
		
func game_over():

		piece_active=false
		get_node("gameover").set_text("Game Over")		
		update()
				
		
func restart_pressed():

		score=0
		score_label.set_text("0")
		cells.clear()
		get_node("gameover").set_text("")		
		piece_active=true
		update()
		
		

func piece_move_down():

	if (!piece_active):
		return
	if (piece_check_fit(Vector2(0,1))):
		piece_pos.y+=1
		update()		
	else:

		for c in block_shapes[piece_shape]:
			var pos = piece_cell_xform(c)
			cells[pos]=piece_shape
		test_collapse_rows()
		new_piece()
		

func piece_rotate():

	var adv = 1
	if (not piece_check_fit(Vector2(),1)):
		return
	piece_rot = (piece_rot + adv) % 4
	update()
	
	

func _input(ie):


	if (not piece_active):
		return
	if (!ie.is_pressed()):
		return

	if (ie.is_action("move_left")):
		if (piece_check_fit(Vector2(-1,0))):
			piece_pos.x-=1
			update()
	elif (ie.is_action("move_right")):
		if (piece_check_fit(Vector2(1,0))):
			piece_pos.x+=1
			update()
	elif (ie.is_action("move_down")):
		piece_move_down()
	elif (ie.is_action("rotate")):
		piece_rotate()
		
		
func setup(w,h):
	width=w
	height=h
	set_size( Vector2(w,h)*block.get_size() )
	new_piece()
	get_node("timer").start()
	

func _ready():
	# Initalization here

	setup(10,20)
	score_label = get_node("../score")

	set_process_input(true)