Skip to content

Commit

Permalink
[#66] Door handles (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed May 24, 2021
1 parent a31f2d6 commit a48ae26
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 144 deletions.
18 changes: 17 additions & 1 deletion resources/styles/doors/wooden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,20 @@ edge_style:
- Rendering:
FillArea:
color: "#696969"
depth: 200
depth: 200
handle_style:
distance_to_end: 40
offset: 0
size:
width: 96
height: 48
component:
Shape:
shape_factory:
RoundedRectangle: 0.7
color:
ConstantColor: "#432711"
depth:
Dome:
center: 120
border: 100
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum RenderingDefinition {
color: String,
depth: u8,
},
Mock,
Shape {
shape_factory: ShapeFactorDefinition,
color: ColorSelectorDefinition,
Expand All @@ -33,6 +34,7 @@ impl RenderingDefinition {
))?;
Ok(RenderingComponent::new_fill_area(color, *depth))
}
RenderingDefinition::Mock => Ok(RenderingComponent::Mock),
RenderingDefinition::Shape {
shape_factory,
color,
Expand Down
7 changes: 6 additions & 1 deletion texture_generation/src/generation/component/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ pub mod depth_factory;
/// Renders the texture.
pub enum RenderingComponent {
/// Fills the area with a color.
FillArea { color: Color, depth: u8 },
FillArea {
color: Color,
depth: u8,
},
Mock,
/// Renders a [`Shape`].
Shape {
shape_factory: ShapeFactory,
Expand Down Expand Up @@ -78,6 +82,7 @@ impl RenderingComponent {
point.y += 1;
}
}
RenderingComponent::Mock => {}
RenderingComponent::Shape {
shape_factory,
color_selector,
Expand Down
11 changes: 11 additions & 0 deletions texture_generation/src/math/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ impl Point {
Point { x, y }
}

/// Returns a new point with switched x & <height>.
///
/// ```
///# use texture_generation::math::point::Point;
/// let point = Point::new(-10, 30);
/// assert_eq!(point.flip(), Point::new(30, -10));
/// ```
pub fn flip(&self) -> Point {
Point::new(self.y, self.x)
}

/// Calculates the euclidean distance to another point.
///
/// ```
Expand Down
73 changes: 41 additions & 32 deletions tilemap/src/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,12 @@ impl Renderer {
Border::Empty => {}
Border::Wall(id) => {
let wall_style = self.resources.wall_styles.get(id);
let start_index = get_start_of_horizontal_border(index, y);
let end_index = get_end_of_horizontal_border(index, y);

wall_style.get_edge_style().render_horizontal(
&data,
start,
self.tile_size,
self.calculate_horizontal_edge(nodes, index, y),
0,
nodes[start_index],
nodes[end_index],
texture,
);
}
Expand All @@ -139,32 +135,24 @@ impl Renderer {
} => {
let wall_style = self.resources.wall_styles.get(wall_id);
let door_style = self.resources.door_styles.get(door_id);
let start_index = get_start_of_horizontal_border(index, y);
let end_index = get_end_of_horizontal_border(index, y);
let offset = door_style
.get_offset(wall_style.get_edge_style().get_thickness(), is_front);

door_style.get_edge_style().render_horizontal(
door_style.render_horizontal(
&data,
start,
self.tile_size,
self.calculate_horizontal_edge(nodes, index, y),
offset,
nodes[start_index],
nodes[end_index],
texture,
);
}
Border::Window { window_id, .. } => {
let window_style = self.resources.window_styles.get(window_id);
let start_index = get_start_of_horizontal_border(index, y);
let end_index = get_end_of_horizontal_border(index, y);

window_style.render_horizontal(
&data,
start,
self.tile_size,
nodes[start_index],
nodes[end_index],
self.calculate_horizontal_edge(nodes, index, y),
texture,
);
}
Expand Down Expand Up @@ -207,16 +195,12 @@ impl Renderer {
Border::Empty => {}
Border::Wall(id) => {
let wall_style = self.resources.wall_styles.get(id);
let start_index = get_start_of_vertical_border(index);
let end_index = get_end_of_vertical_border(size, index);

wall_style.get_edge_style().render_vertical(
&data,
start,
self.tile_size,
self.calculate_vertical_edge(nodes, size, index),
0,
nodes[start_index],
nodes[end_index],
texture,
);
}
Expand All @@ -227,32 +211,24 @@ impl Renderer {
} => {
let wall_style = self.resources.wall_styles.get(wall_id);
let door_style = self.resources.door_styles.get(door_id);
let start_index = get_start_of_vertical_border(index);
let end_index = get_end_of_vertical_border(size, index);
let offset = door_style
.get_offset(wall_style.get_edge_style().get_thickness(), is_front);

door_style.get_edge_style().render_vertical(
door_style.render_vertical(
&data,
start,
self.tile_size,
self.calculate_vertical_edge(nodes, size, index),
offset,
nodes[start_index],
nodes[end_index],
texture,
);
}
Border::Window { window_id, .. } => {
let window_style = self.resources.window_styles.get(window_id);
let start_index = get_start_of_vertical_border(index);
let end_index = get_end_of_vertical_border(size, index);

window_style.render_vertical(
&data,
start,
self.tile_size,
nodes[start_index],
nodes[end_index],
self.calculate_vertical_edge(nodes, size, index),
texture,
);
}
Expand Down Expand Up @@ -300,6 +276,39 @@ impl Renderer {
texture.set_base_depth(height);
generator.render(texture, &data);
}

fn calculate_horizontal_edge(
&self,
nodes: &[Option<&NodeStyle>],
border_index: usize,
y: u32,
) -> (i32, u32) {
let start_index = get_start_of_horizontal_border(border_index, y);
let end_index = get_end_of_horizontal_border(border_index, y);
self.calculate_edge(nodes, start_index, end_index)
}

fn calculate_vertical_edge(
&self,
nodes: &[Option<&NodeStyle>],
size: Size,
border_index: usize,
) -> (i32, u32) {
let start_index = get_start_of_vertical_border(border_index);
let end_index = get_end_of_vertical_border(size, border_index);
self.calculate_edge(nodes, start_index, end_index)
}

fn calculate_edge(
&self,
nodes: &[Option<&NodeStyle>],
start_index: usize,
end_index: usize,
) -> (i32, u32) {
let start_half = nodes[start_index].map(|n| n.get_half()).unwrap_or(0);
let end_half = nodes[end_index].map(|n| n.get_half()).unwrap_or(0);
(start_half, self.tile_size - (start_half + end_half) as u32)
}
}

#[cfg(test)]
Expand Down
56 changes: 50 additions & 6 deletions tilemap/src/rendering/style/door.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::rendering::style::edge::EdgeStyle;
use crate::rendering::style::handle::HandleStyle;
use texture_generation::generation::data::texture::Texture;
use texture_generation::generation::data::Data;
use texture_generation::math::point::Point;
use texture_generation::utils::resource::Resource;

#[derive(Clone, Debug, PartialEq)]
Expand All @@ -7,26 +11,34 @@ pub struct DoorStyle {
name: String,
/// The style of a movable part of the door.
edge_style: EdgeStyle,
handle_style: Option<HandleStyle>,
is_centered: bool,
}

impl DoorStyle {
pub fn default(thickness: u32) -> DoorStyle {
Self::new("default", EdgeStyle::default(thickness).unwrap(), true)
Self::new(
"default",
EdgeStyle::default(thickness).unwrap(),
None,
true,
)
}

pub fn new<S: Into<String>>(name: S, edge_style: EdgeStyle, is_centered: bool) -> DoorStyle {
pub fn new<S: Into<String>>(
name: S,
edge_style: EdgeStyle,
handle_style: Option<HandleStyle>,
is_centered: bool,
) -> DoorStyle {
DoorStyle {
name: name.into(),
edge_style,
handle_style,
is_centered,
}
}

pub fn get_edge_style(&self) -> &EdgeStyle {
&self.edge_style
}

pub fn get_offset(&self, wall_thickness: u32, is_front: bool) -> i32 {
if self.is_centered {
return 0;
Expand All @@ -40,6 +52,38 @@ impl DoorStyle {
-offset
}
}

pub fn render_horizontal(
&self,
data: &Data,
node: Point,
edge: (i32, u32),
offset: i32,
texture: &mut Texture,
) {
if let Some(handle) = &self.handle_style {
handle.render_horizontal(data, node, edge, offset, texture);
}

self.edge_style
.render_horizontal(data, node, edge, offset, texture);
}

pub fn render_vertical(
&self,
data: &Data,
node: Point,
edge: (i32, u32),
offset: i32,
texture: &mut Texture,
) {
if let Some(handle) = &self.handle_style {
handle.render_vertical(data, node, edge, offset, texture);
}

self.edge_style
.render_vertical(data, node, edge, offset, texture);
}
}

impl Default for DoorStyle {
Expand Down
Loading

0 comments on commit a48ae26

Please sign in to comment.