aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <silas@exvacuum.dev>2024-06-03 20:00:08 -0400
committerLibravatar Silas Bartha <silas@exvacuum.dev>2024-06-03 20:00:08 -0400
commitebc3ccfea4027a3eb0c80f6a3d64b6425d32d1ef (patch)
tree287a5a2cea0f5d37970cea888ffbd2b676dee14d /src
Initial Commit
Diffstat (limited to 'src')
-rw-r--r--src/components.rs5
-rw-r--r--src/events.rs18
-rw-r--r--src/lib.rs32
-rw-r--r--src/resources.rs80
4 files changed, 135 insertions, 0 deletions
diff --git a/src/components.rs b/src/components.rs
new file mode 100644
index 0000000..69a5cde
--- /dev/null
+++ b/src/components.rs
@@ -0,0 +1,5 @@
+use bevy::prelude::*;
+
+/// A tooltip on an object, which can be displayed.
+#[derive(Component)]
+pub struct Tooltip(pub String);
diff --git a/src/events.rs b/src/events.rs
new file mode 100644
index 0000000..0c2e7f1
--- /dev/null
+++ b/src/events.rs
@@ -0,0 +1,18 @@
+use std::path::PathBuf;
+
+use bevy::prelude::*;
+
+/// Events related to activities in the dirworld.
+#[derive(Event)]
+pub enum DirworldNavigationEvent {
+ /// Triggered when a room is left.
+ LeftRoom {
+ /// Path of room just left.
+ path: PathBuf,
+ },
+ /// Triggered when a room is entered.
+ EnteredRoom {
+ /// Path of room just entered.
+ path: PathBuf,
+ },
+}
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..6b46502
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,32 @@
+#![warn(missing_docs)]
+
+//! Plugin for bevy engine enabling interaction with and representation of the file system in the world.
+
+use std::path::PathBuf;
+
+use bevy::prelude::*;
+use events::DirworldNavigationEvent;
+use resources::{Dirworld, DirworldConfig};
+
+/// Components used by this plugin
+pub mod components;
+
+/// Events used by this plugin
+pub mod events;
+
+/// Resources used by this plugin
+pub mod resources;
+
+/// Plugin which enables high-level interaction
+pub struct DirworldPlugin {
+ /// Root path of world
+ pub path: PathBuf,
+}
+
+impl Plugin for DirworldPlugin {
+ fn build(&self, app: &mut App) {
+ app.insert_resource(DirworldConfig::new(self.path.clone()))
+ .add_event::<DirworldNavigationEvent>()
+ .init_resource::<Dirworld>();
+ }
+}
diff --git a/src/resources.rs b/src/resources.rs
new file mode 100644
index 0000000..c145960
--- /dev/null
+++ b/src/resources.rs
@@ -0,0 +1,80 @@
+use std::{
+ fs,
+ path::{Path, PathBuf},
+};
+
+use anyhow::{Context, Result};
+use bevy::prelude::*;
+
+use crate::events::DirworldNavigationEvent;
+
+/// Configuration for Dirworld.
+#[derive(Resource)]
+pub struct DirworldConfig {
+ root: PathBuf,
+}
+
+impl DirworldConfig {
+ /// Construct a new dirworld config with the given root path. Will panic if the provided path
+ /// cannot be canonicalized.
+ // TODO: Don't panic? lol
+ pub fn new(root: PathBuf) -> Self {
+ Self {
+ root: fs::canonicalize(root).expect("Failed to canonicalize path!"),
+ }
+ }
+
+ ///
+ pub fn root(&self) -> &PathBuf {
+ &self.root
+ }
+}
+
+/// Contains the dirworld state.
+#[derive(Resource)]
+pub struct Dirworld {
+ /// Current active directory.
+ pub path: PathBuf,
+
+ /// Entities local to the current room.
+ pub tracked_entities: Vec<Entity>,
+}
+
+impl FromWorld for Dirworld {
+ fn from_world(world: &mut World) -> Self {
+ let config = world.remove_resource::<DirworldConfig>().unwrap();
+ world.send_event(DirworldNavigationEvent::EnteredRoom {
+ path: config.root().clone(),
+ });
+ let result = Self {
+ path: config.root().clone(),
+ tracked_entities: vec![],
+ };
+ world.insert_resource(config);
+ result
+ }
+}
+
+impl Dirworld {
+ /// Move into a new room.
+ // TODO: Clear tracked entities?
+ // TODO: Make into command extension trait?
+ pub fn navigate_to(
+ &mut self,
+ path: PathBuf,
+ event_writer: &mut EventWriter<DirworldNavigationEvent>,
+ ) -> Result<()> {
+ event_writer.send(DirworldNavigationEvent::LeftRoom {
+ path: self.path.clone(),
+ });
+ self.path = Path::new(&self.path)
+ .join(path)
+ .to_str()
+ .context("Path not valid UTF-8")?
+ .into();
+ event_writer.send(DirworldNavigationEvent::EnteredRoom {
+ path: self.path.clone(),
+ });
+ Ok(())
+ }
+}