aboutsummaryrefslogtreecommitdiff
path: root/src/world/wall.rs
blob: 45b0b1ed4df166f1cd2cd6ce3756d6500718cb5f (plain) (blame)
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
//! Walls, solid barriers that are generally unclimbable.
//!
//! This interpretation is generally up to the GM to decide, but generally
//! speaking, a wall cannot be crossed by a player. If special conditions apply
//! (for instance, when the player wants to scale the wall), a check is necessary.
//! If a check is not necessary, then maybe you were not thinking about a wall.

use super::Component;
use crate::math::{LineSegment, Rect};
use crate::transformable::NonRigidTransformable;
use nalgebra::Matrix3;
use serde::{Deserialize, Serialize};

/// Representation of a solid wall a player cannot go through, except if special
/// conditions apply.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Wall {
    shape: LineSegment<f64>,
}

impl Wall {
    /// Create a new wall with the line segment defining it's start and end.
    pub fn new(shape: LineSegment<f64>) -> Self {
        Self { shape }
    }

    /// Get the line shape this wall is based on.
    pub fn shape(&self) -> &LineSegment<f64> {
        &self.shape
    }
}

impl Component for Wall {
    fn bounding_rect(&self) -> Rect<f64> {
        Rect::bounding_rect(self.shape.start, self.shape.end)
    }

    fn as_non_rigid(&self) -> Option<&dyn NonRigidTransformable> {
        Some(self as &dyn NonRigidTransformable)
    }

    fn as_non_rigid_mut(&mut self) -> Option<&mut dyn NonRigidTransformable> {
        Some(self as &mut dyn NonRigidTransformable)
    }
}

impl NonRigidTransformable for Wall {
    fn apply_matrix(&mut self, matrix: &Matrix3<f64>) {
        self.shape.start = matrix.transform_point(&self.shape.start.into()).into();
        self.shape.end = matrix.transform_point(&self.shape.end.into()).into();
    }
}