-
Notifications
You must be signed in to change notification settings - Fork 1
/
tetromino.e
349 lines (296 loc) · 7.42 KB
/
tetromino.e
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
note
description : "Agncement of 4 {BLOCK}"
author : "Louis Marchand"
date : "July 19 2012"
revision : "1.0"
deferred class
TETROMINO
feature {NONE} -- Initialisation
make(l_surface:GAME_SURFACE;l_index:NATURAL_8;block_width,block_height:NATURAL;rotation:BOOLEAN)
-- Initialisation of `Current' using the images on `l_surface' at index
-- `l_index' with {BLOCK} of dimension `block_width'x`block_height'.
-- If `rotation' is `True', apply rotation on individual {BLOCK}.
do
index:=l_index
default_state
create_matrix
fill_matrix(l_surface,block_width,block_height,rotation)
init_wall_kicks_list
end
make_from_other(other:TETROMINO)
-- Initialisation of `Current' using values of `other'.
do
index:=other.index
list_blocks_matrix:=other.list_blocks_matrix
copy_state_from_other(other)
init_wall_kicks_list
end
feature -- Access
index:NATURAL_8
-- Sub-image block index of `Current'
rotation_index:INTEGER
-- The number of 90 degree rotation to apply
blocks_matrix:LIST[LIST[BLOCK]]
-- Every {BLOCK} of `Current'
do
Result:=list_blocks_matrix.at (rotation_index+1)
end
rotate_left
-- Rotate `Current' 90 degree to the left.
do
save_state
rotation_index:=(rotation_index+3)\\4
wall_kick_exhausted:=false
wall_kick_index:=0
wall_kick_nb:=old_rotation_index+1
end
rotate_right
-- Rotate `Current' 90 degree to the right.
do
save_state
rotation_index:=(rotation_index+1)\\4
wall_kick_exhausted:=false
wall_kick_index:=0
wall_kick_nb:=old_rotation_index+1
end
move_left
-- Move `Current' to the left.
do
save_state
x:=x-1
end
move_right
-- Move `Current' to the right.
do
save_state
x:=x+1
end
go_down
-- Move down `Current'.
do
save_state
y:=y-1
end
x:INTEGER
-- Horizontal position of `Current'
y:INTEGER
-- Vertical position of `Current'
cancel_last_move
-- Get back to the previous position and rotation.
do
x:=old_x
y:=old_y
rotation_index:=old_rotation_index
wall_kick_exhausted:=true
end
set_ghost_effect(alpha_value:NATURAL_8)
-- Apply a ghost effect of `alpha_value' transparency
local
h,i,j:INTEGER
do
from
h:=1
until
h>4
loop
from
i:=1
until
i>4
loop
from
j:=1
until
j>4
loop
if list_blocks_matrix.at (h).at (i).at (j)/=Void then
list_blocks_matrix.at (h).at (i).at (j).surface.set_overall_alpha_value (alpha_value)
end
j:=j+1
end
i:=i+1
end
h:=h+1
end
end
copy_state_from_other(other:TETROMINO)
-- Copy every values from `other' into `Current'
require
Tetromino_Copy_State_Same_Tetromino: other.index=index
do
x:=other.x
y:=other.y
rotation_index:=other.rotation_index
wall_kick_exhausted:=true
end
default_state
-- Set default values of `Current'
do
rotation_index:=0
x:=4
y:=20
wall_kick_exhausted:=true
end
print_on_surface(l_surface:GAME_SURFACE;l_x,l_y:INTEGER)
-- Show `Current' on `l_surface' at position (`l_x',`l_y').
local
i,j:INTEGER
do
from
i:=1
until
i>4
loop
from
j:=1
until
j>4
loop
if blocks_matrix.at (i).at (j)/=Void then
l_surface.draw_surface (blocks_matrix.at (i).at (j).surface,(j-1)*blocks_matrix.at (i).at (j).surface.width+l_x,(i-1)*blocks_matrix.at (i).at (j).surface.height+l_y)
end
j:=j+1
end
i:=i+1
end
end
wall_kick
-- Manage the kick of a wall
do
if not wall_kick_exhausted then
wall_kick_index:=wall_kick_index+1
x:=old_x+wall_kicks_list.at (wall_kick_nb).at (wall_kick_index).x
y:=old_y+wall_kicks_list.at (wall_kick_nb).at (wall_kick_index).y
if wall_kick_index+1>wall_kicks_list.at (wall_kick_nb).count then
wall_kick_exhausted:=true
end
end
end
wall_kick_index:INTEGER
-- Manage the number of `wall_kick' managed by `current'
wall_kick_exhausted:BOOLEAN
-- Apply a maximum number of `wall_kick' managed by `current'
feature {NONE} -- Implementation - Routines
blocks_positions_init:ARRAY[TUPLE[row,column:INTEGER]]
-- The position of the {BLOCK} in `Current'
deferred
end
create_matrix
-- Generate the {BOLCK} matrix of `Current' using `blocks_positions_init'
local
matrix:ARRAYED_LIST[LIST[BLOCK]]
line:ARRAYED_LIST[BLOCK]
i,j,k:NATURAL
do
create {ARRAYED_LIST[LIST[LIST[BLOCK]]]} list_blocks_matrix.make(4)
from
i:=1
until
i>4
loop
create matrix.make (4)
from
j:=1
until
j>4
loop
create line.make (4)
from
k:=1
until
k>4
loop
line.extend (Void)
k:=k+1
end
matrix.extend (line)
j:=j+1
end
list_blocks_matrix.extend (matrix)
i:=i+1
end
end
fill_matrix(l_surface:GAME_SURFACE;block_width,block_height:NATURAL;rotation:BOOLEAN)
-- Show `block_width' by `block_height' {BLOCK} of `Current' on `l_surface'.
-- If `rotation' is `True', apply a rotation on individual {BLOCK}.
local
i:INTEGER
nb_rotation:INTEGER
do
from
i:=1
until
i>16
loop
if rotation then
nb_rotation:=(i-1)//4
else
nb_rotation:=0
end
list_blocks_matrix.at ((i-1)//4+1).at (blocks_positions_init.at (i).row).at (blocks_positions_init.at (i).column) :=
create {BLOCK}.make(l_surface.sub_surface (((i-1)\\4)*block_width.to_integer_32, (index-1)*block_height.to_integer_32,
block_width.to_integer_32, block_height.to_integer_32).surface_rotated_90_degree (nb_rotation))
i:=i+1
end
end
save_state
do
old_x:=x
old_y:=y
old_rotation_index:=rotation_index
end
init_wall_kicks_list
-- List used to manage wall kick
local
temp_list:ARRAYED_LIST[TUPLE[x,y:INTEGER]]
do
create {ARRAYED_LIST[LIST[TUPLE[x,y:INTEGER]]]} wall_kicks_list.make(8)
create temp_list.make(5)
temp_list.extend ([0,0])
temp_list.extend ([-1,0])
temp_list.extend ([-1,1])
temp_list.extend ([0,-2])
temp_list.extend ([-1,-2])
wall_kicks_list.extend (temp_list) -- 0->R
create temp_list.make(5)
temp_list.extend ([0,0])
temp_list.extend ([1,0])
temp_list.extend ([1,-1])
temp_list.extend ([0,2])
temp_list.extend ([1,2])
wall_kicks_list.extend (temp_list) -- R->2
create temp_list.make(5)
temp_list.extend ([0,0])
temp_list.extend ([1,0])
temp_list.extend ([1,1])
temp_list.extend ([0,-2])
temp_list.extend ([1,-2])
wall_kicks_list.extend (temp_list) -- 2->L
create temp_list.make(5)
temp_list.extend ([0,0])
temp_list.extend ([-1,0])
temp_list.extend ([-1,-1])
temp_list.extend ([0,2])
temp_list.extend ([-1,2])
wall_kicks_list.extend (temp_list) -- L->0
wall_kicks_list.extend (wall_kicks_list.at (3)) -- 0->L
wall_kicks_list.extend (wall_kicks_list.at (2)) -- R->0
wall_kicks_list.extend (wall_kicks_list.at (1)) -- 2->R
wall_kicks_list.extend (wall_kicks_list.at (4)) -- L->2
end
feature {TETROMINO} -- Implementation - Variables
list_blocks_matrix:LIST[LIST[LIST[BLOCK]]]
-- List of Matrix of {BLOCK} (one matrix per rotation).
old_x:INTEGER
-- Previous horizontal position.
old_y:INTEGER
-- Previous vertical position.
old_rotation_index:INTEGER
-- Previous rotation index.
wall_kicks_list:LIST[LIST[TUPLE[x,y:INTEGER]]]
-- List of wall kick possibility
wall_kick_nb:INTEGER
-- Number of wall kick used in `Current'
invariant
Blocks_Matrix_Valid: list_blocks_matrix /= Void
end