aboutsummaryrefslogtreecommitdiff
path: root/src/client/tool/wall_tool.rs
diff options
context:
space:
mode:
authorArne Dußin2021-01-27 14:01:50 +0100
committerArne Dußin2021-02-02 22:16:15 +0100
commitf92e9f6f07b1e3834c2ca58ce3510734819d08e4 (patch)
tree20e3d3afce342a56ae98f6c20491482ccd2b5c6b /src/client/tool/wall_tool.rs
parentc60a6d07efb120724b308e29e8e70f27c87c952d (diff)
downloadgraf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.tar.gz
graf_karto-f92e9f6f07b1e3834c2ca58ce3510734819d08e4.zip
Rework graf karto to fit the client/server structure
Diffstat (limited to 'src/client/tool/wall_tool.rs')
-rw-r--r--src/client/tool/wall_tool.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/client/tool/wall_tool.rs b/src/client/tool/wall_tool.rs
new file mode 100644
index 0000000..857beea
--- /dev/null
+++ b/src/client/tool/wall_tool.rs
@@ -0,0 +1,76 @@
+//! Tool to create walls. For information about walls, see also
+//! [the wall module](crate::map::wall).
+
+use super::Tool;
+use crate::client::map::Map;
+use crate::client::transform::Transform;
+use crate::math::{LineSegment, Vec2};
+use crate::net::{Cargo, Connection};
+use crate::world::Wall;
+use raylib::core::drawing::{RaylibDraw, RaylibDrawHandle};
+use raylib::ffi::{Color, Vector2};
+
+/// The wall tool to create solid barriers a player usually cannot cross.
+pub struct WallTool {
+ unfinished_wall: Option<LineSegment<f64>>,
+}
+
+impl WallTool {
+ /// Create a new wall tool. There should only be one wall tool per program instance, which should
+ /// be created inside of the editor.
+ #[allow(clippy::new_without_default)]
+ pub fn new() -> Self {
+ Self {
+ unfinished_wall: None,
+ }
+ }
+}
+
+impl Tool for WallTool {
+ fn deactivate(&mut self) {
+ self.unfinished_wall = None;
+ }
+
+ fn update(&mut self, _map: &Map, mouse_pos_m: &Vec2<f64>) {
+ if let Some(ref mut wall) = &mut self.unfinished_wall {
+ wall.end = *mouse_pos_m;
+ }
+ }
+
+ fn draw(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
+ if let Some(ref wall) = self.unfinished_wall {
+ let start: Vector2 = transform.point_m_to_px(&wall.start).into();
+ let end: Vector2 = transform.point_m_to_px(&wall.end).into();
+ rld.draw_line_ex(
+ start,
+ end,
+ transform.length_m_to_px(0.1) as f32,
+ Color {
+ r: 150,
+ g: 200,
+ b: 150,
+ a: 255,
+ },
+ );
+ }
+ }
+
+ fn place_single(
+ &mut self,
+ _map: &mut Map,
+ server: &Connection<Cargo>,
+ mouse_pos_m: &Vec2<f64>,
+ ) {
+ if let Some(wall) = self.unfinished_wall.take() {
+ // Continue with the next wall straight away.
+ self.unfinished_wall = Some(LineSegment::new(wall.end, wall.end));
+ server.send(Cargo::AddWall(Wall::new(wall)));
+ } else {
+ self.unfinished_wall = Some(LineSegment::new(*mouse_pos_m, *mouse_pos_m));
+ }
+ }
+
+ fn abort(&mut self) {
+ self.unfinished_wall = None;
+ }
+}