Struct Layer

Source
pub struct Layer {
    pub name: String,
    pub masks: Vec<Mask>,
    pub z: u32,
}
Expand description

§Layer

A Layer is a logical or visual overlay composed of one or more Masks, each of which defines a group of tile-based Effects (such as collisions, textures, or actions).

Layers enable the modular composition of game behavior, visual rendering, and interactive elements. They can be stacked using their z index to control rendering or processing order.


§Fields

§name: String

A human-readable identifier for the layer (e.g., "collision", "decals", "interactions"). Useful for debugging, editor tools, or dynamic filtering.


§masks: Vec<Mask>

A list of Masks applied in this layer. Each mask can apply one or more effects over a rectangular region.


§z: u32

The z-index of the layer. Layers are rendered or evaluated in ascending z-order (lower z appears below higher z).


§Methods

§Layer::new(name: String, masks: Vec<Mask>, z: u32) -> Self

Constructs a new Layer with the given name, list of masks, and z-index.

use rpgx::prelude::*;

let layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

§fn get_shape(&self) -> Shape

Returns the bounding Shape that contains all the masks in the layer.

use rpgx::prelude::*;

let layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

let bounds = layer.get_shape();

§fn offset(&mut self, delta: Delta)

Applies a positional shift to all the masks in the layer, updating all coordinates and effect areas.

use rpgx::prelude::*;

let mut layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

layer.offset(Delta::new(2, 3));

This modifies the layer in-place and is useful for relocating or merging map segments.


§fn translate(&self, delta: Delta) -> Self

Returns a new Layer with the same masks but offset by the specified Delta.

use rpgx::prelude::*;

let layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

let translated = layer.translate(Delta::new(1, 1));

The original layer remains unchanged.


§fn contains(&self, coord: &Coordinates) -> bool

Returns true if any mask in the layer contains the given coordinate.

use rpgx::prelude::*;

let layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

if layer.contains(&Coordinates::new(5, 5)) {
    // ...
}

§fn is_blocking_at(&self, target: &Coordinates) -> bool

Returns true if the specified coordinate is marked as blocking in any mask within the layer.

use rpgx::prelude::*;

let layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

if layer.is_blocking_at(&Coordinates::new(2, 3)) {
    // Prevent movement
}

§fn get_actions_at(&self, target: &Coordinates) -> Vec<u32>

Returns a list of action effect IDs applied at the given coordinate. If no action applies, returns an empty list.

use rpgx::prelude::*;

let layer = Layer::new(
    "combined_grounds".into(),
    vec![
        Mask::new(
            "ground".into(),
            Rect::from_shape(Shape::from_square(10)).into_many(),
            vec![Effect::Texture(1)]
        ),
        Mask::new(
            "inner_ground".into(),
            Rect::new(Coordinates::new(1,1), Shape::from_square(8)).into_many(),
            vec![Effect::Texture(2)]
        )
    ],
    1
);

let actions = layer.get_actions_at(&Coordinates::new(1, 2));

§Usage Example

use rpgx::prelude::*;

let mask = Mask::new(
    "interaction_zone".into(),
    vec![Rect::from_xywh(1, 1, 2, 2)],
    vec![Effect::Action(42)],
);

let layer = Layer::new("interactions".into(), vec![mask], 2);

assert!(layer.contains(&Coordinates::new(2, 2)));
assert_eq!(layer.get_actions_at(&Coordinates::new(2, 2)), vec![42]);
assert!(!layer.is_blocking_at(&Coordinates::new(0, 0)));

§Design Notes

  • Layers enable composition of logic like collision, decoration, event zones, and rendering overlays.
  • They can be programmatically offset or duplicated via offset and translate.
  • Layers are independent of the map grid and do not need to cover the full area.
  • When merged into a Map, their effects are combined according to their z-index.

§See Also

  • Mask: A group of Rects with associated Effects.
  • Effect: The behaviors or visuals associated with a tile.
  • Rect: A rectangular region.
  • Shape: Width and height abstraction.
  • Coordinates: X, Y position on the map.
  • Delta: A vector used for position shifting.
  • Map: The main structure that contains base layers and overlays. A Layer is a logical or visual overlay composed of Masks that apply [Effect]s to specific tiles.

Layers allow grouped application of tile-based modifications (e.g. collision, decoration, triggers) without altering the original base grid. Layers are rendered or processed in Z-order, determined by z.

Fields§

§name: String

The name of the layer (e.g., "collision", "visuals")

§masks: Vec<Mask>

A list of masks (effect areas) applied in this layer

§z: u32

Z-index for stacking and rendering order

Implementations§

Source§

impl Layer

Source

pub fn new(name: String, masks: Vec<Mask>, z: u32) -> Self

Creates a new layer with a name, masks, and z-index.

Source§

impl Layer

Source

pub fn is_blocking_at(&self, target: &Coordinates) -> bool

Source

pub fn get_actions_at(&self, target: &Coordinates) -> Vec<u32>

Trait Implementations§

Source§

impl Clone for Layer

Source§

fn clone(&self) -> Layer

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Layer

Source§

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

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

impl Grid for Layer

Source§

fn contains(&self, coord: &Coordinates) -> bool

Checks if the layer contains a tile at the specified coordinate.

Source§

impl Shaped for Layer

Source§

fn get_shape(&self) -> Shape

Returns the overall bounding shape of all masks.

Source§

impl Shiftable for Layer

Source§

fn offset(&mut self, delta: Delta)

Offsets all tiles in the layer by the given delta.

Source§

fn translate(&self, delta: Delta) -> Self

Auto Trait Implementations§

§

impl Freeze for Layer

§

impl RefUnwindSafe for Layer

§

impl Send for Layer

§

impl Sync for Layer

§

impl Unpin for Layer

§

impl UnwindSafe for Layer

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> 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.