Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ members = [
"src/game_systems",
"src/game_systems/src/background",
"src/game_systems/src/mobs",
"src/game_systems/src/interactions",
"src/game_systems/src/packets",
"src/game_systems/src/physics",
"src/game_systems/src/player",
Expand Down Expand Up @@ -148,6 +149,7 @@ temper-physics = { path = "src/physics" }
temper-particles = { path = "src/particles" }
temper-resources = { path = "src/resources" }
temper-game-systems = { path = "src/game_systems" }
interactions = { path = "src/game_systems/src/interactions" }

# Asynchronous
tokio = { version = "1.50.0", features = [
Expand Down
2 changes: 1 addition & 1 deletion src/components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ tokio = { workspace = true }

temper-inventories = { workspace = true }
temper-codec = { workspace = true }
temper-core = { workspace = true }
temper-config = { workspace = true }
bitcode = { workspace = true }
bitcode_derive = { workspace = true }
temper-data = { workspace = true }
bevy_math = { workspace = true }
uuid = { workspace = true }
temper-core = { workspace = true }
type_hash = { workspace = true }
31 changes: 31 additions & 0 deletions src/components/src/interaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bevy_ecs::prelude::*;

/// Marker: this block entity is interactive.
#[derive(Component, Debug, Clone, Copy)]
pub struct InteractableBlock;

/// Anti-spam cooldown for block interactions.
#[derive(Component, Debug, Clone)]
pub struct InteractionCooldown {
pub cooldown_ms: u64,
pub last_interaction: Option<std::time::Instant>,
}

impl Default for InteractionCooldown {
fn default() -> Self {
Self {
cooldown_ms: 200,
last_interaction: None,
}
}
}

/// Block that toggles between two states (open/closed).
#[derive(Component, Debug, Clone, Copy)]
pub struct Toggleable {
pub is_active: bool,
}

/// Marker for door block entities.
#[derive(Component, Debug, Clone, Copy)]
pub struct Door;
5 changes: 5 additions & 0 deletions src/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ pub mod active_effects;
pub mod bounds;
pub mod entity_identity;
pub mod health;
pub mod interaction;
pub mod player;

// Core entity components based on temper-data
pub mod combat;
pub mod last_synced_position;
pub mod metadata;
pub mod physical;
pub mod spawn;

// Interaction components re-exports
pub use interaction::{Door, InteractableBlock, InteractionCooldown, Toggleable};
36 changes: 36 additions & 0 deletions src/entities/src/bundles/interactive/door.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use bevy_ecs::prelude::Bundle;
use temper_components::interaction::{Door, InteractableBlock, InteractionCooldown, Toggleable};
use temper_components::player::position::Position;
use temper_core::pos::BlockPos;

/// Bundle to spawn a door block entity in Bevy ECS.
#[derive(Bundle)]
pub struct DoorBlockBundle {
pub position: Position,
pub interactable: InteractableBlock,
pub toggleable: Toggleable,
pub cooldown: InteractionCooldown,
pub door: Door,
}

impl DoorBlockBundle {
pub fn new(pos: BlockPos) -> Self {
Self {
position: Position::new(pos.pos.x as f64, pos.pos.y as f64, pos.pos.z as f64),
interactable: InteractableBlock,
toggleable: Toggleable { is_active: false },
cooldown: InteractionCooldown::default(),
door: Door,
}
}

pub fn new_open(pos: BlockPos) -> Self {
Self {
position: Position::new(pos.pos.x as f64, pos.pos.y as f64, pos.pos.z as f64),
interactable: InteractableBlock,
toggleable: Toggleable { is_active: true },
cooldown: InteractionCooldown::default(),
door: Door,
}
}
}
30 changes: 30 additions & 0 deletions src/entities/src/bundles/interactive/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Bundles for interactive block entities.
//!
//! These bundles link ECS entities to block positions in the world grid,
//! allowing blocks like doors, levers, and chests to have complex behavior
//! managed through the ECS interaction system.
//!
//! ## Architecture
//!
//! Interactive blocks are represented as ECS entities with:
//! - `BlockPosition` - Links to world grid coordinates
//! - `InteractableBlock` - Marks as interactive
//! - Capability components (`Toggleable`, `Container`, `RedstoneEmitter`)
//! - Type markers (`Door`, `Lever`, `Chest`)
//!
//! When a player interacts with a block position:
//! 1. The packet handler looks up the entity by `BlockPosition`
//! 2. The interaction system checks for `InteractableBlock`
//! 3. Observers react based on capability components
//!
//! ## Adding a New Interactive Block
//!
//! 1. Create a new bundle file (e.g., `button.rs`)
//! 2. Add a marker component to `components/interaction.rs`
//! 3. Create a bundle with the appropriate capability components
//! 4. (Optional) Add an Observer for custom behavior

pub mod door;

// Re-export all bundles and their type markers
pub use door::DoorBlockBundle;
2 changes: 2 additions & 0 deletions src/entities/src/bundles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Organized by behavior category

pub mod hostile;
pub mod interactive;
pub mod neutral;
pub mod passive;

// Re-export all bundles for convenience
pub use hostile::*;
pub use interactive::*;
pub use neutral::*;
pub use passive::*;

Expand Down
1 change: 1 addition & 0 deletions src/game_systems/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ physics = { path = "./src/physics" }
player = { path = "./src/player" }
shutdown = { path = "./src/shutdown" }
world = { path = "./src/world" }
interactions = { path = "./src/interactions" }

bevy_ecs = { workspace = true }

Expand Down
20 changes: 20 additions & 0 deletions src/game_systems/src/interactions/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "interactions"
version = "0.1.0"
edition = "2024"

[dependencies]
bevy_ecs = { workspace = true }
temper-codec = { workspace = true }
temper-components = { workspace = true }
temper-config = { workspace = true }
temper-core = { workspace = true }
temper-messages = { workspace = true }
temper-net-runtime = { workspace = true }
temper-protocol = { workspace = true }
temper-state = { workspace = true }
temper-world = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
temper-macros = { workspace = true }
Loading