Struct all_is_cubes::block::Block

source ·
pub struct Block(/* private fields */);
Expand description

A Block is something that can exist in the grid of a Space; it occupies one unit cube of simulated physical space, and has a specified appearance and behavior.

A Block is made up of a Primitive and zero or more Modifiers.

In general, when a block appears multiple times from an in-game perspective, that may or may not be the the same copy; Blocks are “by value” and any block Eq to another will behave identically and should be treated identically. However, some blocks are defined by reference to shared mutable data, and Block containers such as Space must follow those changes.

To determine the concrete appearance and behavior of a block, use Block::evaluate() or Block::evaluate_and_listen(), which will return an EvaluatedBlock value.

§Serialization stability warning

This type implements serde::Serialize and serde::Deserialize, but serialization support is still experimental (as is the game data model in general). We do not guarantee that future versions of all-is-cubes will be able to deserialize data which is serialized by this version.

Additionally, the serialization schema is designed with serde_json in mind. It is not guaranteed that using a different data format crate, which may use a different subset of the information exposed via serde::Serialize, will produce stable results.

Implementations§

source§

impl Block

source

pub const fn builder() -> BlockBuilder<NeedsPrimitive, ()>

Returns a new BlockBuilder which may be used to construct a Block value from various inputs with convenient syntax.

source

pub fn from_primitive(p: Primitive) -> Self

Construct a Block from a Primitive value.

source

pub fn primitive(&self) -> &Primitive

Returns the Primitive which defines this block before any Modifiers are applied.

source

pub fn primitive_mut(&mut self) -> &mut Primitive

Returns a mutable reference to the Primitive which defines this block before any Modifiers are applied.

This may cause part or all of the block’s data to stop sharing storage with other blocks.

source

pub fn modifiers(&self) -> &[Modifier]

Returns all the modifiers of this block.

Modifiers are arranged in order of their application to the primitive, or “innermost” to “outermost”.

Note that this does not necessarily return all modifiers involved in its definition; modifiers on the far end of a Primitive::Indirect are not reported here, even though they take effect when evaluated.

source

pub fn modifiers_mut(&mut self) -> &mut Vec<Modifier>

Returns a mutable reference to the vector of Modifiers on this block.

This may cause part or all of the block’s data to stop sharing storage with other blocks.

source

pub fn with_modifier(self, modifier: impl Into<Modifier>) -> Self

Add the given modifier to this block.

This is a convenience operation which is exactly equivalent to doing block.modifiers_mut().push(modifier.into()). It does not do any of the special case logic that, for example, Block::rotate() does.

source

pub fn rotate(self, rotation: GridRotation) -> Self

Rotates this block by the specified rotation.

Compared to direct use of Modifier::Rotate, this will:

  • Avoid constructing chains of redundant modifiers.
  • Not rotate blocks that should never appear rotated (including atom blocks).

(TODO: This should be replaced with with_modifier() or similar having a general rule set for combining modifiers.)

use all_is_cubes::block::{AIR, Block, Modifier};
use all_is_cubes::color_block;
use all_is_cubes::content::make_some_voxel_blocks;
use all_is_cubes::math::{GridRotation, Rgba};
use all_is_cubes::universe::Universe;

let mut universe = Universe::new();
let [block] = make_some_voxel_blocks(&mut universe);
let clockwise = GridRotation::CLOCKWISE;

// Basic rotation
let rotated = block.clone().rotate(clockwise);
assert_eq!(rotated.modifiers(), &[Modifier::Rotate(clockwise)]);

// Multiple rotations are combined
let double = rotated.clone().rotate(clockwise);
assert_eq!(double.modifiers(), &[Modifier::Rotate(clockwise * clockwise)]);

// Atoms and AIR are never rotated
let atom = color_block!(Rgba::WHITE);
assert_eq!(atom.clone().rotate(clockwise), atom);
assert_eq!(AIR.rotate(clockwise), AIR);
source

pub fn unspecialize(&self) -> Vec<Block>

Standardizes any characteristics of this block which may be presumed to be specific to its usage in its current location, so that it can be used elsewhere or compared with others. Specifically, it has the following effects:

In future versions there may be additional changes or ones customizable per block.

§Examples

Removing rotation:

use all_is_cubes::block::Block;
use all_is_cubes::math::GridRotation;
use all_is_cubes::universe::Universe;

