Struct tetrs::block::Block
[−]
[src]
pub struct Block { pub x: i32, pub y: i32, pub id: Id, pub r: Rotation, pub rs: &'static RotationSystem, }
A struct representing a single tetrimino.
Blocks are defined by an (x, y)
coordinate pair, a Rotation
, Id
,
and an accompanying set of offset data which specifies exactly which
pieces in the field this block occupies.
Internally, (x, y)
coordinates are taken with their origin from the top
right of the grid, to preserve some compatibility with GUI systems. All
internal block calculations follow this as well:
(0, 0)-----------------(10, 0)
| |
| |
| .(4, 12) |
| |
| |
| |
(0, 25)----------------(0, 25)
Internally, the (x, y)
and data
fields look like the following:
(x, y) = (4, 4)
data = [(1, 0), (0, 1), (1, 1), (2, 1)]
When calculating a block's position, the data offsets are added to the
base coordinates to produce the block. The previous block is a Id::T
,
with Rotation::R0
, as the final data represented is:
block = [(5, 4), (4, 5), (5, 5), (6, 5)]
It is important to note that offsets are all non-negative, and as such the
coordinate effectively partitions the space where the block can reside.
Further, a block could have many different internal representations which
appear equal, by adjusting the (x, y)
coordinates and data
in
conjunction.
Fields
x | X-coordinate of the piece |
y | Y-coordinate of the piece |
id | Id of the block |
r | Rotation state of the block |
rs | Rotation system used to calculate block offsets. |
Methods
impl Block
[src]
fn new(id: Id, field: &Field) -> Block
Construct a Block
object with default values.
fn with_options(id: Id, field: &Field, options: BlockOptions) -> Block
Construct a Block
object with specific values.
Examples
use tetrs::import::*; let field = Field::new(); let block = Block::with_options(block::Id::I, &field, BlockOptions { rotation: Rotation::R180, ..Default::default() });
fn collides_at_offset(&self, field: &Field, (xo, yo): (i32, i32)) -> bool
Return whether the block collides with field
at the specified offset.
fn collides(&self, field: &Field) -> bool
Return whether the current Block
collides with field
at its current
position.
fn shift(&mut self, field: &Field, direction: Direction) -> bool
Shift the block one step in the specified direction.
Examples
use tetrs::import::*; let field = Field::new(); let mut block = Block::new(block::Id::Z, &field); block.shift(&field, Direction::Left);
fn shift_extend(&mut self, field: &Field, direction: Direction)
Repeatedly shift a block as far as we can until a collision occurs.
Examples
use tetrs::import::*; let field = Field::new(); let mut block = Block::new(block::Id::Z, &field); // Performs a 'HardDrop' block.shift_extend(&field, Direction::Down);
fn rotate_at_offset(&mut self, field: &Field, rotation: Rotation, (x, y): (i32, i32)) -> bool
Rotate the block by a specified amount and then apply an offset.
This is useful for calculating wallkicks. See the rotate_with_wallkick
function in the utility
module for an easier function.
use tetrs::import::*; let field = Field::new(); let mut block = Block::new(block::Id::Z, &field); // Rotate then move down 2 and right 1 and test for collision. // If we collide, then do not move the piece. block.rotate_at_offset(&field, Rotation::R90, (2, 1));
fn rotate(&mut self, field: &Field, rotation: Rotation) -> bool
Rotate the block by the specified amount.
fn occupies(&self, (a, b): (usize, usize)) -> bool
Check if the block occupies a particular (x, y)
absolute location.
fn ghost(&self, field: &Field) -> Block
Returns a new Block
which is the current blocks ghost.
Examples
use tetrs::import::*; let field = Field::new(); let block = Block::new(block::Id::Z, &field); let ghost = block.ghost(&field);