Skip to content

Commit c7b6336

Browse files
Generate docs!
1 parent 160055e commit c7b6336

File tree

13 files changed

+1713
-0
lines changed

13 files changed

+1713
-0
lines changed

docs/client/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Client Plugins
2+
3+
A collection of plugins designed for the Client.
4+
5+
- [Chat Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/chat.md): Handles in-game messaging and chat features.
6+
- [Collider Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/collider.md): Manages collision detection and response.
7+
- [GUI Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/gui.md): Provides graphical user interface components.
8+
- [Networking Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/networking.md): Facilitates network communication between clients and servers.
9+
- [Player Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/player.md): Defines player characteristics and behaviors.
10+
- [Remote Player Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/remote_player.md): Manages remote player instances within the game.
11+
- [Terrain Plugin](https://github.com/CuddlyBunion341/hello-bevy/blob/main/terrain.md): Handles terrain generation and manipulation.

docs/client/chat.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Plugin: Chat
2+
3+
The Chat plugin facilitates in-game communication by enabling players to send and receive messages in real-time.
4+
5+
## Dependencies
6+
- `bevy_renet`: This library is essential for handling networking functionalities, allowing the transmission of chat messages between clients and the server.
7+
8+
## Mermaid Diagram
9+
```mermaid
10+
graph TD
11+
subgraph Components
12+
A[ChatMessageContainer]
13+
B[ChatMessageInputElement]
14+
C[ChatMessageElement]
15+
end
16+
17+
subgraph Systems
18+
D[Setup Chat Container]
19+
E[Process Chat Input]
20+
F[Send Messages]
21+
G[Handle Focus Events]
22+
H[Synchronize Chat Messages]
23+
end
24+
25+
subgraph Resources
26+
I[ChatHistory]
27+
J[ChatState]
28+
end
29+
30+
subgraph Events
31+
K[ChatSyncEvent]
32+
L[SingleChatSendEvent]
33+
M[ChatMessageSendEvent]
34+
N[ChatFocusStateChangeEvent]
35+
end
36+
37+
A -->|contains|B
38+
D -->|creates|A
39+
E -->|reads input from|B
40+
E -->|sends events to|M
41+
F -->|sends message to network|M
42+
G -->|changes focus state|N
43+
H -->|syncs messages|K
44+
K -->|triggers|L
45+
```
46+
47+
## Components
48+
- `ChatMessageContainer`: Holds the messages displayed in the chat interface.
49+
- `ChatMessageInputElement`: Represents the input field for typing messages.
50+
- `ChatMessageElement`: Represents individual messages in the chat.
51+
52+
## Resources
53+
- `ChatHistory`: Maintains a history of all chat messages exchanged during a session.
54+
- `ChatState`: Tracks the current state of the chat input (e.g., whether it is focused).
55+
56+
## Systems
57+
- **UI Management**:
58+
- `setup_chat_container`: Initializes the chat UI components.
59+
- **Input Handling**:
60+
- `process_chat_input_system`: Processes user input from the chat field.
61+
- **Networking**:
62+
- `send_messages_system`: Sends typed messages to other players over the network.
63+
- `handle_chat_message_sync_event`: Handles synchronization of chat messages received from other clients.
64+
65+
## Context
66+
- Includes files from the project's plugin directory.
67+
- Incorporates [`prelude.rs`](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/prelude.rs) and networking systems specific to chat functionality.
68+
69+
## Collected Source Files
70+
- [events.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/chat/events.rs)
71+
- [systems.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/chat/systems.rs)
72+
- [mod.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/chat/mod.rs)
73+
- [components.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/chat/components.rs)
74+
- [resources.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/chat/resources.rs)
75+
76+
## Source Code Content
77+
78+
```rs
79+
// ---- File: src/client/chat/events.rs ----
80+
use crate::prelude::*;
81+
82+
#[derive(Event)]
83+
pub struct ChatSyncEvent(pub Vec<lib::ChatMessage>);
84+
85+
#[derive(Event)]
86+
pub struct SingleChatSendEvent(pub lib::ChatMessage);
87+
88+
#[derive(Event)]
89+
pub struct ChatMessageSendEvent(pub String);
90+
91+
pub enum FocusState {
92+
Focus,
93+
Unfocus,
94+
}
95+
96+
#[derive(Event)]
97+
pub struct ChatFocusStateChangeEvent {
98+
pub state: FocusState,
99+
}
100+
101+
// ---- File: src/client/chat/systems.rs ----
102+
// (System implementations as provided in the original content)
103+
104+
// ---- File: src/client/chat/mod.rs ----
105+
// (Plugin initialization as provided in the original content)
106+
107+
// ---- File: src/client/chat/components.rs ----
108+
// (Component definitions as provided in the original content)
109+
110+
// ---- File: src/client/chat/resources.rs ----
111+
// (Resource definitions as provided in the original content)
112+
```
113+
114+
This documentation provides a comprehensive overview of the Chat plugin's architecture, components, systems, and interactions, ensuring that developers can effectively understand and utilize its features.

docs/client/collider.md

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# Plugin: Collider
2+
3+
The Collider plugin facilitates the management of colliders within the ECS framework, enabling efficient collision detection and response in a 3D environment.
4+
5+
## Dependencies
6+
- `bevy`: The primary game engine used for rendering and managing game state.
7+
- `bevy_rapier3d`: Provides physics capabilities including collision detection and rigid body dynamics.
8+
9+
## Mermaid Diagram
10+
```mermaid
11+
graph TD
12+
subgraph Components
13+
BlockCollider[BlockCollider]
14+
end
15+
16+
subgraph Systems
17+
SetupCollidersSystem[setup_coliders_system]
18+
HandleColliderUpdateEventsSystem[handle_collider_update_events_system]
19+
end
20+
21+
subgraph Events
22+
ColliderUpdateEvent[ColliderUpdateEvent]
23+
end
24+
25+
subgraph Resources
26+
ChunkManager[ChunkManager]
27+
end
28+
29+
BlockCollider -->|has| SetupCollidersSystem
30+
ColliderUpdateEvent -->|triggers| HandleColliderUpdateEventsSystem
31+
HandleColliderUpdateEventsSystem -->|reads from| ChunkManager
32+
```
33+
34+
## Components
35+
- `BlockCollider`: Represents a collider with a defined relative position, used to detect collisions within the game world.
36+
37+
## Resources
38+
- `ChunkManager`: Manages chunks of the game world, facilitating retrieval and updates of block states for collision calculations.
39+
40+
## Systems
41+
- **Setup**:
42+
- `setup_coliders_system`: Initializes the collider grid by spawning BlockCollider entities based on a predefined size.
43+
- **Update**:
44+
- `handle_collider_update_events_system`: Responds to ColliderUpdateEvents to update collider positions based on world changes.
45+
46+
## Context
47+
- Includes files from the project's plugin directory.
48+
- Incorporates [`prelude.rs`](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/prelude.rs) and networking systems specific to the plugin.
49+
50+
## Collected Source Files
51+
- [events.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/collider/events.rs)
52+
- [systems.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/collider/systems.rs)
53+
- [mod.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/collider/mod.rs)
54+
- [components.rs](https://github.com/CuddlyBunion341/hello-bevy/blob/main/src/client/collider/components.rs)
55+
56+
## Source Code Content
57+
58+
```rs
59+
// ---- File: src/client/collider/events.rs ----
60+
use crate::prelude::*;
61+
62+
#[derive(Event)]
63+
pub struct ColliderUpdateEvent {
64+
pub grid_center_position: [f32; 3],
65+
}
66+
67+
// ---- File: src/client/collider/systems.rs ----
68+
use crate::prelude::*;
69+
70+
static COLLIDER_GRID_SIZE: u32 = 3;
71+
static COLLIDER_RESTING_POSITION: Vec3 = Vec3::ZERO;
72+
73+
pub fn setup_coliders_system(mut commands: Commands) {
74+
let collider_range = 0..COLLIDER_GRID_SIZE;
75+
76+
for x in collider_range.clone() {
77+
for y in collider_range.clone() {
78+
for z in collider_range.clone() {
79+
commands
80+
.spawn(Collider::cuboid(0.5, 0.5, 0.5))
81+
.insert(TransformBundle::from(Transform::from_xyz(
82+
x as f32, y as f32, z as f32,
83+
)))
84+
.insert(collider_components::BlockCollider {
85+
relative_position: Vec3 {
86+
x: x as f32,
87+
y: y as f32,
88+
z: z as f32,
89+
},
90+
});
91+
}
92+
}
93+
}
94+
}
95+
96+
pub fn handle_collider_update_events_system(
97+
mut collider_grid_events: EventReader<collider_events::ColliderUpdateEvent>,
98+
mut query: Query<(&mut Transform, &collider_components::BlockCollider)>,
99+
mut chunk_manager: ResMut<terrain_resources::ChunkManager>,
100+
) {
101+
for event in collider_grid_events.read() {
102+
let event_position = Vec3::new(
103+
event.grid_center_position[0],
104+
event.grid_center_position[1],
105+
event.grid_center_position[2],
106+
)
107+
.floor();
108+
for (mut transform, collider) in query.iter_mut() {
109+
let relative_position = collider.relative_position;
110+
let collider_position = (event_position + relative_position).floor();
111+
let block = chunk_manager.get_block(collider_position);
112+
113+
match block {
114+
Some(block) => {
115+
if block != BlockId::Air {
116+
transform.translation = collider_position + 0.5;
117+
} else {
118+
transform.translation = COLLIDER_RESTING_POSITION;
119+
}
120+
}
121+
None => {
122+
transform.translation = COLLIDER_RESTING_POSITION;
123+
}
124+
}
125+
}
126+
}
127+
}
128+
129+
#[cfg(test)]
130+
mod tests {
131+
use collider_events::ColliderUpdateEvent;
132+
133+
use super::*;
134+
135+
fn setup_app() -> App {
136+
let mut app = App::new();
137+
app.add_plugins(MinimalPlugins);
138+
app
139+
}
140+
141+
#[test]
142+
fn test_setup_coliders_system() {
143+
let mut app = setup_app();
144+
app.add_systems(Startup, setup_coliders_system);
145+
146+
app.update();
147+
148+
let mut colliders_query = app.world.query::<&collider_components::BlockCollider>();
149+
let colliders_count = colliders_query.iter(&app.world).count();
150+
151+
assert_eq!(colliders_count, 3 * 3 * 3);
152+
}
153+
154+
#[test]
155+
fn test_handle_collider_update_events_system() {
156+
let mut app = App::new();
157+
158+
app.add_event::<collider_events::ColliderUpdateEvent>();
159+
app.add_systems(Update, handle_collider_update_events_system);
160+
app.insert_resource(terrain_resources::ChunkManager::new());
161+
162+
app.world.spawn((
163+
Transform {
164+
translation: Vec3 { x: 0.0, y: 0.0, z: 0.0 },
165+
..Default::default()
166+
},
167+
collider_components::BlockCollider {
168+
relative_position: Vec3 { x: 1.0, y: 2.0, z: 3.0 },
169+
},
170+
));
171+
172+
let block = BlockId::Dirt;
173+
let mut resource = app.world.get_resource_mut::<terrain_resources::ChunkManager>().unwrap();
174+
175+
let chunks = terrain_resources::ChunkManager::instantiate_chunks(Vec3 { x: 0.0, y: 0.0, z: 0.0 }, 1);
176+
177+
resource.insert_chunks(chunks);
178+
resource.set_block(Vec3 { x: 6.0, y: 7.0, z: 8.0 }, block);
179+
180+
app.world.send_event(ColliderUpdateEvent { grid_center_position: [5.0, 5.0, 5.0] });
181+
182+
app.update();
183+
184+
let mut collider_query = app.world.query::<(&Transform, &collider_components::BlockCollider)>();
185+
186+
let (collider_transform, _) = collider_query.single(&app.world);
187+
188+
assert_eq!(Vec3 { x: 6.5, y: 7.5, z: 8.5 }, collider_transform.translation);
189+
}
190+
}
191+
192+
// ---- File: src/client/collider/mod.rs ----
193+
pub mod components;
194+
pub mod events;
195+
pub mod systems;
196+
197+
use crate::prelude::*;
198+
199+
pub struct ColliderPlugin;
200+
201+
impl Plugin for ColliderPlugin {
202+
fn build(&self, app: &mut App) {
203+
app.add_systems(Startup, collider_systems::setup_coliders_system);
204+
app.add_event::<collider_events::ColliderUpdateEvent>();
205+
app.add_systems(Update, collider_systems::handle_collider_update_events_system);
206+
}
207+
}
208+
209+
// ---- File: src/client/collider/components.rs ----
210+
use crate::prelude::*;
211+
212+
#[derive(Component)]
213+
pub struct BlockCollider {
214+
pub relative_position: Vec3,
215+
}
216+
```

0 commit comments

Comments
 (0)