let mut universe = Universe::new();
let [block] = make_some_voxel_blocks(&mut universe);
let rotated = block.clone().rotate(GridRotation::CLOCKWISE);

assert_ne!(&block, &rotated);
assert_eq!(vec![block], rotated.clone().unspecialize());
source

pub fn evaluate(&self) -> Result<EvaluatedBlock, EvalBlockError>

Converts this Block into a “flattened” and snapshotted form which contains all information needed for rendering and physics, and does not require Handle access to other objects.

source

pub fn evaluate_and_listen( &self, listener: impl Listener<BlockChange> + 'static, ) -> Result<EvaluatedBlock, EvalBlockError>

As Block::evaluate(), but also installs a listener which will be notified of changes in all data sources that might affect the evaluation result.

Note that this does not listen for mutations of the Block value itself, in the sense that none of the methods on Block will cause this listener to fire. Rather, it listens for changes in by-reference-to-interior-mutable-data sources such as the Space referred to by a Primitive::Recur or the BlockDef referred to by a Primitive::Indirect.

§Errors

If an evaluation error is reported, the Listener may have been installed incompletely or not at all. It should not be relied on.

source

pub fn color(&self) -> Rgba

Returns the single Rgba color of this block’s Primitive::Atom or Primitive::Air, or panics if it has a different kind of primitive. Intended for use in tests only.

Trait Implementations§

source§

impl<'a> Arbitrary<'a> for Block

source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

impl AsRef<Block> for BlockDef

source§

fn as_ref(&self) -> &Block

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Block

source§

fn clone(&self) -> Block

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Block

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for Block

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a> From<&'a Block> for Cow<'a, Block>

source§

fn from(block: &'a Block) -> Self

Converts to this type from the input type.
source§

impl From<&'static Primitive> for Block

source§

fn from(r: &'static Primitive) -> Self

Constructs a Block which references the given static Primitive.

This performs no allocation.

source§

impl From<Atom> for Block

source§

fn from(value: Atom) -> Self

Converts to this type from the input type.
source§

impl From<Block> for Cow<'_, Block>

source§

fn from(block: Block) -> Self

Converts to this type from the input type.
source§

impl<C: BuildPrimitive> From<BlockBuilder<C, ()>> for Block

Allows implicitly converting BlockBuilder to the block it would build.

source§

fn from(builder: BlockBuilder<C, ()>) -> Self

Converts to this type from the input type.
source§

impl From<Handle<BlockDef>> for Block

source§

fn from(block_def_handle: Handle<BlockDef>) -> Self

Convert a Handle<BlockDef> into a block with Primitive::Indirect that refers to it.

The returned block will evaluate to the same EvaluatedBlock as the block contained within the given BlockDef (except in case of errors).

source§

impl From<Primitive> for Block

source§

fn from(primitive: Primitive) -> Self

Constructs a Block that owns the given Primitive.

This operation creates a heap allocation for the Primitive.

source§

impl From<Rgb> for Block

source§

fn from(color: Rgb) -> Self

Constructs a Block with the given reflectance color, and default attributes.

This operation allocates a new Primitive value on the heap. If the color is a constant, you may use color_block! instead to avoid allocation.

source§

impl From<Rgba> for Block

source§

fn from(color: Rgba) -> Self

Construct a Block with the given reflectance color, and default attributes.

This operation allocates a new Primitive value on the heap. If the color is a constant, you may use color_block! instead to avoid allocation.

source§

impl Hash for Block

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Block

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PixelColor for &'a Block

§

type Raw = ()

Raw data type. Read more
source§

impl Serialize for Block

source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
source§

impl VisitHandles for Block

source§

fn visit_handles(&self, visitor: &mut dyn HandleVisitor)

For each Handle contained within self that is reachable without traversing another Handle, call visitor with a reference to it.
source§

impl<'a> VoxelColor<'a> for &'a Block

source§

fn into_blocks(self) -> VoxelBrush<'a>

Returns a corresponding VoxelBrush, the most general form of blocky drawing.
source§

impl Eq for Block

Auto Trait Implementations§

§

impl Freeze for Block

§

impl RefUnwindSafe for Block

§

impl Send for Block

§

impl Sync for Block

§

impl Unpin for Block

§

impl UnwindSafe for Block

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> Downcast for T
where T: Any,

source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Is for T
where T: ?Sized,

§

type EqTo = T

source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,