Skip to content

Commit

Permalink
Symmetry functionally done
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyTornetta committed Oct 26, 2023
1 parent 8b6083c commit b0c2cf1
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 99 deletions.
19 changes: 16 additions & 3 deletions cosmos_client/src/events/ship/create_ship.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
//! Event & its processing for when a player wants to create a ship
use bevy::prelude::{in_state, App, Event, EventReader, EventWriter, IntoSystemConfigs, ResMut, Update};
use bevy::prelude::{in_state, App, Event, EventReader, EventWriter, IntoSystemConfigs, Query, ResMut, Update, With};
use bevy_renet::renet::RenetClient;
use cosmos_core::netty::{client_reliable_messages::ClientReliableMessages, cosmos_encoder, NettyChannelClient};
use cosmos_core::{
netty::{client_reliable_messages::ClientReliableMessages, cosmos_encoder, NettyChannelClient},
structure::ship::build_mode::BuildMode,
};

use crate::{
input::inputs::{CosmosInputs, InputChecker, InputHandler},
netty::flags::LocalPlayer,
state::game_state::GameState,
};

Expand All @@ -15,7 +19,16 @@ pub struct CreateShipEvent {
name: String,
}

fn listener(input_handler: InputChecker, mut event_writer: EventWriter<CreateShipEvent>) {
fn listener(
in_build_mode: Query<(), (With<LocalPlayer>, With<BuildMode>)>,
input_handler: InputChecker,
mut event_writer: EventWriter<CreateShipEvent>,
) {
if in_build_mode.get_single().is_ok() {
// Don't create ships while in build mode
return;
}

if input_handler.check_just_pressed(CosmosInputs::CreateShip) {
event_writer.send(CreateShipEvent { name: "Cool name".into() });
}
Expand Down
4 changes: 4 additions & 0 deletions cosmos_client/src/input/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,13 @@ pub enum CosmosInputs {

/// Toggles the player between being in build mode and not on a ship
ToggleBuildMode,
/// A toggle to clear the symmetry - when combined with a symmetry key the symmetry will be cleared
ClearSymmetry,
/// Creates an X symmetry
SymmetryX,
/// Creates a Y symmetry
SymmetryY,
/// Creates a Z symmetry
SymmetryZ,
}

Expand Down
4 changes: 3 additions & 1 deletion cosmos_client/src/interactions/block_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ pub enum InteractionType {
Primary,
}

#[derive(Component)]
#[derive(Component, Debug)]
/// Stores the block the player is last noted as looked at
pub struct LookingAt {
/// The block the player is looking at
pub looking_at_block: Option<(Entity, StructureBlock)>,
}

Expand Down
12 changes: 10 additions & 2 deletions cosmos_client/src/structure/ship/build_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,22 @@ fn control_build_mode(
}
}

fn place_symmetries(mut client: ResMut<RenetClient>, input_handler: InputChecker, query: Query<&LookingAt, With<LocalPlayer>>) {
fn place_symmetries(
mut client: ResMut<RenetClient>,
input_handler: InputChecker,
query: Query<&LookingAt, (With<LocalPlayer>, With<BuildMode>)>,
) {
let Ok(looking_at) = query.get_single() else {
return;
};

let clearing = input_handler.check_pressed(CosmosInputs::ClearSymmetry);

let looking_at_block = &looking_at.looking_at_block.map(|x| x.1);
let looking_at_block = if !clearing {
looking_at.looking_at_block.map(|x| x.1)
} else {
None
};

if !clearing && looking_at_block.is_none() {
return;
Expand Down
3 changes: 3 additions & 0 deletions cosmos_core/src/netty/client_reliable_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ pub enum ClientReliableMessages {
///
/// Requires server confirmation via [`ServerReliableMessages::PlayerExitBuildMode`] or client will do nothing
ExitBuildMode,
/// Sent by the player to update their symmetry
SetSymmetry {
/// The axis they are changing
axis: BuildAxis,
/// None if they want to remove it, otherwise the respective axis's coordinate
coordinate: Option<CoordinateType>,
},
}
4 changes: 4 additions & 0 deletions cosmos_core/src/netty/server_reliable_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ pub enum ServerReliableMessages {
/// The server's player entity that's exiting
player_entity: Entity,
},
/// Updates the player's build mode.
///
/// Only used to update symmetry axis.
UpdateBuildMode {
/// The new build mode
build_mode: BuildMode,
},
}
7 changes: 7 additions & 0 deletions cosmos_core/src/structure/ship/build_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ pub struct BuildMode {
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
/// Represents the X/Y/Z symmetry axis
pub enum BuildAxis {
/// X axis
X,
/// Y axis
Y,
/// Z axis
Z,
}

Expand All @@ -37,14 +41,17 @@ impl BuildMode {
}
}

/// Sets the symmetry for this axis
pub fn set_symmetry(&mut self, axis: BuildAxis, coordinate: CoordinateType) {
self.internal_set_symmetry(axis, Some(coordinate));
}

/// Removes the symmetry from this axis
pub fn remove_symmetry(&mut self, axis: BuildAxis) {
self.internal_set_symmetry(axis, None);
}

/// Gets the symmetry for this axis - `None` if there is no symmetry present.
pub fn get_symmetry(&self, axis: BuildAxis) -> Option<CoordinateType> {
match axis {
BuildAxis::X => self.symmetries.0,
Expand Down
Loading

0 comments on commit b0c2cf1

Please sign in to comment.