Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

206 multiblock creations #207

Merged
merged 18 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ See [the issues page](https://github.com/AnthonyTornetta/Cosmos/issues) for the
- [ ] Perhaps done via a sphere surrounding the planet that always faces the nearest star
- [ ] Sun-set skybox
- [ ] Night-side skybox
- [ ] Caves
- [ ] Lit up underground blocks
- [ ] Animated textures
- [ ] Sounds
- [x] Laser cannon fire
Expand All @@ -80,10 +78,10 @@ See [the issues page](https://github.com/AnthonyTornetta/Cosmos/issues) for the
- [x] Background space music
- [ ] Block break
- [ ] Place rotated blocks
- [ ] Multiblock machines (if enough time)
- [ ] Revamp power generation to use reactor multiblock structure
- [ ] Colored laser
- [ ] Colored glass placed in front of laser
- [x] Multiblock machines
- [x] Revamp power generation to use reactor multiblock structure
- [ ] Colored laser
- [ ] Colored glass placed in front of laser
- [x] Inventory GUI
- [x] Able to open inventory
- [x] Abstract the 3d block GUI camera
Expand Down
4 changes: 2 additions & 2 deletions cosmos_client/assets/blocks/laser_cannon.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"texture": {
"back": "laser_cannon_back",
"front": "laser_cannon_front",
"front": "laser_cannon_back",
"back": "laser_cannon_front",
"left": "laser_cannon_left_right",
"right": "laser_cannon_left_right",
"top": "laser_cannon_top_bottom",
Expand Down
6 changes: 6 additions & 0 deletions cosmos_client/assets/blocks/reactor_controller.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"texture": {
"all": "reactor_casing",
"back": "reactor_controller"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added cosmos_client/assets/images/blocks/reactor_cell.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions cosmos_client/assets/lang/blocks/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ cosmos:ship_hull_dark_red=Dark Red Ship Hull
cosmos:ship_hull_yellow=Yellow Ship Hull
cosmos:ship_hull_dark_yellow=Dark Yellow Ship Hull
cosmos:ship_hull_mint=Mint Ship Hull
cosmos:reactor_controller=Reactor Controller
cosmos:reactor_casing=Reactor Casing
cosmos:reactor_window=Reactor Window
cosmos:reactor_cell=Reactor Power Cell
13 changes: 2 additions & 11 deletions cosmos_client/src/events/block/block_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use bevy::prelude::*;
use bevy_renet::renet::RenetClient;
use cosmos_core::{
block::BlockFace,
block::{block_events::BlockInteractEvent, BlockFace},
netty::{client_reliable_messages::ClientReliableMessages, cosmos_encoder, NettyChannelClient},
structure::structure_block::StructureBlock,
};
Expand Down Expand Up @@ -34,15 +34,6 @@ pub struct BlockPlaceEvent {
pub block_up: BlockFace,
}

#[derive(Debug, Event)]
/// Sent whenever the player interacts with a block
pub struct BlockInteractEvent {
/// The structure this block is on
pub structure_entity: Entity,
/// block coords
pub coords: StructureBlock,
}

fn handle_block_break(
mut event_reader: EventReader<BlockBreakEvent>,
mut client: ResMut<RenetClient>,
Expand Down Expand Up @@ -88,7 +79,7 @@ fn handle_block_interact(
NettyChannelClient::Reliable,
cosmos_encoder::serialize(&ClientReliableMessages::InteractWithBlock {
structure_entity: network_mapping.server_from_client(&ev.structure_entity).unwrap(),
block: ev.coords,
block: ev.structure_block,
}),
);
}
Expand Down
11 changes: 6 additions & 5 deletions cosmos_client/src/interactions/block_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use bevy::prelude::*;
use bevy_rapier3d::prelude::{QueryFilter, RapierContext, DEFAULT_WORLD_ID};
use cosmos_core::{
block::BlockFace,
block::{block_events::BlockInteractEvent, BlockFace},
blockitems::BlockItems,
inventory::Inventory,
item::Item,
Expand Down Expand Up @@ -58,7 +58,7 @@ fn process_player_interaction(
}

// this fails if the player is a pilot
let Ok((player_body, mut inventory, looking_at)) = player_body.get_single_mut() else {
let Ok((player_entity, mut inventory, looking_at)) = player_body.get_single_mut() else {
return;
};

Expand All @@ -75,7 +75,7 @@ fn process_player_interaction(
cam_trans.forward(),
10.0,
true,
QueryFilter::new().exclude_rigid_body(player_body), // don't want to hit yourself
QueryFilter::new().exclude_rigid_body(player_entity), // don't want to hit yourself
) else {
if let Some(mut looking_at) = looking_at {
looking_at.looking_at_block = None;
Expand Down Expand Up @@ -110,7 +110,7 @@ fn process_player_interaction(
if let Some(mut looking_at) = looking_at {
looking_at.looking_at_block = looking_at_block;
} else {
commands.entity(player_body).insert(LookingAt { looking_at_block });
commands.entity(player_entity).insert(LookingAt { looking_at_block });
}
}

Expand Down Expand Up @@ -163,7 +163,8 @@ fn process_player_interaction(
if let Ok(coords) = structure.relative_coords_to_local_coords_checked(point.x, point.y, point.z) {
interact_writer.send(BlockInteractEvent {
structure_entity: structure.get_entity().unwrap(),
coords: StructureBlock::new(coords),
structure_block: StructureBlock::new(coords),
interactor: player_entity,
});
}
}
Expand Down
88 changes: 55 additions & 33 deletions cosmos_client/src/netty/gameplay/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ use crate::{
rendering::MainCamera,
state::game_state::GameState,
structure::{planet::client_planet_builder::ClientPlanetBuilder, ship::client_ship_builder::ClientShipBuilder},
ui::crosshair::CrosshairOffset,
ui::{
crosshair::CrosshairOffset,
message::{HudMessage, HudMessages},
},
};

#[derive(Component)]
Expand Down Expand Up @@ -99,9 +102,16 @@ fn update_crosshair(
}
}

#[derive(Resource, Debug, Clone, Copy)]
struct RequestedEntity {
server_entity: Entity,
client_entity: Entity,
seconds_since_request: f32,
}

#[derive(Resource, Debug, Default)]
pub(crate) struct RequestedEntities {
entities: Vec<(Entity, f32)>,
entities: Vec<RequestedEntity>,
}

#[derive(Component, Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
Expand Down Expand Up @@ -182,20 +192,21 @@ pub(crate) fn client_sync_players(
time: Res<Time>,
local_player: Query<Entity, With<LocalPlayer>>,

mut hud_messages: ResMut<HudMessages>,

(mut build_mode_enter, mut build_mode_exit): (EventWriter<EnterBuildModeEvent>, EventWriter<ExitBuildModeEvent>),
) {
let client_id = transport.client_id();

let mut new_entities = Vec::with_capacity(requested_entities.entities.len());

for ent in requested_entities.entities.iter_mut() {
ent.1 += time.delta_seconds();
if ent.1 < 10.0 {
new_entities.push(*ent);
requested_entities.entities.retain_mut(|x| {
x.seconds_since_request += time.delta_seconds();
if x.seconds_since_request < 10.0 {
true
} else {
commands.entity(x.client_entity).despawn_recursive();
false
}
}

requested_entities.entities = new_entities;
});

while let Some(message) = client.receive_message(NettyChannelServer::Unreliable) {
let msg: ServerUnreliableMessages = cosmos_encoder::deserialize(&message).unwrap();
Expand Down Expand Up @@ -240,8 +251,15 @@ pub(crate) fn client_sync_players(
commands.entity(entity).insert((loc, body.create_velocity(), LerpTowards(body)));
}
}
} else if !requested_entities.entities.iter().any(|x| x.0 == *server_entity) {
requested_entities.entities.push((*server_entity, 0.0));
} else if !requested_entities.entities.iter().any(|x| x.server_entity == *server_entity) {
let client_entity = commands.spawn_empty().id();

requested_entities.entities.push(RequestedEntity {
server_entity: *server_entity,
client_entity,
seconds_since_request: 0.0,
});
network_mapping.add_mapping(client_entity, *server_entity);

client.send_message(
NettyChannelClient::Reliable,
Expand Down Expand Up @@ -273,7 +291,7 @@ pub(crate) fn client_sync_players(
// Prevents creation of duplicate players
if lobby.players.contains_key(&id) {
warn!("DUPLICATE PLAYER RECEIVED {id}");
break;
continue;
}

let Ok(body) = body.map(&network_mapping) else {
Expand Down Expand Up @@ -392,33 +410,27 @@ pub(crate) fn client_sync_players(
biosphere,
location,
} => {
if network_mapping.contains_server_entity(server_entity) {
warn!("Got duplicate planet! Is the server lagging?");
break;
}
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
};

let mut entity_cmds = commands.spawn_empty();
let mut entity_cmds = commands.entity(entity);
let mut structure = Structure::Dynamic(DynamicStructure::new(dimensions));

let builder = ClientPlanetBuilder::default();
builder.insert_planet(&mut entity_cmds, location, &mut structure, planet);

entity_cmds.insert((structure, BiosphereMarker::new(biosphere)));

let entity = entity_cmds.id();

network_mapping.add_mapping(entity, server_entity);
}
ServerReliableMessages::Ship {
entity: server_entity,
body,
dimensions,
chunks_needed,
} => {
if network_mapping.contains_server_entity(server_entity) {
warn!("Got duplicate ship! Is the server lagging?");
break;
}
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
};

let Ok(body) = body.map(&network_mapping) else {
continue;
Expand All @@ -433,18 +445,14 @@ pub(crate) fn client_sync_players(
}
};

let mut entity_cmds = commands.spawn_empty();
let mut entity_cmds = commands.entity(entity);
let mut structure = Structure::Full(FullStructure::new(dimensions));

let builder = ClientShipBuilder::default();
builder.insert_ship(&mut entity_cmds, location, body.create_velocity(), &mut structure);

entity_cmds.insert((structure, chunks_needed));

let entity = entity_cmds.id();

network_mapping.add_mapping(entity, server_entity);

client.send_message(
NettyChannelClient::Reliable,
cosmos_encoder::serialize(&ClientReliableMessages::PilotQuery {
Expand Down Expand Up @@ -488,7 +496,7 @@ pub(crate) fn client_sync_players(
}
}
ServerReliableMessages::MOTD { motd } => {
info!("Server MOTD: {motd}");
hud_messages.display_message(motd.into());
}
ServerReliableMessages::BlockChange {
blocks_changed_packet,
Expand Down Expand Up @@ -616,6 +624,20 @@ pub(crate) fn client_sync_players(
commands.entity(player_entity).insert(build_mode);
}
}
ServerReliableMessages::InvalidReactor { reason } => {
hud_messages.display_message(HudMessage::with_colored_string(
format!("Invalid reactor setup: {reason}"),
Color::ORANGE_RED,
));
}
ServerReliableMessages::Reactors { reactors, structure } => {
if let Some(structure_entity) = network_mapping.client_from_server(&structure) {
commands.entity(structure_entity).insert(reactors);
}
}
ServerReliableMessages::RequestedEntityReceived(entity) => {
requested_entities.entities.retain(|x| x.server_entity != entity);
}
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions cosmos_client/src/structure/asteroid/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ fn receive_asteroids(
mut client: ResMut<RenetClient>,
query_loc: Query<&Location>,
mut commands: Commands,
mut network_mapping: ResMut<NetworkMapping>,
network_mapping: ResMut<NetworkMapping>,
) {
while let Some(message) = client.receive_message(NettyChannelServer::Asteroid) {
let msg: AsteroidServerMessages = cosmos_encoder::deserialize(&message).unwrap();

match msg {
AsteroidServerMessages::Asteroid { entity, body, dimensions } => {
AsteroidServerMessages::Asteroid {
entity: server_entity,
body,
dimensions,
} => {
let Some(entity) = network_mapping.client_from_server(&server_entity) else {
continue;
};

let Ok(body) = body.map(&network_mapping) else {
continue;
};
Expand All @@ -40,7 +48,7 @@ fn receive_asteroids(
}
};

let mut entity_cmds = commands.spawn_empty();
let mut entity_cmds = commands.entity(entity);

let mut structure = Structure::Full(FullStructure::new(dimensions));

Expand All @@ -49,8 +57,6 @@ fn receive_asteroids(
builder.insert_asteroid(&mut entity_cmds, location, &mut structure);

entity_cmds.insert(structure);

network_mapping.add_mapping(entity_cmds.id(), entity);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cosmos_client/src/structure/chunk_retreiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use crate::NetworkMapping;
pub struct NeedsPopulated;

fn populate_structures(
mut commands: Commands,
player_location: Query<&Location, With<LocalPlayer>>,
query: Query<(Entity, &Location), (With<NeedsPopulated>, With<Structure>)>,
mut client: ResMut<RenetClient>,
network_mapping: Res<NetworkMapping>,
mut commands: Commands,
) {
let Ok(player_location) = player_location.get_single() else {
return;
Expand Down
Loading
Loading