-
Notifications
You must be signed in to change notification settings - Fork 3
/
c_collisions.ino
executable file
·52 lines (41 loc) · 1.27 KB
/
c_collisions.ino
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
/**
* collision detection
* checks if a tetromino can move in a certain direction or rotate
* returns true, if a collision is about to happen
*/
bool check_collision ( const byte dir = 0 ) {
uint16_t temp_tet = pgm_read_word(TETROMINOES + 4*tetr_type + tetr_rotation);
int temp_tetr_offsX = tetr_offsX;
int temp_tetr_offsY = tetr_offsY;
switch ( dir ) {
case ROTATE:
temp_tet = pgm_read_word(TETROMINOES + 4*tetr_type + (tetr_rotation + 1)%4 );
break;
case LEFT:
temp_tetr_offsX--;
break;
case RIGHT:
temp_tetr_offsX++;
break;
case FALL:
temp_tetr_offsY++;
break;
default: // just dropped tetromino
break;
}
// now check every set bit in the tetromino container
// if it collides with a set bit in the bucket
for ( byte i = 0; i < 16; i++ ) {
if ( bitRead(temp_tet, i) ) {
byte row = floor(i / 4);
byte col = i % 4;
// the floor is not included in the bucket, so check it here
if ( row+temp_tetr_offsY > 16 )
return true;
// temp_tetr_offsX is offset by another 1 because the walls of the bucket are included
if ( bitRead(bucket[row+temp_tetr_offsY], col+temp_tetr_offsX+1) )
return true;
}
}
return false;
}