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

9 implement ship core #40

Merged
merged 4 commits into from
Feb 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
56 changes: 35 additions & 21 deletions cosmos_client/src/netty/gameplay/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,35 @@ fn client_sync_players(

match msg {
ServerUnreliableMessages::PlayerBody { id, body } => {
let entity = lobby.players.get(&id).unwrap().client_entity;

let (mut transform, mut velocity, _) = query_body.get_mut(entity).unwrap();
if let Some(entity) = lobby
.players
.get(&id).map(|x| x.client_entity)
{
let (mut transform, mut velocity, _) = query_body.get_mut(entity).unwrap();

transform.translation = body.translation.into();
transform.rotation = body.rotation.into();
transform.translation = body.translation.into();
transform.rotation = body.rotation.into();

velocity.linvel = body.body_vel.linvel.into();
velocity.angvel = body.body_vel.angvel.into();
velocity.linvel = body.body_vel.linvel.into();
velocity.angvel = body.body_vel.angvel.into();
}
}
ServerUnreliableMessages::BulkBodies {
bodies,
time_stamp: _,
} => {
for (server_entity, body) in bodies.iter() {
if let Some(entity) = network_mapping.client_from_server(server_entity) {
let (mut transform, mut velocity, local) =
query_body.get_mut(*entity).unwrap();

if local.is_none() {
transform.translation = body.translation.into();
transform.rotation = body.rotation.into();

velocity.linvel = body.body_vel.linvel.into();
velocity.angvel = body.body_vel.angvel.into();
if let Ok((mut transform, mut velocity, local)) =
query_body.get_mut(*entity)
{
if local.is_none() {
transform.translation = body.translation.into();
transform.rotation = body.rotation.into();

velocity.linvel = body.body_vel.linvel.into();
velocity.angvel = body.body_vel.angvel.into();
}
}
}
}
Expand Down Expand Up @@ -232,7 +236,12 @@ fn client_sync_players(
body,
} => {
let mut entity = commands.spawn_empty();
let mut structure = Structure::new(width as usize, height as usize, length as usize, entity.id());
let mut structure = Structure::new(
width as usize,
height as usize,
length as usize,
entity.id(),
);

let builder = ClientPlanetBuilder::default();
builder.insert_planet(&mut entity, body.create_transform(), &mut structure);
Expand All @@ -253,7 +262,12 @@ fn client_sync_players(
length,
} => {
let mut entity = commands.spawn_empty();
let mut structure = Structure::new(width as usize, height as usize, length as usize, entity.id());
let mut structure = Structure::new(
width as usize,
height as usize,
length as usize,
entity.id(),
);

let builder = ClientShipBuilder::default();
builder.insert_ship(
Expand Down Expand Up @@ -305,9 +319,9 @@ fn client_sync_players(
ServerReliableMessages::StructureRemove {
entity: server_entity,
} => {
commands
.entity(*network_mapping.client_from_server(&server_entity).unwrap())
.despawn_recursive();
if let Some(entity) = network_mapping.client_from_server(&server_entity) {
commands.entity(*entity).despawn_recursive();
}
}
ServerReliableMessages::MOTD { motd } => {
println!("Server MOTD: {motd}");
Expand Down
1 change: 1 addition & 0 deletions cosmos_core/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::{ecs::schedule::StateData, prelude::App};

pub mod block_events;
pub mod structure;
pub mod wrappers;

pub fn register<T: StateData + Clone + Copy>(app: &mut App, playing_state: T) {
block_events::register(app);
Expand Down
62 changes: 62 additions & 0 deletions cosmos_core/src/events/wrappers/cancellable_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// use bevy::{
// prelude::Resource,
// reflect::{FromReflect, Reflect},
// utils::HashSet,
// };

// pub struct CancellableEvent<T> {
// pub event: T,
// id: u64,
// }

// impl<T> CancellableEvent<T> {
// pub fn new(event: T, event_manager: &mut CancellableEventManager) -> Self {
// Self {
// event,
// id: event_manager.new_event_entry(),
// }
// }

// pub fn cancel(&self, event_manager: &mut CancellableEventManager) {
// event_manager.cancel_event(self.id);
// }

// pub fn is_active(&self, event_manager: &mut CancellableEventManager) -> bool {
// event_manager.is_event_active(self.id)
// }

// pub fn unwrap_and_send()
// }

// #[derive(Resource, Reflect, FromReflect, Debug, Default)]
// pub struct CancellableEventManager {
// active_events: HashSet<u64>,
// next_id: u64,
// }

// impl CancellableEventManager {
// fn new_event_entry(&mut self) -> u64 {
// self.next_id += 1;

// self.active_events.insert(self.next_id);

// self.next_id
// }

// /// Marks an event as inactive
// pub fn finish_event(&mut self, id: u64) {
// // It's fine if this removes nothing
// self.active_events.remove(&id);
// }

// /// Marks an event as inactive
// pub fn cancel_event(&mut self, id: u64) {
// // It's fine if this removes nothing
// self.active_events.remove(&id);
// }

// /// Returns true if an event with this id exists & has not been cancelled.
// pub fn is_event_active(&mut self, id: u64) -> bool {
// self.active_events.contains(&id)
// }
// }
1 change: 1 addition & 0 deletions cosmos_core/src/events/wrappers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod cancellable_event;
24 changes: 11 additions & 13 deletions cosmos_core/src/projectiles/laser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,17 @@ fn handle_events(
laser_strength: laser.strength,
});
}
} else {
if let Ok(transform) = transform_query.get(entity) {
let pos = toi.witness1 + velocity.linvel.normalize() * 0.01;
let lph = Quat::from_affine3(&transform.affine())
.inverse()
.mul_vec3(pos - transform.translation());

event_writer.send(LaserCollideEvent {
entity_hit: entity,
local_position_hit: lph,
laser_strength: laser.strength,
});
}
} else if let Ok(transform) = transform_query.get(entity) {
let pos = toi.witness1 + velocity.linvel.normalize() * 0.01;
let lph = Quat::from_affine3(&transform.affine())
.inverse()
.mul_vec3(pos - transform.translation());

event_writer.send(LaserCollideEvent {
entity_hit: entity,
local_position_hit: lph,
laser_strength: laser.strength,
});
}
laser.active = false;
commands.entity(laser_entity).despawn_recursive();
Expand Down
2 changes: 1 addition & 1 deletion cosmos_core/src/structure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ pub fn register<T: StateData + Clone + Copy>(
playing_game_state: T,
) {
systems::register(app, post_loading_state, playing_game_state);
ship::register(app);
ship::register(app, playing_game_state);
events::register(app);
loading::register(app);
block_health::register(app);
Expand Down
37 changes: 37 additions & 0 deletions cosmos_core/src/structure/ship/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Ship core handler

use bevy::{
ecs::schedule::StateData,
prelude::{App, Commands, Component, EventReader, Res, SystemSet},
reflect::{FromReflect, Reflect},
};

use crate::{
block::Block,
events::block_events::BlockChangedEvent,
registry::{identifiable::Identifiable, Registry},
};

#[derive(Component, Default, FromReflect, Reflect, Debug, Copy, Clone)]
/// Represents the time since the last block was broken
pub struct MeltingDown(pub f32);

fn monitor_block_events(
mut commands: Commands,
blocks: Res<Registry<Block>>,
mut event_reader: EventReader<BlockChangedEvent>,
) {
for ev in event_reader.iter() {
let block = blocks.from_numeric_id(ev.old_block);

if block.unlocalized_name() == "cosmos:ship_core" {
commands
.entity(ev.structure_entity)
.insert(MeltingDown::default());
}
}
}

pub(crate) fn register<T: StateData + Clone + Copy>(app: &mut App, playing_state: T) {
app.add_system_set(SystemSet::on_update(playing_state).with_system(monitor_block_events));
}
5 changes: 4 additions & 1 deletion cosmos_core/src/structure/ship/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use bevy::ecs::schedule::StateData;
use bevy::prelude::App;
use bevy::prelude::Component;

pub mod core;
pub mod pilot;
pub mod ship_builder;
pub mod ship_movement;

#[derive(Component)]
pub struct Ship;

pub fn register(app: &mut App) {
pub fn register<T: StateData + Clone + Copy>(app: &mut App, playing_state: T) {
pilot::regiter(app);
ship_movement::register(app);
core::register(app, playing_state);
}
4 changes: 0 additions & 4 deletions cosmos_core/src/structure/ship/ship.rs

This file was deleted.

16 changes: 15 additions & 1 deletion cosmos_server/src/events/blocks/block_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use cosmos_core::{
inventory::Inventory,
item::Item,
netty::{server_reliable_messages::ServerReliableMessages, NettyChannel},
registry::Registry,
registry::{identifiable::Identifiable, Registry},
structure::{structure_block::StructureBlock, Structure},
};

Expand Down Expand Up @@ -52,6 +52,20 @@ fn handle_block_break_events(

let block_id = structure.block_id_at(ev.x, ev.y, ev.z);

let block = blocks.from_numeric_id(block_id);

// Eventually seperate this into another event lsitener that some how interacts with this one
// Idk if bevy supports this yet without some hacky stuff?
if block.unlocalized_name() == "cosmos:ship_core" {
let mut itr = structure.all_blocks_iter(false);

// ship core some other block
if itr.next().is_some() && itr.next().is_some() {
// Do not allow player to mine ship core if another block exists on the ship
return;
}
}

if let Ok(mut inventory) = inventory_query.get_mut(ev.breaker) {
let block = blocks.from_numeric_id(block_id);

Expand Down
62 changes: 62 additions & 0 deletions cosmos_server/src/events/structure/ship/core.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use bevy::prelude::*;
use bevy_renet::renet::RenetServer;
use cosmos_core::{
block::Block,
events::{block_events::BlockChangedEvent, structure::change_pilot_event::ChangePilotEvent},
netty::{server_reliable_messages::ServerReliableMessages, NettyChannel},
registry::Registry,
structure::{
ship::{core::MeltingDown, pilot::Pilot},
Structure,
},
};

use crate::state::GameState;

fn on_melting_down(
mut commands: Commands,
mut query: Query<(Entity, &mut Structure, &mut MeltingDown)>,
mut event_writer: EventWriter<BlockChangedEvent>,
blocks: Res<Registry<Block>>,
time: Res<Time>,
pilot_query: Query<&Pilot>,
mut change_pilot_event: EventWriter<ChangePilotEvent>,
mut server: ResMut<RenetServer>,
) {
for (entity, mut structure, mut melting_down) in query.iter_mut() {
if pilot_query.contains(entity) {
change_pilot_event.send(ChangePilotEvent {
structure_entity: entity,
pilot_entity: None,
});
}

if melting_down.0 >= 1.0 {
melting_down.0 -= 1.0;

if let Some(block) = structure.all_blocks_iter(false).next() {
structure.remove_block_at(
block.x,
block.y,
block.z,
&blocks,
Some(&mut event_writer),
);
} else {
commands.entity(entity).despawn_recursive();

server.broadcast_message(
NettyChannel::Reliable.id(),
bincode::serialize(&ServerReliableMessages::StructureRemove { entity })
.unwrap(),
);
}
}

melting_down.0 += time.delta_seconds();
}
}

pub(crate) fn register(app: &mut App) {
app.add_system_set(SystemSet::on_update(GameState::Playing).with_system(on_melting_down));
}
6 changes: 5 additions & 1 deletion cosmos_server/src/events/structure/ship/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use cosmos_core::{

use crate::state::GameState;

pub mod core;

pub struct ShipSetMovementEvent {
pub ship: Entity,
pub movement: ShipMovement,
Expand Down Expand Up @@ -53,7 +55,9 @@ fn monitor_pilot_changes(
}
}

pub fn register(app: &mut App) {
pub(crate) fn register(app: &mut App) {
core::register(app);

app.add_event::<ShipSetMovementEvent>().add_system_set(
SystemSet::on_update(GameState::Playing)
.with_system(monitor_set_movement_events)
Expand Down