bool collide(tetromino te, int new_rot, int new_x, int new_y) { bool res = false; int row = 0; int col = 0; int b_i = 0; tetromino_type type = this.tetrominoes[te.type]; uint rot = type.rot[new_rot]; for (uint b = 0x8000; b > 0; b = b >> 1, ++b_i) { int index = (int)((new_x + col) + ((new_y + row)) * COL_COUNT); bool collide = false; if (index < (this.gr.gc.Length - 1) && index >= 0) { grid_case gc = this.gr.gc[index]; collide = (gc.color != 0xFFFFFFFF) && (!gc.is_cur); } bool border = ((new_y + row) < 0) || ((new_x + col) < 0) || ((new_x + col) >= COL_COUNT); if (((rot & b) > 0) && (border || collide)) { res = true; break; } if (++col == 4) { ++row; col = 0; } } return(res); }
void Update() { if (this.death) { if (Input.GetKeyDown(KeyCode.S)) { SceneManager.LoadScene(0); } return; } this.t += Time.deltaTime; int new_x = this.cur.pos_x; int new_y = this.cur.pos_y; bool next = false; int dir_x = 0; int new_rot = this.cur.rotation; int desired_rot = new_rot; int move_y = this.cur.pos_y; if (this.t >= this.speed) { move_y -= 1; bool col = collide(this.cur, this.cur.rotation, this.cur.pos_x, move_y); if (col) { // TODO(flo): not quite correct but okay for our purposes? if (move_y >= ROW_COUNT - 3) { this.death = true; } next = true; } else { new_y = move_y; } this.t = 0.0f; } if (Input.GetKeyDown(KeyCode.LeftArrow)) { dir_x = -1; } else if (Input.GetKeyDown(KeyCode.RightArrow)) { dir_x = 1; } else if (Input.GetKeyDown(KeyCode.DownArrow)) { this.speed = this.base_speed * 0.1f; } else if (Input.GetKeyUp(KeyCode.DownArrow)) { this.speed = this.base_speed; } else if (Input.GetKeyDown(KeyCode.Q)) { desired_rot--; if (desired_rot < 0) { desired_rot = 3; } } else if (Input.GetKeyDown(KeyCode.W)) { desired_rot++; if (desired_rot > 3) { desired_rot = 0; } } else if (Input.GetKeyDown(KeyCode.D)) { int test_y = cur.pos_y; while (!collide(this.cur, this.cur.rotation, this.cur.pos_x, test_y)) { test_y--; } new_y = test_y + 1; } else if (Input.GetKeyDown(KeyCode.S)) { SceneManager.LoadScene(0); } if (dir_x != 0) { int test_x = cur.pos_x + dir_x; if (!collide(this.cur, this.cur.rotation, test_x, move_y)) { new_x = test_x; new_y = move_y; next = false; } } move(this.cur, new_x, new_y); this.cur.pos_x = new_x; this.cur.pos_y = new_y; if (desired_rot != new_rot && !next) { if (!collide(this.cur, desired_rot, this.cur.pos_x, this.cur.pos_y)) { new_rot = desired_rot; } } rotate(this.cur, new_rot); this.cur.rotation = new_rot; if (next) { remove_lines(); this.cur = next_tetromino(); } for (int y = 0; y < ROW_COUNT; ++y) { for (int x = 0; x < COL_COUNT; ++x) { int index = (int)(x + y * COL_COUNT); grid_case gc = this.gr.gc[index]; Color32 c = argb_to_unity_color32(gc.color); int color_ind = index * 4; this.t_mesh.colors[color_ind + 0] = c; this.t_mesh.colors[color_ind + 1] = c; this.t_mesh.colors[color_ind + 2] = c; this.t_mesh.colors[color_ind + 3] = c; } } send_tetris_mesh_to_mesh(this.t_mesh, this.mesh); this.score_txt.text = this.score.ToString(); this.line_txt.text = this.total_lines.ToString(); this.level_txt.text = this.level.ToString(); }